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 17 package com.android.server.wm.flicker.rules 18 19 import android.app.Instrumentation 20 import android.content.Context 21 import android.os.RemoteException 22 import android.util.Log 23 import android.view.Surface 24 import android.view.WindowManager 25 import androidx.test.platform.app.InstrumentationRegistry 26 import androidx.test.uiautomator.UiDevice 27 import com.android.server.wm.flicker.FLICKER_TAG 28 import com.android.server.wm.traces.parser.windowmanager.WindowManagerStateHelper 29 import org.junit.rules.TestWatcher 30 import org.junit.runner.Description 31 32 data class ChangeDisplayOrientationRule @JvmOverloads constructor( 33 private val targetOrientation: Int, 34 private val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation() 35 ) : TestWatcher() { 36 private var initialOrientation = -1 37 startingnull38 override fun starting(description: Description?) { 39 Log.v(FLICKER_TAG, "Changing display orientation to " + 40 "$targetOrientation ${Surface.rotationToString(targetOrientation)}") 41 val wm = instrumentation.context.getSystemService(Context.WINDOW_SERVICE) as WindowManager 42 initialOrientation = wm.defaultDisplay.rotation 43 setRotation(targetOrientation, instrumentation) 44 } 45 finishednull46 override fun finished(description: Description?) { 47 setRotation(initialOrientation, instrumentation) 48 } 49 50 companion object { 51 @JvmOverloads setRotationnull52 fun setRotation( 53 rotation: Int, 54 instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation(), 55 wmHelper: WindowManagerStateHelper = WindowManagerStateHelper(instrumentation) 56 ) { 57 val device: UiDevice = UiDevice.getInstance(instrumentation) 58 59 try { 60 when (rotation) { 61 Surface.ROTATION_270 -> device.setOrientationRight() 62 Surface.ROTATION_90 -> device.setOrientationLeft() 63 Surface.ROTATION_0 -> device.setOrientationNatural() 64 else -> device.setOrientationNatural() 65 } 66 67 wmHelper.waitForRotation(rotation) 68 // During seamless rotation the app window is shown 69 val currWmState = wmHelper.currentState.wmState 70 if (currWmState.visibleWindows.none { it.isFullscreen }) { 71 wmHelper.waitForNavBarStatusBarVisible() 72 } 73 wmHelper.waitForAppTransitionIdle() 74 75 // Ensure WindowManagerService wait until all animations have completed 76 instrumentation.uiAutomation.syncInputTransactions() 77 } catch (e: RemoteException) { 78 throw RuntimeException(e) 79 } 80 } 81 } 82 }