blob: 6daebf7c18c33078f24e9e34a5642dc3dcca03a5 [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)) {
Jarkko Pöyry91d00e02015-01-15 18:02:15 -0800250 DeqpTestRunner deqpTest =
251 new DeqpTestRunner(mAppPackageName, mName, mTests, mTestInstanceArguments);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700252 deqpTest.setAbi(mAbi);
253 return deqpTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800254 } else if (NATIVE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700255 GeeTest geeTest = new GeeTest(mAppPackageName, mName);
256 geeTest.setAbi(mAbi);
257 return geeTest;
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -0700258 } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) {
259 CLog.d("Creating new wrapped native test for %s", mName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700260 WrappedGTest wrappedGeeTest = new WrappedGTest(mAppNameSpace, mAppPackageName, mName, mRunner);
261 wrappedGeeTest.setAbi(mAbi);
262 return wrappedGeeTest;
Svetoslav Ganovb803f802012-03-07 19:13:05 -0800263 } else if (ACCESSIBILITY_TEST.equals(mTestType)) {
264 AccessibilityTestRunner test = new AccessibilityTestRunner();
265 return setInstrumentationTest(test, testCaseDir);
Svetoslav12c82d42013-12-20 12:34:52 -0800266 } else if (PRINT_TEST.equals(mTestType)) {
267 PrintTestRunner test = new PrintTestRunner();
268 return setPrintTest(test, testCaseDir);
Brett Chabota595baa2013-07-01 11:55:36 -0700269 } else if (ACCESSIBILITY_SERVICE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700270 @SuppressWarnings("deprecation")
Brett Chabota595baa2013-07-01 11:55:36 -0700271 AccessibilityServiceTestRunner test = new AccessibilityServiceTestRunner();
272 return setInstrumentationTest(test, testCaseDir);
Craig Mautnera8e522f2012-10-22 11:46:30 -0700273 } else if (DISPLAY_TEST.equals(mTestType)) {
274 DisplayTestRunner test = new DisplayTestRunner();
275 return setInstrumentationTest(test, testCaseDir);
Brett Chabot87f44512012-11-16 16:00:02 -0800276 } else if (UIAUTOMATOR_TEST.equals(mTestType)) {
277 UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest();
278 return setUiAutomatorTest(uiautomatorTest);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700279 } else if (JUNIT_DEVICE_TEST.equals(mTestType)){
280 CLog.d("Creating JUnit device test %s", mName);
281 JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700282 jUnitDeviceTest.setRunName(mAppPackageName);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700283 jUnitDeviceTest.addTestJarFileName(mJarPath);
284 jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs);
285 jUnitDeviceTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700286 jUnitDeviceTest.setAbi(mAbi);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700287 mDigest = generateDigest(testCaseDir, mJarPath);
288 return jUnitDeviceTest;
Brett Chabota0f443c2011-02-04 13:57:55 -0800289 } else {
Brett Chabot13638ab2011-10-16 13:59:03 -0700290 CLog.d("Creating instrumentation test for %s", mName);
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700291 CtsInstrumentationApkTest instrTest = new CtsInstrumentationApkTest();
Keun young Park34246562012-09-18 11:18:20 -0700292 if (mTimeoutInMins >= 0) {
293 // as timeout cannot be set for each test,
294 // increase the time-out of the whole package
295 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
296 instrTest.setTestTimeout(mTimeoutInMins * 60 * 1000);
297 }
Brett Chabot53e68a32011-10-05 14:14:55 -0700298 return setInstrumentationTest(instrTest, testCaseDir);
Brett Chabot3add9162010-09-12 17:23:05 -0700299 }
300 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700301
Svetoslav12c82d42013-12-20 12:34:52 -0800302 private PrintTestRunner setPrintTest(PrintTestRunner printTest,
303 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700304 printTest.setRunName(mAppPackageName);
Svetoslav12c82d42013-12-20 12:34:52 -0800305 printTest.setPackageName(mAppNameSpace);
306 printTest.setRunnerName(mRunner);
307 printTest.setTestPackageName(mTestPackageName);
308 printTest.setClassName(mClassName);
309 printTest.setMethodName(mMethodName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700310 printTest.setAbi(mAbi);
Svetoslav12c82d42013-12-20 12:34:52 -0800311 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
312 return printTest;
313 }
314
Brett Chabot82d9daf2010-10-20 20:07:14 -0700315 /**
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700316 * Populates given {@link CtsInstrumentationApkTest} with data from the package xml.
Brett Chabota0f443c2011-02-04 13:57:55 -0800317 *
318 * @param testCaseDir
Brett Chabota0f443c2011-02-04 13:57:55 -0800319 * @param instrTest
320 * @return the populated {@link InstrumentationTest} or <code>null</code>
321 */
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700322 private InstrumentationTest setInstrumentationTest(CtsInstrumentationApkTest instrTest,
Brett Chabot53e68a32011-10-05 14:14:55 -0700323 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700324 instrTest.setRunName(mAppPackageName);
Brett Chabota0f443c2011-02-04 13:57:55 -0800325 instrTest.setPackageName(mAppNameSpace);
326 instrTest.setRunnerName(mRunner);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700327 instrTest.setTestPackageName(mTestPackageName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700328 instrTest.setClassName(mClassName);
329 instrTest.setMethodName(mMethodName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700330 instrTest.setAbi(mAbi);
Nick Korostelev4bfd0752014-08-06 14:56:08 -0700331 instrTest.setTestsToRun(mTests, false
332 /* force batch mode off to always run using testFile */);
333 instrTest.setReRunUsingTestFile(true);
Brett Chabota0f443c2011-02-04 13:57:55 -0800334 // mName means 'apk file name' for instrumentation tests
Brett Chabot4263db42011-04-15 13:51:48 -0700335 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700336 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot4263db42011-04-15 13:51:48 -0700337 if (mTests.size() > 1000) {
338 // TODO: hack, large test suites can take longer to collect tests, increase timeout
Brett Chabot13638ab2011-10-16 13:59:03 -0700339 instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000);
Brett Chabota0f443c2011-02-04 13:57:55 -0800340 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800341 return instrTest;
342 }
343
344 /**
Brett Chabot87f44512012-11-16 16:00:02 -0800345 * Populates given {@link UiAutomatorJarTest} with data from the package xml.
346 *
347 * @param uiautomatorTest
348 * @return the populated {@link UiAutomatorJarTest} or <code>null</code>
349 */
Stuart Scotte00a2b82014-09-05 17:37:18 -0700350 @SuppressWarnings("deprecation")
Brett Chabot87f44512012-11-16 16:00:02 -0800351 private IRemoteTest setUiAutomatorTest(UiAutomatorJarTest uiautomatorTest) {
352 uiautomatorTest.setInstallArtifacts(getJarPath());
353 if (mClassName != null) {
354 if (mMethodName != null) {
355 CLog.logAndDisplay(LogLevel.WARN, "ui automator tests don't currently support" +
356 "running individual methods");
357 }
358 uiautomatorTest.addClassName(mClassName);
359 } else {
360 uiautomatorTest.addClassNames(mTestClasses);
361 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700362 uiautomatorTest.setRunName(mAppPackageName);
Brett Chabot87f44512012-11-16 16:00:02 -0800363 uiautomatorTest.setCaptureLogs(false);
364 return uiautomatorTest;
365 }
366
367 /**
Stuart Scotte00a2b82014-09-05 17:37:18 -0700368 * Filter the tests to run based on list of included/excluded tests, class and method name.
Brett Chabotef5a6042011-01-19 19:54:14 -0800369 *
Brett Chabotef5a6042011-01-19 19:54:14 -0800370 * @return the filtered collection of tests
371 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700372 private Collection<TestIdentifier> filterTests() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700373 mTestFilter.setTestInclusion(mClassName, mMethodName);
374 return mTestFilter.filter(mTests);
Brett Chabotef5a6042011-01-19 19:54:14 -0800375 }
376
Nicholas Sauer6396d212014-11-05 20:10:21 -0800377 boolean isKnownTestClass(String className) {
Brett Chabotef5a6042011-01-19 19:54:14 -0800378 return mTestClasses.contains(className);
379 }
380
381 /**
Brian Muramatsu29d34782012-01-06 18:00:21 -0800382 * Add a {@link TestIdentifier} to the list of tests in this package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700383 *
Brian Muramatsu29d34782012-01-06 18:00:21 -0800384 * @param testDef
Keun young Park34246562012-09-18 11:18:20 -0700385 * @param timeout in mins
Brett Chabot82d9daf2010-10-20 20:07:14 -0700386 */
Keun young Park34246562012-09-18 11:18:20 -0700387 void addTest(TestIdentifier testDef, int timeout) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700388 mTests.add(testDef);
Brett Chabotef5a6042011-01-19 19:54:14 -0800389 mTestClasses.add(testDef.getClassName());
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800390 mTestInstanceArguments.put(testDef, new LinkedList<Map<String, String>>());
Keun young Park34246562012-09-18 11:18:20 -0700391 // 0 means no timeout, so keep 0 if already is.
392 if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) {
393 mTimeoutInMins = timeout;
394 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700395 }
396
397 /**
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800398 * Add a test instance to an existing {@link TestIdentifier}.
399 */
400 void addTestInstance(TestIdentifier testDef, Map<String, String> instanceArguments) {
401 if (!mTestInstanceArguments.containsKey(testDef)) {
402 throw new IllegalStateException("test id does not name an existing test");
403 }
404 mTestInstanceArguments.get(testDef).add(instanceArguments);
405 }
406
407 /**
Stuart Scottd6065522014-10-02 10:34:27 -0700408 * {@inheritDoc}
Brett Chabot82d9daf2010-10-20 20:07:14 -0700409 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800410 @Override
411 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700412 return mTests;
413 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700414
415 /**
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800416 * Get the instance argument map for tests.
417 * <p/>
418 * Exposed for unit testing.
419 */
420 public Map<TestIdentifier, List<Map<String, String>>> getTestInstanceArguments() {
421 return mTestInstanceArguments;
422 }
423
424 /**
Brett Chabot58c43a82011-09-13 14:13:57 -0700425 * {@inheritDoc}
426 */
427 @Override
428 public String getDigest() {
429 return mDigest;
430 }
431
432 /**
433 * Generate a sha1sum digest for a file.
434 * <p/>
435 * Exposed for unit testing.
436 *
437 * @param fileDir the directory of the file
438 * @param fileName the name of the file
439 * @return a hex {@link String} of the digest
440 */
Brett Chabot13638ab2011-10-16 13:59:03 -0700441 String generateDigest(File fileDir, String fileName) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700442 final String algorithm = "SHA-1";
443 InputStream fileStream = null;
Brett Chabot13638ab2011-10-16 13:59:03 -0700444 DigestInputStream d = null;
Brett Chabot58c43a82011-09-13 14:13:57 -0700445 try {
446 fileStream = getFileStream(fileDir, fileName);
447 MessageDigest md = MessageDigest.getInstance(algorithm);
448 d = new DigestInputStream(fileStream, md);
449 byte[] buffer = new byte[8196];
Brian Muramatsu29d34782012-01-06 18:00:21 -0800450 while (d.read(buffer) != -1) {
451 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700452 return toHexString(md.digest());
453 } catch (NoSuchAlgorithmException e) {
454 return algorithm + " not found";
455 } catch (IOException e) {
456 CLog.e(e);
457 } finally {
458 StreamUtil.closeStream(d);
459 StreamUtil.closeStream(fileStream);
460 }
461 return "failed to generate digest";
462 }
463
464 /**
465 * Retrieve an input stream for given file
466 * <p/>
467 * Exposed so unit tests can mock.
468 */
469 InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException {
470 InputStream fileStream;
471 fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName)));
472 return fileStream;
473 }
474
475 /**
476 * Convert the given byte array into a lowercase hex string.
477 *
478 * @param arr The array to convert.
479 * @return The hex encoded string.
480 */
481 private String toHexString(byte[] arr) {
Nicholas Sauer6396d212014-11-05 20:10:21 -0800482 StringBuilder buf = new StringBuilder(arr.length * 2);
Brett Chabot58c43a82011-09-13 14:13:57 -0700483 for (byte b : arr) {
484 buf.append(String.format("%02x", b & 0xFF));
485 }
486 return buf.toString();
487 }
Nicholas Sauerc176cac2014-10-23 15:36:49 -0700488
489 @Override
490 public int compareTo(ITestPackageDef testPackageDef) {
491 return getId().compareTo(testPackageDef.getId());
492 }
Brett Chabot3add9162010-09-12 17:23:05 -0700493}