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 package com.android.safetycenter.testing
17 
18 import android.app.Activity
19 import android.content.ComponentName
20 import android.content.Context
21 import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED
22 import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED
23 import android.content.pm.PackageManager.DONT_KILL_APP
24 import android.os.Bundle
25 import android.view.View
26 import android.widget.TextView
27 import androidx.test.core.app.ApplicationProvider
28 
29 /** An activity used in tests to assert the redirects. */
30 class TestActivity : Activity() {
onCreatenull31     override fun onCreate(savedInstanceState: Bundle?) {
32         super.onCreate(savedInstanceState)
33         setContentView(R.layout.test_activity)
34         val bundleText: TextView? = findViewById(R.id.bundle_text)
35         val isFromSettingsHomepage = getIntent().getBooleanExtra("is_from_settings_homepage", false)
36         bundleText?.text = "is_from_settings_homepage $isFromSettingsHomepage"
37 
38         val exitButton: View? = findViewById(R.id.button)
39         exitButton?.setOnClickListener { finish() }
40     }
41 
42     companion object {
43 
44         /**
45          * Enable a higher-priority alias of TestActivity.
46          *
47          * <p>We have seen flakes where implicit intents for TEST_ACTIVITY fail owing to multiple
48          * receivers, perhaps due to an older CTS APK hanging around. This component should be
49          * turned on (and off in tidyup) in tests in the hope of only resolving to the actively
50          * running test in these cases.
51          */
enableHighPriorityAliasnull52         fun enableHighPriorityAlias() {
53             setAliasEnabledState(COMPONENT_ENABLED_STATE_ENABLED)
54         }
55         /** @see [enableHighPriorityAlias] */
disableHighPriorityAliasnull56         fun disableHighPriorityAlias() {
57             setAliasEnabledState(COMPONENT_ENABLED_STATE_DISABLED)
58         }
setAliasEnabledStatenull59         private fun setAliasEnabledState(state: Int) {
60             val name =
61                 ComponentName(getApplicationContext(), TestActivity::class.java.name + "Priority")
62             getApplicationContext()
63                 .packageManager
64                 .setComponentEnabledSetting(name, state, DONT_KILL_APP)
65         }
66 
getApplicationContextnull67         private fun getApplicationContext(): Context = ApplicationProvider.getApplicationContext()
68     }
69 }
70