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.flicker.rules
18 
19 import android.app.ActivityTaskManager
20 import android.app.WindowConfiguration
21 import android.tools.FLICKER_TAG
22 import android.tools.traces.parsers.WindowManagerStateHelper
23 import android.tools.withTracing
24 import android.util.Log
25 import org.junit.rules.TestWatcher
26 import org.junit.runner.Description
27 
28 /** Test rule to ensure no tasks as running before executing the test */
29 class RemoveAllTasksButHomeRule() : TestWatcher() {
startingnull30     override fun starting(description: Description?) {
31         withTracing("$RemoveAllTasksButHomeRule:starting") {
32             Log.v(FLICKER_TAG, "Removing all tasks (except home)")
33             removeAllTasksButHome()
34             WindowManagerStateHelper()
35                 .StateSyncBuilder()
36                 .withAppTransitionIdle()
37                 .withHomeActivityVisible()
38                 .waitForAndVerify()
39         }
40     }
41 
42     companion object {
43         @JvmStatic
removeAllTasksButHomenull44         fun removeAllTasksButHome() {
45             val atm = ActivityTaskManager.getService()
46             atm.removeRootTasksWithActivityTypes(ALL_ACTIVITY_TYPE_BUT_HOME)
47         }
48 
49         private val ALL_ACTIVITY_TYPE_BUT_HOME =
50             intArrayOf(
51                 WindowConfiguration.ACTIVITY_TYPE_STANDARD,
52                 WindowConfiguration.ACTIVITY_TYPE_ASSISTANT,
53                 WindowConfiguration.ACTIVITY_TYPE_RECENTS,
54                 WindowConfiguration.ACTIVITY_TYPE_UNDEFINED
55             )
56     }
57 }
58