1 /*
2  * Copyright (C) 2020 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 package android.packageinstaller.install.cts
17 
18 import android.app.AppOpsManager.MODE_ALLOWED
19 import android.content.Intent
20 import android.platform.test.annotations.AppModeFull
21 import android.provider.Settings
22 import androidx.test.InstrumentationRegistry
23 import androidx.test.filters.MediumTest
24 import androidx.test.runner.AndroidJUnit4
25 import android.support.test.uiautomator.By
26 import android.support.test.uiautomator.BySelector
27 import android.support.test.uiautomator.UiDevice
28 import android.support.test.uiautomator.Until
29 import com.android.compatibility.common.util.AppOpsUtils
30 import com.google.common.truth.Truth.assertThat
31 import org.junit.Assert.assertNotNull
32 import org.junit.Assert.assertTrue
33 import org.junit.Before
34 import org.junit.Test
35 import org.junit.runner.RunWith
36 
37 private const val INSTALL_CONFIRM_TEXT_ID = "install_confirm_question"
38 private const val ALERT_DIALOG_TITLE_ID = "android:id/alertTitle"
39 
40 @RunWith(AndroidJUnit4::class)
41 @MediumTest
42 @AppModeFull
43 class ExternalSourcesTestAppOpAllowed : PackageInstallerTestBase() {
44     private val context = InstrumentationRegistry.getTargetContext()
45     private val pm = context.packageManager
46     private val packageName = context.packageName
47     private val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
48 
assertUiObjectnull49     private fun assertUiObject(errorMessage: String, selector: BySelector) {
50         assertNotNull(errorMessage, uiDevice.wait(Until.findObject(selector), TIMEOUT))
51     }
52 
assertInstallAllowednull53     private fun assertInstallAllowed(errorMessage: String) {
54         assertUiObject(errorMessage, By.res(PACKAGE_INSTALLER_PACKAGE_NAME,
55                 INSTALL_CONFIRM_TEXT_ID))
56         uiDevice.pressBack()
57     }
58 
allowedSourceTestnull59     private fun allowedSourceTest(startInstallation: () -> Unit) {
60         assertTrue("Package $packageName blocked from installing packages after setting app op " +
61                 "to allowed", pm.canRequestPackageInstalls())
62 
63         startInstallation()
64         assertInstallAllowed("Install confirmation not shown when app op set to allowed")
65 
66         assertTrue("Operation not logged", AppOpsUtils.allowedOperationLogged(packageName,
67                 APP_OP_STR))
68     }
69 
70     @Before
verifyAppOpAllowednull71     fun verifyAppOpAllowed() {
72         assertThat(AppOpsUtils.getOpMode(packageName, APP_OP_STR))
73                 .isEqualTo(MODE_ALLOWED)
74     }
75 
76     @Test
allowedSourceTestViaIntentnull77     fun allowedSourceTestViaIntent() {
78         assumeNotWatch()
79         allowedSourceTest { startInstallationViaIntent() }
80     }
81 
82     @Test
allowedSourceTestViaSessionnull83     fun allowedSourceTestViaSession() {
84         assumeNotWatch()
85         allowedSourceTest { startInstallationViaSession() }
86     }
87 
88     @Test
allowedSourceTestnull89     fun allowedSourceTest() {
90         assertTrue("Package $packageName blocked from installing packages after setting app op " +
91                 "to allowed", pm.canRequestPackageInstalls())
92     }
93 
94     @Test
testManageUnknownSourcesExistsnull95     fun testManageUnknownSourcesExists() {
96         val manageUnknownSources = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
97         assertNotNull("No activity found for ${manageUnknownSources.action}",
98                 pm.resolveActivity(manageUnknownSources, 0))
99     }
100 }
101