1 /* 2 * Copyright (C) 2016 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.settings.functional; 18 19 import android.content.Context; 20 import android.net.wifi.WifiManager; 21 import android.os.RemoteException; 22 import android.platform.test.helpers.SettingsHelperImpl; 23 import android.provider.Settings; 24 import android.support.test.uiautomator.By; 25 import android.support.test.uiautomator.Direction; 26 import android.support.test.uiautomator.UiDevice; 27 import android.support.test.uiautomator.UiObject2; 28 import android.support.test.uiautomator.Until; 29 import android.test.InstrumentationTestCase; 30 import android.test.suitebuilder.annotation.MediumTest; 31 import android.test.suitebuilder.annotation.Suppress; 32 33 public class AccessibilitySettingsTests extends InstrumentationTestCase { 34 35 private static final String SETTINGS_PACKAGE = "com.android.settings"; 36 private static final int TIMEOUT = 2000; 37 private UiDevice mDevice; 38 39 @Override 40 public void setUp() throws Exception { 41 super.setUp(); 42 mDevice = UiDevice.getInstance(getInstrumentation()); 43 try { 44 mDevice.setOrientationNatural(); 45 } catch (RemoteException e) { 46 throw new RuntimeException("failed to freeze device orientaion", e); 47 } 48 } 49 50 @Override 51 protected void tearDown() throws Exception { 52 // Need to finish settings activity 53 mDevice.pressBack(); 54 mDevice.pressHome(); 55 mDevice.waitForIdle(); 56 super.tearDown(); 57 } 58 59 @MediumTest 60 public void testHighContrastTextOn() throws Exception { 61 verifyAccessibilitySettingOnOrOff("High contrast text", 62 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, 0, 1); 63 } 64 65 @MediumTest 66 public void testHighContrastTextOff() throws Exception { 67 verifyAccessibilitySettingOnOrOff("High contrast text", 68 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, 1, 0); 69 } 70 71 @MediumTest 72 public void testPowerButtonEndsCallOn() throws Exception { 73 verifyAccessibilitySettingOnOrOff("Power button ends call", 74 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR, 1, 2); 75 } 76 77 @MediumTest 78 public void testPowerButtonEndsCallOff() throws Exception { 79 verifyAccessibilitySettingOnOrOff("Power button ends call", 80 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR, 2, 1); 81 } 82 83 /* Suppressing these four tests. The settings don't play 84 * nice with Settings.System.putInt or Settings.Secure.putInt. 85 * Need further clarification. Filed bug b/27792029 86 */ 87 @Suppress 88 @MediumTest 89 public void testAutoRotateScreenOn() throws Exception { 90 verifyAccessibilitySettingOnOrOff("Auto-rotate screen", 91 Settings.System.ACCELEROMETER_ROTATION, 0, 1); 92 } 93 94 @Suppress 95 @MediumTest 96 public void testAutoRotateScreenOff() throws Exception { 97 verifyAccessibilitySettingOnOrOff("Auto-rotate screen", 98 Settings.System.ACCELEROMETER_ROTATION, 1, 0); 99 } 100 101 @Suppress 102 @MediumTest 103 public void testMonoAudioOn() throws Exception { 104 verifyAccessibilitySettingOnOrOff("Mono audio", 105 Settings.System.MASTER_MONO, 0, 1); 106 } 107 108 @Suppress 109 @MediumTest 110 public void testMonoAudioOff() throws Exception { 111 verifyAccessibilitySettingOnOrOff("Mono audio", 112 Settings.System.MASTER_MONO, 1, 0); 113 } 114 115 @MediumTest 116 public void testSpeakPasswordsOn() throws Exception { 117 verifyAccessibilitySettingOnOrOff("Speak passwords", 118 Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, 0, 1); 119 } 120 121 @MediumTest 122 public void testSpeakPasswordsOff() throws Exception { 123 verifyAccessibilitySettingOnOrOff("Speak passwords", 124 Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, 1, 0); 125 } 126 127 @MediumTest 128 public void testLargeMousePointerOn() throws Exception { 129 verifyAccessibilitySettingOnOrOff("Large mouse pointer", 130 Settings.Secure.ACCESSIBILITY_LARGE_POINTER_ICON, 0, 1); 131 } 132 133 @MediumTest 134 public void testLargeMousePointerOff() throws Exception { 135 verifyAccessibilitySettingOnOrOff("Large mouse pointer", 136 Settings.Secure.ACCESSIBILITY_LARGE_POINTER_ICON, 1, 0); 137 } 138 139 @MediumTest 140 public void testColorCorrection() throws Exception { 141 verifySettingToggleAfterScreenLoad("Color correction", 142 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED); 143 } 144 145 // Suppressing this test, since UiAutomator + talkback don't play nice 146 @Suppress 147 @MediumTest 148 public void testTalkback() throws Exception { 149 verifySettingToggleAfterScreenLoad("TalkBack", 150 Settings.Secure.ACCESSIBILITY_ENABLED); 151 } 152 153 @MediumTest 154 public void testCaptions() throws Exception { 155 verifySettingToggleAfterScreenLoad("Captions", 156 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED); 157 } 158 159 @MediumTest 160 public void testMagnificationGesture() throws Exception { 161 verifySettingToggleAfterScreenLoad("Magnification gesture", 162 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED); 163 } 164 165 @MediumTest 166 public void testClickAfterPointerStopsMoving() throws Exception { 167 verifySettingToggleAfterScreenLoad("Click after pointer stops moving", 168 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED); 169 } 170 171 public void launchAccessibilitySettings() throws Exception { 172 SettingsHelperImpl.launchSettingsPage(getInstrumentation().getContext(), 173 Settings.ACTION_ACCESSIBILITY_SETTINGS); 174 } 175 176 private void verifyAccessibilitySettingOnOrOff(String settingText, 177 String settingFlag, int initialFlagValue, int expectedFlagValue) throws Exception { 178 Settings.Secure.putInt(getInstrumentation().getContext().getContentResolver(), 179 settingFlag, initialFlagValue); 180 launchAccessibilitySettings(); 181 UiObject2 settingsTitle = findItemOnScreen(settingText); 182 settingsTitle.click(); 183 Thread.sleep(TIMEOUT); 184 int settingValue = Settings.Secure 185 .getInt(getInstrumentation().getContext().getContentResolver(), settingFlag); 186 assertEquals(settingText + " not correctly set after toggle", expectedFlagValue, settingValue); 187 } 188 189 private void verifySettingToggleAfterScreenLoad(String settingText, String settingFlag) throws Exception { 190 // Load accessibility settings 191 launchAccessibilitySettings(); 192 Settings.Secure.putInt(getInstrumentation().getContext().getContentResolver(), 193 settingFlag, 0); 194 Thread.sleep(TIMEOUT); 195 // Tap on setting required 196 UiObject2 settingTitle = findItemOnScreen(settingText); 197 // Load screen 198 settingTitle.click(); 199 Thread.sleep(TIMEOUT); 200 // Toggle value 201 UiObject2 settingToggle = mDevice.wait(Until.findObject(By.text("Off")), 202 TIMEOUT); 203 settingToggle.click(); 204 dismissOpenDialog(); 205 Thread.sleep(TIMEOUT); 206 // Assert new value 207 int settingValue = Settings.Secure. 208 getInt(getInstrumentation().getContext().getContentResolver(), settingFlag); 209 assertEquals(settingText + " value not set correctly", 1, settingValue); 210 // Toogle value 211 settingToggle.click(); 212 dismissOpenDialog(); 213 mDevice.pressBack(); 214 Thread.sleep(TIMEOUT); 215 // Assert reset to old value 216 settingValue = Settings.Secure. 217 getInt(getInstrumentation().getContext().getContentResolver(), settingFlag); 218 assertEquals(settingText + " value not set correctly", 0, settingValue); 219 } 220 221 private UiObject2 findItemOnScreen(String item) throws Exception { 222 int count = 0; 223 UiObject2 settingsPanel = mDevice.wait(Until.findObject 224 (By.res(SETTINGS_PACKAGE, "list")), TIMEOUT); 225 while (settingsPanel.fling(Direction.UP) && count < 3) { 226 count++; 227 } 228 count = 0; 229 UiObject2 setting = null; 230 while(count < 3 && setting == null) { 231 setting = mDevice.wait(Until.findObject(By.text(item)), TIMEOUT); 232 if (setting == null) { 233 settingsPanel.scroll(Direction.DOWN, 1.0f); 234 } 235 count++; 236 } 237 return setting; 238 } 239 240 private void dismissOpenDialog() throws Exception { 241 UiObject2 okButton = mDevice.wait(Until.findObject 242 (By.res("android:id/button1")), TIMEOUT*2); 243 if (okButton != null) { 244 okButton.click(); 245 } 246 } 247 } 248