1 /* 2 * Copyright (C) 2008 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 android.widget.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertNull; 23 import static org.junit.Assert.assertSame; 24 import static org.junit.Assert.assertTrue; 25 26 import android.Manifest; 27 import android.app.Activity; 28 import android.content.Context; 29 import android.graphics.drawable.Drawable; 30 import android.graphics.drawable.StateListDrawable; 31 import android.util.AttributeSet; 32 import android.widget.ToggleButton; 33 34 import androidx.test.annotation.UiThreadTest; 35 import androidx.test.filters.SmallTest; 36 import androidx.test.rule.ActivityTestRule; 37 import androidx.test.runner.AndroidJUnit4; 38 39 import com.android.compatibility.common.util.AdoptShellPermissionsRule; 40 41 import org.junit.Before; 42 import org.junit.Rule; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 46 /** 47 * Test {@link ToggleButton}. 48 */ 49 @SmallTest 50 @RunWith(AndroidJUnit4.class) 51 public class ToggleButtonTest { 52 private static final String TEXT_OFF = "text off"; 53 private static final String TEXT_ON = "text on"; 54 55 private Activity mActivity; 56 57 @Rule(order = 0) 58 public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule( 59 androidx.test.platform.app.InstrumentationRegistry 60 .getInstrumentation().getUiAutomation(), 61 Manifest.permission.START_ACTIVITIES_FROM_SDK_SANDBOX); 62 63 @Rule(order = 1) 64 public ActivityTestRule<ToggleButtonCtsActivity> mActivityRule = 65 new ActivityTestRule<>(ToggleButtonCtsActivity.class); 66 67 @Before setup()68 public void setup() { 69 mActivity = mActivityRule.getActivity(); 70 } 71 72 @Test testConstructor()73 public void testConstructor() { 74 new ToggleButton(mActivity); 75 new ToggleButton(mActivity, null); 76 new ToggleButton(mActivity, null, android.R.attr.buttonStyleToggle); 77 new ToggleButton(mActivity, null, 0, android.R.style.Widget_DeviceDefault_Button_Toggle); 78 new ToggleButton(mActivity, null, 0, 79 android.R.style.Widget_DeviceDefault_Light_Button_Toggle); 80 new ToggleButton(mActivity, null, 0, android.R.style.Widget_Material_Button_Toggle); 81 new ToggleButton(mActivity, null, 0, android.R.style.Widget_Material_Light_Button_Toggle); 82 } 83 84 @Test(expected=NullPointerException.class) testConstructorWithNullContext1()85 public void testConstructorWithNullContext1() { 86 new ToggleButton(null); 87 } 88 89 @Test(expected=NullPointerException.class) testConstructorWithNullContext2()90 public void testConstructorWithNullContext2() { 91 new ToggleButton(null, null); 92 } 93 94 @Test(expected=NullPointerException.class) testConstructorWithNullContext3()95 public void testConstructorWithNullContext3() { 96 new ToggleButton(null, null, -1); 97 } 98 99 @Test testAttributesFromStyle()100 public void testAttributesFromStyle() { 101 final ToggleButton toggleButton = 102 (ToggleButton) mActivity.findViewById(R.id.toggle_with_style); 103 assertEquals(mActivity.getString(R.string.toggle_text_on), toggleButton.getTextOn()); 104 assertEquals(mActivity.getString(R.string.toggle_text_off), toggleButton.getTextOff()); 105 } 106 107 @Test testAttributesFromLayout()108 public void testAttributesFromLayout() { 109 final ToggleButton toggleButton = 110 (ToggleButton) mActivity.findViewById(R.id.toggle_with_defaults); 111 assertEquals(mActivity.getString(R.string.toggle_text_on_alt), toggleButton.getTextOn()); 112 assertEquals(mActivity.getString(R.string.toggle_text_off_alt), toggleButton.getTextOff()); 113 } 114 115 @UiThreadTest 116 @Test testAccessTextOff()117 public void testAccessTextOff() { 118 final ToggleButton toggleButton = (ToggleButton) mActivity.findViewById(R.id.toggle1); 119 toggleButton.setTextOff("android"); 120 assertEquals("android", toggleButton.getTextOff()); 121 toggleButton.setChecked(false); 122 123 toggleButton.setTextOff(null); 124 assertNull(toggleButton.getTextOff()); 125 126 toggleButton.setTextOff(""); 127 assertEquals("", toggleButton.getTextOff()); 128 } 129 130 @UiThreadTest 131 @Test testDrawableStateChanged()132 public void testDrawableStateChanged() { 133 final MockToggleButton toggleButton = 134 (MockToggleButton) mActivity.findViewById(R.id.toggle_custom); 135 136 // drawableStateChanged without any drawable. 137 toggleButton.drawableStateChanged(); 138 139 final StateListDrawable drawable = new StateListDrawable(); 140 drawable.addState(new int[] { android.R.attr.state_pressed }, 141 mActivity.getDrawable(R.drawable.scenery)); 142 drawable.addState(new int[] {}, 143 mActivity.getDrawable(R.drawable.scenery)); 144 145 // drawableStateChanged when CheckMarkDrawable is not null. 146 toggleButton.setButtonDrawable(drawable); 147 drawable.setState(null); 148 assertNull(drawable.getState()); 149 150 toggleButton.drawableStateChanged(); 151 assertNotNull(drawable.getState()); 152 assertEquals(toggleButton.getDrawableState(), drawable.getState()); 153 } 154 155 @UiThreadTest 156 @Test testOnFinishInflate()157 public void testOnFinishInflate() { 158 final MockToggleButton toggleButton = 159 (MockToggleButton) mActivity.findViewById(R.id.toggle_custom); 160 toggleButton.onFinishInflate(); 161 } 162 163 @UiThreadTest 164 @Test testSetChecked()165 public void testSetChecked() { 166 final ToggleButton toggleButton = (ToggleButton) mActivity.findViewById(R.id.toggle1); 167 assertFalse(toggleButton.isChecked()); 168 169 toggleButton.setChecked(true); 170 assertTrue(toggleButton.isChecked()); 171 172 toggleButton.setChecked(false); 173 assertFalse(toggleButton.isChecked()); 174 } 175 176 @UiThreadTest 177 @Test testToggleText()178 public void testToggleText() { 179 final ToggleButton toggleButton = (ToggleButton) mActivity.findViewById(R.id.toggle1); 180 toggleButton.setText("default text"); 181 toggleButton.setTextOn(TEXT_ON); 182 toggleButton.setTextOff(TEXT_OFF); 183 toggleButton.setChecked(true); 184 assertEquals(TEXT_ON, toggleButton.getText().toString()); 185 toggleButton.setChecked(false); 186 assertFalse(toggleButton.isChecked()); 187 assertEquals(TEXT_OFF, toggleButton.getText().toString()); 188 189 // Set the current displaying text as TEXT_OFF. 190 // Then set checked button, but textOn is null. 191 toggleButton.setTextOff(TEXT_OFF); 192 toggleButton.setChecked(false); 193 toggleButton.setTextOn(null); 194 toggleButton.setChecked(true); 195 assertEquals(TEXT_OFF, toggleButton.getText().toString()); 196 197 // Set the current displaying text as TEXT_ON. Then set unchecked button, 198 // but textOff is null. 199 toggleButton.setTextOn(TEXT_ON); 200 toggleButton.setChecked(true); 201 toggleButton.setTextOff(null); 202 toggleButton.setChecked(false); 203 assertEquals(TEXT_ON, toggleButton.getText().toString()); 204 } 205 206 @UiThreadTest 207 @Test testSetBackgroundDrawable()208 public void testSetBackgroundDrawable() { 209 final ToggleButton toggleButton = (ToggleButton) mActivity.findViewById(R.id.toggle1); 210 final Drawable drawable = mActivity.getDrawable(R.drawable.scenery); 211 212 toggleButton.setBackgroundDrawable(drawable); 213 assertSame(drawable, toggleButton.getBackground()); 214 215 // remove the background 216 toggleButton.setBackgroundDrawable(null); 217 assertNull(toggleButton.getBackground()); 218 } 219 220 @UiThreadTest 221 @Test testAccessTextOn()222 public void testAccessTextOn() { 223 final ToggleButton toggleButton = (ToggleButton) mActivity.findViewById(R.id.toggle1); 224 toggleButton.setTextOn("cts"); 225 assertEquals("cts", toggleButton.getTextOn()); 226 227 toggleButton.setTextOn(null); 228 assertNull(toggleButton.getTextOn()); 229 230 toggleButton.setTextOn(""); 231 assertEquals("", toggleButton.getTextOn()); 232 } 233 234 /** 235 * MockToggleButton class for testing. 236 */ 237 public static final class MockToggleButton extends ToggleButton { MockToggleButton(Context context)238 public MockToggleButton(Context context) { 239 super(context); 240 } 241 MockToggleButton(Context context, AttributeSet attrs)242 public MockToggleButton(Context context, AttributeSet attrs) { 243 super(context, attrs); 244 } 245 246 @Override drawableStateChanged()247 protected void drawableStateChanged() { 248 super.drawableStateChanged(); 249 } 250 251 @Override onFinishInflate()252 protected void onFinishInflate() { 253 super.onFinishInflate(); 254 } 255 } 256 } 257