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