1 /* 2 * Copyright (C) 2019 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 static android.platform.uiautomator_helpers.DeviceHelpers.assertInvisible; 19 20 import static com.android.systemui.Flags.keyguardBottomAreaRefactor; 21 22 import android.os.RemoteException; 23 24 import androidx.annotation.NonNull; 25 import androidx.test.uiautomator.By; 26 import androidx.test.uiautomator.BySelector; 27 import androidx.test.uiautomator.UiDevice; 28 29 import org.junit.runner.Description; 30 31 import java.time.Duration; 32 33 /** This rule will unlock phone screen before a test case. */ 34 public class UnlockScreenRule extends TestWatcher { 35 36 protected static final BySelector KEYGUARD_BOTTOM_AREA_VIEW = 37 By.res("com.android.systemui", "keyguard_bottom_area"); 38 39 protected static final BySelector KEYGUARD_ROOT_VIEW = 40 By.res("com.android.systemui", "keyguard_root_view"); 41 42 @Override starting(Description description)43 protected void starting(Description description) { 44 unlockScreen(getUiDevice()); 45 } 46 47 /** 48 * Unlocks the screen on the given device and asserts that the lock screen has disappeared. 49 * 50 * @param uiDevice the device to unlock the screen for. 51 */ unlockScreen(@onNull UiDevice uiDevice)52 public static void unlockScreen(@NonNull UiDevice uiDevice) { 53 try { 54 // Turn on the screen if necessary. 55 if (!uiDevice.isScreenOn()) { 56 uiDevice.wakeUp(); 57 } 58 59 BySelector screenLock; 60 if (keyguardBottomAreaRefactor()) { 61 screenLock = KEYGUARD_ROOT_VIEW; 62 } else { 63 screenLock = KEYGUARD_BOTTOM_AREA_VIEW; 64 } 65 66 if (uiDevice.hasObject(screenLock)) { 67 uiDevice.pressMenu(); 68 uiDevice.waitForIdle(); 69 assertInvisible(screenLock, /* timeout= */ Duration.ofSeconds(20)); 70 } 71 } catch (RemoteException e) { 72 throw new RuntimeException("Could not unlock device.", e); 73 } 74 } 75 } 76