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