• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.helpers
18 
19 import android.content.Context
20 import android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK
21 import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
22 import android.platform.helpers.LaunchAppUtils.launchApp
23 import android.platform.uiautomatorhelpers.DeviceHelpers
24 import androidx.test.platform.app.InstrumentationRegistry
25 import androidx.test.uiautomator.By
26 import androidx.test.uiautomator.UiDevice
27 import androidx.test.uiautomator.Until
28 import java.time.Duration
29 import org.junit.rules.TestWatcher
30 import org.junit.runner.Description
31 
32 /** Utilities to launch an [App]. */
33 object LaunchAppUtils {
34 
35     /** Launches an [App]. */
36     @JvmStatic
Contextnull37     fun Context.launchApp(app: App) {
38         val appIntent =
39             packageManager.getLaunchIntentForPackage(app.packageName)?.apply {
40                 flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
41             } ?: error("Package ${app.packageName} not available")
42 
43         startActivity(appIntent)
44         assertAppInForeground(app)
45     }
46 
47     /** Asserts that a given app is in the foreground. */
48     @JvmStatic
assertAppInForegroundnull49     fun assertAppInForeground(app: App) {
50         assertAppInForeground(app.packageName)
51     }
52 
53     /** Asserts that a given app is in the foreground. */
54     @JvmStatic
assertAppInForegroundnull55     fun assertAppInForeground(packageName: String) {
56         check(device.wait(Until.hasObject(By.pkg(packageName).depth(0)), MAX_TIMEOUT.toMillis())) {
57             "$packageName not in the foreground after ${MAX_TIMEOUT.toSeconds()} seconds"
58         }
59     }
60 
61     private val device: UiDevice
62         get() = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
63 }
64 
65 /** Rule that launches specified app and closes it after the test execution */
66 class LaunchAppRule(private val app: App) : TestWatcher() {
67 
startingnull68     override fun starting(description: Description?) {
69         InstrumentationRegistry.getInstrumentation().targetContext.launchApp(app)
70     }
71 
finishednull72     override fun finished(description: Description?) {
73         DeviceHelpers.uiDevice.executeShellCommand("am force-stop ${app.packageName}")
74     }
75 }
76 
77 /** Describes an app that can be launched with [LaunchAppUtils]. */
78 enum class App(internal val packageName: String) {
79     CALCULATOR("com.google.android.calculator"),
80     MAPS("com.google.android.apps.maps"),
81     CAMERA("com.google.android.GoogleCamera"),
82     SETTINGS("com.android.settings"),
83 }
84 
85 private val MAX_TIMEOUT = Duration.ofSeconds(10)
86