1 /*
2  * Copyright (C) 2016 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 android.system.helpers;
18 
19 import android.app.Instrumentation;
20 import android.content.ContentResolver;
21 import android.graphics.Point;
22 import android.provider.Settings;
23 import android.support.test.uiautomator.By;
24 import android.support.test.uiautomator.UiDevice;
25 import android.support.test.uiautomator.UiObject2;
26 import android.support.test.uiautomator.Until;
27 
28 import org.junit.Assert;
29 
30 /**
31  * Implement common helper methods for Quick settings.
32  */
33 public class QuickSettingsHelper {
34 
35     private UiDevice mDevice = null;
36     private ContentResolver mResolver;
37     private Instrumentation mInstrumentation;
38     private static final int LONG_TIMEOUT = 2000;
39     private static final int SHORT_TIMEOUT = 500;
40 
QuickSettingsHelper(UiDevice device, Instrumentation inst, ContentResolver resolver)41     public QuickSettingsHelper(UiDevice device, Instrumentation inst, ContentResolver resolver) {
42         this.mDevice = device;
43         mInstrumentation = inst;
44         mResolver = resolver;
45     }
46 
47     public enum QuickSettingDefaultTiles {
48         WIFI("Wi-Fi"), SIM("Mobile data"), DND("Do not disturb"), FLASHLIGHT("Flashlight"), SCREEN(
49                 "Auto-rotate screen"), BLUETOOTH("Bluetooth"), AIRPLANE("Airplane mode"),
50                 BRIGHTNESS("Display brightness");
51 
52         private final String name;
53 
QuickSettingDefaultTiles(String name)54         private QuickSettingDefaultTiles(String name) {
55             this.name = name;
56         }
57 
getName()58         public String getName() {
59             return this.name;
60         }
61     };
62 
63     public enum QuickSettingEditMenuTiles {
64         LOCATION("Location"), HOTSPOT("Hotspot"), INVERTCOLORS("Invert colors"),
65                 DATASAVER("Data Saver"), CAST("Cast"), NEARBY("Nearby");
66 
67         private final String name;
68 
QuickSettingEditMenuTiles(String name)69         private QuickSettingEditMenuTiles(String name) {
70             this.name = name;
71         }
72 
getName()73         public String getName() {
74             return this.name;
75         }
76     };
77 
addQuickSettingTileFromEditMenu(String quickSettingTile, String quickSettingTileToReplace, String quickSettingTileToCheckForInCSV)78     public void addQuickSettingTileFromEditMenu(String quickSettingTile,
79             String quickSettingTileToReplace, String quickSettingTileToCheckForInCSV)
80             throws Exception {
81         // Draw down quick settings
82         launchQuickSetting();
83         // Press Edit button
84         UiObject2 quickSettingEdit = mDevice.wait(Until.findObject
85                 (By.descContains("Edit")), LONG_TIMEOUT);
86         quickSettingEdit.click();
87         // Scroll down to bottom to see all QS options on Edit
88         swipeDown();
89         // Drag and drop QS item onto existing QS tile to replace it
90         // This is because we need specific coordinates on which to
91         // drop the quick setting tile.
92         UiObject2 quickSettingTileObject = mDevice.wait(Until.findObject
93                 (By.descContains(quickSettingTile)), LONG_TIMEOUT);
94         Point destination = mDevice.wait(Until.findObject
95                 (By.descContains(quickSettingTileToReplace)), LONG_TIMEOUT)
96                 .getVisibleCenter();
97         Assert.assertNotNull(quickSettingTile + " in Edit menu can't be found",
98                 quickSettingTileObject);
99         Assert.assertNotNull(quickSettingTileToReplace + " in QS menu can't be found",
100                 destination);
101         // Long press the icon, then drag it to the destination slowly.
102         // Without the long press, it ends up scrolling down quick settings.
103         quickSettingTileObject.click(2000);
104         quickSettingTileObject.drag(destination, 1000);
105         // Hit the back button in the QS menu to go back to quick settings.
106         mDevice.wait(Until.findObject(By.descContains("Navigate up")), LONG_TIMEOUT);
107         // Retrieve the quick settings CSV string and verify that the newly
108         // added item is present.
109         String quickSettingsList = Settings.Secure.getString
110                 (mInstrumentation.getContext().getContentResolver(),
111                 "sysui_qs_tiles");
112         Assert.assertTrue(quickSettingTile + " not present in qs tiles after addition.",
113                 quickSettingsList.contains(quickSettingTileToCheckForInCSV));
114     }
115 
setQuickSettingsDefaultTiles()116     public void setQuickSettingsDefaultTiles() throws Exception {
117         modifyListOfQuickSettingsTiles
118                 ("wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,location");
119     }
120 
modifyListOfQuickSettingsTiles(String commaSeparatedList)121     public void modifyListOfQuickSettingsTiles(String commaSeparatedList) throws Exception {
122         Settings.Secure.putString(mInstrumentation.getContext().getContentResolver(),
123                 "sysui_qs_tiles", commaSeparatedList);
124         Thread.sleep(LONG_TIMEOUT);
125     }
126 
launchQuickSetting()127     public void launchQuickSetting() throws Exception {
128         mDevice.pressHome();
129         swipeDown();
130         Thread.sleep(LONG_TIMEOUT);
131         swipeDown();
132     }
133 
swipeUp()134     public void swipeUp() throws Exception {
135         mDevice.swipe(mDevice.getDisplayWidth() / 2, mDevice.getDisplayHeight(),
136                 mDevice.getDisplayWidth() / 2, 0, 30);
137         Thread.sleep(SHORT_TIMEOUT);
138     }
139 
swipeDown()140     public void swipeDown() throws Exception {
141         mDevice.swipe(mDevice.getDisplayWidth() / 2, 0, mDevice.getDisplayWidth() / 2,
142                 mDevice.getDisplayHeight() / 2 + 50, 20);
143         Thread.sleep(SHORT_TIMEOUT);
144     }
145 
swipeLeft()146     public void swipeLeft() {
147         mDevice.swipe(mDevice.getDisplayWidth() / 2, mDevice.getDisplayHeight() / 2, 0,
148                 mDevice.getDisplayHeight() / 2, 5);
149     }
150 }
151