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 | |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 19 | import com.android.cts.util.AbiUtils; |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 20 | import com.android.ddmlib.Log.LogLevel; |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 21 | import com.android.ddmlib.testrunner.TestIdentifier; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 22 | import com.android.tradefed.log.LogUtil.CLog; |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 23 | import com.android.tradefed.testtype.IAbi; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 24 | import com.android.tradefed.testtype.IRemoteTest; |
| 25 | import com.android.tradefed.testtype.InstrumentationTest; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 26 | import com.android.tradefed.util.StreamUtil; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 27 | |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 28 | import java.io.BufferedInputStream; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 29 | import java.io.File; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 30 | import java.io.FileInputStream; |
| 31 | import java.io.FileNotFoundException; |
| 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | import java.security.DigestInputStream; |
| 35 | import java.security.MessageDigest; |
| 36 | import java.security.NoSuchAlgorithmException; |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 37 | import java.util.Collection; |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 38 | import java.util.LinkedHashMap; |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 39 | import java.util.LinkedHashSet; |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 40 | import java.util.LinkedList; |
| 41 | import java.util.List; |
| 42 | import java.util.Map; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Container for CTS test info. |
| 46 | * <p/> |
| 47 | * Knows how to translate this info into a runnable {@link IRemoteTest}. |
| 48 | */ |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 49 | class TestPackageDef implements ITestPackageDef { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 50 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 51 | public static final String HOST_SIDE_ONLY_TEST = "hostSideOnly"; |
| 52 | public static final String NATIVE_TEST = "native"; |
Thomas Tafertshofer | b5385a6 | 2012-08-15 15:40:45 -0700 | [diff] [blame] | 53 | public static final String WRAPPED_NATIVE_TEST = "wrappednative"; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 54 | public static final String VM_HOST_TEST = "vmHostTest"; |
Mika Isojärvi | f1e23ee | 2014-08-25 13:17:58 +0300 | [diff] [blame] | 55 | public static final String DEQP_TEST = "deqpTest"; |
Svetoslav Ganov | b803f80 | 2012-03-07 19:13:05 -0800 | [diff] [blame] | 56 | public static final String ACCESSIBILITY_TEST = |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 57 | "com.android.cts.tradefed.testtype.AccessibilityTestRunner"; |
Brett Chabot | a595baa | 2013-07-01 11:55:36 -0700 | [diff] [blame] | 58 | public static final String ACCESSIBILITY_SERVICE_TEST = |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 59 | "com.android.cts.tradefed.testtype.AccessibilityServiceTestRunner"; |
| 60 | public static final String PRINT_TEST = |
| 61 | "com.android.cts.tradefed.testtype.PrintTestRunner"; |
Craig Mautner | a8e522f | 2012-10-22 11:46:30 -0700 | [diff] [blame] | 62 | public static final String DISPLAY_TEST = |
| 63 | "com.android.cts.tradefed.testtype.DisplayTestRunner"; |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 64 | public static final String UIAUTOMATOR_TEST = "uiAutomator"; |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 65 | public static final String JUNIT_DEVICE_TEST = "jUnitDeviceTest"; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 66 | |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 67 | private String mAppPackageName = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 68 | private String mAppNameSpace = null; |
| 69 | private String mName = null; |
| 70 | private String mRunner = null; |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 71 | private String mTestType = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 72 | private String mJarPath = null; |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 73 | private String mRunTimeArgs = null; |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 74 | private String mTestPackageName = null; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 75 | private String mDigest = null; |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 76 | private IAbi mAbi = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 77 | |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 78 | // use a LinkedHashSet for predictable iteration insertion-order, and fast |
| 79 | // lookups |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 80 | 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öyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 83 | // store instance arguments in order too for consistency |
| 84 | private Map<TestIdentifier, List<Map<String, String>>> mTestInstanceArguments = |
| 85 | new LinkedHashMap<>(); |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 86 | |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 87 | // dynamic options, not parsed from package xml |
| 88 | private String mClassName; |
| 89 | private String mMethodName; |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 90 | private TestFilter mTestFilter = new TestFilter(); |
Brett Chabot | 358dc56 | 2011-10-25 15:46:48 -0700 | [diff] [blame] | 91 | private String mTargetBinaryName; |
| 92 | private String mTargetNameSpace; |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 93 | // 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 Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 97 | |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 98 | @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 Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Nicholas Sauer | 6396d21 | 2014-11-05 20:10:21 -0800 | [diff] [blame] | 122 | String getAppPackageName() { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 123 | return mAppPackageName; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 126 | void setRunTimeArgs(String runTimeArgs) { |
| 127 | mRunTimeArgs = runTimeArgs; |
| 128 | } |
| 129 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 130 | 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 Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 142 | /** |
| 143 | * {@inheritDoc} |
| 144 | */ |
| 145 | @Override |
| 146 | public String getName() { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 147 | return mName; |
| 148 | } |
| 149 | |
| 150 | void setRunner(String runnerName) { |
| 151 | mRunner = runnerName; |
| 152 | } |
| 153 | |
| 154 | String getRunner() { |
| 155 | return mRunner; |
| 156 | } |
| 157 | |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 158 | void setTestType(String testType) { |
| 159 | mTestType = testType; |
| 160 | } |
| 161 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 162 | String getTestType() { |
| 163 | return mTestType; |
| 164 | } |
| 165 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 166 | void setJarPath(String jarPath) { |
| 167 | mJarPath = jarPath; |
| 168 | } |
| 169 | |
| 170 | String getJarPath() { |
| 171 | return mJarPath; |
| 172 | } |
| 173 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 174 | void setTestPackageName(String testPackageName) { |
| 175 | mTestPackageName = testPackageName; |
| 176 | } |
| 177 | |
Brett Chabot | 358dc56 | 2011-10-25 15:46:48 -0700 | [diff] [blame] | 178 | 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 Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 202 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 203 | * {@inheritDoc} |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 204 | */ |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 205 | @Override |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 206 | public void setTestFilter(TestFilter testFilter) { |
| 207 | mTestFilter = testFilter; |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 208 | } |
| 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 Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 224 | mTestFilter.setTestInclusion(mClassName, mMethodName); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 225 | mTests = filterTests(); |
| 226 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 227 | if (HOST_SIDE_ONLY_TEST.equals(mTestType)) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 228 | CLog.d("Creating host test for %s", mName); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 229 | JarHostTest hostTest = new JarHostTest(); |
Keun young Park | 14a5bfa | 2012-10-02 13:45:30 -0700 | [diff] [blame] | 230 | if (mTimeoutInMins >= 0) { |
| 231 | CLog.d("Setting new timeout to " + mTimeoutInMins + " mins"); |
| 232 | hostTest.setTimeout(mTimeoutInMins * 60 * 1000); |
| 233 | } |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 234 | hostTest.setRunName(mAppPackageName); |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 235 | hostTest.setJarFileName(mJarPath); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 236 | hostTest.setTests(mTests); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 237 | hostTest.setAbi(mAbi); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 238 | mDigest = generateDigest(testCaseDir, mJarPath); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 239 | return hostTest; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 240 | } else if (VM_HOST_TEST.equals(mTestType)) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 241 | CLog.d("Creating vm host test for %s", mName); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 242 | VMHostTest vmHostTest = new VMHostTest(); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 243 | vmHostTest.setRunName(mAppPackageName); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 244 | vmHostTest.setJarFileName(mJarPath); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 245 | vmHostTest.setTests(mTests); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 246 | vmHostTest.setAbi(mAbi); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 247 | mDigest = generateDigest(testCaseDir, mJarPath); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 248 | return vmHostTest; |
Mika Isojärvi | f1e23ee | 2014-08-25 13:17:58 +0300 | [diff] [blame] | 249 | } else if (DEQP_TEST.equals(mTestType)) { |
Jarkko Pöyry | 91d00e0 | 2015-01-15 18:02:15 -0800 | [diff] [blame^] | 250 | DeqpTestRunner deqpTest = |
| 251 | new DeqpTestRunner(mAppPackageName, mName, mTests, mTestInstanceArguments); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 252 | deqpTest.setAbi(mAbi); |
| 253 | return deqpTest; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 254 | } else if (NATIVE_TEST.equals(mTestType)) { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 255 | GeeTest geeTest = new GeeTest(mAppPackageName, mName); |
| 256 | geeTest.setAbi(mAbi); |
| 257 | return geeTest; |
Thomas Tafertshofer | b5385a6 | 2012-08-15 15:40:45 -0700 | [diff] [blame] | 258 | } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) { |
| 259 | CLog.d("Creating new wrapped native test for %s", mName); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 260 | WrappedGTest wrappedGeeTest = new WrappedGTest(mAppNameSpace, mAppPackageName, mName, mRunner); |
| 261 | wrappedGeeTest.setAbi(mAbi); |
| 262 | return wrappedGeeTest; |
Svetoslav Ganov | b803f80 | 2012-03-07 19:13:05 -0800 | [diff] [blame] | 263 | } else if (ACCESSIBILITY_TEST.equals(mTestType)) { |
| 264 | AccessibilityTestRunner test = new AccessibilityTestRunner(); |
| 265 | return setInstrumentationTest(test, testCaseDir); |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 266 | } else if (PRINT_TEST.equals(mTestType)) { |
| 267 | PrintTestRunner test = new PrintTestRunner(); |
| 268 | return setPrintTest(test, testCaseDir); |
Brett Chabot | a595baa | 2013-07-01 11:55:36 -0700 | [diff] [blame] | 269 | } else if (ACCESSIBILITY_SERVICE_TEST.equals(mTestType)) { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 270 | @SuppressWarnings("deprecation") |
Brett Chabot | a595baa | 2013-07-01 11:55:36 -0700 | [diff] [blame] | 271 | AccessibilityServiceTestRunner test = new AccessibilityServiceTestRunner(); |
| 272 | return setInstrumentationTest(test, testCaseDir); |
Craig Mautner | a8e522f | 2012-10-22 11:46:30 -0700 | [diff] [blame] | 273 | } else if (DISPLAY_TEST.equals(mTestType)) { |
| 274 | DisplayTestRunner test = new DisplayTestRunner(); |
| 275 | return setInstrumentationTest(test, testCaseDir); |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 276 | } else if (UIAUTOMATOR_TEST.equals(mTestType)) { |
| 277 | UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest(); |
| 278 | return setUiAutomatorTest(uiautomatorTest); |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 279 | } else if (JUNIT_DEVICE_TEST.equals(mTestType)){ |
| 280 | CLog.d("Creating JUnit device test %s", mName); |
| 281 | JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest(); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 282 | jUnitDeviceTest.setRunName(mAppPackageName); |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 283 | jUnitDeviceTest.addTestJarFileName(mJarPath); |
| 284 | jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs); |
| 285 | jUnitDeviceTest.setTests(mTests); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 286 | jUnitDeviceTest.setAbi(mAbi); |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 287 | mDigest = generateDigest(testCaseDir, mJarPath); |
| 288 | return jUnitDeviceTest; |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 289 | } else { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 290 | CLog.d("Creating instrumentation test for %s", mName); |
Stuart Scott | fef5e2c | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 291 | CtsInstrumentationApkTest instrTest = new CtsInstrumentationApkTest(); |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 292 | 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 Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 298 | return setInstrumentationTest(instrTest, testCaseDir); |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 301 | |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 302 | private PrintTestRunner setPrintTest(PrintTestRunner printTest, |
| 303 | File testCaseDir) { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 304 | printTest.setRunName(mAppPackageName); |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 305 | printTest.setPackageName(mAppNameSpace); |
| 306 | printTest.setRunnerName(mRunner); |
| 307 | printTest.setTestPackageName(mTestPackageName); |
| 308 | printTest.setClassName(mClassName); |
| 309 | printTest.setMethodName(mMethodName); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 310 | printTest.setAbi(mAbi); |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 311 | mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName)); |
| 312 | return printTest; |
| 313 | } |
| 314 | |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 315 | /** |
Stuart Scott | fef5e2c | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 316 | * Populates given {@link CtsInstrumentationApkTest} with data from the package xml. |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 317 | * |
| 318 | * @param testCaseDir |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 319 | * @param instrTest |
| 320 | * @return the populated {@link InstrumentationTest} or <code>null</code> |
| 321 | */ |
Stuart Scott | fef5e2c | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 322 | private InstrumentationTest setInstrumentationTest(CtsInstrumentationApkTest instrTest, |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 323 | File testCaseDir) { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 324 | instrTest.setRunName(mAppPackageName); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 325 | instrTest.setPackageName(mAppNameSpace); |
| 326 | instrTest.setRunnerName(mRunner); |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 327 | instrTest.setTestPackageName(mTestPackageName); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 328 | instrTest.setClassName(mClassName); |
| 329 | instrTest.setMethodName(mMethodName); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 330 | instrTest.setAbi(mAbi); |
Nick Korostelev | 4bfd075 | 2014-08-06 14:56:08 -0700 | [diff] [blame] | 331 | instrTest.setTestsToRun(mTests, false |
| 332 | /* force batch mode off to always run using testFile */); |
| 333 | instrTest.setReRunUsingTestFile(true); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 334 | // mName means 'apk file name' for instrumentation tests |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 335 | instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 336 | mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName)); |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 337 | if (mTests.size() > 1000) { |
| 338 | // 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] | 339 | instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 340 | } |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 341 | return instrTest; |
| 342 | } |
| 343 | |
| 344 | /** |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 345 | * 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 Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 350 | @SuppressWarnings("deprecation") |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 351 | 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 Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 362 | uiautomatorTest.setRunName(mAppPackageName); |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 363 | uiautomatorTest.setCaptureLogs(false); |
| 364 | return uiautomatorTest; |
| 365 | } |
| 366 | |
| 367 | /** |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 368 | * Filter the tests to run based on list of included/excluded tests, class and method name. |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 369 | * |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 370 | * @return the filtered collection of tests |
| 371 | */ |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 372 | private Collection<TestIdentifier> filterTests() { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 373 | mTestFilter.setTestInclusion(mClassName, mMethodName); |
| 374 | return mTestFilter.filter(mTests); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Nicholas Sauer | 6396d21 | 2014-11-05 20:10:21 -0800 | [diff] [blame] | 377 | boolean isKnownTestClass(String className) { |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 378 | return mTestClasses.contains(className); |
| 379 | } |
| 380 | |
| 381 | /** |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 382 | * Add a {@link TestIdentifier} to the list of tests in this package. |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 383 | * |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 384 | * @param testDef |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 385 | * @param timeout in mins |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 386 | */ |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 387 | void addTest(TestIdentifier testDef, int timeout) { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 388 | mTests.add(testDef); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 389 | mTestClasses.add(testDef.getClassName()); |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 390 | mTestInstanceArguments.put(testDef, new LinkedList<Map<String, String>>()); |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 391 | // 0 means no timeout, so keep 0 if already is. |
| 392 | if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) { |
| 393 | mTimeoutInMins = timeout; |
| 394 | } |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 398 | * 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 Scott | d606552 | 2014-10-02 10:34:27 -0700 | [diff] [blame] | 408 | * {@inheritDoc} |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 409 | */ |
Brett Chabot | 1dcb9a5 | 2011-02-10 20:26:57 -0800 | [diff] [blame] | 410 | @Override |
| 411 | public Collection<TestIdentifier> getTests() { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 412 | return mTests; |
| 413 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 414 | |
| 415 | /** |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 416 | * 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 Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 425 | * {@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 Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 441 | String generateDigest(File fileDir, String fileName) { |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 442 | final String algorithm = "SHA-1"; |
| 443 | InputStream fileStream = null; |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 444 | DigestInputStream d = null; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 445 | 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 Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 450 | while (d.read(buffer) != -1) { |
| 451 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 452 | 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 Sauer | 6396d21 | 2014-11-05 20:10:21 -0800 | [diff] [blame] | 482 | StringBuilder buf = new StringBuilder(arr.length * 2); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 483 | for (byte b : arr) { |
| 484 | buf.append(String.format("%02x", b & 0xFF)); |
| 485 | } |
| 486 | return buf.toString(); |
| 487 | } |
Nicholas Sauer | c176cac | 2014-10-23 15:36:49 -0700 | [diff] [blame] | 488 | |
| 489 | @Override |
| 490 | public int compareTo(ITestPackageDef testPackageDef) { |
| 491 | return getId().compareTo(testPackageDef.getId()); |
| 492 | } |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 493 | } |