/* * Copyright (C) 2023 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.healthconnect.cts.hosttestapp import android.content.Context import android.content.pm.PackageManager import android.healthconnect.testing.cts.TestUtils import android.healthconnect.testing.cts.ui.ActivityLauncher.launchMainActivity import android.healthconnect.testing.cts.ui.UiTestUtils import android.healthconnect.testing.shared.DataFactory import androidx.test.core.app.ApplicationProvider import com.android.compatibility.common.util.DisableAnimationRule import com.android.compatibility.common.util.NonApiTest import com.android.compatibility.common.util.SystemUtil import org.junit.After import org.junit.Assume import org.junit.Before import org.junit.Rule import org.junit.Test /** * These tests are run by statsdatom/healthconnect to log atoms by triggering Health Connect APIs. * *
They only trigger the APIs, but don't test anything themselves. */ @NonApiTest(exemptionReasons = [], justification = "METRIC") class UiTestHelper { @get:Rule val disableAnimationRule = DisableAnimationRule() private val context: Context = ApplicationProvider.getApplicationContext() @Before fun setUpClass() { Assume.assumeTrue(isHardwareSupported()) // Collapse notifications SystemUtil.runShellCommandOrThrow("cmd statusbar collapse") unlockDevice() } @Before fun before() { TestUtils.deleteAllDataFromHealthConnect() } @After fun after() { TestUtils.deleteAllDataFromHealthConnect() } private fun unlockDevice() { SystemUtil.runShellCommandOrThrow("input keyevent KEYCODE_WAKEUP") if ("false".equals(SystemUtil.runShellCommandOrThrow("cmd lock_settings get-disabled"))) { // Unlock screen only when it's lock settings enabled to prevent showing "wallpaper // picker" which may cover another UI elements on freeform window configuration. SystemUtil.runShellCommandOrThrow("input keyevent 82") } SystemUtil.runShellCommandOrThrow("wm dismiss-keyguard") // Skip onboarding SystemUtil.runShellCommandOrThrow( "am start -a android.health.connect.action.SKIP_HEALTH_CONNECT_ONBOARDING" ) } private fun isHardwareSupported(): Boolean { // These UI tests are not optimised for Watches, TVs, Auto; // IoT devices do not have a UI to run these UI tests val pm: PackageManager = context.packageManager return (!pm.hasSystemFeature(PackageManager.FEATURE_EMBEDDED) && !pm.hasSystemFeature(PackageManager.FEATURE_WATCH) && !pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK) && !pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) } @Test fun openHomeFragment() { TestUtils.insertRecords( listOf( DataFactory.getBloodPressureRecord(), DataFactory.getHeartRateRecord(), DataFactory.getStepsRecord(), ) ) context.launchMainActivity { UiTestUtils.skipOnboardingIfAppears() UiTestUtils.scrollDownToAndFindText("App permissions") UiTestUtils.scrollDownToAndFindText("Manage data") UiTestUtils.clickOnTextAndWaitForNewWindow("Manage data") UiTestUtils.findText("Auto-delete") } } }