1 /* 2 * Copyright (C) 2020 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 17 package com.android.compatibility.targetprep; 18 19 import com.android.tradefed.config.Option; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.invoker.TestInformation; 22 import com.android.tradefed.log.LogUtil.CLog; 23 import com.android.tradefed.targetprep.ITargetPreparer; 24 import com.android.tradefed.targetprep.TargetSetupError; 25 import com.android.tradefed.util.CommandResult; 26 27 import com.google.common.annotations.VisibleForTesting; 28 29 /** 30 * Checks and recover GMS on a device. 31 * 32 * <p>This preparer checks whether the GMS process is running during setUp and tearDown. If GMS is 33 * not running before the test, a reboot will be attempted to recover. 34 */ 35 public final class CheckGmsPreparer implements ITargetPreparer { 36 37 private static final long WAIT_FOR_BOOT_COMPLETE_TIMEOUT_MILLIS = 1000 * 60; 38 @VisibleForTesting static final String CHECK_GMS_COMMAND = "pidof com.google.android.gms"; 39 @VisibleForTesting static final String OPTION_ENABLE = "enable"; 40 41 @Option(name = OPTION_ENABLE, description = "Enable GMS checks.") 42 protected boolean mEnable = false; 43 44 /** {@inheritDoc} */ 45 @Override setUp(TestInformation testInfo)46 public void setUp(TestInformation testInfo) 47 throws TargetSetupError, DeviceNotAvailableException { 48 if (!mEnable || isGmsRunning(testInfo)) { 49 return; 50 } 51 52 CLog.e("Did not detect a running GMS process, rebooting device to recover"); 53 testInfo.getDevice().reboot(); 54 testInfo.getDevice().waitForBootComplete(WAIT_FOR_BOOT_COMPLETE_TIMEOUT_MILLIS); 55 56 if (!isGmsRunning(testInfo)) { 57 CLog.e("GMS process still not running, throwing error"); 58 mEnable = false; 59 throw new TargetSetupError( 60 "GMS required but did not detect a running GMS process after device reboot"); 61 } 62 } 63 isGmsRunning(TestInformation testInfo)64 private static boolean isGmsRunning(TestInformation testInfo) 65 throws DeviceNotAvailableException { 66 CommandResult res = testInfo.getDevice().executeShellV2Command(CHECK_GMS_COMMAND); 67 if (res.getExitCode() == 0) { 68 CLog.d("Detected a running GMS process with PID: %s", res.getStdout()); 69 return true; 70 } 71 72 CLog.e( 73 "Check GMS command returned non zero exit code. Command: %s, Result: %s", 74 CHECK_GMS_COMMAND, res.toString()); 75 return false; 76 } 77 78 /** {@inheritDoc} */ 79 @Override tearDown(TestInformation testInfo, Throwable e)80 public void tearDown(TestInformation testInfo, Throwable e) throws DeviceNotAvailableException { 81 if (!mEnable || isGmsRunning(testInfo)) { 82 return; 83 } 84 85 CLog.e("Did not detect a running GMS process on tearDown"); 86 } 87 } 88