ExpandableSwitchItemTest.java revision 66815fe66392bcbb12e2fc93bbf326a5c2d8782f
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 17package com.android.setupwizardlib.items; 18 19import static org.hamcrest.Matchers.hasItem; 20import static org.hamcrest.Matchers.not; 21import static org.junit.Assert.assertEquals; 22import static org.junit.Assert.assertFalse; 23import static org.junit.Assert.assertThat; 24import static org.junit.Assert.assertTrue; 25import static org.robolectric.RuntimeEnvironment.application; 26 27import android.support.v7.widget.SwitchCompat; 28import android.view.LayoutInflater; 29import android.view.View; 30import android.view.ViewGroup; 31import android.widget.FrameLayout; 32import android.widget.ImageView; 33import android.widget.TextView; 34 35import com.android.setupwizardlib.R; 36import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; 37 38import org.junit.Before; 39import org.junit.Test; 40import org.junit.runner.RunWith; 41import org.robolectric.annotation.Config; 42 43import java.util.ArrayList; 44 45@RunWith(SuwLibRobolectricTestRunner.class) 46@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) 47public class ExpandableSwitchItemTest { 48 49 private TextView mSummaryView; 50 private ExpandableSwitchItem mItem; 51 52 @Before 53 public void setUp() { 54 mItem = new ExpandableSwitchItem(); 55 mItem.setTitle("TestTitle"); 56 mItem.setCollapsedSummary("TestSummary"); 57 mItem.setExpandedSummary("TestSummaryExpanded"); 58 } 59 60 @Test 61 public void testInitialState() { 62 View view = createLayout(); 63 mItem.onBindView(view); 64 65 assertEquals("Collapsed summary should be TestSummary", 66 "TestSummary", mItem.getCollapsedSummary()); 67 assertEquals("Expanded summary should be TestSummaryExpanded", 68 "TestSummaryExpanded", mItem.getExpandedSummary()); 69 70 assertEquals("Should be collapsed initially", "TestSummary", mItem.getSummary()); 71 assertEquals("Summary view should display collapsed summary", 72 "TestSummary", mSummaryView.getText()); 73 74 assertFalse("Expandable switch item itself should not be focusable", view.isFocusable()); 75 } 76 77 @Test 78 public void testExpanded() { 79 View view = createLayout(); 80 mItem.onBindView(view); 81 82 mItem.setExpanded(true); 83 84 assertEquals("Collapsed summary should be TestSummary", 85 "TestSummary", mItem.getCollapsedSummary()); 86 assertEquals("Expanded summary should be TestSummaryExpanded", 87 "TestSummaryExpanded", mItem.getExpandedSummary()); 88 89 assertTrue("Should be expanded", mItem.isExpanded()); 90 assertEquals("getSummary should be expanded summary", 91 "TestSummaryExpanded", mItem.getSummary()); 92 } 93 94 @Test 95 public void testCollapsed() { 96 View view = createLayout(); 97 mItem.onBindView(view); 98 99 mItem.setExpanded(true); 100 assertTrue("Should be expanded", mItem.isExpanded()); 101 102 mItem.setExpanded(false); 103 104 assertEquals("Collapsed summary should be TestSummary", 105 "TestSummary", mItem.getCollapsedSummary()); 106 assertEquals("Expanded summary should be TestSummaryExpanded", 107 "TestSummaryExpanded", mItem.getExpandedSummary()); 108 109 assertFalse("Should be expanded", mItem.isExpanded()); 110 assertEquals("getSummary should be collapsed summary", 111 "TestSummary", mItem.getSummary()); 112 } 113 114 @Test 115 public void testClick() { 116 View view = createLayout(); 117 mItem.onBindView(view); 118 119 assertFalse("Should not be expanded initially", mItem.isExpanded()); 120 121 final View content = view.findViewById(R.id.suw_items_expandable_switch_content); 122 content.performClick(); 123 assertTrue("Should be expanded after clicking", mItem.isExpanded()); 124 125 content.performClick(); 126 assertFalse("Should be collapsed again after clicking", mItem.isExpanded()); 127 } 128 129 @Test 130 public void testDrawableState() { 131 final View view = 132 LayoutInflater.from(application).inflate(mItem.getLayoutResource(), null); 133 mItem.onBindView(view); 134 135 final View titleView = view.findViewById(R.id.suw_items_title); 136 assertThat("state_checked should not be set initially", 137 toArrayList(titleView.getDrawableState()), 138 not(hasItem(android.R.attr.state_checked))); 139 140 mItem.setExpanded(true); 141 mItem.onBindView(view); 142 assertThat("state_checked should not be set initially", 143 toArrayList(titleView.getDrawableState()), 144 hasItem(android.R.attr.state_checked)); 145 146 mItem.setExpanded(false); 147 mItem.onBindView(view); 148 assertThat("state_checked should not be set initially", 149 toArrayList(titleView.getDrawableState()), 150 not(hasItem(android.R.attr.state_checked))); 151 } 152 153 private ArrayList<Integer> toArrayList(int[] array) { 154 ArrayList<Integer> arrayList = new ArrayList<>(array.length); 155 for (int i : array) { 156 arrayList.add(i); 157 } 158 return arrayList; 159 } 160 161 private ViewGroup createLayout() { 162 ViewGroup root = new FrameLayout(application); 163 164 ViewGroup content = new FrameLayout(application); 165 content.setId(R.id.suw_items_expandable_switch_content); 166 root.addView(content); 167 168 TextView titleView = new TextView(application); 169 titleView.setId(R.id.suw_items_title); 170 content.addView(titleView); 171 172 mSummaryView = new TextView(application); 173 mSummaryView.setId(R.id.suw_items_summary); 174 content.addView(mSummaryView); 175 176 FrameLayout iconContainer = new FrameLayout(application); 177 iconContainer.setId(R.id.suw_items_icon_container); 178 content.addView(iconContainer); 179 180 ImageView iconView = new ImageView(application); 181 iconView.setId(R.id.suw_items_icon); 182 iconContainer.addView(iconView); 183 184 SwitchCompat switchView = new SwitchCompat(application); 185 switchView.setId(R.id.suw_items_switch); 186 root.addView(switchView); 187 188 return root; 189 } 190} 191