• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.tools.device.apphelpers
18 
19 import android.app.Instrumentation
20 import android.content.Intent
21 import android.content.pm.PackageManager
22 import android.net.Uri
23 import android.tools.traces.component.ComponentNameMatcher
24 import android.tools.traces.component.IComponentNameMatcher
25 import androidx.test.platform.app.InstrumentationRegistry
26 
27 /**
28  * Helper to launch the default browser app (compatible with AOSP)
29  *
30  * This helper has no other functionality but the app launch.
31  *
32  * This helper is used to launch an app after some operations (e.g., navigation mode change), so
33  * that the device is stable before executing flicker tests
34  */
35 class BrowserAppHelper
36 @JvmOverloads
37 constructor(
38     instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation(),
39     pkgManager: PackageManager = instrumentation.context.packageManager,
40     appName: String = getBrowserName(pkgManager),
41     appComponent: IComponentNameMatcher = getBrowserComponent(pkgManager),
42 ) : StandardAppHelper(instrumentation, appName, appComponent) {
43     override val openAppIntent =
44         pkgManager.getLaunchIntentForPackage(packageName)
45             ?: error("Unable to find intent for browser")
46 
47     companion object {
getBrowserIntentnull48         private fun getBrowserIntent(): Intent {
49             val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://"))
50             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
51             return intent
52         }
53 
getBrowserNamenull54         private fun getBrowserName(pkgManager: PackageManager): String {
55             val intent = getBrowserIntent()
56             val resolveInfo =
57                 pkgManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
58                     ?: error("Unable to resolve browser activity")
59 
60             return resolveInfo.loadLabel(pkgManager).toString()
61         }
62 
getBrowserComponentnull63         private fun getBrowserComponent(pkgManager: PackageManager): IComponentNameMatcher {
64             val intent = getBrowserIntent()
65             val resolveInfo =
66                 pkgManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
67                     ?: error("Unable to resolve browser activity")
68             return ComponentNameMatcher(resolveInfo.activityInfo.packageName, className = "")
69         }
70     }
71 }
72