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