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.content.Context
20 import android.content.pm.PackageManager
21 import androidx.test.core.app.ApplicationProvider
22 import com.android.compatibility.common.util.SystemUtil.runShellCommandOrThrow
23 import org.junit.Assume
24 import org.junit.Before
25 
26 open class HealthConnectBaseTest {
27 
28     protected val context: Context = ApplicationProvider.getApplicationContext()
29 
30     @Before
setUpClassnull31     fun setUpClass() {
32         Assume.assumeTrue(isHardwareSupported())
33         // Collapse notifications
34         runShellCommandOrThrow("cmd statusbar collapse")
35 
36         unlockDevice()
37     }
38 
unlockDevicenull39     private fun unlockDevice() {
40         runShellCommandOrThrow("input keyevent KEYCODE_WAKEUP")
41         if ("false".equals(runShellCommandOrThrow("cmd lock_settings get-disabled"))) {
42             // Unlock screen only when it's lock settings enabled to prevent showing "wallpaper
43             // picker" which may cover another UI elements on freeform window configuration.
44             runShellCommandOrThrow("input keyevent 82")
45         }
46         runShellCommandOrThrow("wm dismiss-keyguard")
47     }
48 
isHardwareSupportednull49     private fun isHardwareSupported(): Boolean {
50         // These UI tests are not optimised for Watches, TVs, Auto;
51         // IoT devices do not have a UI to run these UI tests
52         val pm: PackageManager = context.packageManager
53         return (!pm.hasSystemFeature(PackageManager.FEATURE_EMBEDDED) &&
54             !pm.hasSystemFeature(PackageManager.FEATURE_WATCH) &&
55             !pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK) &&
56             !pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE))
57     }
58 }
59