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 com.android.functional.permissiontests;
18 
19 import android.app.UiAutomation;
20 import android.content.Context;
21 import android.support.test.launcherhelper2.ILauncherStrategy;
22 import android.support.test.launcherhelper2.LauncherStrategyFactory;
23 import android.system.helpers.PackageHelper;
24 import android.system.helpers.PermissionHelper;
25 import android.test.InstrumentationTestCase;
26 
27 import androidx.test.filters.MediumTest;
28 import androidx.test.filters.SmallTest;
29 
30 import androidx.test.uiautomator.By;
31 import androidx.test.uiautomator.BySelector;
32 import androidx.test.uiautomator.UiDevice;
33 import androidx.test.uiautomator.UiObject2;
34 import androidx.test.uiautomator.UiObjectNotFoundException;
35 import androidx.test.uiautomator.Until;
36 
37 import java.util.Arrays;
38 import java.util.List;
39 
40 public class GenericAppPermissionTests extends InstrumentationTestCase {
41     protected final String PACKAGE_INSTALLER = "com.android.packageinstaller";
42     protected final String TARGET_APP_PKG = "com.android.functional.permissiontests";
43     private final String PERMISSION_TEST_APP_PKG = "com.android.permissiontestappmv1";
44     private final String PERMISSION_TEST_APP = "PermissionTestAppMV1";
45     private final String GET_PERMISSION_BUTTON = "buttonGetPermission";
46     private UiDevice mDevice = null;
47     private Context mContext = null;
48     private UiAutomation mUiAutomation = null;
49     private PermissionHelper pHelper = null;
50     private PackageHelper pkgHelper = null;
51     private ILauncherStrategy mILauncherStrategy = null;
52     private final String[] mDefaultPermittedGroups = new String[] {
53             "CONTACTS", "SMS", "STORAGE"
54     };
55 
56     private final String[] mDefaultPermittedDangerousPermissions = new String[] {
57             "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.WRITE_CONTACTS",
58             "android.permission.SEND_SMS", "android.permission.RECEIVE_SMS"
59     };
60 
61     private List<String> mDefaultGrantedPermissions;
62 
63     @Override
setUp()64     protected void setUp() throws Exception {
65         super.setUp();
66         mDevice = UiDevice.getInstance(getInstrumentation());
67         mContext = getInstrumentation().getContext();
68         mUiAutomation = getInstrumentation().getUiAutomation();
69         mDevice.setOrientationNatural();
70         pHelper = PermissionHelper.getInstance(getInstrumentation());
71         pkgHelper = PackageHelper.getInstance(getInstrumentation());
72         mDefaultGrantedPermissions = pHelper.getPermissionByPackage(TARGET_APP_PKG, Boolean.TRUE);
73         mILauncherStrategy = LauncherStrategyFactory.getInstance(mDevice).getLauncherStrategy();
74     }
75 
76     @SmallTest
testDefaultDangerousPermissionGranted()77     public void testDefaultDangerousPermissionGranted() {
78         pHelper.verifyDefaultDangerousPermissionGranted(TARGET_APP_PKG,
79                 mDefaultPermittedDangerousPermissions, Boolean.FALSE);
80     }
81 
82     @SmallTest
testExtraDangerousPermissionNotGranted()83     public void testExtraDangerousPermissionNotGranted() {
84         pHelper.verifyExtraDangerousPermissionNotGranted(TARGET_APP_PKG, mDefaultPermittedGroups);
85     }
86 
87     @SmallTest
testNormalPermissionsAutoGranted()88     public void testNormalPermissionsAutoGranted() {
89         pHelper.verifyNormalPermissionsAutoGranted(TARGET_APP_PKG);
90     }
91 
testToggleAppPermisssionOFF()92     public void testToggleAppPermisssionOFF() throws UiObjectNotFoundException {
93         pHelper.togglePermissionSetting(PERMISSION_TEST_APP, "Contacts", Boolean.FALSE);
94         pHelper.verifyPermissionSettingStatus(
95                 PERMISSION_TEST_APP, "Contacts", PermissionHelper.PermissionStatus.OFF);
96     }
97 
testToggleAppPermisssionON()98     public void testToggleAppPermisssionON() throws UiObjectNotFoundException {
99         pHelper.togglePermissionSetting(PERMISSION_TEST_APP, "Contacts", Boolean.TRUE);
100         pHelper.verifyPermissionSettingStatus(PERMISSION_TEST_APP, "Contacts",
101                 PermissionHelper.PermissionStatus.ON);
102     }
103 
104     @MediumTest
testViewPermissionDescription()105     public void testViewPermissionDescription() throws UiObjectNotFoundException {
106         List<String> permissionDescGrpNamesList = pHelper
107                 .getPermissionDescGroupNames(TARGET_APP_PKG);
108         permissionDescGrpNamesList.removeAll(Arrays.asList(mDefaultPermittedGroups));
109         assertTrue("Still there are more", permissionDescGrpNamesList.isEmpty());
110     }
111 
testPermissionDialogAllow()112     public void testPermissionDialogAllow() {
113         pkgHelper.cleanPackage(PERMISSION_TEST_APP_PKG);
114         if (!mDevice.hasObject(By.pkg(PERMISSION_TEST_APP_PKG).depth(0))) {
115             mILauncherStrategy.launch(PERMISSION_TEST_APP, PERMISSION_TEST_APP_PKG);
116         }
117         mDevice.wait(Until.findObject(By.res(PERMISSION_TEST_APP_PKG, GET_PERMISSION_BUTTON)),
118                 pHelper.TIMEOUT).click();
119         mDevice.wait(Until.findObject(
120                 By.res(PACKAGE_INSTALLER, "permission_allow_button")), pHelper.TIMEOUT).click();
121         assertTrue("Permission hasn't been granted",
122                 pHelper.getAllDangerousPermissionsByPermGrpNames(
123                         new String[] {"Contacts"} ).size() > 0);
124     }
125 
testPermissionDialogDenyFlow()126     public void testPermissionDialogDenyFlow() {
127         pkgHelper.cleanPackage(PERMISSION_TEST_APP_PKG);
128         if (!mDevice.hasObject(By.pkg(PERMISSION_TEST_APP_PKG).depth(0))) {
129             mILauncherStrategy.launch(PERMISSION_TEST_APP, PERMISSION_TEST_APP_PKG);
130         }
131         pHelper.grantOrRevokePermissionViaAdb(
132                 PERMISSION_TEST_APP_PKG, "android.permission.READ_CONTACTS",
133                 PermissionHelper.PermissionOp.REVOKE);
134         BySelector getContactSelector = By.res(PERMISSION_TEST_APP_PKG, GET_PERMISSION_BUTTON);
135         BySelector dontAskChkSelector = By.res(PACKAGE_INSTALLER, "do_not_ask_checkbox");
136         BySelector denySelctor = By.res(PACKAGE_INSTALLER, "permission_deny_button");
137 
138         mDevice.wait(Until.findObject(getContactSelector), pHelper.TIMEOUT).click();
139         UiObject2 dontAskChk = mDevice.wait(Until.findObject(dontAskChkSelector), pHelper.TIMEOUT);
140         assertNull(dontAskChk);
141         mDevice.wait(Until.findObject(denySelctor), pHelper.TIMEOUT).click();
142         mDevice.wait(Until.findObject(getContactSelector), pHelper.TIMEOUT).click();
143         mDevice.wait(Until.findObject(dontAskChkSelector), pHelper.TIMEOUT).click();
144         mDevice.wait(Until.findObject(denySelctor), pHelper.TIMEOUT).click();
145         mDevice.wait(Until.findObject(getContactSelector), pHelper.TIMEOUT).click();;
146         assertNull(mDevice.wait(Until.findObject(denySelctor), pHelper.TIMEOUT));
147     }
148 
149     @Override
tearDown()150     protected void tearDown() throws Exception {
151         mDevice.unfreezeRotation();
152         super.tearDown();
153     }
154 }
155