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.waitForValueToSettle 25 import android.platform.uiautomator_helpers.WaitUtils.ensureThat 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 java.time.Duration 31 import org.junit.runner.Description 32 33 /** Locks the orientation in Landscape before starting the test, and goes back to natural after. */ 34 internal class LandscapeOrientationRule : BaseOrientationRule(LANDSCAPE) 35 36 /** Locks the orientation in Portrait before starting the test, and goes back to natural after. */ 37 internal class PortraitOrientationRule : BaseOrientationRule(PORTRAIT) 38 39 class NaturalOrientationRule : BaseOrientationRule(NATURAL) 40 41 /** 42 * Possible device orientations. 43 * 44 * See [UiDevice.orientation] for their definitions. 45 */ 46 enum class Orientation { 47 LANDSCAPE, 48 PORTRAIT, 49 NATURAL 50 } 51 52 /** Returns whether the device is landscape or portrait , based on display dimensions. */ 53 val UiDevice.orientation: Orientation 54 get() = 55 if (displayWidth > displayHeight) { 56 LANDSCAPE 57 } else { 58 PORTRAIT 59 } 60 61 val UiDevice.naturalOrientation: Orientation 62 get() { 63 if (isNaturalOrientation) { 64 return stableOrientation 65 } 66 return when (stableOrientation) { 67 LANDSCAPE -> PORTRAIT 68 PORTRAIT -> LANDSCAPE 69 else -> throw RuntimeException("Unexpected orientation: $stableOrientation.") 70 } 71 } 72 73 // This makes sure that the orientation stabilised before returning it. 74 private val UiDevice.stableOrientation: Orientation 75 get() = 76 waitForValueToSettle( <lambda>null77 /* errorMessage= */ { "Device orientation didn't settle" }, <lambda>null78 /* supplier */ { orientation }, 79 /* minimumSettleTime= */ 1_000, 80 /* timeoutMs= */ 5_000 81 ) 82 83 /** Uses launcher rect to decide which rotation to apply to match [expectedOrientation]. */ 84 sealed class BaseOrientationRule constructor(private val expectedOrientation: Orientation) : 85 TestWatcher() { 86 startingnull87 override fun starting(description: Description) { 88 setOrientationOverride(expectedOrientation) 89 } 90 finishednull91 override fun finished(description: Description) { 92 clearOrientationOverride() 93 } 94 } 95 96 /** Provides a way to set and clear a rotation override. */ 97 object RotationUtils { 98 private val device: UiDevice = 99 UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) 100 101 private val launcher: LauncherInstrumentation 102 get() = LauncherInstrumentation() 103 104 /** 105 * Sets device orientation to [expectedOrientation], according to [Rect.orientation] definition. 106 * 107 * Important: call [clearOrientationOverride] after the end of the test. If a single orientation 108 * is needed for the entire test, please use the TestRule [OrientationRule] that automatically 109 * takes care of cleaning the override. Those should be called only when the test needs to 110 * change orientation in the middle. 111 */ setOrientationOverridenull112 fun setOrientationOverride( 113 orientation: Orientation, 114 timeoutDuration: Duration = Duration.ofSeconds(10) 115 ) { 116 val expectedOrientation = 117 if (orientation == NATURAL) device.naturalOrientation else orientation 118 launcher.setEnableRotation(true) 119 if (device.stableOrientation == expectedOrientation) { 120 return 121 } 122 123 // Change orientation might be called soon before the device is unfolded, and lose its 124 // effect after unfold 125 ensureThat( 126 "orientation is $expectedOrientation", 127 timeout = timeoutDuration, 128 ignoreException = true 129 ) { 130 changeOrientation() 131 device.stableOrientation == expectedOrientation 132 } 133 134 log("Rotation override set to ${expectedOrientation.name}") 135 } 136 changeOrientationnull137 private fun changeOrientation() { 138 if (device.isNaturalOrientation) { 139 device.setOrientationLeft() 140 } else { 141 device.setOrientationNatural() 142 } 143 } 144 145 /** returns stable orientation, doesn't necessarily mean orientation needs to happen */ waitForOrientationToSettlenull146 fun waitForOrientationToSettle(): Orientation { 147 return device.stableOrientation 148 } 149 clearOrientationOverridenull150 fun clearOrientationOverride() { 151 device.setOrientationNatural() 152 launcher.setEnableRotation(false) 153 device.unfreezeRotation() 154 waitForOrientationToSettle() 155 log("Rotation override cleared.") 156 } 157 lognull158 private fun log(message: String) = Log.d("RotationUtils", message) 159 } 160