• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 package android.platform.test.rule
17 
18 import android.graphics.Rect
19 import android.platform.test.rule.Orientation.LANDSCAPE
20 import android.platform.test.rule.Orientation.NATURAL
21 import android.platform.test.rule.Orientation.PORTRAIT
22 import android.platform.test.rule.RotationUtils.clearOrientationOverride
23 import android.platform.test.rule.RotationUtils.setOrientationOverride
24 import android.platform.test.util.HealthTestingUtils.waitForCondition
25 import android.platform.test.util.HealthTestingUtils.waitForValueToSettle
26 import android.util.Log
27 import androidx.test.InstrumentationRegistry
28 import androidx.test.uiautomator.UiDevice
29 import com.android.launcher3.tapl.LauncherInstrumentation
30 import org.junit.runner.Description
31 
32 /** Locks the orientation in Landscape before starting the test, and goes back to natural after. */
33 internal class LandscapeOrientationRule : BaseOrientationRule(LANDSCAPE)
34 
35 /** Locks the orientation in Portrait before starting the test, and goes back to natural after. */
36 internal class PortraitOrientationRule : BaseOrientationRule(PORTRAIT)
37 
38 class NaturalOrientationRule : BaseOrientationRule(NATURAL)
39 
40 /**
41  * Possible device orientations.
42  *
43  * See [UiDevice.orientation] for their definitions.
44  */
45 enum class Orientation {
46     LANDSCAPE,
47     PORTRAIT,
48     NATURAL
49 }
50 
51 /** Returns whether the device is landscape or portrait , based on display dimensions. */
52 val UiDevice.orientation: Orientation
53     get() =
54         if (displayWidth > displayHeight) {
55             LANDSCAPE
56         } else {
57             PORTRAIT
58         }
59 
60 val UiDevice.naturalOrientation: Orientation
61     get() {
62         if (isNaturalOrientation) {
63             return stableOrientation
64         }
65         return when (stableOrientation) {
66             LANDSCAPE -> PORTRAIT
67             PORTRAIT -> LANDSCAPE
68             else -> throw RuntimeException("Unexpected orientation: $stableOrientation.")
69         }
70     }
71 
72 // This makes sure that the orientation stabilised before returning it.
73 private val UiDevice.stableOrientation: Orientation
74     get() =
75         waitForValueToSettle(
<lambda>null76             /* errorMessage= */ { "Device orientation didn't settle" },
<lambda>null77             /* supplier */ { orientation },
78             /* minimumSettleTime= */ 1_000,
79             /* timeoutMs= */ 5_000
80         )
81 
82 /** Uses launcher rect to decide which rotation to apply to match [expectedOrientation]. */
83 sealed class BaseOrientationRule constructor(private val expectedOrientation: Orientation) :
84     TestWatcher() {
85 
startingnull86     override fun starting(description: Description) {
87         setOrientationOverride(expectedOrientation)
88     }
89 
finishednull90     override fun finished(description: Description) {
91         clearOrientationOverride()
92     }
93 }
94 
95 /** Provides a way to set and clear a rotation override. */
96 object RotationUtils {
97     private val device: UiDevice =
98         UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
99 
100     private val launcher: LauncherInstrumentation
101         get() = LauncherInstrumentation()
102 
103     /**
104      * Sets device orientation to [expectedOrientation], according to [Rect.orientation] definition.
105      *
106      * Important: call [clearOrientationOverride] after the end of the test. If a single orientation
107      * is needed for the entire test, please use the TestRule [OrientationRule] that automatically
108      * takes care of cleaning the override. Those should be called only when the test needs to
109      * change orientation in the middle.
110      */
setOrientationOverridenull111     fun setOrientationOverride(orientation: Orientation) {
112         val expectedOrientation =
113             if (orientation == NATURAL) device.naturalOrientation else orientation
114         launcher.setEnableRotation(true)
115         if (device.stableOrientation == expectedOrientation) {
116             return
117         }
118         changeOrientation()
119         waitForCondition({ "Visible orientation did not become  ${expectedOrientation.name}" }) {
120             device.stableOrientation == expectedOrientation
121         }
122         log("Rotation override set to ${expectedOrientation.name}")
123     }
124 
changeOrientationnull125     private fun changeOrientation() {
126         if (device.isNaturalOrientation) {
127             device.setOrientationLeft()
128         } else {
129             device.setOrientationNatural()
130         }
131     }
132 
133     /** returns stable orientation, doesn't necessarily mean orientation needs to happen */
waitForOrientationToSettlenull134     fun waitForOrientationToSettle(): Orientation {
135         return device.stableOrientation
136     }
137 
clearOrientationOverridenull138     fun clearOrientationOverride() {
139         device.setOrientationNatural()
140         launcher.setEnableRotation(false)
141         device.unfreezeRotation()
142         waitForOrientationToSettle()
143         log("Rotation override cleared.")
144     }
145 
lognull146     private fun log(message: String) = Log.d("RotationUtils", message)
147 }
148