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; |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 38 | import java.util.LinkedHashSet; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Container for CTS test info. |
| 42 | * <p/> |
| 43 | * Knows how to translate this info into a runnable {@link IRemoteTest}. |
| 44 | */ |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 45 | class TestPackageDef implements ITestPackageDef { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 46 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 47 | public static final String HOST_SIDE_ONLY_TEST = "hostSideOnly"; |
| 48 | public static final String NATIVE_TEST = "native"; |
Thomas Tafertshofer | b5385a6 | 2012-08-15 15:40:45 -0700 | [diff] [blame] | 49 | public static final String WRAPPED_NATIVE_TEST = "wrappednative"; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 50 | public static final String VM_HOST_TEST = "vmHostTest"; |
Mika Isojärvi | f1e23ee | 2014-08-25 13:17:58 +0300 | [diff] [blame] | 51 | public static final String DEQP_TEST = "deqpTest"; |
Svetoslav Ganov | b803f80 | 2012-03-07 19:13:05 -0800 | [diff] [blame] | 52 | public static final String ACCESSIBILITY_TEST = |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 53 | "com.android.cts.tradefed.testtype.AccessibilityTestRunner"; |
Brett Chabot | a595baa | 2013-07-01 11:55:36 -0700 | [diff] [blame] | 54 | public static final String ACCESSIBILITY_SERVICE_TEST = |
Svetoslav | 12c82d4 | 2013-12-20 12:34:52 -0800 | [diff] [blame] | 55 | "com.android.cts.tradefed.testtype.AccessibilityServiceTestRunner"; |
| 56 | public static final String PRINT_TEST = |
| 57 | "com.android.cts.tradefed.testtype.PrintTestRunner"; |
Craig Mautner | a8e522f | 2012-10-22 11:46:30 -0700 | [diff] [blame] | 58 | public static final String DISPLAY_TEST = |
| 59 | "com.android.cts.tradefed.testtype.DisplayTestRunner"; |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 60 | public static final String UIAUTOMATOR_TEST = "uiAutomator"; |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 61 | public static final String JUNIT_DEVICE_TEST = "jUnitDeviceTest"; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 62 | |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 63 | private String mAppPackageName = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 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; |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 70 | private String mTestPackageName = null; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 71 | private String mDigest = null; |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 72 | private IAbi mAbi = 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; |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 83 | private TestFilter mTestFilter = 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 | |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 91 | @Override |
| 92 | public IAbi getAbi() { |
| 93 | return mAbi; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param abi the ABI to run this package on |
| 98 | */ |
| 99 | public void setAbi(IAbi abi) { |
| 100 | mAbi = abi; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return unique id representing this test package for this ABI. |
| 105 | */ |
| 106 | @Override |
| 107 | public String getId() { |
| 108 | return AbiUtils.createId(getAbi().getName(), getAppPackageName()); |
| 109 | } |
| 110 | |
| 111 | void setAppPackageName(String appPackageName) { |
| 112 | mAppPackageName = appPackageName; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 116 | * {@inheritDoc} |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 117 | */ |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 118 | @Override |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 119 | public String getAppPackageName() { |
| 120 | return mAppPackageName; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 123 | void setRunTimeArgs(String runTimeArgs) { |
| 124 | mRunTimeArgs = runTimeArgs; |
| 125 | } |
| 126 | |
| 127 | String getRunTimeArgs() { |
| 128 | return mRunTimeArgs; |
| 129 | } |
| 130 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 131 | void setAppNameSpace(String appNameSpace) { |
| 132 | mAppNameSpace = appNameSpace; |
| 133 | } |
| 134 | |
| 135 | String getAppNameSpace() { |
| 136 | return mAppNameSpace; |
| 137 | } |
| 138 | |
| 139 | void setName(String name) { |
| 140 | mName = name; |
| 141 | } |
| 142 | |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 143 | /** |
| 144 | * {@inheritDoc} |
| 145 | */ |
| 146 | @Override |
| 147 | public String getName() { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 148 | return mName; |
| 149 | } |
| 150 | |
| 151 | void setRunner(String runnerName) { |
| 152 | mRunner = runnerName; |
| 153 | } |
| 154 | |
| 155 | String getRunner() { |
| 156 | return mRunner; |
| 157 | } |
| 158 | |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 159 | void setTestType(String testType) { |
| 160 | mTestType = testType; |
| 161 | } |
| 162 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 163 | String getTestType() { |
| 164 | return mTestType; |
| 165 | } |
| 166 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 167 | void setJarPath(String jarPath) { |
| 168 | mJarPath = jarPath; |
| 169 | } |
| 170 | |
| 171 | String getJarPath() { |
| 172 | return mJarPath; |
| 173 | } |
| 174 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 175 | void setTestPackageName(String testPackageName) { |
| 176 | mTestPackageName = testPackageName; |
| 177 | } |
| 178 | |
Brett Chabot | 358dc56 | 2011-10-25 15:46:48 -0700 | [diff] [blame] | 179 | void setTargetBinaryName(String targetBinaryName) { |
| 180 | mTargetBinaryName = targetBinaryName; |
| 181 | } |
| 182 | |
| 183 | void setTargetNameSpace(String targetNameSpace) { |
| 184 | mTargetNameSpace = targetNameSpace; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public String getTargetApkName() { |
| 189 | if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) { |
| 190 | return String.format("%s.apk", mTargetBinaryName); |
| 191 | } |
| 192 | return null; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public String getTargetPackageName() { |
| 197 | if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) { |
| 198 | return null; |
| 199 | } |
| 200 | return mTargetNameSpace; |
| 201 | } |
| 202 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 203 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 204 | * {@inheritDoc} |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 205 | */ |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 206 | @Override |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 207 | public void setTestFilter(TestFilter testFilter) { |
| 208 | mTestFilter = testFilter; |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * {@inheritDoc} |
| 213 | */ |
| 214 | @Override |
| 215 | public void setClassName(String className, String methodName) { |
| 216 | mClassName = className; |
| 217 | mMethodName = methodName; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * {@inheritDoc} |
| 222 | */ |
| 223 | @Override |
| 224 | public IRemoteTest createTest(File testCaseDir) { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 225 | mTestFilter.setTestInclusion(mClassName, mMethodName); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 226 | mTests = filterTests(); |
| 227 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 228 | if (HOST_SIDE_ONLY_TEST.equals(mTestType)) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 229 | CLog.d("Creating host test for %s", mName); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 230 | JarHostTest hostTest = new JarHostTest(); |
Keun young Park | 14a5bfa | 2012-10-02 13:45:30 -0700 | [diff] [blame] | 231 | if (mTimeoutInMins >= 0) { |
| 232 | CLog.d("Setting new timeout to " + mTimeoutInMins + " mins"); |
| 233 | hostTest.setTimeout(mTimeoutInMins * 60 * 1000); |
| 234 | } |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 235 | hostTest.setRunName(mAppPackageName); |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 236 | hostTest.setJarFileName(mJarPath); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 237 | hostTest.setTests(mTests); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 238 | hostTest.setAbi(mAbi); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 239 | mDigest = generateDigest(testCaseDir, mJarPath); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 240 | return hostTest; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 241 | } else if (VM_HOST_TEST.equals(mTestType)) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 242 | CLog.d("Creating vm host test for %s", mName); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 243 | VMHostTest vmHostTest = new VMHostTest(); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 244 | vmHostTest.setRunName(mAppPackageName); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 245 | vmHostTest.setJarFileName(mJarPath); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 246 | vmHostTest.setTests(mTests); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 247 | vmHostTest.setAbi(mAbi); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 248 | mDigest = generateDigest(testCaseDir, mJarPath); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 249 | return vmHostTest; |
Mika Isojärvi | f1e23ee | 2014-08-25 13:17:58 +0300 | [diff] [blame] | 250 | } else if (DEQP_TEST.equals(mTestType)) { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 251 | DeqpTestRunner deqpTest = new DeqpTestRunner(mAppPackageName, mName, mTests); |
| 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 | |
| 377 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 378 | * {@inheritDoc} |
| 379 | */ |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 380 | @Override |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 381 | public boolean isKnownTest(TestIdentifier testDef) { |
| 382 | return mTests.contains(testDef); |
| 383 | } |
| 384 | |
| 385 | /** |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 386 | * {@inheritDoc} |
| 387 | */ |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 388 | @Override |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 389 | public boolean isKnownTestClass(String className) { |
| 390 | return mTestClasses.contains(className); |
| 391 | } |
| 392 | |
| 393 | /** |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 394 | * Add a {@link TestIdentifier} to the list of tests in this package. |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 395 | * |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 396 | * @param testDef |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 397 | * @param timeout in mins |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 398 | */ |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 399 | void addTest(TestIdentifier testDef, int timeout) { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 400 | mTests.add(testDef); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 401 | mTestClasses.add(testDef.getClassName()); |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 402 | // 0 means no timeout, so keep 0 if already is. |
| 403 | if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) { |
| 404 | mTimeoutInMins = timeout; |
| 405 | } |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /** |
Stuart Scott | d606552 | 2014-10-02 10:34:27 -0700 | [diff] [blame] | 409 | * {@inheritDoc} |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 410 | */ |
Brett Chabot | 1dcb9a5 | 2011-02-10 20:26:57 -0800 | [diff] [blame] | 411 | @Override |
| 412 | public Collection<TestIdentifier> getTests() { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 413 | return mTests; |
| 414 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 415 | |
| 416 | /** |
| 417 | * {@inheritDoc} |
| 418 | */ |
| 419 | @Override |
| 420 | public String getDigest() { |
| 421 | return mDigest; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Generate a sha1sum digest for a file. |
| 426 | * <p/> |
| 427 | * Exposed for unit testing. |
| 428 | * |
| 429 | * @param fileDir the directory of the file |
| 430 | * @param fileName the name of the file |
| 431 | * @return a hex {@link String} of the digest |
| 432 | */ |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 433 | String generateDigest(File fileDir, String fileName) { |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 434 | final String algorithm = "SHA-1"; |
| 435 | InputStream fileStream = null; |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 436 | DigestInputStream d = null; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 437 | try { |
| 438 | fileStream = getFileStream(fileDir, fileName); |
| 439 | MessageDigest md = MessageDigest.getInstance(algorithm); |
| 440 | d = new DigestInputStream(fileStream, md); |
| 441 | byte[] buffer = new byte[8196]; |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 442 | while (d.read(buffer) != -1) { |
| 443 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 444 | return toHexString(md.digest()); |
| 445 | } catch (NoSuchAlgorithmException e) { |
| 446 | return algorithm + " not found"; |
| 447 | } catch (IOException e) { |
| 448 | CLog.e(e); |
| 449 | } finally { |
| 450 | StreamUtil.closeStream(d); |
| 451 | StreamUtil.closeStream(fileStream); |
| 452 | } |
| 453 | return "failed to generate digest"; |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Retrieve an input stream for given file |
| 458 | * <p/> |
| 459 | * Exposed so unit tests can mock. |
| 460 | */ |
| 461 | InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException { |
| 462 | InputStream fileStream; |
| 463 | fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName))); |
| 464 | return fileStream; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Convert the given byte array into a lowercase hex string. |
| 469 | * |
| 470 | * @param arr The array to convert. |
| 471 | * @return The hex encoded string. |
| 472 | */ |
| 473 | private String toHexString(byte[] arr) { |
| 474 | StringBuffer buf = new StringBuffer(arr.length * 2); |
| 475 | for (byte b : arr) { |
| 476 | buf.append(String.format("%02x", b & 0xFF)); |
| 477 | } |
| 478 | return buf.toString(); |
| 479 | } |
Nicholas Sauer | c176cac | 2014-10-23 15:36:49 -0700 | [diff] [blame^] | 480 | |
| 481 | @Override |
| 482 | public int compareTo(ITestPackageDef testPackageDef) { |
| 483 | return getId().compareTo(testPackageDef.getId()); |
| 484 | } |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 485 | } |