Home | History | Annotate | Download | only in items
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.setupwizardlib.items;
     18 
     19 import static org.hamcrest.Matchers.hasItem;
     20 import static org.hamcrest.Matchers.not;
     21 import static org.junit.Assert.assertEquals;
     22 import static org.junit.Assert.assertFalse;
     23 import static org.junit.Assert.assertThat;
     24 import static org.junit.Assert.assertTrue;
     25 import static org.robolectric.RuntimeEnvironment.application;
     26 
     27 import android.support.v7.widget.SwitchCompat;
     28 import android.view.LayoutInflater;
     29 import android.view.View;
     30 import android.view.ViewGroup;
     31 import android.widget.FrameLayout;
     32 import android.widget.ImageView;
     33 import android.widget.TextView;
     34 
     35 import com.android.setupwizardlib.BuildConfig;
     36 import com.android.setupwizardlib.R;
     37 import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
     38 
     39 import org.junit.Before;
     40 import org.junit.Test;
     41 import org.junit.runner.RunWith;
     42 import org.robolectric.annotation.Config;
     43 
     44 import java.util.ArrayList;
     45 
     46 @RunWith(SuwLibRobolectricTestRunner.class)
     47 @Config(constants = BuildConfig.class, sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK })
     48 public class ExpandableSwitchItemTest {
     49 
     50     private TextView mSummaryView;
     51     private ExpandableSwitchItem mItem;
     52 
     53     @Before
     54     public void setUp() {
     55         mItem = new ExpandableSwitchItem();
     56         mItem.setTitle("TestTitle");
     57         mItem.setCollapsedSummary("TestSummary");
     58         mItem.setExpandedSummary("TestSummaryExpanded");
     59     }
     60 
     61     @Test
     62     public void testInitialState() {
     63         View view = createLayout();
     64         mItem.onBindView(view);
     65 
     66         assertEquals("Collapsed summary should be TestSummary",
     67                 "TestSummary", mItem.getCollapsedSummary());
     68         assertEquals("Expanded summary should be TestSummaryExpanded",
     69                 "TestSummaryExpanded", mItem.getExpandedSummary());
     70 
     71         assertEquals("Should be collapsed initially", "TestSummary", mItem.getSummary());
     72         assertEquals("Summary view should display collapsed summary",
     73                 "TestSummary", mSummaryView.getText());
     74     }
     75 
     76     @Test
     77     public void testExpanded() {
     78         View view = createLayout();
     79         mItem.onBindView(view);
     80 
     81         mItem.setExpanded(true);
     82 
     83         assertEquals("Collapsed summary should be TestSummary",
     84                 "TestSummary", mItem.getCollapsedSummary());
     85         assertEquals("Expanded summary should be TestSummaryExpanded",
     86                 "TestSummaryExpanded", mItem.getExpandedSummary());
     87 
     88         assertTrue("Should be expanded", mItem.isExpanded());
     89         assertEquals("getSummary should be expanded summary",
     90                 "TestSummaryExpanded", mItem.getSummary());
     91     }
     92 
     93     @Test
     94     public void testCollapsed() {
     95         View view = createLayout();
     96         mItem.onBindView(view);
     97 
     98         mItem.setExpanded(true);
     99         assertTrue("Should be expanded", mItem.isExpanded());
    100 
    101         mItem.setExpanded(false);
    102 
    103         assertEquals("Collapsed summary should be TestSummary",
    104                 "TestSummary", mItem.getCollapsedSummary());
    105         assertEquals("Expanded summary should be TestSummaryExpanded",
    106                 "TestSummaryExpanded", mItem.getExpandedSummary());
    107 
    108         assertFalse("Should be expanded", mItem.isExpanded());
    109         assertEquals("getSummary should be collapsed summary",
    110                 "TestSummary", mItem.getSummary());
    111     }
    112 
    113     @Test
    114     public void testClick() {
    115         View view = createLayout();
    116         mItem.onBindView(view);
    117 
    118         assertFalse("Should not be expanded initially", mItem.isExpanded());
    119 
    120         final View content = view.findViewById(R.id.suw_items_expandable_switch_content);
    121         content.performClick();
    122         assertTrue("Should be expanded after clicking", mItem.isExpanded());
    123 
    124         content.performClick();
    125         assertFalse("Should be collapsed again after clicking", mItem.isExpanded());
    126     }
    127 
    128     @Test
    129     public void testDrawableState() {
    130         final View view =
    131                 LayoutInflater.from(application).inflate(mItem.getLayoutResource(), null);
    132         mItem.onBindView(view);
    133 
    134         final View titleView = view.findViewById(R.id.suw_items_title);
    135         assertThat("state_checked should not be set initially",
    136                 toArrayList(titleView.getDrawableState()),
    137                 not(hasItem(android.R.attr.state_checked)));
    138 
    139         mItem.setExpanded(true);
    140         mItem.onBindView(view);
    141         assertThat("state_checked should not be set initially",
    142                 toArrayList(titleView.getDrawableState()),
    143                 hasItem(android.R.attr.state_checked));
    144 
    145         mItem.setExpanded(false);
    146         mItem.onBindView(view);
    147         assertThat("state_checked should not be set initially",
    148                 toArrayList(titleView.getDrawableState()),
    149                 not(hasItem(android.R.attr.state_checked)));
    150     }
    151 
    152     private ArrayList<Integer> toArrayList(int[] array) {
    153         ArrayList<Integer> arrayList = new ArrayList<>(array.length);
    154         for (int i : array) {
    155             arrayList.add(i);
    156         }
    157         return arrayList;
    158     }
    159 
    160     private ViewGroup createLayout() {
    161         ViewGroup root = new FrameLayout(application);
    162 
    163         ViewGroup content = new FrameLayout(application);
    164         content.setId(R.id.suw_items_expandable_switch_content);
    165         root.addView(content);
    166 
    167         TextView titleView = new TextView(application);
    168         titleView.setId(R.id.suw_items_title);
    169         content.addView(titleView);
    170 
    171         mSummaryView = new TextView(application);
    172         mSummaryView.setId(R.id.suw_items_summary);
    173         content.addView(mSummaryView);
    174 
    175         FrameLayout iconContainer = new FrameLayout(application);
    176         iconContainer.setId(R.id.suw_items_icon_container);
    177         content.addView(iconContainer);
    178 
    179         ImageView iconView = new ImageView(application);
    180         iconView.setId(R.id.suw_items_icon);
    181         iconContainer.addView(iconView);
    182 
    183         SwitchCompat switchView = new SwitchCompat(application);
    184         switchView.setId(R.id.suw_items_switch);
    185         root.addView(switchView);
    186 
    187         return root;
    188     }
    189 }
    190