• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ddmlib.Log;
20 import com.android.tradefed.build.IBuildInfo;
21 import com.android.tradefed.config.ConfigurationException;
22 import com.android.tradefed.config.Option;
23 import com.android.tradefed.config.OptionSetter;
24 import com.android.tradefed.device.DeviceNotAvailableException;
25 import com.android.tradefed.device.ITestDevice;
26 import com.android.tradefed.log.LogUtil;
27 import com.android.tradefed.log.LogUtil.CLog;
28 import com.android.tradefed.targetprep.BaseTargetPreparer;
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 the device is ready to
38  * run the test suite.
39  */
40 public abstract class PreconditionPreparer extends BaseTargetPreparer {
41 
42     public static final String SKIP_PRECONDITIONS_OPTION = "skip-preconditions";
43     public static final String PRECONDITION_ARG_OPTION = "precondition-arg";
44 
45     @Option(
46         name = SKIP_PRECONDITIONS_OPTION,
47         shortName = 'o',
48         description = "Whether preconditions should be skipped"
49     )
50     private boolean mSkipPreconditions = false;
51 
52     @Option(
53         name = PRECONDITION_ARG_OPTION,
54         description =
55                 "the arguments to pass to a precondition. The expected format is"
56                         + "\"<arg-name>:<arg-value>\""
57     )
58     private List<String> mPreconditionArgs = new ArrayList<>();
59 
60     protected final String mLogTag = getClass().getSimpleName();
61 
62     @Override
setUp(ITestDevice device, IBuildInfo buildInfo)63     public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
64             BuildError, DeviceNotAvailableException {
65         if (mSkipPreconditions) {
66             return;
67         }
68         for (String preconditionArg : mPreconditionArgs) {
69             String[] parts = preconditionArg.split(":");
70             String argName = parts[0];
71             // If arg-value is not supplied, set to "true"
72             String argValue = (parts.length > 1) ? parts[1] : Boolean.toString(true);
73             setOption(argName, argValue);
74         }
75         run(device, buildInfo);
76     }
77 
setOption(String option, String value)78     private void setOption(String option, String value) {
79         try {
80             OptionSetter setter = new OptionSetter(this);
81             setter.setOptionValue(option, value);
82         } catch (ConfigurationException e) {
83             CLog.i("Value %s for option %s not applicable for class %s", value, option,
84                     this.getClass().getName());
85         }
86     }
87 
88     /**
89      * All PreconditionPreparer implementations share a base setup and can implement their own
90      * specific run logic.
91      */
run(ITestDevice device, IBuildInfo buildInfo)92     public abstract void run(ITestDevice device, IBuildInfo buildInfo)
93             throws TargetSetupError, BuildError, DeviceNotAvailableException;
94 
95     /** @deprecated Use {@link CLog} instead. */
96     @Deprecated
logInfo(String info)97     protected void logInfo(String info) {
98         LogUtil.printLog(Log.LogLevel.INFO, mLogTag, info);
99     }
100 
101     /** @deprecated Use {@link CLog} instead. */
102     @Deprecated
logInfo(String infoFormat, Object... args)103     protected void logInfo(String infoFormat, Object... args) {
104         LogUtil.printLog(Log.LogLevel.INFO, mLogTag, String.format(infoFormat, args));
105     }
106 
107     /** @deprecated Use {@link CLog} instead. */
108     @Deprecated
logWarning(String warning)109     protected void logWarning(String warning) {
110         LogUtil.printLog(Log.LogLevel.WARN, mLogTag, warning);
111     }
112 
113     /** @deprecated Use {@link CLog} instead. */
114     @Deprecated
logWarning(String warningFormat, Object... args)115     protected void logWarning(String warningFormat, Object... args) {
116         LogUtil.printLog(Log.LogLevel.WARN, mLogTag, String.format(warningFormat, args));
117     }
118 
119     /** @deprecated Use {@link CLog} instead. */
120     @Deprecated
logError(String error)121     protected void logError(String error) {
122         LogUtil.printLog(Log.LogLevel.ERROR, mLogTag, error);
123     }
124 
125     /** @deprecated Use {@link CLog} instead. */
126     @Deprecated
logError(String errorFormat, Object... args)127     protected void logError(String errorFormat, Object... args) {
128         LogUtil.printLog(Log.LogLevel.ERROR, mLogTag, String.format(errorFormat, args));
129     }
130 
131     /** @deprecated Use {@link CLog} instead. */
132     @Deprecated
logError(Throwable t)133     protected void logError(Throwable t) {
134         if (t != null) {
135             Log.e(mLogTag, t);
136         }
137     }
138 }
139