• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.permissioncontroller.permission
18 
19 import android.app.AppOpsManager
20 import android.app.AppOpsManager.MODE_ALLOWED
21 import android.app.AppOpsManager.OPSTR_CAMERA
22 import android.content.ComponentName
23 import android.content.Intent
24 import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
25 import android.provider.DeviceConfig
26 import android.provider.DeviceConfig.NAMESPACE_PRIVACY
27 import androidx.test.platform.app.InstrumentationRegistry
28 import com.android.compatibility.common.util.SystemUtil
29 import com.android.compatibility.common.util.SystemUtil.eventually
30 import com.android.compatibility.common.util.SystemUtil.runShellCommand
31 import com.google.common.truth.Truth.assertThat
32 import org.junit.AfterClass
33 import org.junit.BeforeClass
34 
35 /**
36  * Super class with utilities for testing permission hub 2 code
37  */
38 open class PermissionHub2Test {
39     private val APP = "com.android.permissioncontroller.tests.appthatrequestpermission"
40 
41     private val instrumentation = InstrumentationRegistry.getInstrumentation()
42     private val context = instrumentation.targetContext
43 
44     companion object {
45         private const val PROPERTY_PERMISSIONS_HUB_2_ENABLED = "permissions_hub_2_enabled"
46 
47         private var wasPermissionHubEnabled = false
48 
49         @JvmStatic
50         @BeforeClass
enablePermissionHub2null51         fun enablePermissionHub2() {
52             wasPermissionHubEnabled = DeviceConfig.getBoolean(NAMESPACE_PRIVACY,
53                     PROPERTY_PERMISSIONS_HUB_2_ENABLED, false)
54 
55             if (!wasPermissionHubEnabled) {
56                 runShellCommand(
57                         "device_config put privacy $PROPERTY_PERMISSIONS_HUB_2_ENABLED true")
58             }
59         }
60 
61         @JvmStatic
62         @AfterClass
disablePermissionHub2null63         fun disablePermissionHub2() {
64             if (!wasPermissionHubEnabled) {
65                 runShellCommand(
66                         "device_config put privacy $PROPERTY_PERMISSIONS_HUB_2_ENABLED false")
67             }
68         }
69     }
70 
71     /**
72      * Make {@value #APP} access the camera
73      */
accessCameranull74     protected fun accessCamera() {
75         // App needs to be in foreground to be able to access camera
76         context.startActivity(
77                 Intent().setComponent(ComponentName.createRelative(APP, ".DummyActivity"))
78                         .setFlags(FLAG_ACTIVITY_NEW_TASK))
79 
80         eventually {
81             assertThat(
82                     SystemUtil.callWithShellPermissionIdentity {
83                         context.getSystemService(AppOpsManager::class.java).startOp(
84                                 OPSTR_CAMERA, context.packageManager.getPackageUid(APP, 0),
85                                 APP, null, null)
86                     }).isEqualTo(MODE_ALLOWED)
87         }
88     }
89 }