1 /* 2 * Copyright (C) 2017 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 com.android.tradefed.suite.checker; 17 18 import static org.junit.Assert.*; 19 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.util.KeyguardControllerState; 23 24 import org.easymock.EasyMock; 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 /** Unit tests for {@link KeyguardStatusChecker} */ 34 @RunWith(JUnit4.class) 35 public class KeyguardStatusCheckerTest { 36 37 private KeyguardStatusChecker mKsc; 38 private ITestDevice mMockDevice; 39 40 @Before 41 public void setUp() { 42 mMockDevice = EasyMock.createMock(ITestDevice.class); 43 mKsc = new KeyguardStatusChecker(); 44 } 45 46 /** 47 * Test that {@link KeyguardStatusChecker#postExecutionCheck(ITestDevice)} is passing when 48 * keyguard is not showing. 49 */ 50 @Test 51 public void testPostExecutionCheck() throws DeviceNotAvailableException { 52 EasyMock.expect(mMockDevice.getKeyguardState()) 53 .andReturn(createKeyguardState(false, false)); 54 EasyMock.replay(mMockDevice); 55 assertTrue(mKsc.postExecutionCheck(mMockDevice)); 56 EasyMock.verify(mMockDevice); 57 } 58 59 /** 60 * Test that {@link KeyguardStatusChecker#postExecutionCheck(ITestDevice)} is failing when the 61 * keyguard is showing. 62 */ 63 @Test 64 public void testPostExecutionCheck_showingAfter() throws DeviceNotAvailableException { 65 EasyMock.expect(mMockDevice.getKeyguardState()).andReturn(createKeyguardState(true, false)); 66 mMockDevice.disableKeyguard(); 67 EasyMock.expectLastCall(); 68 EasyMock.replay(mMockDevice); 69 assertFalse(mKsc.postExecutionCheck(mMockDevice)); 70 EasyMock.verify(mMockDevice); 71 } 72 73 /** 74 * Test that {@link KeyguardStatusChecker#postExecutionCheck(ITestDevice)} is skipping when the 75 * keyguard controller is not supported. 76 */ 77 @Test 78 public void testPostExecutionCheck_notSupported() throws DeviceNotAvailableException { 79 EasyMock.expect(mMockDevice.getKeyguardState()).andReturn(null); 80 EasyMock.replay(mMockDevice); 81 assertTrue(mKsc.postExecutionCheck(mMockDevice)); 82 EasyMock.verify(mMockDevice); 83 } 84 85 /** helper to create a response keyguard state from a fake device. */ 86 private KeyguardControllerState createKeyguardState(boolean showing, boolean occluded) { 87 List<String> testOutput = new ArrayList<String>(); 88 testOutput.add("KeyguardController:"); 89 testOutput.add(String.format(" mKeyguardShowing=%s", showing)); 90 testOutput.add(" mKeyguardGoingAway=false"); 91 testOutput.add(String.format(" mOccluded=%s", occluded)); 92 return KeyguardControllerState.create(testOutput); 93 } 94 } 95