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