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.assertEquals; 19 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.log.ITestLogger; 22 import com.android.tradefed.result.ByteArrayInputStreamSource; 23 import com.android.tradefed.result.InputStreamSource; 24 import com.android.tradefed.result.LogDataType; 25 import com.android.tradefed.suite.checker.StatusCheckerResult.CheckStatus; 26 27 import org.easymock.EasyMock; 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.junit.runners.JUnit4; 32 33 /** Unit tests for {@link ActivityStatusChecker}. */ 34 @RunWith(JUnit4.class) 35 public class ActivityStatusCheckerTest { 36 private ActivityStatusChecker mChecker; 37 private ITestLogger mMockLogger; 38 private ITestDevice mMockDevice; 39 40 @Before 41 public void setUp() { 42 mMockLogger = EasyMock.createStrictMock(ITestLogger.class); 43 mChecker = new ActivityStatusChecker(); 44 mChecker.setTestLogger(mMockLogger); 45 mMockDevice = EasyMock.createStrictMock(ITestDevice.class); 46 } 47 48 /** Test that the status checker is a success if the home/launcher activity is on top. */ 49 @Test 50 public void testCheckerLauncherHomeScreen() throws Exception { 51 EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.anyObject())) 52 .andReturn( 53 " mCurrentFocus=Window{46dd15 u0 com.google.android.apps.nexuslauncher/" 54 + "com.google.android.apps.nexuslauncher.NexusLauncherActivity}\n" 55 + " mFocusedApp=AppWindowToken{37e3c39 token=Token{312ce85 ActivityRecord{a9437fc " 56 + "u0 com.google.android.apps.nexuslauncher/.NexusLauncherActivity t2}}}"); 57 EasyMock.replay(mMockLogger, mMockDevice); 58 assertEquals(CheckStatus.SUCCESS, mChecker.postExecutionCheck(mMockDevice).getStatus()); 59 EasyMock.verify(mMockLogger, mMockDevice); 60 } 61 62 /** Test that if another activity is on top, then we fail the checker and take a screenshot. */ 63 @Test 64 public void testCheckerOtherActivity() throws Exception { 65 EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.anyObject())) 66 .andReturn( 67 "mCurrentFocus=Window{52b89df u0 com.android.chrome/org.chromium.chrome." 68 + "browser.ChromeTabbedActivity}\n" 69 + " mFocusedApp=AppWindowToken{955b485 token=Token{6bebd1b ActivityRecord{fd30b2a " 70 + "u0 com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity t7}}}"); 71 InputStreamSource fake = new ByteArrayInputStreamSource("fakedata".getBytes()); 72 EasyMock.expect(mMockDevice.getScreenshot(EasyMock.anyObject())).andReturn(fake); 73 mMockLogger.testLog("status_checker_front_activity", LogDataType.JPEG, fake); 74 EasyMock.replay(mMockLogger, mMockDevice); 75 assertEquals(CheckStatus.FAILED, mChecker.postExecutionCheck(mMockDevice).getStatus()); 76 EasyMock.verify(mMockLogger, mMockDevice); 77 } 78 } 79