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 androidx.test.platform.app.InstrumentationRegistry
25 
26 /**
27  * Helper to launch the default browser app (compatible with AOSP)
28  *
29  * This helper has no other functionality but the app launch.
30  *
31  * This helper is used to launch an app after some operations (e.g., navigation mode change), so
32  * that the device is stable before executing flicker tests
33  */
34 class BrowserAppHelper
35 @JvmOverloads
36 constructor(
37     instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation(),
38     pkgManager: PackageManager = instrumentation.context.packageManager
39 ) :
40     StandardAppHelper(
41         instrumentation,
42         getBrowserName(pkgManager),
43         getBrowserComponent(pkgManager)
44     ) {
45     override val openAppIntent =
46         pkgManager.getLaunchIntentForPackage(packageName)
47             ?: error("Unable to find intent for browser")
48 
49     companion object {
getBrowserIntentnull50         private fun getBrowserIntent(): Intent {
51             val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://"))
52             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
53             return intent
54         }
55 
getBrowserNamenull56         private fun getBrowserName(pkgManager: PackageManager): String {
57             val intent = getBrowserIntent()
58             val resolveInfo =
59                 pkgManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
60                     ?: error("Unable to resolve browser activity")
61 
62             return resolveInfo.loadLabel(pkgManager).toString()
63         }
64 
getBrowserComponentnull65         private fun getBrowserComponent(pkgManager: PackageManager): ComponentNameMatcher {
66             val intent = getBrowserIntent()
67             val resolveInfo =
68                 pkgManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
69                     ?: error("Unable to resolve browser activity")
70             return ComponentNameMatcher(resolveInfo.activityInfo.packageName, className = "")
71         }
72     }
73 }
74