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
18 
19 import android.util.Log
20 
21 /**
22  * Legacy flicker test scenario
23  *
24  * @param testClass
25  * @param startRotation Initial screen rotation
26  * @param endRotation Final screen rotation
27  * @param navBarMode Navigation mode, such as 3 button or gestural.
28  * @param config Additional configurations
29  *
30  * Defaults to [startRotation]
31  */
32 class ScenarioImpl
33 internal constructor(
34     val testClass: String,
35     override val startRotation: Rotation,
36     override val endRotation: Rotation,
37     override val navBarMode: NavBar,
38     config: Map<String, Any?>,
39     override val description: String
40 ) : Scenario {
41     private val _extraConfig = config.toMutableMap()
42 
43     val extraConfig: Map<String, Any?>
44         get() = _extraConfig
45 
46     override val isEmpty = testClass.isEmpty()
47 
48     override val key = if (isEmpty) "empty" else "${testClass}_$description"
49 
50     /** If the initial screen rotation is 90 (landscape) or 180 (seascape) degrees */
51     val isLandscapeOrSeascapeAtStart: Boolean =
52         startRotation == Rotation.ROTATION_90 || startRotation == Rotation.ROTATION_270
53 
54     val isGesturalNavigation = navBarMode == NavBar.MODE_GESTURAL
55 
56     val isTablet: Boolean
57         get() {
58             if (!extraConfig.containsKey(IS_TABLET)) {
59                 Log.e(
60                     FLICKER_TAG,
61                     "$IS_TABLET property not initialized. Use [setIsTablet] to initialize"
62                 )
63             }
64             return extraConfig[IS_TABLET] as Boolean? ?: false
65         }
66 
setIsTabletnull67     fun setIsTablet(isTablet: Boolean) {
68         _extraConfig[IS_TABLET] = isTablet
69     }
70 
getConfigValuenull71     override fun <T> getConfigValue(key: String): T? = extraConfig[key] as T?
72 
73     override fun toString(): String = key
74 
75     override fun equals(other: Any?): Boolean {
76         if (this === other) return true
77         if (other !is ScenarioImpl) return false
78 
79         if (testClass != other.testClass) return false
80         if (startRotation != other.startRotation) return false
81         if (endRotation != other.endRotation) return false
82         if (navBarMode != other.navBarMode) return false
83         if (description != other.description) return false
84         if (extraConfig != other.extraConfig) return false
85 
86         return true
87     }
88 
hashCodenull89     override fun hashCode(): Int {
90         var result = testClass.hashCode()
91         result = 31 * result + startRotation.hashCode()
92         result = 31 * result + endRotation.hashCode()
93         result = 31 * result + navBarMode.hashCode()
94         result = 31 * result + description.hashCode()
95         result = 31 * result + extraConfig.hashCode()
96         return result
97     }
98 
99     companion object {
100         internal const val IS_TABLET = "isTablet"
101     }
102 }
103