1 /* 2 * Copyright (C) 2023 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.healthconnect.cts.ui 18 19 import android.app.KeyguardManager 20 import android.content.Context 21 import android.content.pm.PackageManager 22 import android.healthconnect.cts.utils.AssumptionCheckerRule 23 import android.healthconnect.cts.utils.DeviceSupportUtils 24 import android.server.wm.WindowManagerStateHelper 25 import android.util.Log 26 import androidx.test.core.app.ApplicationProvider 27 import com.android.compatibility.common.util.FreezeRotationRule 28 import com.android.compatibility.common.util.SystemUtil.eventually 29 import com.android.compatibility.common.util.SystemUtil.runShellCommandOrThrow 30 import com.google.common.truth.Truth.assertWithMessage 31 import org.junit.Before 32 import org.junit.Rule 33 34 open class HealthConnectBaseTest { 35 @get:Rule val freezeRotationRule = FreezeRotationRule() 36 37 @get:Rule 38 var mSupportedHardwareRule = 39 AssumptionCheckerRule( <lambda>null40 { DeviceSupportUtils.isHealthConnectFullySupported() }, 41 "Tests should run on supported hardware only.", 42 ) 43 44 companion object { 45 private const val TAG = "HealthConnectBaseTest" 46 } 47 48 protected val context: Context = ApplicationProvider.getApplicationContext() 49 private val wmState = WindowManagerStateHelper() 50 51 @Before setUpClassnull52 fun setUpClass() { 53 // If the status bar is showing, hide it (swipe it back up) 54 runShellCommandOrThrow("cmd statusbar collapse") 55 56 unlockDevice() 57 } 58 59 /** This assumes that the lock method is SWIPE or NONE. */ unlockDevicenull60 private fun unlockDevice() { 61 val keyguardManager = context.getSystemService(KeyguardManager::class.java) 62 // Wakes up the device in case setup failed 63 runShellCommandOrThrow("input keyevent KEYCODE_WAKEUP") 64 runShellCommandOrThrow("wm dismiss-keyguard") 65 // Check if there is a lock screen set (we assume SWIPE) 66 if ("false".equals(runShellCommandOrThrow("cmd lock_settings get-disabled"))) { 67 // Unlock screen only when it's lock settings enabled to prevent showing "wallpaper 68 // picker" which may cover another UI elements on freeform window configuration. 69 Log.i( 70 TAG, 71 "keyguardManager.isKeyguardLocked (aka is keyguard showing) = ${keyguardManager.isKeyguardLocked}", 72 ) 73 Log.i(TAG, "Lock screen not disabled, send unlock screen event") 74 runShellCommandOrThrow("input keyevent 82") 75 } else { 76 Log.i(TAG, "Lock screen disabled, screen should be awake") 77 } 78 79 eventually { 80 assertWithMessage("device is locked").that(keyguardManager.isDeviceLocked).isFalse() 81 assertWithMessage("keyguard is locked").that(keyguardManager.isKeyguardLocked).isFalse() 82 } 83 } 84 assertPermissionGrantednull85 fun assertPermissionGranted(permission: String, packageName: String) { 86 assertWithMessage("$permission for $packageName is not granted") 87 .that(context.packageManager.checkPermission(permission, packageName)) 88 .isEqualTo(PackageManager.PERMISSION_GRANTED) 89 } 90 assertPermissionDeniednull91 fun assertPermissionDenied(permission: String, packageName: String) { 92 assertWithMessage("$permission for $packageName is not denied") 93 .that(context.packageManager.checkPermission(permission, packageName)) 94 .isEqualTo(PackageManager.PERMISSION_DENIED) 95 } 96 } 97