1 /*
2  * Copyright (C) 2024 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.junit
18 
19 import android.app.Instrumentation
20 import android.os.Bundle
21 import android.tools.Scenario
22 import android.tools.flicker.legacy.runner.FLICKER_RUNNER_TAG
23 import android.tools.traces.ConditionList
24 import android.tools.traces.ConditionsFactory
25 import android.tools.traces.parsers.WindowManagerStateHelper
26 import android.tools.withTracing
27 import android.util.Log
28 import androidx.test.platform.app.InstrumentationRegistry
29 import org.junit.runner.Description
30 
31 /** Helper class for flicker transition rules */
32 object Utils {
33     /**
34      * Conditions that determine when the UI is in a stable and no windows or layers are animating
35      * or changing state.
36      */
37     private val UI_STABLE_CONDITIONS =
38         ConditionList(
39             listOf(
40                 ConditionsFactory.isWMStateComplete(),
41                 ConditionsFactory.hasLayersAnimating().negate()
42             )
43         )
44 
doWaitForUiStabilizenull45     internal fun doWaitForUiStabilize(wmHelper: WindowManagerStateHelper) {
46         withTracing("doWaitForUiStabilize") {
47             wmHelper.StateSyncBuilder().add(UI_STABLE_CONDITIONS).waitFor()
48         }
49     }
50 
notifyRunnerProgressnull51     internal fun notifyRunnerProgress(
52         scenario: Scenario,
53         msg: String,
54         instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation()
55     ) {
56         notifyRunnerProgress(scenario.key, msg, instrumentation)
57     }
58 
notifyRunnerProgressnull59     internal fun notifyRunnerProgress(
60         scenarioName: String,
61         msg: String,
62         instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation()
63     ) {
64         Log.d(FLICKER_RUNNER_TAG, "$scenarioName - $msg")
65         val results = Bundle()
66         results.putString(Instrumentation.REPORT_KEY_STREAMRESULT, "$msg\n")
67         instrumentation.sendStatus(1, results)
68     }
69 
expandDescriptionnull70     internal fun expandDescription(description: Description?, suffix: String): Description? =
71         Description.createTestDescription(
72             description?.className,
73             "${description?.displayName}-$suffix",
74             description?.annotations?.toTypedArray()
75         )
76 }
77