1 package android.settings.functional; 2 3 import android.app.NotificationManager; 4 import android.content.ContentResolver; 5 import android.content.Context; 6 import android.os.Handler; 7 import android.os.SystemClock; 8 import android.provider.Settings; 9 import android.service.notification.ZenModeConfig; 10 import android.platform.test.helpers.SettingsHelperImpl; 11 import android.platform.test.helpers.SettingsHelperImpl.SettingsType; 12 import android.support.test.uiautomator.By; 13 import android.support.test.uiautomator.UiDevice; 14 import android.support.test.uiautomator.Until; 15 import android.test.InstrumentationTestCase; 16 import android.test.suitebuilder.annotation.MediumTest; 17 import android.test.suitebuilder.annotation.Suppress; 18 19 import com.android.server.notification.ConditionProviders; 20 import com.android.server.notification.ManagedServices.UserProfiles; 21 import com.android.server.notification.ZenModeHelper; 22 23 public class SoundSettingsTest extends InstrumentationTestCase { 24 private static final String PAGE = Settings.ACTION_SOUND_SETTINGS; 25 private static final int TIMEOUT = 2000; 26 27 private UiDevice mDevice; 28 private ContentResolver mResolver; 29 private SettingsHelperImpl mHelper; 30 private ZenModeHelper mZenHelper; 31 32 @Override setUp()33 public void setUp() throws Exception { 34 super.setUp(); 35 mDevice = UiDevice.getInstance(getInstrumentation()); 36 mDevice.setOrientationNatural(); 37 mResolver = getInstrumentation().getContext().getContentResolver(); 38 mHelper = new SettingsHelperImpl(getInstrumentation()); 39 ConditionProviders cps = new ConditionProviders( 40 getInstrumentation().getContext(), new Handler(), new UserProfiles()); 41 mZenHelper = new ZenModeHelper(getInstrumentation().getContext(), 42 getInstrumentation().getContext().getMainLooper(), 43 cps); 44 } 45 46 @Override tearDown()47 public void tearDown() throws Exception { 48 mDevice.unfreezeRotation(); 49 super.tearDown(); 50 } 51 52 @MediumTest testCallVibrate()53 public void testCallVibrate() throws Exception { 54 assertTrue(mHelper.verifyToggleSetting(SettingsType.SYSTEM, PAGE, 55 "Also vibrate for calls", Settings.System.VIBRATE_WHEN_RINGING)); 56 assertTrue(mHelper.verifyToggleSetting(SettingsType.SYSTEM, PAGE, 57 "Also vibrate for calls", Settings.System.VIBRATE_WHEN_RINGING)); 58 } 59 60 @MediumTest testOtherSounds()61 public void testOtherSounds() throws Exception { 62 SettingsHelperImpl.launchSettingsPage(getInstrumentation().getContext(), PAGE); 63 mHelper.scrollVert(false); 64 Thread.sleep(1000); 65 mHelper.clickSetting("Other sounds"); 66 Thread.sleep(1000); 67 try { 68 assertTrue("Dial pad tones not toggled", mHelper.verifyToggleSetting( 69 SettingsType.SYSTEM, PAGE, "Dial pad tones", 70 Settings.System.DTMF_TONE_WHEN_DIALING)); 71 assertTrue("Screen locking sounds not toggled", 72 mHelper.verifyToggleSetting(SettingsType.SYSTEM, PAGE, 73 "Screen locking sounds", Settings.System.LOCKSCREEN_SOUNDS_ENABLED)); 74 assertTrue("Charging sounds not toggled", 75 mHelper.verifyToggleSetting(SettingsType.GLOBAL, PAGE, 76 "Charging sounds", Settings.Global.CHARGING_SOUNDS_ENABLED)); 77 assertTrue("Touch sounds not toggled", 78 mHelper.verifyToggleSetting(SettingsType.SYSTEM, PAGE, 79 "Touch sounds", Settings.System.SOUND_EFFECTS_ENABLED)); 80 assertTrue("Vibrate on tap not toggled", 81 mHelper.verifyToggleSetting(SettingsType.SYSTEM, PAGE, 82 "Vibrate on tap", Settings.System.HAPTIC_FEEDBACK_ENABLED)); 83 } finally { 84 mDevice.pressBack(); 85 } 86 } 87 88 @MediumTest 89 @Suppress testDndPriorityAllows()90 public void testDndPriorityAllows() throws Exception { 91 SettingsHelperImpl.launchSettingsPage(getInstrumentation().getContext(), PAGE); 92 Context ctx = getInstrumentation().getContext(); 93 try { 94 mHelper.clickSetting("Do not disturb"); 95 try { 96 mHelper.clickSetting("Priority only allows"); 97 ZenModeConfig baseZenCfg = mZenHelper.getConfig(); 98 99 mHelper.clickSetting("Reminders"); 100 mHelper.clickSetting("Events"); 101 mHelper.clickSetting("Repeat callers"); 102 103 ZenModeConfig changedCfg = mZenHelper.getConfig(); 104 assertFalse(baseZenCfg.allowReminders == changedCfg.allowReminders); 105 assertFalse(baseZenCfg.allowEvents == changedCfg.allowEvents); 106 assertFalse(baseZenCfg.allowRepeatCallers == changedCfg.allowRepeatCallers); 107 108 mHelper.clickSetting("Reminders"); 109 mHelper.clickSetting("Events"); 110 mHelper.clickSetting("Repeat callers"); 111 112 changedCfg = mZenHelper.getConfig(); 113 assertTrue(baseZenCfg.allowReminders == changedCfg.allowReminders); 114 assertTrue(baseZenCfg.allowEvents == changedCfg.allowEvents); 115 assertTrue(baseZenCfg.allowRepeatCallers == changedCfg.allowRepeatCallers); 116 117 mHelper.clickSetting("Messages"); 118 mHelper.clickSetting("From anyone"); 119 mHelper.clickSetting("Calls"); 120 mHelper.clickSetting("From anyone"); 121 122 changedCfg = mZenHelper.getConfig(); 123 assertFalse(baseZenCfg.allowCallsFrom == changedCfg.allowCallsFrom); 124 assertFalse(baseZenCfg.allowMessagesFrom == changedCfg.allowMessagesFrom); 125 } finally { 126 mDevice.pressBack(); 127 } 128 } finally { 129 mDevice.pressHome(); 130 } 131 } 132 133 @MediumTest 134 @Suppress testDndVisualInterruptions()135 public void testDndVisualInterruptions() throws Exception { 136 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 137 try { 138 mHelper.clickSetting("Do not disturb"); 139 try { 140 mHelper.clickSetting("Visual interruptions"); 141 ZenModeConfig baseZenCfg = mZenHelper.getConfig(); 142 143 mHelper.clickSetting("Block when screen is on"); 144 mHelper.clickSetting("Block when screen is off"); 145 146 ZenModeConfig changedCfg = mZenHelper.getConfig(); 147 assertFalse(baseZenCfg.allowWhenScreenOff == changedCfg.allowWhenScreenOff); 148 assertFalse(baseZenCfg.allowWhenScreenOn == changedCfg.allowWhenScreenOn); 149 150 mHelper.clickSetting("Block when screen is on"); 151 mHelper.clickSetting("Block when screen is off"); 152 153 changedCfg = mZenHelper.getConfig(); 154 assertTrue(baseZenCfg.allowWhenScreenOff == changedCfg.allowWhenScreenOff); 155 assertTrue(baseZenCfg.allowWhenScreenOn == changedCfg.allowWhenScreenOn); 156 } finally { 157 mDevice.pressBack(); 158 } 159 } finally { 160 mDevice.pressBack(); 161 } 162 } 163 164 /* 165 * Rather than verifying every ringtone, verify the ones least likely to change 166 * (None and Hangouts) and an arbitrary one from the ringtone pool. 167 */ 168 @MediumTest testPhoneRingtoneNone()169 public void testPhoneRingtoneNone() throws Exception { 170 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 171 mHelper.clickSetting("Phone ringtone"); 172 verifyRingtone(new RingtoneSetting("None", "null"), 173 Settings.System.RINGTONE, ScrollDir.UP); 174 } 175 176 @MediumTest 177 @Suppress testPhoneRingtoneHangouts()178 public void testPhoneRingtoneHangouts() throws Exception { 179 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 180 mHelper.clickSetting("Phone ringtone"); 181 verifyRingtone(new RingtoneSetting("Hangouts Call", "31"), Settings.System.RINGTONE); 182 } 183 184 @MediumTest testPhoneRingtoneUmbriel()185 public void testPhoneRingtoneUmbriel() throws Exception { 186 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 187 mHelper.clickSetting("Phone ringtone"); 188 verifyRingtone(new RingtoneSetting("Umbriel", "49"), 189 Settings.System.RINGTONE, ScrollDir.DOWN); 190 } 191 192 @MediumTest testNotificationRingtoneNone()193 public void testNotificationRingtoneNone() throws Exception { 194 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 195 mHelper.clickSetting("Default notification ringtone"); 196 verifyRingtone(new RingtoneSetting("None", "null"), 197 Settings.System.NOTIFICATION_SOUND, ScrollDir.UP); 198 } 199 200 @MediumTest 201 @Suppress testNotificationRingtoneHangouts()202 public void testNotificationRingtoneHangouts() throws Exception { 203 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 204 mHelper.clickSetting("Default notification ringtone"); 205 verifyRingtone(new RingtoneSetting("Hangouts Message", "30"), 206 Settings.System.NOTIFICATION_SOUND); 207 } 208 209 @MediumTest testNotificationRingtoneTitan()210 public void testNotificationRingtoneTitan() throws Exception { 211 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 212 mHelper.clickSetting("Default notification ringtone"); 213 verifyRingtone(new RingtoneSetting("Titan", "35"), 214 Settings.System.NOTIFICATION_SOUND, ScrollDir.DOWN); 215 } 216 217 @MediumTest testAlarmRingtoneNone()218 public void testAlarmRingtoneNone() throws Exception { 219 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 220 mHelper.clickSetting("Default alarm ringtone"); 221 verifyRingtone(new RingtoneSetting("None", "null"), 222 Settings.System.ALARM_ALERT, ScrollDir.UP); 223 } 224 225 @MediumTest testAlarmRingtoneXenon()226 public void testAlarmRingtoneXenon() throws Exception { 227 SettingsHelper.launchSettingsPage(getInstrumentation().getContext(), PAGE); 228 mHelper.clickSetting("Default alarm ringtone"); 229 verifyRingtone(new RingtoneSetting("Xenon", "22"), 230 Settings.System.ALARM_ALERT, ScrollDir.DOWN); 231 } 232 verifyRingtone(RingtoneSetting r, String settingName)233 private void verifyRingtone(RingtoneSetting r, String settingName) { 234 verifyRingtone(r, settingName, ScrollDir.NOSCROLL); 235 } 236 verifyRingtone(RingtoneSetting r, String settingName, ScrollDir dir)237 private void verifyRingtone(RingtoneSetting r, String settingName, ScrollDir dir) { 238 if (dir != ScrollDir.NOSCROLL) { 239 mHelper.scrollVert(dir == ScrollDir.UP); 240 SystemClock.sleep(1000); 241 } 242 mDevice.wait(Until.findObject(By.text(r.getName())), TIMEOUT).click(); 243 mDevice.wait(Until.findObject(By.text("OK")), TIMEOUT).click(); 244 SystemClock.sleep(1000); 245 if (r.getVal().equals("null")) { 246 assertEquals(null, 247 Settings.System.getString(mResolver, settingName)); 248 } else if (r.getName().contains("Hangouts")) { 249 assertEquals("content://media/external/audio/media/" + r.getVal(), 250 Settings.System.getString(mResolver, settingName)); 251 } else { 252 assertEquals("content://media/internal/audio/media/" + r.getVal(), 253 Settings.System.getString(mResolver, settingName)); 254 } 255 } 256 257 private enum ScrollDir { 258 UP, 259 DOWN, 260 NOSCROLL 261 } 262 263 class RingtoneSetting { 264 private final String mName; 265 private final String mMediaVal; RingtoneSetting(String name, String fname)266 public RingtoneSetting(String name, String fname) { 267 mName = name; 268 mMediaVal = fname; 269 } getName()270 public String getName() { 271 return mName; 272 } getVal()273 public String getVal() { 274 return mMediaVal; 275 } 276 } 277 } 278