1 /*
<lambda>null2  * 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 com.android.wm.shell.flicker.appcompat
18 
19 import android.app.Instrumentation
20 import android.system.helpers.CommandsHelper
21 import androidx.test.platform.app.InstrumentationRegistry
22 import org.junit.rules.TestRule
23 import org.junit.runner.Description
24 import org.junit.runners.model.Statement
25 
26 /** JUnit Rule to handle letterboxStyles and states */
27 class LetterboxRule(
28     private val withLetterboxEducationEnabled: Boolean = false,
29     private val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation(),
30     private val cmdHelper: CommandsHelper = CommandsHelper.getInstance(instrumentation)
31 ) : TestRule {
32 
33     private val execAdb: (String) -> String = { cmd -> cmdHelper.executeShellCommand(cmd) }
34     private lateinit var _letterboxStyle: MutableMap<String, String>
35 
36     val letterboxStyle: Map<String, String>
37         get() {
38             if (!::_letterboxStyle.isInitialized) {
39                 _letterboxStyle = mapLetterboxStyle()
40             }
41             return _letterboxStyle
42         }
43 
44     val cornerRadius: Int?
45         get() = asInt(letterboxStyle["Corner radius"])
46 
47     val hasCornerRadius: Boolean
48         get() {
49             val radius = cornerRadius
50             return radius != null && radius > 0
51         }
52 
53     val isIgnoreOrientationRequest: Boolean
54         get() = execAdb("wm get-ignore-orientation-request")?.contains("true") ?: false
55 
56     override fun apply(base: Statement?, description: Description?): Statement {
57         resetLetterboxStyle()
58         _letterboxStyle = mapLetterboxStyle()
59         val isLetterboxEducationEnabled = _letterboxStyle.getValue("Is education enabled")
60         if ("$withLetterboxEducationEnabled" != isLetterboxEducationEnabled) {
61             execAdb("wm set-letterbox-style --isEducationEnabled " + withLetterboxEducationEnabled)
62         }
63         return object : Statement() {
64             @Throws(Throwable::class)
65             override fun evaluate() {
66                 try {
67                     base!!.evaluate()
68                 } finally {
69                     resetLetterboxStyle()
70                 }
71             }
72         }
73     }
74 
75     private fun mapLetterboxStyle(): HashMap<String, String> {
76         val res = execAdb("wm get-letterbox-style")
77         val lines = res.lines()
78         val map = HashMap<String, String>()
79         for (line in lines) {
80             val keyValuePair = line.split(":")
81             if (keyValuePair.size == 2) {
82                 val key = keyValuePair[0].trim()
83                 map[key] = keyValuePair[1].trim()
84             }
85         }
86         return map
87     }
88 
89     private fun resetLetterboxStyle() {
90         execAdb("wm reset-letterbox-style")
91     }
92 
93     private fun asInt(str: String?): Int? =
94         try {
95             str?.toInt()
96         } catch (e: NumberFormatException) {
97             null
98         }
99 }
100