Home | History | Annotate | Download | only in targetprep
      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.compatibility.common.tradefed.testtype.CompatibilityTest;
     20 import com.android.ddmlib.Log;
     21 import com.android.tradefed.build.IBuildInfo;
     22 import com.android.tradefed.config.ConfigurationException;
     23 import com.android.tradefed.config.Option;
     24 import com.android.tradefed.config.OptionSetter;
     25 import com.android.tradefed.device.DeviceNotAvailableException;
     26 import com.android.tradefed.device.ITestDevice;
     27 import com.android.tradefed.log.LogUtil;
     28 import com.android.tradefed.log.LogUtil.CLog;
     29 import com.android.tradefed.targetprep.BuildError;
     30 import com.android.tradefed.targetprep.ITargetPreparer;
     31 import com.android.tradefed.targetprep.TargetSetupError;
     32 
     33 import java.util.ArrayList;
     34 import java.util.List;
     35 
     36 /**
     37  * An {@link ITargetPreparer} that performs checks and/or tasks to ensure the
     38  * the device is ready to run the test suite.
     39  */
     40 public abstract class PreconditionPreparer implements ITargetPreparer {
     41 
     42     @Option(name = CompatibilityTest.SKIP_PRECONDITIONS_OPTION,
     43             shortName = 'o',
     44             description = "Whether preconditions should be skipped")
     45     private boolean mSkipPreconditions = false;
     46 
     47     @Option(name = CompatibilityTest.PRECONDITION_ARG_OPTION,
     48             description = "the arguments to pass to a precondition. The expected format is"
     49                     + "\"<arg-name>:<arg-value>\"")
     50     private List<String> mPreconditionArgs = new ArrayList<>();
     51 
     52     protected final String mLogTag = getClass().getSimpleName();
     53 
     54     @Override
     55     public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
     56             BuildError, DeviceNotAvailableException {
     57         if (!mSkipPreconditions) {
     58             for (String preconditionArg : mPreconditionArgs) {
     59                 String[] parts = preconditionArg.split(":");
     60                 String argName = parts[0];
     61                 // If arg-value is not supplied, set to "true"
     62                 String argValue = (parts.length > 1) ? parts[1] : Boolean.toString(true);
     63                 setOption(argName, argValue);
     64             }
     65             run(device, buildInfo);
     66         }
     67     }
     68 
     69     private void setOption(String option, String value) {
     70         try {
     71             OptionSetter setter = new OptionSetter(this);
     72             setter.setOptionValue(option, value);
     73         } catch (ConfigurationException e) {
     74             CLog.i("Value %s for option %s not applicable for class %s", value, option,
     75                     this.getClass().getName());
     76         }
     77     }
     78 
     79     public abstract void run(ITestDevice device, IBuildInfo buildInfo)
     80             throws TargetSetupError, BuildError, DeviceNotAvailableException;
     81 
     82     protected void logInfo(String info) {
     83         LogUtil.printLog(Log.LogLevel.INFO, mLogTag, info);
     84     }
     85 
     86     protected void logInfo(String infoFormat, Object... args) {
     87         LogUtil.printLog(Log.LogLevel.INFO, mLogTag, String.format(infoFormat, args));
     88     }
     89 
     90     protected void logWarning(String warning) {
     91         LogUtil.printLog(Log.LogLevel.WARN, mLogTag, warning);
     92     }
     93 
     94     protected void logWarning(String warningFormat, Object... args) {
     95         LogUtil.printLog(Log.LogLevel.WARN, mLogTag, String.format(warningFormat, args));
     96     }
     97 
     98     protected void logError(String error) {
     99         LogUtil.printLog(Log.LogLevel.ERROR, mLogTag, error);
    100     }
    101 
    102     protected void logError(String errorFormat, Object... args) {
    103         LogUtil.printLog(Log.LogLevel.ERROR, mLogTag, String.format(errorFormat, args));
    104     }
    105 
    106     protected void logError(Throwable t) {
    107         if (t != null) {
    108             Log.e(mLogTag, t);
    109         }
    110     }
    111 
    112 }
    113