1 /* 2 * Copyright (C) 2018 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 com.android.systemui.Flags.keyguardBottomAreaRefactor; 19 20 import static org.mockito.Mockito.times; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.os.RemoteException; 25 26 import androidx.test.uiautomator.BySelector; 27 import androidx.test.uiautomator.UiDevice; 28 29 import org.junit.Test; 30 import org.junit.runner.Description; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 import org.junit.runners.model.Statement; 34 import org.mockito.Mockito; 35 36 /** Unit test the logic for {@link UnlockScreenRule} */ 37 @RunWith(JUnit4.class) 38 public class UnlockScreenRuleTest { 39 40 /** Tests that unlock a screen off and locked phone before the test. */ 41 @Test testScreenOff()42 public void testScreenOff() throws Throwable { 43 TestableUnlockScreenRule rule = new TestableUnlockScreenRule(false, false); 44 Statement testStatement = 45 new Statement() { 46 @Override 47 public void evaluate() throws Throwable { 48 // Assert that the device wake up and unlock screen. 49 verify(rule.getUiDevice(), times(1)).wakeUp(); 50 verify(rule.getUiDevice(), times(1)).pressMenu(); 51 } 52 }; 53 rule.apply(testStatement, Description.createTestDescription("clzz", "mthd")).evaluate(); 54 } 55 56 /** Tests that unlock a screen on but locked phone before the test. */ 57 @Test testScreenLocked()58 public void testScreenLocked() throws Throwable { 59 TestableUnlockScreenRule rule = new TestableUnlockScreenRule(true, false); 60 Statement testStatement = 61 new Statement() { 62 @Override 63 public void evaluate() throws Throwable { 64 // Assert that the device has unlocked screen. 65 verify(rule.getUiDevice(), times(0)).wakeUp(); 66 verify(rule.getUiDevice(), times(1)).pressMenu(); 67 } 68 }; 69 rule.apply(testStatement, Description.createTestDescription("clzz", "mthd")).evaluate(); 70 } 71 72 /** Tests that unlock a phone which is already screen on before the test. */ 73 @Test testScreenOn()74 public void testScreenOn() throws Throwable { 75 TestableUnlockScreenRule rule = new TestableUnlockScreenRule(true, true); 76 Statement testStatement = 77 new Statement() { 78 @Override 79 public void evaluate() throws Throwable { 80 // Assert that the device did nothing. 81 verify(rule.getUiDevice(), times(0)).wakeUp(); 82 verify(rule.getUiDevice(), times(0)).pressMenu(); 83 } 84 }; 85 rule.apply(testStatement, Description.createTestDescription("clzz", "mthd")).evaluate(); 86 } 87 88 private static class TestableUnlockScreenRule extends UnlockScreenRule { 89 private UiDevice mUiDevice; 90 private boolean mIsScreenOn; 91 private boolean mIsUnlocked; 92 TestableUnlockScreenRule(boolean isScreenOn, boolean isUnlocked)93 public TestableUnlockScreenRule(boolean isScreenOn, boolean isUnlocked) { 94 mUiDevice = Mockito.mock(UiDevice.class); 95 mIsScreenOn = isScreenOn; 96 mIsUnlocked = isUnlocked; 97 } 98 99 @Override getUiDevice()100 protected UiDevice getUiDevice() { 101 BySelector screenLock; 102 if (keyguardBottomAreaRefactor()) { 103 screenLock = KEYGUARD_ROOT_VIEW; 104 } else { 105 screenLock = KEYGUARD_BOTTOM_AREA_VIEW; 106 } 107 108 try { 109 when(mUiDevice.isScreenOn()).thenReturn(mIsScreenOn); 110 when(mUiDevice.hasObject(screenLock)).thenReturn(!mIsUnlocked); 111 } catch (RemoteException e) { 112 throw new RuntimeException("Could not unlock device.", e); 113 } 114 return mUiDevice; 115 } 116 } 117 } 118