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