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.Intent; 20 import android.os.RemoteException; 21 import android.provider.Settings; 22 import android.support.test.uiautomator.By; 23 import android.support.test.uiautomator.BySelector; 24 import android.support.test.uiautomator.Direction; 25 import android.support.test.uiautomator.UiDevice; 26 import android.support.test.uiautomator.UiObject2; 27 import android.support.test.uiautomator.Until; 28 import android.system.helpers.ActivityHelper; 29 import android.system.helpers.SettingsHelper; 30 import android.test.InstrumentationTestCase; 31 import android.test.suitebuilder.annotation.MediumTest; 32 import android.util.Log; 33 import android.widget.ListView; 34 35 /** Verifies that you can get to the notification app listing page from the apps & notifications 36 * page */ 37 public class NotificationSettingsTests extends InstrumentationTestCase { 38 private static final boolean LOCAL_LOGV = false; 39 private static final String TAG = "NotifiSettingsTests"; 40 private static final int TIMEOUT = 2000; 41 private ActivityHelper mActivityHelper = null; 42 private SettingsHelper mSettingsHelper = null; 43 44 private UiDevice mDevice; 45 @Override setUp()46 public void setUp() throws Exception { 47 if (LOCAL_LOGV) { 48 Log.d(TAG, "-------"); 49 } 50 super.setUp(); 51 mDevice = UiDevice.getInstance(getInstrumentation()); 52 mActivityHelper = ActivityHelper.getInstance(); 53 mSettingsHelper = SettingsHelper.getInstance(); 54 try { 55 mDevice.setOrientationNatural(); 56 } catch (RemoteException e) { 57 throw new RuntimeException("Failed to freeze device orientaion", e); 58 } 59 60 // make sure we are in a clean state before starting the test 61 mDevice.pressHome(); 62 Thread.sleep(TIMEOUT * 2); 63 launchAppsSettings(); 64 } 65 66 @Override tearDown()67 protected void tearDown() throws Exception { 68 mDevice.pressBack(); 69 mDevice.pressHome(); // finish settings activity 70 mDevice.waitForIdle(TIMEOUT * 2); // give UI time to finish animating 71 super.tearDown(); 72 } 73 74 @MediumTest testNotificationsSettingsListForCalculator()75 public void testNotificationsSettingsListForCalculator() { 76 UiObject2 configureNotifications = mDevice.wait( 77 Until.findObject(By.text("Notifications")), TIMEOUT); 78 configureNotifications.click(); 79 mDevice.wait(Until.findObject(By.text("Blink light")), TIMEOUT); 80 UiObject2 appNotifications = mDevice.wait( 81 Until.findObject(By.text("On for all apps")), TIMEOUT); 82 appNotifications.click(); 83 UiObject2 view = 84 mDevice.wait( 85 Until.findObject(By.text("All apps")), TIMEOUT); 86 assertNotNull("Could not find Settings > Apps screen", view); 87 UiObject2 app = mDevice.wait(Until.findObject(By.text("Calculator")), TIMEOUT); 88 assertNotNull("Could not find Calculator notification settings", app); 89 } 90 91 92 @MediumTest testNotificationsSettingsListForPhone()93 public void testNotificationsSettingsListForPhone() { 94 UiObject2 configureNotifications = mDevice.wait( 95 Until.findObject(By.text("Notifications")), TIMEOUT); 96 configureNotifications.click(); 97 mDevice.wait(Until.findObject(By.text("Blink light")), TIMEOUT); 98 UiObject2 appNotifications = mDevice.wait( 99 Until.findObject(By.text("On for all apps")), TIMEOUT); 100 appNotifications.click(); 101 UiObject2 view = 102 mDevice.wait( 103 Until.findObject(By.text("All apps")), TIMEOUT); 104 assertNotNull("Could not find Settings > Apps screen", view); 105 106 final BySelector preferenceListSelector = By.clazz(ListView.class).res("android:id/list"); 107 UiObject2 apps = mDevice.wait(Until.findObject(preferenceListSelector), TIMEOUT); 108 109 UiObject2 phone = scrollTo(mDevice, apps, By.text("Phone"), Direction.DOWN); 110 assertNotNull("Could not find Phone notification settings", phone); 111 phone.click(); 112 UiObject2 incomingCalls = mDevice.wait(Until.findObject(By.text("Incoming calls")), TIMEOUT); 113 assertNotNull("Could not find incoming calls channel", incomingCalls); 114 incomingCalls.click(); 115 116 // here's the meat of this test: make sure that you cannot change 117 // most settings for this channel 118 119 UiObject2 importance = mDevice.wait(Until.findObject(By.text("Importance")), TIMEOUT); 120 assertNotNull("Could not find importance toggle", importance); 121 assertFalse(importance.isEnabled()); 122 assertFalse(mDevice.wait(Until.findObject(By.text("Sound")), TIMEOUT).isEnabled());; 123 assertFalse(mDevice.wait(Until.findObject(By.text("Vibrate")), TIMEOUT).isEnabled()); 124 assertFalse(mDevice.wait(Until.findObject(By.text("Override Do Not Disturb")), TIMEOUT).isEnabled()); 125 126 127 128 129 130 131 } 132 scrollTo(UiDevice device, UiObject2 scrollable, BySelector target, Direction direction)133 private UiObject2 scrollTo(UiDevice device, UiObject2 scrollable, 134 BySelector target, Direction direction) { 135 while (!device.hasObject(target) && scrollable.scroll(direction, 1.0f)) { 136 // continue 137 } 138 if (!device.hasObject(target)) { 139 // Scroll once more if not found; in some cases UiObject2.scroll can return false when 140 // the last item is not fully visible yet for list views. 141 scrollable.scroll(direction, 1.0f); 142 } 143 return device.findObject(target); 144 } 145 146 launchAppsSettings()147 private void launchAppsSettings() throws Exception { 148 Intent appsSettingsIntent = new Intent(Settings.ACTION_SETTINGS); 149 mActivityHelper.launchIntent(appsSettingsIntent); 150 mSettingsHelper.flingSettingsToStart(); 151 UiObject2 view = mDevice.wait( 152 Until.findObject(By.text("Apps & notifications")), TIMEOUT); 153 view.click(); 154 UiObject2 title = mDevice.wait( 155 Until.findObject(By.text("Apps & notifications")), TIMEOUT); 156 assertNotNull("Could not find Settings > Apps & notifications screen", title); 157 } 158 } 159