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.ContentResolver; 20 import android.platform.test.annotations.Presubmit; 21 import android.provider.Settings; 22 import android.support.test.uiautomator.By; 23 import android.support.test.uiautomator.UiDevice; 24 import android.support.test.uiautomator.UiObject2; 25 import android.support.test.uiautomator.Until; 26 import android.system.helpers.SettingsHelper; 27 import android.system.helpers.SettingsHelper.SettingsType; 28 import android.test.InstrumentationTestCase; 29 import android.test.suitebuilder.annotation.MediumTest; 30 import android.test.suitebuilder.annotation.Suppress; 31 32 import java.util.regex.Pattern; 33 34 public class DisplaySettingsTest extends InstrumentationTestCase { 35 36 private static final String PAGE = Settings.ACTION_DISPLAY_SETTINGS; 37 private static final int TIMEOUT = 2000; 38 private static final FontSetting FONT_SMALL = new FontSetting("Small", 0.85f); 39 private static final FontSetting FONT_NORMAL = new FontSetting("Default", 1.00f); 40 private static final FontSetting FONT_LARGE = new FontSetting("Large", 1.15f); 41 private static final FontSetting FONT_HUGE = new FontSetting("Largest", 1.30f); 42 43 private UiDevice mDevice; 44 private ContentResolver mResolver; 45 private SettingsHelper mHelper; 46 47 @Override setUp()48 public void setUp() throws Exception { 49 super.setUp(); 50 mDevice = UiDevice.getInstance(getInstrumentation()); 51 mDevice.setOrientationNatural(); 52 mResolver = getInstrumentation().getContext().getContentResolver(); 53 mHelper = new SettingsHelper(); 54 } 55 56 @Override tearDown()57 public void tearDown() throws Exception { 58 // reset settings we touched that may impact others 59 Settings.System.putFloat(mResolver, Settings.System.FONT_SCALE, 1.00f); 60 mDevice.waitForIdle(); 61 super.tearDown(); 62 } 63 64 @Presubmit 65 @MediumTest testAdaptiveBrightness()66 public void testAdaptiveBrightness() throws Exception { 67 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 68 mHelper.scrollVert(true); 69 Thread.sleep(1000); 70 71 assertTrue(mHelper.verifyToggleSetting(SettingsType.SYSTEM, PAGE, "Adaptive brightness", 72 Settings.System.SCREEN_BRIGHTNESS_MODE)); 73 assertTrue(mHelper.verifyToggleSetting(SettingsType.SYSTEM, PAGE, "Adaptive brightness", 74 Settings.System.SCREEN_BRIGHTNESS_MODE)); 75 } 76 77 78 // blocked on b/27487224 79 @MediumTest 80 @Suppress testDaydreamToggle()81 public void testDaydreamToggle() throws Exception { 82 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 83 clickMore(); 84 Pattern p = Pattern.compile("On|Off"); 85 mHelper.clickSetting("Screen saver"); 86 Thread.sleep(1000); 87 try { 88 assertTrue(mHelper.verifyToggleSetting(SettingsType.SECURE, PAGE, p, 89 Settings.Secure.SCREENSAVER_ENABLED, false)); 90 assertTrue(mHelper.verifyToggleSetting(SettingsType.SECURE, PAGE, p, 91 Settings.Secure.SCREENSAVER_ENABLED, false)); 92 } finally { 93 mDevice.pressBack(); 94 } 95 } 96 97 @MediumTest testAccelRotation()98 public void testAccelRotation() throws Exception { 99 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 100 mHelper.scrollVert(true); 101 clickMore(); 102 Thread.sleep(4000); 103 int currentAccelSetting = Settings.System.getInt( 104 mResolver, Settings.System.ACCELEROMETER_ROTATION); 105 mHelper.clickSetting("Auto-rotate screen"); 106 int newAccelSetting = Settings.System.getInt( 107 mResolver, Settings.System.ACCELEROMETER_ROTATION); 108 assertTrue("Accelorometer setting unchanged after toggle", currentAccelSetting != newAccelSetting); 109 mHelper.clickSetting("Auto-rotate screen"); 110 int revertedAccelSetting = Settings.System.getInt( 111 mResolver, Settings.System.ACCELEROMETER_ROTATION); 112 assertTrue("Accelorometer setting unchanged after toggle", revertedAccelSetting != newAccelSetting); 113 } 114 115 @MediumTest testDaydream()116 public void testDaydream() throws Exception { 117 Settings.Secure.putInt(mResolver, Settings.Secure.SCREENSAVER_ENABLED, 1); 118 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 119 clickMore(); 120 mHelper.scrollVert(false); 121 mDevice.wait(Until.findObject(By.text("Screen saver")), TIMEOUT).click(); 122 try { 123 assertTrue(mHelper.verifyRadioSetting(SettingsType.SECURE, PAGE, 124 "Current screen saver", "Clock", Settings.Secure.SCREENSAVER_COMPONENTS, 125 "com.google.android.deskclock/com.android.deskclock.Screensaver")); 126 assertTrue(mHelper.verifyRadioSetting(SettingsType.SECURE, PAGE, 127 "Current screen saver", "Colors", Settings.Secure.SCREENSAVER_COMPONENTS, 128 "com.android.dreams.basic/com.android.dreams.basic.Colors")); 129 assertTrue(mHelper.verifyRadioSetting(SettingsType.SECURE, PAGE, 130 "Current screen saver", "Photos", Settings.Secure.SCREENSAVER_COMPONENTS, 131 "com.google.android.apps.photos/com.google.android.apps.photos.daydream" 132 + ".PhotosDreamService")); 133 } finally { 134 mDevice.pressBack(); 135 Thread.sleep(2000); 136 } 137 } 138 139 @Presubmit 140 @MediumTest testSleep15Seconds()141 public void testSleep15Seconds() throws Exception { 142 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 143 mHelper.scrollVert(true); 144 assertTrue(mHelper.verifyRadioSetting(SettingsType.SYSTEM, PAGE, 145 "Sleep", "15 seconds", Settings.System.SCREEN_OFF_TIMEOUT, "15000")); 146 } 147 148 @MediumTest testSleep30Seconds()149 public void testSleep30Seconds() throws Exception { 150 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 151 mHelper.scrollVert(true); 152 assertTrue(mHelper.verifyRadioSetting(SettingsType.SYSTEM, PAGE, 153 "Sleep", "30 seconds", Settings.System.SCREEN_OFF_TIMEOUT, "30000")); 154 } 155 156 @MediumTest testSleep1Minute()157 public void testSleep1Minute() throws Exception { 158 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 159 mHelper.scrollVert(true); 160 assertTrue(mHelper.verifyRadioSetting(SettingsType.SYSTEM, PAGE, 161 "Sleep", "1 minute", Settings.System.SCREEN_OFF_TIMEOUT, "60000")); 162 } 163 164 @MediumTest testSleep2Minutes()165 public void testSleep2Minutes() throws Exception { 166 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 167 mHelper.scrollVert(true); 168 assertTrue(mHelper.verifyRadioSetting(SettingsType.SYSTEM, PAGE, 169 "Sleep", "2 minutes", Settings.System.SCREEN_OFF_TIMEOUT, "120000")); 170 } 171 172 @MediumTest testSleep5Minutes()173 public void testSleep5Minutes() throws Exception { 174 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 175 mHelper.scrollVert(true); 176 assertTrue(mHelper.verifyRadioSetting(SettingsType.SYSTEM, PAGE, 177 "Sleep", "5 minutes", Settings.System.SCREEN_OFF_TIMEOUT, "300000")); 178 } 179 180 @MediumTest testSleep10Minutes()181 public void testSleep10Minutes() throws Exception { 182 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 183 mHelper.scrollVert(true); 184 assertTrue(mHelper.verifyRadioSetting(SettingsType.SYSTEM, PAGE, 185 "Sleep", "10 minutes", Settings.System.SCREEN_OFF_TIMEOUT, "600000")); 186 } 187 188 @MediumTest testSleep30Minutes()189 public void testSleep30Minutes() throws Exception { 190 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 191 mHelper.scrollVert(true); 192 assertTrue(mHelper.verifyRadioSetting(SettingsType.SYSTEM, PAGE, 193 "Sleep", "30 minutes", Settings.System.SCREEN_OFF_TIMEOUT, "1800000")); 194 } 195 196 @Presubmit 197 @MediumTest testFontSizeLarge()198 public void testFontSizeLarge() throws Exception { 199 verifyFontSizeSetting(1.00f, FONT_LARGE); 200 // Leaving the font size at large can make later tests fail, so reset it 201 Settings.System.putFloat(mResolver, Settings.System.FONT_SCALE, 1.00f); 202 // It takes a second for the new font size to be picked up 203 Thread.sleep(2000); 204 } 205 206 @MediumTest testFontSizeDefault()207 public void testFontSizeDefault() throws Exception { 208 verifyFontSizeSetting(1.15f, FONT_NORMAL); 209 } 210 211 @MediumTest testFontSizeLargest()212 public void testFontSizeLargest() throws Exception { 213 verifyFontSizeSetting(1.00f, FONT_HUGE); 214 // Leaving the font size at huge can make later tests fail, so reset it 215 Settings.System.putFloat(mResolver, Settings.System.FONT_SCALE, 1.00f); 216 // It takes a second for the new font size to be picked up 217 Thread.sleep(2000); 218 } 219 220 @MediumTest testFontSizeSmall()221 public void testFontSizeSmall() throws Exception { 222 verifyFontSizeSetting(1.00f, FONT_SMALL); 223 } 224 verifyFontSizeSetting(float resetValue, FontSetting setting)225 private void verifyFontSizeSetting(float resetValue, FontSetting setting) 226 throws Exception { 227 Settings.System.putFloat(mResolver, Settings.System.FONT_SCALE, resetValue); 228 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 229 clickMore(); 230 mHelper.clickSetting("Font size"); 231 try { 232 mDevice.wait(Until.findObject(By.desc(setting.getName())), TIMEOUT).click(); 233 Thread.sleep(1000); 234 float changedValue = Settings.System.getFloat( 235 mResolver, Settings.System.FONT_SCALE); 236 assertEquals(setting.getSize(), changedValue, 0.0001); 237 } finally { 238 // Make sure to back out of the font menu 239 mDevice.pressBack(); 240 } 241 } 242 clickMore()243 private void clickMore() throws InterruptedException { 244 UiObject2 more = mDevice.wait(Until.findObject(By.text("Advanced")), TIMEOUT); 245 if (more != null) { 246 more.click(); 247 Thread.sleep(TIMEOUT); 248 } 249 } 250 251 private static class FontSetting { 252 private final String mSizeName; 253 private final float mSizeVal; 254 FontSetting(String sizeName, float sizeVal)255 public FontSetting(String sizeName, float sizeVal) { 256 mSizeName = sizeName; 257 mSizeVal = sizeVal; 258 } 259 getName()260 public String getName() { 261 return mSizeName; 262 } 263 getSize()264 public float getSize() { 265 return mSizeVal; 266 } 267 } 268 } 269