1 /* 2 * Copyright (C) 2017 Google Inc. 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 package com.android.tradefed.testtype.junit4; 17 18 import com.android.tradefed.device.ITestDevice; 19 import com.android.tradefed.result.ITestLifeCycleReceiver; 20 21 import java.util.ArrayList; 22 import java.util.LinkedHashMap; 23 import java.util.List; 24 import java.util.Map; 25 26 /** A builder class for options related to running device tests through BaseHostJUnit4Test. */ 27 public class DeviceTestRunOptions { 28 private ITestDevice mDevice; // optional 29 private String mRunner = null; // optional 30 private final String mPackageName; // required 31 32 private String mTestClassName; // optional 33 private String mTestMethodName; // optional 34 private Integer mUserId; // optional 35 private Long mTestTimeoutMs = BaseHostJUnit4Test.DEFAULT_TEST_TIMEOUT_MS; // optional 36 private Long mMaxTimeToOutputMs; // optional 37 private Long mMaxInstrumentationTimeoutMs; // optional 38 private boolean mCheckResults = true; // optional 39 private boolean mDisableHiddenApiCheck = false; // optional 40 private boolean mDisableIsolatedStorage = false; // optional 41 private Map<String, String> mInstrumentationArgs = new LinkedHashMap<>(); // optional 42 private List<ITestLifeCycleReceiver> mExtraListeners = new ArrayList<>(); // optional 43 DeviceTestRunOptions(String packageName)44 public DeviceTestRunOptions(String packageName) { 45 this.mPackageName = packageName; 46 } 47 getDevice()48 public ITestDevice getDevice() { 49 return mDevice; 50 } 51 setDevice(ITestDevice device)52 public DeviceTestRunOptions setDevice(ITestDevice device) { 53 this.mDevice = device; 54 return this; 55 } 56 getRunner()57 public String getRunner() { 58 return mRunner; 59 } 60 61 /** 62 * Sets the instrumentation runner that should be used to run the instrumentation. Default 63 * runner is 'android.support.test.runner.AndroidJUnitRunner'. Optional. 64 */ setRunner(String runner)65 public DeviceTestRunOptions setRunner(String runner) { 66 this.mRunner = runner; 67 return this; 68 } 69 getPackageName()70 public String getPackageName() { 71 return mPackageName; 72 } 73 getTestClassName()74 public String getTestClassName() { 75 return mTestClassName; 76 } 77 78 /** 79 * Sets the classname that the instrumentation should run. The instrumentation will be filtered 80 * to only run the class. Can be used with {@link #setTestMethodName(String)}. Optional. 81 */ setTestClassName(String testClassName)82 public DeviceTestRunOptions setTestClassName(String testClassName) { 83 this.mTestClassName = testClassName; 84 return this; 85 } 86 getTestMethodName()87 public String getTestMethodName() { 88 return mTestMethodName; 89 } 90 91 /** 92 * Sets the method name that the instrumentation should run. Requires {@link 93 * #setTestClassName(String)} to be set in order to work properly. Optional. 94 */ setTestMethodName(String testMethodName)95 public DeviceTestRunOptions setTestMethodName(String testMethodName) { 96 this.mTestMethodName = testMethodName; 97 return this; 98 } 99 getUserId()100 public Integer getUserId() { 101 return mUserId; 102 } 103 104 /** Sets the user id against which the instrumentation should run. Optional. */ setUserId(Integer userId)105 public DeviceTestRunOptions setUserId(Integer userId) { 106 this.mUserId = userId; 107 return this; 108 } 109 getTestTimeoutMs()110 public Long getTestTimeoutMs() { 111 return mTestTimeoutMs; 112 } 113 114 /** 115 * Sets the maximum time (in milliseconds) a test can run before being interrupted. Set to 0 for 116 * no timeout. Optional. 117 */ setTestTimeoutMs(Long testTimeoutMs)118 public DeviceTestRunOptions setTestTimeoutMs(Long testTimeoutMs) { 119 this.mTestTimeoutMs = testTimeoutMs; 120 return this; 121 } 122 getMaxTimeToOutputMs()123 public Long getMaxTimeToOutputMs() { 124 return mMaxTimeToOutputMs; 125 } 126 127 /** 128 * Sets the maximum time (in milliseconds) the instrumentation can stop outputting before being 129 * stopped. Set to 0 for no timeout. Optional. 130 */ setMaxTimeToOutputMs(Long maxTimeToOutputMs)131 public DeviceTestRunOptions setMaxTimeToOutputMs(Long maxTimeToOutputMs) { 132 this.mMaxTimeToOutputMs = maxTimeToOutputMs; 133 return this; 134 } 135 getMaxInstrumentationTimeoutMs()136 public Long getMaxInstrumentationTimeoutMs() { 137 return mMaxInstrumentationTimeoutMs; 138 } 139 140 /** 141 * Sets the maximum time (in milliseconds) the complete instrumentation will have to run and 142 * complete. Set to 0 for no timeout. Optional. 143 */ setMaxInstrumentationTimeoutMs(Long maxInstrumentationTimeoutMs)144 public DeviceTestRunOptions setMaxInstrumentationTimeoutMs(Long maxInstrumentationTimeoutMs) { 145 this.mMaxInstrumentationTimeoutMs = maxInstrumentationTimeoutMs; 146 return this; 147 } 148 shouldCheckResults()149 public boolean shouldCheckResults() { 150 return mCheckResults; 151 } 152 153 /** 154 * Sets whether or not the results of the instrumentation run should be checked and ensure no 155 * failures occured. 156 */ setCheckResults(boolean checkResults)157 public DeviceTestRunOptions setCheckResults(boolean checkResults) { 158 this.mCheckResults = checkResults; 159 return this; 160 } 161 162 /** 163 * sets whether or not to add the --no-hidden-api-checks to the 'am instrument' used from the 164 * host side. 165 */ setDisableHiddenApiCheck(boolean disableHiddenApiCheck)166 public DeviceTestRunOptions setDisableHiddenApiCheck(boolean disableHiddenApiCheck) { 167 this.mDisableHiddenApiCheck = disableHiddenApiCheck; 168 return this; 169 } 170 isHiddenApiCheckDisabled()171 public boolean isHiddenApiCheckDisabled() { 172 return mDisableHiddenApiCheck; 173 } 174 175 /** 176 * sets whether or not to add the --no-isolated-storage to the 'am instrument' used from the 177 * host side. 178 */ setDisableIsolatedStorage(boolean disableIsolatedStorage)179 public DeviceTestRunOptions setDisableIsolatedStorage(boolean disableIsolatedStorage) { 180 this.mDisableIsolatedStorage = disableIsolatedStorage; 181 return this; 182 } 183 isIsolatedStorageDisabled()184 public boolean isIsolatedStorageDisabled() { 185 return mDisableIsolatedStorage; 186 } 187 188 /** Add an argument that will be passed to the instrumentation. */ addInstrumentationArg(String key, String value)189 public DeviceTestRunOptions addInstrumentationArg(String key, String value) { 190 this.mInstrumentationArgs.put(key, value); 191 return this; 192 } 193 194 /** Add an extra listener to the instrumentation being run. */ addExtraListener(ITestLifeCycleReceiver listener)195 public DeviceTestRunOptions addExtraListener(ITestLifeCycleReceiver listener) { 196 this.mExtraListeners.add(listener); 197 return this; 198 } 199 200 /** 201 * Clear all instrumentation arguments that have been set with {@link 202 * #addInstrumentationArg(String, String)} previously. 203 */ clearInstrumentationArgs()204 public void clearInstrumentationArgs() { 205 mInstrumentationArgs.clear(); 206 } 207 getInstrumentationArgs()208 public Map<String, String> getInstrumentationArgs() { 209 return mInstrumentationArgs; 210 } 211 getExtraListeners()212 public List<ITestLifeCycleReceiver> getExtraListeners() { 213 return mExtraListeners; 214 } 215 clearExtraListeners()216 public void clearExtraListeners() { 217 mExtraListeners.clear(); 218 } 219 } 220