1 /* 2 * Copyright (C) 2024 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 android.platform.systemui_tapl.permissions.rule 18 19 import android.app.UiAutomation 20 import android.util.Log 21 import androidx.test.platform.app.InstrumentationRegistry 22 import org.junit.rules.TestWatcher 23 import org.junit.runner.Description 24 25 /** 26 * Adopts shell [permissions] identity for the [uiAutomation]. 27 * 28 * @see 29 * cts/common/device-side/util-axt/src/com/android/compatibility/common/util/AdoptShellPermissionsRule.java 30 * @see UiAutomation.adoptShellPermissionIdentity 31 */ 32 class AdoptShellPermissionsRule( 33 vararg permissions: String, 34 private val uiAutomation: UiAutomation = 35 InstrumentationRegistry.getInstrumentation().uiAutomation, 36 ) : TestWatcher() { 37 38 private val permissionSet: Set<String> = permissions.toSet() 39 40 init { 41 require(permissionSet.isNotEmpty()) 42 } 43 startingnull44 override fun starting(description: Description?) { 45 super.starting(description) 46 for (permission in permissionSet) { 47 uiAutomation.adoptShellPermissionIdentity(permission) 48 } 49 Log.d("AdoptShellPermissionsRule", "Adopted identities=${permissionSet}") 50 } 51 finishednull52 override fun finished(description: Description?) { 53 uiAutomation.dropShellPermissionIdentity() 54 Log.d("AdoptShellPermissionsRule", "Dropped adopted identities") 55 super.finished(description) 56 } 57 } 58