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 /** Helper class to create [Scenario]s */
20 class ScenarioBuilder {
21     private var testClass: String = ""
22     private var startRotation = DEFAULT_ROTATION
23     private var endRotation = DEFAULT_ROTATION
24     private var navBarMode = DEFAULT_NAVBAR_MODE
25     private var extraConfig = mutableMapOf<String, Any?>()
26     private var description = ""
27 
<lambda>null28     fun forClass(cls: String) = apply { testClass = cls }
29 
<lambda>null30     fun withStartRotation(rotation: Rotation) = apply { startRotation = rotation }
31 
<lambda>null32     fun withEndRotation(rotation: Rotation) = apply { endRotation = rotation }
33 
<lambda>null34     fun withNavBarMode(mode: NavBar) = apply { navBarMode = mode }
35 
<lambda>null36     fun withExtraConfig(key: String, value: Any?) = apply { extraConfig[key] = value }
37 
<lambda>null38     fun withExtraConfigs(cfg: Map<String, Any?>) = apply { extraConfig.putAll(cfg) }
39 
<lambda>null40     fun withDescriptionOverride(description: String) = apply { this.description = description }
41 
buildnull42     fun build(): Scenario {
43         require(testClass.isNotEmpty()) { "Test class missing" }
44         val description =
45             description.ifEmpty {
46                 buildString {
47                     append(startRotation.description)
48                     if (endRotation != startRotation) {
49                         append("_${endRotation.description}")
50                     }
51                     append("_${navBarMode.description}")
52                 }
53             }
54         return ScenarioImpl(
55             testClass,
56             startRotation,
57             endRotation,
58             navBarMode,
59             extraConfig,
60             description
61         )
62     }
63 
createEmptyScenarionull64     fun createEmptyScenario(): Scenario =
65         ScenarioImpl(
66             testClass = "",
67             startRotation = DEFAULT_ROTATION,
68             endRotation = DEFAULT_ROTATION,
69             navBarMode = DEFAULT_NAVBAR_MODE,
70             config = emptyMap(),
71             description =
72                 defaultDescription(DEFAULT_ROTATION, DEFAULT_ROTATION, DEFAULT_NAVBAR_MODE)
73         )
74 
75     companion object {
76         val DEFAULT_ROTATION = Rotation.ROTATION_0
77         val DEFAULT_NAVBAR_MODE = NavBar.MODE_GESTURAL
78 
79         private fun defaultDescription(
80             startOrientation: Rotation,
81             endOrientation: Rotation,
82             navBarMode: NavBar
83         ): String = buildString {
84             append(startOrientation.description)
85             if (endOrientation != startOrientation) {
86                 append("_${endOrientation.description}")
87             }
88             append("_${navBarMode.description}")
89         }
90     }
91 }
92