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.Direction;
24 import android.support.test.uiautomator.UiDevice;
25 import android.support.test.uiautomator.UiObject2;
26 import android.support.test.uiautomator.Until;
27 import android.system.helpers.ActivityHelper;
28 import android.test.InstrumentationTestCase;
29 import android.test.suitebuilder.annotation.MediumTest;
30 import android.util.Log;
31 
32 /** Verifies basic functionality of the About Phone screen */
33 public class AppsSettingsTests extends InstrumentationTestCase {
34     private static final boolean LOCAL_LOGV = false;
35     private static final String SETTINGS_PACKAGE = "com.android.settings";
36     private static final String TAG = "AboutPhoneSettingsTest";
37     private static final int TIMEOUT = 2000;
38     private ActivityHelper mActivityHelper = null;
39 
40     private UiDevice mDevice;
41 
42     private static final String[] sResourceTexts = {
43         "Storage",
44         "Data usage",
45         "Permissions",
46         "App notifications",
47         "Open by default",
48         "Battery",
49         "Memory"
50     };
51 
52     @Override
setUp()53     public void setUp() throws Exception {
54         if (LOCAL_LOGV) {
55             Log.d(TAG, "-------");
56         }
57         super.setUp();
58         mDevice = UiDevice.getInstance(getInstrumentation());
59         mActivityHelper = ActivityHelper.getInstance();
60         try {
61             mDevice.setOrientationNatural();
62         } catch (RemoteException e) {
63             throw new RuntimeException("Failed to freeze device orientaion", e);
64         }
65 
66         // make sure we are in a clean state before starting the test
67         mDevice.pressHome();
68         Thread.sleep(TIMEOUT * 2);
69         launchAppsSettings();
70         UiObject2 view =
71                 mDevice.wait(
72                         Until.findObject(By.text("All apps")), TIMEOUT);
73         assertNotNull("Could not find Settings > Apps screen", view);
74     }
75 
76     @Override
tearDown()77     protected void tearDown() throws Exception {
78         mDevice.pressBack();
79         mDevice.pressHome(); // finish settings activity
80         mDevice.waitForIdle(TIMEOUT * 2); // give UI time to finish animating
81         super.tearDown();
82     }
83 
84     @MediumTest
testAppSettingsListForCalculator()85     public void testAppSettingsListForCalculator() {
86         UiObject2 calculator = mDevice.wait(
87                 Until.findObject(By.text("Calculator")), TIMEOUT);
88         calculator.click();
89         for (String setting : sResourceTexts) {
90             UiObject2 appSetting =
91                 mDevice.wait(
92                         Until.findObject(By.text(setting)), TIMEOUT);
93             assertNotNull("Missing setting for Calculator: " + setting, appSetting);
94             appSetting.scroll(Direction.DOWN, 10.0f);
95         }
96     }
97 
98     @MediumTest
testDisablingAndEnablingSystemApp()99     public void testDisablingAndEnablingSystemApp() throws Exception {
100         launchAppsSettings();
101         UiObject2 calculator = mDevice.wait(
102                 Until.findObject(By.text("Calculator")), TIMEOUT);
103         calculator.click();
104         mDevice.waitForIdle(TIMEOUT);
105         UiObject2 appInfoList = mDevice.wait(
106             Until.findObject(By.res(SETTINGS_PACKAGE, "list")), TIMEOUT);
107         appInfoList.scroll(Direction.DOWN, 100.0f);
108         UiObject2 disableButton = mDevice.wait(
109                 Until.findObject(By.text("DISABLE")), TIMEOUT);
110         disableButton.click();
111         mDevice.waitForIdle(TIMEOUT);
112         // Click on "Disable App" on dialog.
113         mDevice.wait(
114                 Until.findObject(By.text("DISABLE APP")), TIMEOUT).click();
115         mDevice.waitForIdle(TIMEOUT);
116         UiObject2 enableButton = mDevice.wait(
117                 Until.findObject(By.text("ENABLE")), TIMEOUT);
118         assertNotNull("App not disabled successfully", enableButton);
119         enableButton.click();
120         mDevice.waitForIdle(TIMEOUT);
121         disableButton = mDevice.wait(
122                 Until.findObject(By.text("DISABLE")), TIMEOUT);
123         assertNotNull("App not enabled successfully", disableButton);
124     }
125 
launchAppsSettings()126     private void launchAppsSettings() throws Exception {
127         Intent appsSettingsIntent = new
128                 Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
129         mActivityHelper.launchIntent(appsSettingsIntent);
130     }
131 }
132