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 com.android.tradefed.device.DeviceNotAvailableException; 19 import com.android.tradefed.device.ITestDevice; 20 import com.android.tradefed.log.ITestLogger; 21 import com.android.tradefed.log.LogUtil.CLog; 22 import com.android.tradefed.result.ITestLoggerReceiver; 23 import com.android.tradefed.result.InputStreamSource; 24 import com.android.tradefed.result.LogDataType; 25 import com.android.tradefed.suite.checker.StatusCheckerResult.CheckStatus; 26 import com.android.tradefed.util.StreamUtil; 27 28 /** Status checker for left over activities running at the end of a module. */ 29 public class ActivityStatusChecker implements ISystemStatusChecker, ITestLoggerReceiver { 30 31 private ITestLogger mLogger; 32 33 @Override postExecutionCheck(ITestDevice device)34 public StatusCheckerResult postExecutionCheck(ITestDevice device) 35 throws DeviceNotAvailableException { 36 return isFrontActivityLauncher(device); 37 } 38 isFrontActivityLauncher(ITestDevice device)39 private StatusCheckerResult isFrontActivityLauncher(ITestDevice device) 40 throws DeviceNotAvailableException { 41 StatusCheckerResult result = new StatusCheckerResult(); 42 // For API >= 29, `dumpsys window windows` is deprecated, use `displays` instead 43 String output = ""; 44 int apiLevel = device.getApiLevel(); 45 if (apiLevel >= 29) { 46 output = 47 device.executeShellCommand( 48 "dumpsys window displays | grep -E 'mCurrentFocus|mFocusedApp'"); 49 CLog.d("dumpsys window displays: %s", output); 50 } else { 51 output = 52 device.executeShellCommand( 53 "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'"); 54 CLog.d("dumpsys window windows: %s", output); 55 } 56 if (output.contains("Launcher")) { 57 result.setStatus(CheckStatus.SUCCESS); 58 return result; 59 } else { 60 InputStreamSource screen = device.getScreenshot("JPEG"); 61 try { 62 mLogger.testLog("status_checker_front_activity", LogDataType.JPEG, screen); 63 } finally { 64 StreamUtil.cancel(screen); 65 } 66 // TODO: Add a step to return to home page, or refresh the device (reboot?) 67 result.setStatus(CheckStatus.FAILED); 68 result.setBugreportNeeded(true); 69 result.setErrorMessage("Launcher activity is not in front."); 70 return result; 71 } 72 } 73 74 @Override setTestLogger(ITestLogger testLogger)75 public void setTestLogger(ITestLogger testLogger) { 76 mLogger = testLogger; 77 } 78 } 79