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