Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 16 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 17 | package com.android.cts.tradefed.testtype; |
| 18 | |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 19 | import com.android.ddmlib.Log.LogLevel; |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 20 | import com.android.ddmlib.testrunner.TestIdentifier; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 21 | import com.android.tradefed.log.LogUtil.CLog; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 22 | import com.android.tradefed.testtype.IRemoteTest; |
| 23 | import com.android.tradefed.testtype.InstrumentationTest; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 24 | import com.android.tradefed.util.StreamUtil; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 25 | |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 26 | import java.io.BufferedInputStream; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 27 | import java.io.File; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 28 | import java.io.FileInputStream; |
| 29 | import java.io.FileNotFoundException; |
| 30 | import java.io.IOException; |
| 31 | import java.io.InputStream; |
| 32 | import java.security.DigestInputStream; |
| 33 | import java.security.MessageDigest; |
| 34 | import java.security.NoSuchAlgorithmException; |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 35 | import java.util.Collection; |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 36 | import java.util.LinkedHashSet; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Container for CTS test info. |
| 40 | * <p/> |
| 41 | * Knows how to translate this info into a runnable {@link IRemoteTest}. |
| 42 | */ |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 43 | class TestPackageDef implements ITestPackageDef { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 44 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 45 | public static final String HOST_SIDE_ONLY_TEST = "hostSideOnly"; |
| 46 | public static final String NATIVE_TEST = "native"; |
Thomas Tafertshofer | b5385a6 | 2012-08-15 15:40:45 -0700 | [diff] [blame] | 47 | public static final String WRAPPED_NATIVE_TEST = "wrappednative"; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 48 | public static final String VM_HOST_TEST = "vmHostTest"; |
Svetoslav Ganov | b803f80 | 2012-03-07 19:13:05 -0800 | [diff] [blame] | 49 | public static final String ACCESSIBILITY_TEST = |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 50 | "com.android.cts.tradefed.testtype.AccessibilityTestRunner"; |
Brett Chabot | a595baa | 2013-07-01 11:55:36 -0700 | [diff] [blame] | 51 | public static final String ACCESSIBILITY_SERVICE_TEST = |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 52 | "com.android.cts.tradefed.testtype.AccessibilityServiceTestRunner"; |
| 53 | public static final String PRINT_TEST = |
| 54 | "com.android.cts.tradefed.testtype.PrintTestRunner"; |
Craig Mautner | a8e522f | 2012-10-22 11:46:30 -0700 | [diff] [blame] | 55 | public static final String DISPLAY_TEST = |
| 56 | "com.android.cts.tradefed.testtype.DisplayTestRunner"; |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 57 | public static final String UIAUTOMATOR_TEST = "uiAutomator"; |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame^] | 58 | public static final String JUNIT_DEVICE_TEST = "jUnitDeviceTest"; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 59 | |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 60 | private static final String SIGNATURE_TEST_METHOD = "testSignature"; |
Stuart Scott | c09a2e0 | 2013-11-15 13:03:29 -0800 | [diff] [blame] | 61 | private static final String SIGNATURE_TEST_CLASS = "android.tests.sigtest.SignatureTest"; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 62 | |
| 63 | private String mUri = null; |
| 64 | private String mAppNameSpace = null; |
| 65 | private String mName = null; |
| 66 | private String mRunner = null; |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 67 | private String mTestType = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 68 | private String mJarPath = null; |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame^] | 69 | private String mRunTimeArgs = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 70 | private boolean mIsSignatureTest = false; |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 71 | private String mTestPackageName = null; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 72 | private String mDigest = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 73 | |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 74 | // use a LinkedHashSet for predictable iteration insertion-order, and fast |
| 75 | // lookups |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 76 | private Collection<TestIdentifier> mTests = new LinkedHashSet<TestIdentifier>(); |
| 77 | // also maintain an index of known test classes |
| 78 | private Collection<String> mTestClasses = new LinkedHashSet<String>(); |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 79 | |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 80 | // dynamic options, not parsed from package xml |
| 81 | private String mClassName; |
| 82 | private String mMethodName; |
| 83 | private TestFilter mExcludedTestFilter = new TestFilter(); |
Brett Chabot | 358dc56 | 2011-10-25 15:46:48 -0700 | [diff] [blame] | 84 | private String mTargetBinaryName; |
| 85 | private String mTargetNameSpace; |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 86 | // only timeout per package is supported. To change this to method granularity, |
| 87 | // test invocation should be done in method level. |
| 88 | // So for now, only max timeout for the package is used. |
| 89 | private int mTimeoutInMins = -1; |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 90 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 91 | void setUri(String uri) { |
| 92 | mUri = uri; |
| 93 | } |
| 94 | |
| 95 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 96 | * {@inheritDoc} |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 97 | */ |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 98 | @Override |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 99 | public String getUri() { |
| 100 | return mUri; |
| 101 | } |
| 102 | |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame^] | 103 | void setRunTimeArgs(String runTimeArgs) { |
| 104 | mRunTimeArgs = runTimeArgs; |
| 105 | } |
| 106 | |
| 107 | String getRunTimeArgs() { |
| 108 | return mRunTimeArgs; |
| 109 | } |
| 110 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 111 | void setAppNameSpace(String appNameSpace) { |
| 112 | mAppNameSpace = appNameSpace; |
| 113 | } |
| 114 | |
| 115 | String getAppNameSpace() { |
| 116 | return mAppNameSpace; |
| 117 | } |
| 118 | |
| 119 | void setName(String name) { |
| 120 | mName = name; |
| 121 | } |
| 122 | |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 123 | /** |
| 124 | * {@inheritDoc} |
| 125 | */ |
| 126 | @Override |
| 127 | public String getName() { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 128 | return mName; |
| 129 | } |
| 130 | |
| 131 | void setRunner(String runnerName) { |
| 132 | mRunner = runnerName; |
| 133 | } |
| 134 | |
| 135 | String getRunner() { |
| 136 | return mRunner; |
| 137 | } |
| 138 | |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 139 | void setTestType(String testType) { |
| 140 | mTestType = testType; |
| 141 | } |
| 142 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 143 | String getTestType() { |
| 144 | return mTestType; |
| 145 | } |
| 146 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 147 | void setJarPath(String jarPath) { |
| 148 | mJarPath = jarPath; |
| 149 | } |
| 150 | |
| 151 | String getJarPath() { |
| 152 | return mJarPath; |
| 153 | } |
| 154 | |
| 155 | void setIsSignatureCheck(boolean isSignatureCheckTest) { |
| 156 | mIsSignatureTest = isSignatureCheckTest; |
| 157 | } |
| 158 | |
| 159 | boolean isSignatureCheck() { |
| 160 | return mIsSignatureTest; |
| 161 | } |
| 162 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 163 | void setTestPackageName(String testPackageName) { |
| 164 | mTestPackageName = testPackageName; |
| 165 | } |
| 166 | |
Brett Chabot | 358dc56 | 2011-10-25 15:46:48 -0700 | [diff] [blame] | 167 | void setTargetBinaryName(String targetBinaryName) { |
| 168 | mTargetBinaryName = targetBinaryName; |
| 169 | } |
| 170 | |
| 171 | void setTargetNameSpace(String targetNameSpace) { |
| 172 | mTargetNameSpace = targetNameSpace; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public String getTargetApkName() { |
| 177 | if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) { |
| 178 | return String.format("%s.apk", mTargetBinaryName); |
| 179 | } |
| 180 | return null; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public String getTargetPackageName() { |
| 185 | if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) { |
| 186 | return null; |
| 187 | } |
| 188 | return mTargetNameSpace; |
| 189 | } |
| 190 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 191 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 192 | * {@inheritDoc} |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 193 | */ |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 194 | @Override |
| 195 | public void setExcludedTestFilter(TestFilter excludeFilter) { |
| 196 | mExcludedTestFilter = excludeFilter; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * {@inheritDoc} |
| 201 | */ |
| 202 | @Override |
| 203 | public void setClassName(String className, String methodName) { |
| 204 | mClassName = className; |
| 205 | mMethodName = methodName; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * {@inheritDoc} |
| 210 | */ |
| 211 | @Override |
| 212 | public IRemoteTest createTest(File testCaseDir) { |
| 213 | mExcludedTestFilter.setTestInclusion(mClassName, mMethodName); |
| 214 | mTests = filterTests(); |
| 215 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 216 | if (HOST_SIDE_ONLY_TEST.equals(mTestType)) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 217 | CLog.d("Creating host test for %s", mName); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 218 | JarHostTest hostTest = new JarHostTest(); |
Keun young Park | 14a5bfa | 2012-10-02 13:45:30 -0700 | [diff] [blame] | 219 | if (mTimeoutInMins >= 0) { |
| 220 | CLog.d("Setting new timeout to " + mTimeoutInMins + " mins"); |
| 221 | hostTest.setTimeout(mTimeoutInMins * 60 * 1000); |
| 222 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 223 | hostTest.setRunName(getUri()); |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 224 | hostTest.setJarFileName(mJarPath); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 225 | hostTest.setTests(mTests); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 226 | mDigest = generateDigest(testCaseDir, mJarPath); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 227 | return hostTest; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 228 | } else if (VM_HOST_TEST.equals(mTestType)) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 229 | CLog.d("Creating vm host test for %s", mName); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 230 | VMHostTest vmHostTest = new VMHostTest(); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 231 | vmHostTest.setRunName(getUri()); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 232 | vmHostTest.setJarFileName(mJarPath); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 233 | vmHostTest.setTests(mTests); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 234 | mDigest = generateDigest(testCaseDir, mJarPath); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 235 | return vmHostTest; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 236 | } else if (NATIVE_TEST.equals(mTestType)) { |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 237 | return new GeeTest(mUri, mName); |
Thomas Tafertshofer | b5385a6 | 2012-08-15 15:40:45 -0700 | [diff] [blame] | 238 | } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) { |
| 239 | CLog.d("Creating new wrapped native test for %s", mName); |
| 240 | return new WrappedGTest(mAppNameSpace, mUri, mName, mRunner); |
Svetoslav Ganov | b803f80 | 2012-03-07 19:13:05 -0800 | [diff] [blame] | 241 | } else if (ACCESSIBILITY_TEST.equals(mTestType)) { |
| 242 | AccessibilityTestRunner test = new AccessibilityTestRunner(); |
| 243 | return setInstrumentationTest(test, testCaseDir); |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 244 | } else if (PRINT_TEST.equals(mTestType)) { |
| 245 | PrintTestRunner test = new PrintTestRunner(); |
| 246 | return setPrintTest(test, testCaseDir); |
Brett Chabot | a595baa | 2013-07-01 11:55:36 -0700 | [diff] [blame] | 247 | } else if (ACCESSIBILITY_SERVICE_TEST.equals(mTestType)) { |
| 248 | AccessibilityServiceTestRunner test = new AccessibilityServiceTestRunner(); |
| 249 | return setInstrumentationTest(test, testCaseDir); |
Craig Mautner | a8e522f | 2012-10-22 11:46:30 -0700 | [diff] [blame] | 250 | } else if (DISPLAY_TEST.equals(mTestType)) { |
| 251 | DisplayTestRunner test = new DisplayTestRunner(); |
| 252 | return setInstrumentationTest(test, testCaseDir); |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 253 | } else if (UIAUTOMATOR_TEST.equals(mTestType)) { |
| 254 | UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest(); |
| 255 | return setUiAutomatorTest(uiautomatorTest); |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 256 | } else if (mIsSignatureTest) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 257 | // TODO: hardcode the runner/class/method for now, since current package xml points to |
| 258 | // specialized instrumentation. Eventually this special case for signatureTest can be |
| 259 | // removed, and it can be treated just like a normal InstrumentationTest |
| 260 | CLog.d("Creating signature test %s", mName); |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 261 | InstrumentationApkTest instrTest = new InstrumentationApkTest(); |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 262 | instrTest.setPackageName(mAppNameSpace); |
| 263 | instrTest.setRunnerName("android.test.InstrumentationTestRunner"); |
| 264 | instrTest.setClassName(SIGNATURE_TEST_CLASS); |
| 265 | instrTest.setMethodName(SIGNATURE_TEST_METHOD); |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 266 | // set expected tests to the single signature test |
| 267 | TestIdentifier t = new TestIdentifier(SIGNATURE_TEST_CLASS, SIGNATURE_TEST_METHOD); |
| 268 | mTests.clear(); |
| 269 | mTests.add(t); |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 270 | // mName means 'apk file name' for instrumentation tests |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 271 | instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 272 | mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName)); |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 273 | return instrTest; |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame^] | 274 | } else if (JUNIT_DEVICE_TEST.equals(mTestType)){ |
| 275 | CLog.d("Creating JUnit device test %s", mName); |
| 276 | JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest(); |
| 277 | jUnitDeviceTest.setRunName(getUri()); |
| 278 | jUnitDeviceTest.addTestJarFileName(mJarPath); |
| 279 | jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs); |
| 280 | jUnitDeviceTest.setTests(mTests); |
| 281 | mDigest = generateDigest(testCaseDir, mJarPath); |
| 282 | return jUnitDeviceTest; |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 283 | } else { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 284 | CLog.d("Creating instrumentation test for %s", mName); |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 285 | InstrumentationApkTest instrTest = new InstrumentationApkTest(); |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 286 | if (mTimeoutInMins >= 0) { |
| 287 | // as timeout cannot be set for each test, |
| 288 | // increase the time-out of the whole package |
| 289 | CLog.d("Setting new timeout to " + mTimeoutInMins + " mins"); |
| 290 | instrTest.setTestTimeout(mTimeoutInMins * 60 * 1000); |
| 291 | } |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 292 | return setInstrumentationTest(instrTest, testCaseDir); |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 295 | |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 296 | private PrintTestRunner setPrintTest(PrintTestRunner printTest, |
| 297 | File testCaseDir) { |
| 298 | printTest.setRunName(getUri()); |
| 299 | printTest.setPackageName(mAppNameSpace); |
| 300 | printTest.setRunnerName(mRunner); |
| 301 | printTest.setTestPackageName(mTestPackageName); |
| 302 | printTest.setClassName(mClassName); |
| 303 | printTest.setMethodName(mMethodName); |
| 304 | mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName)); |
| 305 | return printTest; |
| 306 | } |
| 307 | |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 308 | /** |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 309 | * Populates given {@link InstrumentationApkTest} with data from the package xml. |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 310 | * |
| 311 | * @param testCaseDir |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 312 | * @param instrTest |
| 313 | * @return the populated {@link InstrumentationTest} or <code>null</code> |
| 314 | */ |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 315 | private InstrumentationTest setInstrumentationTest(InstrumentationApkTest instrTest, |
| 316 | File testCaseDir) { |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 317 | instrTest.setRunName(getUri()); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 318 | instrTest.setPackageName(mAppNameSpace); |
| 319 | instrTest.setRunnerName(mRunner); |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 320 | instrTest.setTestPackageName(mTestPackageName); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 321 | instrTest.setClassName(mClassName); |
| 322 | instrTest.setMethodName(mMethodName); |
| 323 | instrTest.setTestsToRun(mTests, |
| 324 | !mExcludedTestFilter.hasExclusion() |
| 325 | /* only force batch mode if no tests are excluded */); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 326 | // mName means 'apk file name' for instrumentation tests |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 327 | instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 328 | mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName)); |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 329 | if (mTests.size() > 1000) { |
| 330 | // TODO: hack, large test suites can take longer to collect tests, increase timeout |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 331 | instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 332 | } |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 333 | return instrTest; |
| 334 | } |
| 335 | |
| 336 | /** |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 337 | * Populates given {@link UiAutomatorJarTest} with data from the package xml. |
| 338 | * |
| 339 | * @param uiautomatorTest |
| 340 | * @return the populated {@link UiAutomatorJarTest} or <code>null</code> |
| 341 | */ |
| 342 | private IRemoteTest setUiAutomatorTest(UiAutomatorJarTest uiautomatorTest) { |
| 343 | uiautomatorTest.setInstallArtifacts(getJarPath()); |
| 344 | if (mClassName != null) { |
| 345 | if (mMethodName != null) { |
| 346 | CLog.logAndDisplay(LogLevel.WARN, "ui automator tests don't currently support" + |
| 347 | "running individual methods"); |
| 348 | } |
| 349 | uiautomatorTest.addClassName(mClassName); |
| 350 | } else { |
| 351 | uiautomatorTest.addClassNames(mTestClasses); |
| 352 | } |
| 353 | uiautomatorTest.setRunName(getUri()); |
| 354 | uiautomatorTest.setCaptureLogs(false); |
| 355 | return uiautomatorTest; |
| 356 | } |
| 357 | |
| 358 | /** |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 359 | * Filter the tests to run based on list of excluded tests, class and method name. |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 360 | * |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 361 | * @return the filtered collection of tests |
| 362 | */ |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 363 | private Collection<TestIdentifier> filterTests() { |
| 364 | mExcludedTestFilter.setTestInclusion(mClassName, mMethodName); |
| 365 | return mExcludedTestFilter.filter(mTests); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 369 | * {@inheritDoc} |
| 370 | */ |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 371 | @Override |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 372 | public boolean isKnownTest(TestIdentifier testDef) { |
| 373 | return mTests.contains(testDef); |
| 374 | } |
| 375 | |
| 376 | /** |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 377 | * {@inheritDoc} |
| 378 | */ |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 379 | @Override |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 380 | public boolean isKnownTestClass(String className) { |
| 381 | return mTestClasses.contains(className); |
| 382 | } |
| 383 | |
| 384 | /** |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 385 | * Add a {@link TestIdentifier} to the list of tests in this package. |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 386 | * |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 387 | * @param testDef |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 388 | * @param timeout in mins |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 389 | */ |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 390 | void addTest(TestIdentifier testDef, int timeout) { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 391 | mTests.add(testDef); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 392 | mTestClasses.add(testDef.getClassName()); |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 393 | // 0 means no timeout, so keep 0 if already is. |
| 394 | if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) { |
| 395 | mTimeoutInMins = timeout; |
| 396 | } |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Get the collection of tests in this test package. |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 401 | */ |
Brett Chabot | 1dcb9a5 | 2011-02-10 20:26:57 -0800 | [diff] [blame] | 402 | @Override |
| 403 | public Collection<TestIdentifier> getTests() { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 404 | return mTests; |
| 405 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 406 | |
| 407 | /** |
| 408 | * {@inheritDoc} |
| 409 | */ |
| 410 | @Override |
| 411 | public String getDigest() { |
| 412 | return mDigest; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Generate a sha1sum digest for a file. |
| 417 | * <p/> |
| 418 | * Exposed for unit testing. |
| 419 | * |
| 420 | * @param fileDir the directory of the file |
| 421 | * @param fileName the name of the file |
| 422 | * @return a hex {@link String} of the digest |
| 423 | */ |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 424 | String generateDigest(File fileDir, String fileName) { |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 425 | final String algorithm = "SHA-1"; |
| 426 | InputStream fileStream = null; |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 427 | DigestInputStream d = null; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 428 | try { |
| 429 | fileStream = getFileStream(fileDir, fileName); |
| 430 | MessageDigest md = MessageDigest.getInstance(algorithm); |
| 431 | d = new DigestInputStream(fileStream, md); |
| 432 | byte[] buffer = new byte[8196]; |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 433 | while (d.read(buffer) != -1) { |
| 434 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 435 | return toHexString(md.digest()); |
| 436 | } catch (NoSuchAlgorithmException e) { |
| 437 | return algorithm + " not found"; |
| 438 | } catch (IOException e) { |
| 439 | CLog.e(e); |
| 440 | } finally { |
| 441 | StreamUtil.closeStream(d); |
| 442 | StreamUtil.closeStream(fileStream); |
| 443 | } |
| 444 | return "failed to generate digest"; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Retrieve an input stream for given file |
| 449 | * <p/> |
| 450 | * Exposed so unit tests can mock. |
| 451 | */ |
| 452 | InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException { |
| 453 | InputStream fileStream; |
| 454 | fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName))); |
| 455 | return fileStream; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Convert the given byte array into a lowercase hex string. |
| 460 | * |
| 461 | * @param arr The array to convert. |
| 462 | * @return The hex encoded string. |
| 463 | */ |
| 464 | private String toHexString(byte[] arr) { |
| 465 | StringBuffer buf = new StringBuffer(arr.length * 2); |
| 466 | for (byte b : arr) { |
| 467 | buf.append(String.format("%02x", b & 0xFF)); |
| 468 | } |
| 469 | return buf.toString(); |
| 470 | } |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 471 | } |