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.filters.MediumTest
23 import androidx.test.runner.AndroidJUnit4
24 import androidx.test.uiautomator.By
25 import androidx.test.uiautomator.BySelector
26 import androidx.test.uiautomator.Until
27 import com.android.compatibility.common.util.AppOpsUtils
28 import com.google.common.truth.Truth.assertThat
29 import org.junit.Assert.assertNotNull
30 import org.junit.Assert.assertTrue
31 import org.junit.Before
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 
35 private const val INSTALL_CONFIRM_TEXT_ID = "install_confirm_question"
36 private const val ALERT_DIALOG_TITLE_ID = "android:id/alertTitle"
37 
38 @RunWith(AndroidJUnit4::class)
39 @MediumTest
40 @AppModeFull
41 class ExternalSourcesTestAppOpAllowed : PackageInstallerTestBase() {
42     private val packageName = context.packageName
43 
assertUiObjectnull44     private fun assertUiObject(errorMessage: String, selector: BySelector) {
45         assertNotNull(errorMessage, uiDevice.wait(Until.findObject(selector), GLOBAL_TIMEOUT))
46     }
47 
assertInstallAllowednull48     private fun assertInstallAllowed(errorMessage: String) {
49         assertUiObject(errorMessage, By.res(
50             PACKAGE_INSTALLER_PACKAGE_NAME,
51                 INSTALL_CONFIRM_TEXT_ID
52         ))
53         uiDevice.pressBack()
54     }
55 
allowedSourceTestnull56     private fun allowedSourceTest(startInstallation: () -> Unit) {
57         assertTrue(
58             "Package $packageName blocked from installing packages after setting app op " +
59                 "to allowed",
60             pm.canRequestPackageInstalls()
61         )
62 
63         startInstallation()
64         assertInstallAllowed("Install confirmation not shown when app op set to allowed")
65 
66         assertTrue("Operation not logged", AppOpsUtils.allowedOperationLogged(
67             packageName,
68                 APP_OP_STR
69         ))
70     }
71 
72     @Before
verifyAppOpAllowednull73     fun verifyAppOpAllowed() {
74         assertThat(AppOpsUtils.getOpMode(packageName, APP_OP_STR))
75                 .isEqualTo(MODE_ALLOWED)
76     }
77 
78     @Test
allowedSourceTestViaIntentnull79     fun allowedSourceTestViaIntent() {
80         allowedSourceTest { startInstallationViaIntent() }
81     }
82 
83     @Test
allowedSourceTestViaSessionnull84     fun allowedSourceTestViaSession() {
85         allowedSourceTest { startInstallationViaSession() }
86     }
87 
88     @Test
allowedSourceTestnull89     fun allowedSourceTest() {
90         assertTrue(
91             "Package $packageName blocked from installing packages after setting app op " +
92                 "to allowed",
93             pm.canRequestPackageInstalls()
94         )
95     }
96 
97     @Test
testManageUnknownSourcesExistsnull98     fun testManageUnknownSourcesExists() {
99         val manageUnknownSources = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
100         assertNotNull(
101             "No activity found for ${manageUnknownSources.action}",
102                 pm.resolveActivity(manageUnknownSources, 0)
103         )
104     }
105 }
106