1 /* 2 * Copyright (C) 2015 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.common.tradefed.targetprep; 18 19 import com.android.tradefed.build.IBuildInfo; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.log.LogUtil.CLog; 23 import com.android.tradefed.targetprep.BuildError; 24 import com.android.tradefed.targetprep.ITargetCleaner; 25 import com.android.tradefed.targetprep.TargetSetupError; 26 27 /** 28 * An {@link ITargetCleaner} that performs checks on system status and returns a boolean to indicate 29 * if the system is in an expected state. Such check maybe performed either prior to or after a 30 * module execution. 31 * <p>Note: the checker must be reentrant: meaning that the same instance will be called multiple 32 * times for each module executed, so it should not leave a state so as to interfere with the checks 33 * to be performed for the following modules. 34 */ 35 public abstract class SystemStatusChecker implements ITargetCleaner { 36 37 private String mFailureMessage = null; 38 39 /** 40 * {@inheritDoc} 41 */ 42 @Override setUp(ITestDevice device, IBuildInfo buildInfo)43 public void setUp(ITestDevice device, IBuildInfo buildInfo) 44 throws TargetSetupError, BuildError, DeviceNotAvailableException { 45 boolean check = preExecutionCheck(device); 46 if (!check) { 47 CLog.w("Failed pre-module-execution status check, message: %s", mFailureMessage); 48 } else { 49 CLog.d("Passed system status check"); 50 } 51 } 52 53 /** 54 * {@inheritDoc} 55 */ 56 @Override tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable t)57 public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable t) 58 throws DeviceNotAvailableException { 59 boolean check = postExecutionCheck(device); 60 if (!check) { 61 CLog.w("Failed post-module-execution status check, message: %s", mFailureMessage); 62 } else { 63 CLog.d("Passed system status check"); 64 } 65 } 66 67 /** 68 * Check system condition before test module execution. Subclass should override this method if 69 * a check is desirable here. Implementation must return a <code>boolean</code> value to 70 * indicate if the status check has passed or failed. 71 * <p>It's strongly recommended that system status be checked <strong>after</strong> module 72 * execution, and this method may be used for the purpose of caching certain system state 73 * prior to module execution. 74 * 75 * @return result of system status check 76 * @throws DeviceNotAvailableException 77 */ preExecutionCheck(ITestDevice device)78 public boolean preExecutionCheck(ITestDevice device) throws DeviceNotAvailableException { 79 return true; 80 } 81 82 /** 83 * Check system condition after test module execution. Subclass should override this method if 84 * a check is desirable here. Implementation must return a <code>boolean</code> value to 85 * indicate if the status check has passed or failed. 86 * 87 * @return result of system status check 88 * @throws DeviceNotAvailableException 89 */ postExecutionCheck(ITestDevice device)90 public boolean postExecutionCheck(ITestDevice device) throws DeviceNotAvailableException { 91 return true; 92 } 93 94 /** 95 * Sets failure message when a system status check failed for reporting purpose 96 * @param failureMessage 97 */ setFailureMessage(String failureMessage)98 protected void setFailureMessage(String failureMessage) { 99 mFailureMessage = failureMessage; 100 } 101 102 /** 103 * Returns failure message set by the failed system status check 104 * @return 105 */ getFailureMessage()106 public String getFailureMessage() { 107 return mFailureMessage; 108 } 109 } 110