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.test; 18 19import android.support.v7.widget.SwitchCompat; 20import android.test.AndroidTestCase; 21import android.test.suitebuilder.annotation.SmallTest; 22import android.view.View; 23import android.view.ViewGroup; 24import android.widget.FrameLayout; 25import android.widget.ImageView; 26import android.widget.TextView; 27 28import com.android.setupwizardlib.R; 29import com.android.setupwizardlib.items.SwitchItem; 30 31public class SwitchItemTest extends AndroidTestCase { 32 33 private SwitchCompat mSwitch; 34 35 @SmallTest 36 public void testChecked() { 37 SwitchItem item = new SwitchItem(); 38 item.setTitle("TestTitle"); 39 item.setSummary("TestSummary"); 40 View view = createLayout(); 41 42 item.setChecked(true); 43 44 item.onBindView(view); 45 46 assertTrue("Switch should be checked", mSwitch.isChecked()); 47 } 48 49 @SmallTest 50 public void testNotChecked() { 51 SwitchItem item = new SwitchItem(); 52 item.setTitle("TestTitle"); 53 item.setSummary("TestSummary"); 54 View view = createLayout(); 55 56 item.setChecked(false); 57 58 item.onBindView(view); 59 60 assertFalse("Switch should be unchecked", mSwitch.isChecked()); 61 } 62 63 @SmallTest 64 public void testListener() { 65 SwitchItem item = new SwitchItem(); 66 item.setTitle("TestTitle"); 67 item.setSummary("TestSummary"); 68 View view = createLayout(); 69 70 item.setChecked(true); 71 72 final TestOnCheckedChangeListener listener = new TestOnCheckedChangeListener(); 73 item.setOnCheckedChangeListener(listener); 74 75 item.onBindView(view); 76 77 assertTrue("Switch should be checked", mSwitch.isChecked()); 78 mSwitch.setChecked(false); 79 80 assertTrue("Listener should be called", listener.called); 81 assertFalse("Listener should not be checked", listener.checked); 82 83 mSwitch.setChecked(true); 84 85 assertTrue("Listener should be called", listener.called); 86 assertTrue("Listener should be checked", listener.checked); 87 } 88 89 @SmallTest 90 public void testListenerSetChecked() { 91 // Check that calling setChecked on the item will also call the listener. 92 93 SwitchItem item = new SwitchItem(); 94 item.setTitle("TestTitle"); 95 item.setSummary("TestSummary"); 96 View view = createLayout(); 97 98 item.setChecked(true); 99 100 final TestOnCheckedChangeListener listener = new TestOnCheckedChangeListener(); 101 item.setOnCheckedChangeListener(listener); 102 103 item.onBindView(view); 104 105 assertTrue("Switch should be checked", mSwitch.isChecked()); 106 item.setChecked(false); 107 108 assertTrue("Listener should be called", listener.called); 109 assertFalse("Listener should not be checked", listener.checked); 110 111 item.setChecked(true); 112 113 assertTrue("Listener should be called", listener.called); 114 assertTrue("Listener should be checked", listener.checked); 115 } 116 117 @SmallTest 118 public void testToggle() { 119 SwitchItem item = new SwitchItem(); 120 item.setTitle("TestTitle"); 121 item.setSummary("TestSummary"); 122 View view = createLayout(); 123 124 item.setChecked(true); 125 item.onBindView(view); 126 127 assertTrue("Switch should be checked", mSwitch.isChecked()); 128 129 item.toggle(view); 130 131 assertFalse("Switch should be unchecked", mSwitch.isChecked()); 132 } 133 134 private ViewGroup createLayout() { 135 ViewGroup root = new FrameLayout(mContext); 136 137 TextView titleView = new TextView(mContext); 138 titleView.setId(R.id.suw_items_title); 139 root.addView(titleView); 140 141 TextView summaryView = new TextView(mContext); 142 summaryView.setId(R.id.suw_items_summary); 143 root.addView(summaryView); 144 145 FrameLayout iconContainer = new FrameLayout(mContext); 146 iconContainer.setId(R.id.suw_items_icon_container); 147 root.addView(iconContainer); 148 149 ImageView iconView = new ImageView(mContext); 150 iconView.setId(R.id.suw_items_icon); 151 iconContainer.addView(iconView); 152 153 mSwitch = new SwitchCompat(mContext); 154 mSwitch.setId(R.id.suw_items_switch); 155 root.addView(mSwitch); 156 157 return root; 158 } 159 160 private static class TestOnCheckedChangeListener implements SwitchItem.OnCheckedChangeListener { 161 162 public boolean called = false; 163 public boolean checked = false; 164 165 @Override 166 public void onCheckedChange(SwitchItem item, boolean isChecked) { 167 called = true; 168 checked = isChecked; 169 } 170 } 171} 172