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