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