blob: 372609aee8587e5377623df5494b88331730366c [file] [log] [blame]
Brett Chabot3add9162010-09-12 17:23:05 -07001/*
2 * Copyright (C) 2010 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 */
Brett Chabot13638ab2011-10-16 13:59:03 -070016
Brett Chabot3add9162010-09-12 17:23:05 -070017package com.android.cts.tradefed.testtype;
18
Stuart Scotte00a2b82014-09-05 17:37:18 -070019import com.android.cts.util.AbiUtils;
Brett Chabot87f44512012-11-16 16:00:02 -080020import com.android.ddmlib.Log.LogLevel;
Brett Chabot82d9daf2010-10-20 20:07:14 -070021import com.android.ddmlib.testrunner.TestIdentifier;
Brett Chabot58c43a82011-09-13 14:13:57 -070022import com.android.tradefed.log.LogUtil.CLog;
Stuart Scotte00a2b82014-09-05 17:37:18 -070023import com.android.tradefed.testtype.IAbi;
Brett Chabot3add9162010-09-12 17:23:05 -070024import com.android.tradefed.testtype.IRemoteTest;
25import com.android.tradefed.testtype.InstrumentationTest;
Brett Chabot58c43a82011-09-13 14:13:57 -070026import com.android.tradefed.util.StreamUtil;
Brett Chabot3add9162010-09-12 17:23:05 -070027
Brett Chabot58c43a82011-09-13 14:13:57 -070028import java.io.BufferedInputStream;
Brett Chabot3add9162010-09-12 17:23:05 -070029import java.io.File;
Brett Chabot58c43a82011-09-13 14:13:57 -070030import java.io.FileInputStream;
31import java.io.FileNotFoundException;
32import java.io.IOException;
33import java.io.InputStream;
34import java.security.DigestInputStream;
35import java.security.MessageDigest;
36import java.security.NoSuchAlgorithmException;
Brett Chabot82d9daf2010-10-20 20:07:14 -070037import java.util.Collection;
Jarkko Pöyry0df34742015-01-14 10:46:41 -080038import java.util.LinkedHashMap;
Brett Chabotef5a6042011-01-19 19:54:14 -080039import java.util.LinkedHashSet;
Jarkko Pöyry0df34742015-01-14 10:46:41 -080040import java.util.LinkedList;
41import java.util.List;
42import java.util.Map;
Brett Chabot3add9162010-09-12 17:23:05 -070043
44/**
45 * Container for CTS test info.
46 * <p/>
47 * Knows how to translate this info into a runnable {@link IRemoteTest}.
48 */
Brett Chabot4f8143c2010-12-14 18:29:44 -080049class TestPackageDef implements ITestPackageDef {
Brett Chabot3add9162010-09-12 17:23:05 -070050
Brian Muramatsu60695072012-01-09 18:11:03 -080051 public static final String HOST_SIDE_ONLY_TEST = "hostSideOnly";
52 public static final String NATIVE_TEST = "native";
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -070053 public static final String WRAPPED_NATIVE_TEST = "wrappednative";
Brian Muramatsu60695072012-01-09 18:11:03 -080054 public static final String VM_HOST_TEST = "vmHostTest";
Mika Isojärvif1e23ee2014-08-25 13:17:58 +030055 public static final String DEQP_TEST = "deqpTest";
Svetoslav Ganovb803f802012-03-07 19:13:05 -080056 public static final String ACCESSIBILITY_TEST =
Svetoslav12c82d42013-12-20 12:34:52 -080057 "com.android.cts.tradefed.testtype.AccessibilityTestRunner";
Brett Chabota595baa2013-07-01 11:55:36 -070058 public static final String ACCESSIBILITY_SERVICE_TEST =
Svetoslav12c82d42013-12-20 12:34:52 -080059 "com.android.cts.tradefed.testtype.AccessibilityServiceTestRunner";
60 public static final String PRINT_TEST =
61 "com.android.cts.tradefed.testtype.PrintTestRunner";
Craig Mautnera8e522f2012-10-22 11:46:30 -070062 public static final String DISPLAY_TEST =
63 "com.android.cts.tradefed.testtype.DisplayTestRunner";
Brett Chabot87f44512012-11-16 16:00:02 -080064 public static final String UIAUTOMATOR_TEST = "uiAutomator";
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -070065 public static final String JUNIT_DEVICE_TEST = "jUnitDeviceTest";
Brian Muramatsu60695072012-01-09 18:11:03 -080066
Stuart Scotte00a2b82014-09-05 17:37:18 -070067 private String mAppPackageName = null;
Brett Chabot3add9162010-09-12 17:23:05 -070068 private String mAppNameSpace = null;
69 private String mName = null;
70 private String mRunner = null;
Brian Muramatsu88d32a82011-12-02 10:55:12 -080071 private String mTestType = null;
Brett Chabot3add9162010-09-12 17:23:05 -070072 private String mJarPath = null;
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -070073 private String mRunTimeArgs = null;
Brian Carlstrom022aff42011-05-17 23:16:51 -070074 private String mTestPackageName = null;
Brett Chabot58c43a82011-09-13 14:13:57 -070075 private String mDigest = null;
Stuart Scotte00a2b82014-09-05 17:37:18 -070076 private IAbi mAbi = null;
Brett Chabot3add9162010-09-12 17:23:05 -070077
Brett Chabot13638ab2011-10-16 13:59:03 -070078 // use a LinkedHashSet for predictable iteration insertion-order, and fast
79 // lookups
Brett Chabotef5a6042011-01-19 19:54:14 -080080 private Collection<TestIdentifier> mTests = new LinkedHashSet<TestIdentifier>();
81 // also maintain an index of known test classes
82 private Collection<String> mTestClasses = new LinkedHashSet<String>();
Jarkko Pöyry0df34742015-01-14 10:46:41 -080083 // store instance arguments in order too for consistency
84 private Map<TestIdentifier, List<Map<String, String>>> mTestInstanceArguments =
85 new LinkedHashMap<>();
Brett Chabot4f8143c2010-12-14 18:29:44 -080086
Brett Chabot53e68a32011-10-05 14:14:55 -070087 // dynamic options, not parsed from package xml
88 private String mClassName;
89 private String mMethodName;
Stuart Scotte00a2b82014-09-05 17:37:18 -070090 private TestFilter mTestFilter = new TestFilter();
Brett Chabot358dc562011-10-25 15:46:48 -070091 private String mTargetBinaryName;
92 private String mTargetNameSpace;
Keun young Park34246562012-09-18 11:18:20 -070093 // only timeout per package is supported. To change this to method granularity,
94 // test invocation should be done in method level.
95 // So for now, only max timeout for the package is used.
96 private int mTimeoutInMins = -1;
Brett Chabot53e68a32011-10-05 14:14:55 -070097
Stuart Scotte00a2b82014-09-05 17:37:18 -070098 @Override
99 public IAbi getAbi() {
100 return mAbi;
101 }
102
103 /**
104 * @param abi the ABI to run this package on
105 */
106 public void setAbi(IAbi abi) {
107 mAbi = abi;
108 }
109
110 /**
111 * @return unique id representing this test package for this ABI.
112 */
113 @Override
114 public String getId() {
115 return AbiUtils.createId(getAbi().getName(), getAppPackageName());
116 }
117
118 void setAppPackageName(String appPackageName) {
119 mAppPackageName = appPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700120 }
121
Nicholas Sauer6396d212014-11-05 20:10:21 -0800122 String getAppPackageName() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700123 return mAppPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700124 }
125
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700126 void setRunTimeArgs(String runTimeArgs) {
127 mRunTimeArgs = runTimeArgs;
128 }
129
Brett Chabot3add9162010-09-12 17:23:05 -0700130 void setAppNameSpace(String appNameSpace) {
131 mAppNameSpace = appNameSpace;
132 }
133
134 String getAppNameSpace() {
135 return mAppNameSpace;
136 }
137
138 void setName(String name) {
139 mName = name;
140 }
141
Brett Chabot58c43a82011-09-13 14:13:57 -0700142 /**
143 * {@inheritDoc}
144 */
145 @Override
146 public String getName() {
Brett Chabot3add9162010-09-12 17:23:05 -0700147 return mName;
148 }
149
150 void setRunner(String runnerName) {
151 mRunner = runnerName;
152 }
153
154 String getRunner() {
155 return mRunner;
156 }
157
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800158 void setTestType(String testType) {
159 mTestType = testType;
160 }
161
Brian Muramatsu60695072012-01-09 18:11:03 -0800162 String getTestType() {
163 return mTestType;
164 }
165
Brett Chabot3add9162010-09-12 17:23:05 -0700166 void setJarPath(String jarPath) {
167 mJarPath = jarPath;
168 }
169
170 String getJarPath() {
171 return mJarPath;
172 }
173
Brian Carlstrom022aff42011-05-17 23:16:51 -0700174 void setTestPackageName(String testPackageName) {
175 mTestPackageName = testPackageName;
176 }
177
Brett Chabot358dc562011-10-25 15:46:48 -0700178 void setTargetBinaryName(String targetBinaryName) {
179 mTargetBinaryName = targetBinaryName;
180 }
181
182 void setTargetNameSpace(String targetNameSpace) {
183 mTargetNameSpace = targetNameSpace;
184 }
185
186 @Override
187 public String getTargetApkName() {
188 if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) {
189 return String.format("%s.apk", mTargetBinaryName);
190 }
191 return null;
192 }
193
194 @Override
195 public String getTargetPackageName() {
196 if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) {
197 return null;
198 }
199 return mTargetNameSpace;
200 }
201
Brett Chabot3add9162010-09-12 17:23:05 -0700202 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800203 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700204 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700205 @Override
Stuart Scotte00a2b82014-09-05 17:37:18 -0700206 public void setTestFilter(TestFilter testFilter) {
207 mTestFilter = testFilter;
Brett Chabot53e68a32011-10-05 14:14:55 -0700208 }
209
210 /**
211 * {@inheritDoc}
212 */
213 @Override
214 public void setClassName(String className, String methodName) {
215 mClassName = className;
216 mMethodName = methodName;
217 }
218
219 /**
220 * {@inheritDoc}
221 */
222 @Override
223 public IRemoteTest createTest(File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700224 mTestFilter.setTestInclusion(mClassName, mMethodName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700225 mTests = filterTests();
226
Brian Muramatsu60695072012-01-09 18:11:03 -0800227 if (HOST_SIDE_ONLY_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700228 CLog.d("Creating host test for %s", mName);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700229 JarHostTest hostTest = new JarHostTest();
Keun young Park14a5bfa2012-10-02 13:45:30 -0700230 if (mTimeoutInMins >= 0) {
231 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
232 hostTest.setTimeout(mTimeoutInMins * 60 * 1000);
233 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700234 hostTest.setRunName(mAppPackageName);
Brett Chabotf4bec732011-04-19 17:31:06 -0700235 hostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700236 hostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700237 hostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700238 mDigest = generateDigest(testCaseDir, mJarPath);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700239 return hostTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800240 } else if (VM_HOST_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700241 CLog.d("Creating vm host test for %s", mName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700242 VMHostTest vmHostTest = new VMHostTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700243 vmHostTest.setRunName(mAppPackageName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700244 vmHostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700245 vmHostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700246 vmHostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700247 mDigest = generateDigest(testCaseDir, mJarPath);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700248 return vmHostTest;
Mika Isojärvif1e23ee2014-08-25 13:17:58 +0300249 } else if (DEQP_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700250 DeqpTestRunner deqpTest = new DeqpTestRunner(mAppPackageName, mName, mTests);
251 deqpTest.setAbi(mAbi);
252 return deqpTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800253 } else if (NATIVE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700254 GeeTest geeTest = new GeeTest(mAppPackageName, mName);
255 geeTest.setAbi(mAbi);
256 return geeTest;
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -0700257 } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) {
258 CLog.d("Creating new wrapped native test for %s", mName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700259 WrappedGTest wrappedGeeTest = new WrappedGTest(mAppNameSpace, mAppPackageName, mName, mRunner);
260 wrappedGeeTest.setAbi(mAbi);
261 return wrappedGeeTest;
Svetoslav Ganovb803f802012-03-07 19:13:05 -0800262 } else if (ACCESSIBILITY_TEST.equals(mTestType)) {
263 AccessibilityTestRunner test = new AccessibilityTestRunner();
264 return setInstrumentationTest(test, testCaseDir);
Svetoslav12c82d42013-12-20 12:34:52 -0800265 } else if (PRINT_TEST.equals(mTestType)) {
266 PrintTestRunner test = new PrintTestRunner();
267 return setPrintTest(test, testCaseDir);
Brett Chabota595baa2013-07-01 11:55:36 -0700268 } else if (ACCESSIBILITY_SERVICE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700269 @SuppressWarnings("deprecation")
Brett Chabota595baa2013-07-01 11:55:36 -0700270 AccessibilityServiceTestRunner test = new AccessibilityServiceTestRunner();
271 return setInstrumentationTest(test, testCaseDir);
Craig Mautnera8e522f2012-10-22 11:46:30 -0700272 } else if (DISPLAY_TEST.equals(mTestType)) {
273 DisplayTestRunner test = new DisplayTestRunner();
274 return setInstrumentationTest(test, testCaseDir);
Brett Chabot87f44512012-11-16 16:00:02 -0800275 } else if (UIAUTOMATOR_TEST.equals(mTestType)) {
276 UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest();
277 return setUiAutomatorTest(uiautomatorTest);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700278 } else if (JUNIT_DEVICE_TEST.equals(mTestType)){
279 CLog.d("Creating JUnit device test %s", mName);
280 JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700281 jUnitDeviceTest.setRunName(mAppPackageName);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700282 jUnitDeviceTest.addTestJarFileName(mJarPath);
283 jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs);
284 jUnitDeviceTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700285 jUnitDeviceTest.setAbi(mAbi);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700286 mDigest = generateDigest(testCaseDir, mJarPath);
287 return jUnitDeviceTest;
Brett Chabota0f443c2011-02-04 13:57:55 -0800288 } else {
Brett Chabot13638ab2011-10-16 13:59:03 -0700289 CLog.d("Creating instrumentation test for %s", mName);
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700290 CtsInstrumentationApkTest instrTest = new CtsInstrumentationApkTest();
Keun young Park34246562012-09-18 11:18:20 -0700291 if (mTimeoutInMins >= 0) {
292 // as timeout cannot be set for each test,
293 // increase the time-out of the whole package
294 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
295 instrTest.setTestTimeout(mTimeoutInMins * 60 * 1000);
296 }
Brett Chabot53e68a32011-10-05 14:14:55 -0700297 return setInstrumentationTest(instrTest, testCaseDir);
Brett Chabot3add9162010-09-12 17:23:05 -0700298 }
299 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700300
Svetoslav12c82d42013-12-20 12:34:52 -0800301 private PrintTestRunner setPrintTest(PrintTestRunner printTest,
302 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700303 printTest.setRunName(mAppPackageName);
Svetoslav12c82d42013-12-20 12:34:52 -0800304 printTest.setPackageName(mAppNameSpace);
305 printTest.setRunnerName(mRunner);
306 printTest.setTestPackageName(mTestPackageName);
307 printTest.setClassName(mClassName);
308 printTest.setMethodName(mMethodName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700309 printTest.setAbi(mAbi);
Svetoslav12c82d42013-12-20 12:34:52 -0800310 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
311 return printTest;
312 }
313
Brett Chabot82d9daf2010-10-20 20:07:14 -0700314 /**
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700315 * Populates given {@link CtsInstrumentationApkTest} with data from the package xml.
Brett Chabota0f443c2011-02-04 13:57:55 -0800316 *
317 * @param testCaseDir
Brett Chabota0f443c2011-02-04 13:57:55 -0800318 * @param instrTest
319 * @return the populated {@link InstrumentationTest} or <code>null</code>
320 */
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700321 private InstrumentationTest setInstrumentationTest(CtsInstrumentationApkTest instrTest,
Brett Chabot53e68a32011-10-05 14:14:55 -0700322 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700323 instrTest.setRunName(mAppPackageName);
Brett Chabota0f443c2011-02-04 13:57:55 -0800324 instrTest.setPackageName(mAppNameSpace);
325 instrTest.setRunnerName(mRunner);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700326 instrTest.setTestPackageName(mTestPackageName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700327 instrTest.setClassName(mClassName);
328 instrTest.setMethodName(mMethodName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700329 instrTest.setAbi(mAbi);
Nick Korostelev4bfd0752014-08-06 14:56:08 -0700330 instrTest.setTestsToRun(mTests, false
331 /* force batch mode off to always run using testFile */);
332 instrTest.setReRunUsingTestFile(true);
Brett Chabota0f443c2011-02-04 13:57:55 -0800333 // mName means 'apk file name' for instrumentation tests
Brett Chabot4263db42011-04-15 13:51:48 -0700334 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700335 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot4263db42011-04-15 13:51:48 -0700336 if (mTests.size() > 1000) {
337 // TODO: hack, large test suites can take longer to collect tests, increase timeout
Brett Chabot13638ab2011-10-16 13:59:03 -0700338 instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000);
Brett Chabota0f443c2011-02-04 13:57:55 -0800339 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800340 return instrTest;
341 }
342
343 /**
Brett Chabot87f44512012-11-16 16:00:02 -0800344 * Populates given {@link UiAutomatorJarTest} with data from the package xml.
345 *
346 * @param uiautomatorTest
347 * @return the populated {@link UiAutomatorJarTest} or <code>null</code>
348 */
Stuart Scotte00a2b82014-09-05 17:37:18 -0700349 @SuppressWarnings("deprecation")
Brett Chabot87f44512012-11-16 16:00:02 -0800350 private IRemoteTest setUiAutomatorTest(UiAutomatorJarTest uiautomatorTest) {
351 uiautomatorTest.setInstallArtifacts(getJarPath());
352 if (mClassName != null) {
353 if (mMethodName != null) {
354 CLog.logAndDisplay(LogLevel.WARN, "ui automator tests don't currently support" +
355 "running individual methods");
356 }
357 uiautomatorTest.addClassName(mClassName);
358 } else {
359 uiautomatorTest.addClassNames(mTestClasses);
360 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700361 uiautomatorTest.setRunName(mAppPackageName);
Brett Chabot87f44512012-11-16 16:00:02 -0800362 uiautomatorTest.setCaptureLogs(false);
363 return uiautomatorTest;
364 }
365
366 /**
Stuart Scotte00a2b82014-09-05 17:37:18 -0700367 * Filter the tests to run based on list of included/excluded tests, class and method name.
Brett Chabotef5a6042011-01-19 19:54:14 -0800368 *
Brett Chabotef5a6042011-01-19 19:54:14 -0800369 * @return the filtered collection of tests
370 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700371 private Collection<TestIdentifier> filterTests() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700372 mTestFilter.setTestInclusion(mClassName, mMethodName);
373 return mTestFilter.filter(mTests);
Brett Chabotef5a6042011-01-19 19:54:14 -0800374 }
375
Nicholas Sauer6396d212014-11-05 20:10:21 -0800376 boolean isKnownTestClass(String className) {
Brett Chabotef5a6042011-01-19 19:54:14 -0800377 return mTestClasses.contains(className);
378 }
379
380 /**
Brian Muramatsu29d34782012-01-06 18:00:21 -0800381 * Add a {@link TestIdentifier} to the list of tests in this package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700382 *
Brian Muramatsu29d34782012-01-06 18:00:21 -0800383 * @param testDef
Keun young Park34246562012-09-18 11:18:20 -0700384 * @param timeout in mins
Brett Chabot82d9daf2010-10-20 20:07:14 -0700385 */
Keun young Park34246562012-09-18 11:18:20 -0700386 void addTest(TestIdentifier testDef, int timeout) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700387 mTests.add(testDef);
Brett Chabotef5a6042011-01-19 19:54:14 -0800388 mTestClasses.add(testDef.getClassName());
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800389 mTestInstanceArguments.put(testDef, new LinkedList<Map<String, String>>());
Keun young Park34246562012-09-18 11:18:20 -0700390 // 0 means no timeout, so keep 0 if already is.
391 if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) {
392 mTimeoutInMins = timeout;
393 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700394 }
395
396 /**
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800397 * Add a test instance to an existing {@link TestIdentifier}.
398 */
399 void addTestInstance(TestIdentifier testDef, Map<String, String> instanceArguments) {
400 if (!mTestInstanceArguments.containsKey(testDef)) {
401 throw new IllegalStateException("test id does not name an existing test");
402 }
403 mTestInstanceArguments.get(testDef).add(instanceArguments);
404 }
405
406 /**
Stuart Scottd6065522014-10-02 10:34:27 -0700407 * {@inheritDoc}
Brett Chabot82d9daf2010-10-20 20:07:14 -0700408 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800409 @Override
410 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700411 return mTests;
412 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700413
414 /**
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800415 * Get the instance argument map for tests.
416 * <p/>
417 * Exposed for unit testing.
418 */
419 public Map<TestIdentifier, List<Map<String, String>>> getTestInstanceArguments() {
420 return mTestInstanceArguments;
421 }
422
423 /**
Brett Chabot58c43a82011-09-13 14:13:57 -0700424 * {@inheritDoc}
425 */
426 @Override
427 public String getDigest() {
428 return mDigest;
429 }
430
431 /**
432 * Generate a sha1sum digest for a file.
433 * <p/>
434 * Exposed for unit testing.
435 *
436 * @param fileDir the directory of the file
437 * @param fileName the name of the file
438 * @return a hex {@link String} of the digest
439 */
Brett Chabot13638ab2011-10-16 13:59:03 -0700440 String generateDigest(File fileDir, String fileName) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700441 final String algorithm = "SHA-1";
442 InputStream fileStream = null;
Brett Chabot13638ab2011-10-16 13:59:03 -0700443 DigestInputStream d = null;
Brett Chabot58c43a82011-09-13 14:13:57 -0700444 try {
445 fileStream = getFileStream(fileDir, fileName);
446 MessageDigest md = MessageDigest.getInstance(algorithm);
447 d = new DigestInputStream(fileStream, md);
448 byte[] buffer = new byte[8196];
Brian Muramatsu29d34782012-01-06 18:00:21 -0800449 while (d.read(buffer) != -1) {
450 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700451 return toHexString(md.digest());
452 } catch (NoSuchAlgorithmException e) {
453 return algorithm + " not found";
454 } catch (IOException e) {
455 CLog.e(e);
456 } finally {
457 StreamUtil.closeStream(d);
458 StreamUtil.closeStream(fileStream);
459 }
460 return "failed to generate digest";
461 }
462
463 /**
464 * Retrieve an input stream for given file
465 * <p/>
466 * Exposed so unit tests can mock.
467 */
468 InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException {
469 InputStream fileStream;
470 fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName)));
471 return fileStream;
472 }
473
474 /**
475 * Convert the given byte array into a lowercase hex string.
476 *
477 * @param arr The array to convert.
478 * @return The hex encoded string.
479 */
480 private String toHexString(byte[] arr) {
Nicholas Sauer6396d212014-11-05 20:10:21 -0800481 StringBuilder buf = new StringBuilder(arr.length * 2);
Brett Chabot58c43a82011-09-13 14:13:57 -0700482 for (byte b : arr) {
483 buf.append(String.format("%02x", b & 0xFF));
484 }
485 return buf.toString();
486 }
Nicholas Sauerc176cac2014-10-23 15:36:49 -0700487
488 @Override
489 public int compareTo(ITestPackageDef testPackageDef) {
490 return getId().compareTo(testPackageDef.getId());
491 }
Brett Chabot3add9162010-09-12 17:23:05 -0700492}