• 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.integration
18 
19 import android.app.Instrumentation
20 import android.graphics.Region
21 import android.platform.test.annotations.Presubmit
22 import android.tools.Scenario
23 import android.tools.device.apphelpers.MessagingAppHelper
24 import android.tools.flicker.annotation.FlickerServiceCompatible
25 import android.tools.flicker.junit.FlickerBuilderProvider
26 import android.tools.flicker.junit.FlickerParametersRunnerFactory
27 import android.tools.flicker.legacy.FlickerBuilder
28 import android.tools.flicker.legacy.LegacyFlickerTest
29 import android.tools.flicker.legacy.LegacyFlickerTestFactory
30 import android.tools.flicker.subject.FlickerSubject
31 import android.tools.flicker.subject.layers.LayersTraceSubject
32 import android.tools.flicker.subject.region.RegionSubject
33 import android.tools.flicker.subject.wm.WindowManagerTraceSubject
34 import android.tools.io.RunStatus
35 import android.tools.traces.parsers.WindowManagerStateHelper
36 import androidx.test.platform.app.InstrumentationRegistry
37 import com.android.launcher3.tapl.LauncherInstrumentation
38 import com.google.common.truth.Truth
39 import org.junit.Test
40 import org.junit.runner.RunWith
41 import org.junit.runners.Parameterized
42 
43 /**
44  * Contains an integration test running the legacy flicker test using
45  * [FlickerParametersRunnerFactory] with flicker service compatibility enabled.
46  *
47  * To run this test: `atest FlickerLibTestE2e:FullLegacyRunTest`
48  */
49 @FlickerServiceCompatible(expectedCujs = ["ENTIRE_TRACE"])
50 @RunWith(Parameterized::class)
51 @Parameterized.UseParametersRunnerFactory(FlickerParametersRunnerFactory::class)
52 class FullLegacyRunTest(private val flicker: LegacyFlickerTest) {
53     private val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation()
54     private val testApp = MessagingAppHelper(instrumentation)
55     private val tapl: LauncherInstrumentation = LauncherInstrumentation()
56 
57     init {
58         flicker.scenario.setIsTablet(
59             WindowManagerStateHelper(instrumentation, clearCacheAfterParsing = false)
60                 .currentState
61                 .wmState
62                 .isTablet
63         )
64         tapl.setExpectedRotationCheckEnabled(true)
65     }
66 
67     /**
68      * Entry point for the test runner. It will use this method to initialize and cache flicker
69      * executions
70      */
71     @FlickerBuilderProvider
buildFlickernull72     fun buildFlicker(): FlickerBuilder {
73         return FlickerBuilder(instrumentation).apply {
74             setup { flicker.scenario.setIsTablet(wmHelper.currentState.wmState.isTablet) }
75             teardown { testApp.exit(wmHelper) }
76             transitions { testApp.launchViaIntent(wmHelper) }
77         }
78     }
79 
80     /**
81      * This is a shel test from the flicker infra to ensure the WM tracing pipeline executed
82      * entirely executed correctly
83      */
84     @Presubmit
85     @Test
internalWmChecknull86     fun internalWmCheck() {
87         var trace: WindowManagerTraceSubject? = null
88         var executionCount = 0
89         flicker.assertWm {
90             executionCount++
91             trace = this
92             this.isNotEmpty()
93         }
94         flicker.assertWm {
95             executionCount++
96             val failure: Result<Any> = runCatching { this.isEmpty() }
97             if (failure.isSuccess) {
98                 error("Should have thrown failure")
99             }
100         }
101         flicker.assertWmStart {
102             executionCount++
103             validateState(this, trace?.first())
104             validateVisibleRegion(this.visibleRegion(), trace?.first()?.visibleRegion())
105         }
106         flicker.assertWmEnd {
107             executionCount++
108             validateState(this, trace?.last())
109             validateVisibleRegion(this.visibleRegion(), trace?.last()?.visibleRegion())
110         }
111         Truth.assertWithMessage("Execution count").that(executionCount).isEqualTo(4)
112     }
113 
114     /**
115      * This is a shel test from the flicker infra to ensure the Layers tracing pipeline executed
116      * entirely executed correctly
117      */
118     @Presubmit
119     @Test
internalLayersChecknull120     fun internalLayersCheck() {
121         var trace: LayersTraceSubject? = null
122         var executionCount = 0
123         flicker.assertLayers {
124             executionCount++
125             trace = this
126             this.isNotEmpty()
127         }
128         flicker.assertLayers {
129             executionCount++
130             val failure: Result<Any> = runCatching { this.isEmpty() }
131             if (failure.isSuccess) {
132                 error("Should have thrown failure")
133             }
134         }
135         flicker.assertLayersStart {
136             executionCount++
137             validateState(this, trace?.first())
138             validateVisibleRegion(this.visibleRegion(), trace?.first()?.visibleRegion())
139         }
140         flicker.assertLayersEnd {
141             executionCount++
142             validateState(this, trace?.last())
143             validateVisibleRegion(this.visibleRegion(), trace?.last()?.visibleRegion())
144         }
145         Truth.assertWithMessage("Execution count").that(executionCount).isEqualTo(4)
146     }
147 
148     @Presubmit
149     @Test
exceptionMessageChecknull150     fun exceptionMessageCheck() {
151         val failure: Result<Any> = runCatching { flicker.assertLayers { this.isEmpty() } }
152         val exception = failure.exceptionOrNull() ?: error("Should have thrown failure")
153         Truth.assertWithMessage("Artifact path on exception")
154             .that(exception)
155             .hasMessageThat()
156             .contains(RunStatus.ASSERTION_FAILED.prefix)
157     }
158 
validateStatenull159     private fun validateState(actual: FlickerSubject?, expected: FlickerSubject?) {
160         Truth.assertWithMessage("Actual state").that(actual).isNotNull()
161         Truth.assertWithMessage("Expected state").that(expected).isNotNull()
162     }
163 
validateVisibleRegionnull164     private fun validateVisibleRegion(actual: RegionSubject?, expected: RegionSubject?) {
165         Truth.assertWithMessage("Actual visible region").that(actual).isNotNull()
166         Truth.assertWithMessage("Expected visible region").that(expected).isNotNull()
167         actual?.coversExactly(expected?.region ?: Region())
168 
169         val failure: Result<Any?> = runCatching { actual?.isHigher(expected?.region ?: Region()) }
170         if (failure.isSuccess) {
171             error("Should have thrown failure")
172         }
173     }
174 
175     companion object {
176         /**
177          * Creates the test configurations.
178          *
179          * See [LegacyFlickerTestFactory.nonRotationTests] for configuring screen orientation and
180          * navigation modes.
181          */
182         @Parameterized.Parameters(name = "{0}")
183         @JvmStatic
getParamsnull184         fun getParams() =
185             LegacyFlickerTestFactory.nonRotationTests(
186                 extraArgs = mapOf(Scenario.FAAS_BLOCKING to true)
187             )
188     }
189 }
190