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.annotation.SuppressLint
20 import android.app.Instrumentation
21 import android.tools.device.apphelpers.BrowserAppHelper
22 import android.tools.device.apphelpers.MessagingAppHelper
23 import android.tools.flicker.datastore.DataStore
24 import android.tools.flicker.legacy.FlickerBuilder
25 import android.tools.flicker.legacy.runner.TransitionRunner
26 import android.tools.utils.TEST_SCENARIO
27 import androidx.test.platform.app.InstrumentationRegistry
28 import org.junit.runner.Description
29 
30 @SuppressLint("VisibleForTests")
31 object Utils {
32     private val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation()
33     internal const val TAG = "tag"
34     internal const val FAILURE = "Expected failure"
35     internal val setupAndTearDownTestApp = BrowserAppHelper(instrumentation)
36     internal val transitionTestApp = MessagingAppHelper(instrumentation)
37 
createFlickernull38     private fun createFlicker(onExecuted: () -> Unit) =
39         FlickerBuilder(instrumentation)
40             .apply {
41                 setup {
42                     // Shouldn't be in the trace we run assertions on
43                     setupAndTearDownTestApp.launchViaIntent(wmHelper)
44                     setupAndTearDownTestApp.exit(wmHelper)
45                 }
46                 transitions {
47                     // Should be in the trace we run assertions on
48                     transitionTestApp.launchViaIntent(wmHelper)
49                     createTag(TAG)
50                     onExecuted()
51                 }
52                 teardown {
53                     // Shouldn't be in the trace we run assertions on
54                     setupAndTearDownTestApp.launchViaIntent(wmHelper)
55                     setupAndTearDownTestApp.exit(wmHelper)
56                 }
57             }
58             .build()
59 
runTransitionnull60     fun runTransition(onExecuted: () -> Unit) {
61         android.tools.flicker.datastore.DataStore.clear()
62         val flicker = createFlicker(onExecuted)
63 
64         val runner = TransitionRunner(TEST_SCENARIO, instrumentation)
65         runner.execute(flicker, Description.createTestDescription(this::class.java, "test"))
66     }
67 }
68