1/*
2 * Copyright (C) 2015 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 com.google.common.truth.Truth.assertThat;
20
21import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertFalse;
23import static org.junit.Assert.assertTrue;
24import static org.robolectric.RuntimeEnvironment.application;
25
26import android.annotation.TargetApi;
27import android.os.Build.VERSION;
28import android.os.Build.VERSION_CODES;
29import android.support.v7.widget.SwitchCompat;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.FrameLayout;
34import android.widget.ImageView;
35import android.widget.TextView;
36
37import com.android.setupwizardlib.R;
38import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
39
40import org.junit.Assume;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43import org.robolectric.annotation.Config;
44
45@RunWith(SuwLibRobolectricTestRunner.class)
46@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK })
47public class SwitchItemTest {
48
49    private SwitchCompat mSwitch;
50
51    @Test
52    public void testLayout() {
53        Assume.assumeTrue(VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP);
54        SwitchItem item = new SwitchItem();
55        LayoutInflater inflater = LayoutInflater.from(application);
56        ViewGroup layout = (ViewGroup) inflater.inflate(item.getDefaultLayoutResource(), null);
57        assertThat(layout.getClipToPadding()).isFalse();
58    }
59
60    @Test
61    public void testChecked() {
62        SwitchItem item = new SwitchItem();
63        item.setTitle("TestTitle");
64        item.setSummary("TestSummary");
65        View view = createLayout();
66
67        item.setChecked(true);
68
69        item.onBindView(view);
70
71        assertTrue("Switch should be checked", mSwitch.isChecked());
72    }
73
74    @Test
75    public void testNotChecked() {
76        SwitchItem item = new SwitchItem();
77        item.setTitle("TestTitle");
78        item.setSummary("TestSummary");
79        View view = createLayout();
80
81        item.setChecked(false);
82
83        item.onBindView(view);
84
85        assertFalse("Switch should be unchecked", mSwitch.isChecked());
86    }
87
88    @Test
89    public void testListener() {
90        SwitchItem item = new SwitchItem();
91        item.setTitle("TestTitle");
92        item.setSummary("TestSummary");
93        View view = createLayout();
94
95        item.setChecked(true);
96
97        final TestOnCheckedChangeListener listener = new TestOnCheckedChangeListener();
98        item.setOnCheckedChangeListener(listener);
99
100        item.onBindView(view);
101
102        assertTrue("Switch should be checked", mSwitch.isChecked());
103        mSwitch.setChecked(false);
104
105        assertTrue("Listener should be called", listener.mCalled);
106        assertFalse("Listener should not be checked", listener.mChecked);
107
108        mSwitch.setChecked(true);
109
110        assertTrue("Listener should be called", listener.mCalled);
111        assertTrue("Listener should be checked", listener.mChecked);
112    }
113
114    @Test
115    public void testRebind() {
116        SwitchItem item1 = new SwitchItem();
117        item1.setTitle("TestTitle1");
118        item1.setSummary("TestSummary1");
119        item1.setChecked(false);
120
121        SwitchItem item2 = new SwitchItem();
122        item2.setTitle("TestTitle2");
123        item2.setSummary("TestSummary2");
124        item2.setChecked(true);
125
126        View view = createLayout();
127
128        item1.onBindView(view);
129        item2.onBindView(view);
130
131        // Switch should be bound to item2, and therefore checked
132        assertTrue("Switch should be checked", mSwitch.isChecked());
133
134        // Switching the switch to false should change the checked state of item 2 only
135        mSwitch.setChecked(false);
136        assertFalse("Item1 should still be unchecked", item1.isChecked());
137        assertFalse("Item2 should not be checked", item2.isChecked());
138
139        // Switching the switch to true should change the checked state of item 2 only
140        mSwitch.setChecked(true);
141        assertFalse("Item1 should still be unchecked", item1.isChecked());
142        assertTrue("Item2 should be checked", item2.isChecked());
143    }
144
145    @Test
146    public void testListenerSetChecked() {
147        // Check that calling setChecked on the item will also call the listener.
148
149        SwitchItem item = new SwitchItem();
150        item.setTitle("TestTitle");
151        item.setSummary("TestSummary");
152        View view = createLayout();
153
154        item.setChecked(true);
155
156        final TestOnCheckedChangeListener listener = new TestOnCheckedChangeListener();
157        item.setOnCheckedChangeListener(listener);
158
159        item.onBindView(view);
160
161        assertTrue("Switch should be checked", mSwitch.isChecked());
162        item.setChecked(false);
163
164        assertTrue("Listener should be called", listener.mCalled);
165        assertFalse("Listener should not be checked", listener.mChecked);
166
167        item.setChecked(true);
168
169        assertTrue("Listener should be called", listener.mCalled);
170        assertTrue("Listener should be checked", listener.mChecked);
171    }
172
173    @Test
174    public void testToggle() {
175        SwitchItem item = new SwitchItem();
176        item.setTitle("TestTitle");
177        item.setSummary("TestSummary");
178        View view = createLayout();
179
180        item.setChecked(true);
181        item.onBindView(view);
182
183        assertTrue("Switch should be checked", mSwitch.isChecked());
184
185        item.toggle(view);
186
187        assertFalse("Switch should be unchecked", mSwitch.isChecked());
188    }
189
190    @TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
191    @Config(minSdk = VERSION_CODES.JELLY_BEAN_MR1)
192    @Test
193    public void testAccessibility() {
194        SwitchItem item = new SwitchItem();
195        item.setTitle("TestTitle");
196        item.setSummary("TestSummary");
197
198        View view = LayoutInflater.from(application).inflate(R.layout.suw_items_switch, null);
199        item.onBindView(view);
200
201        final View titleView = view.findViewById(R.id.suw_items_title);
202        assertEquals("Title view should label for switch",
203                R.id.suw_items_switch, titleView.getLabelFor());
204    }
205
206    private ViewGroup createLayout() {
207        ViewGroup root = new FrameLayout(application);
208
209        TextView titleView = new TextView(application);
210        titleView.setId(R.id.suw_items_title);
211        root.addView(titleView);
212
213        TextView summaryView = new TextView(application);
214        summaryView.setId(R.id.suw_items_summary);
215        root.addView(summaryView);
216
217        FrameLayout iconContainer = new FrameLayout(application);
218        iconContainer.setId(R.id.suw_items_icon_container);
219        root.addView(iconContainer);
220
221        ImageView iconView = new ImageView(application);
222        iconView.setId(R.id.suw_items_icon);
223        iconContainer.addView(iconView);
224
225        mSwitch = new SwitchCompat(application);
226        mSwitch.setId(R.id.suw_items_switch);
227        root.addView(mSwitch);
228
229        return root;
230    }
231
232    private static class TestOnCheckedChangeListener implements SwitchItem.OnCheckedChangeListener {
233
234        boolean mCalled = false;
235        boolean mChecked = false;
236
237        @Override
238        public void onCheckedChange(SwitchItem item, boolean isChecked) {
239            mCalled = true;
240            mChecked = isChecked;
241        }
242    }
243}
244