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; |
Guang Zhu | 7afa91e | 2015-02-06 00:44:34 -0800 | [diff] [blame] | 23 | import com.android.tradefed.targetprep.ITargetPreparer; |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 24 | import com.android.tradefed.testtype.IAbi; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 25 | import com.android.tradefed.testtype.IRemoteTest; |
| 26 | import com.android.tradefed.testtype.InstrumentationTest; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 27 | import com.android.tradefed.util.StreamUtil; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 28 | |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 29 | import java.io.BufferedInputStream; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 30 | import java.io.File; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 31 | import java.io.FileInputStream; |
| 32 | import java.io.FileNotFoundException; |
| 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
| 35 | import java.security.DigestInputStream; |
| 36 | import java.security.MessageDigest; |
| 37 | import java.security.NoSuchAlgorithmException; |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 38 | import java.util.Collection; |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 39 | import java.util.LinkedHashMap; |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 40 | import java.util.LinkedHashSet; |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 41 | import java.util.LinkedList; |
| 42 | import java.util.List; |
| 43 | import java.util.Map; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Container for CTS test info. |
| 47 | * <p/> |
| 48 | * Knows how to translate this info into a runnable {@link IRemoteTest}. |
| 49 | */ |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 50 | class TestPackageDef implements ITestPackageDef { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 51 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 52 | public static final String HOST_SIDE_ONLY_TEST = "hostSideOnly"; |
| 53 | public static final String NATIVE_TEST = "native"; |
Thomas Tafertshofer | b5385a6 | 2012-08-15 15:40:45 -0700 | [diff] [blame] | 54 | public static final String WRAPPED_NATIVE_TEST = "wrappednative"; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 55 | public static final String VM_HOST_TEST = "vmHostTest"; |
Mika Isojärvi | f1e23ee | 2014-08-25 13:17:58 +0300 | [diff] [blame] | 56 | public static final String DEQP_TEST = "deqpTest"; |
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 | |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 60 | private String mAppPackageName = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 61 | private String mAppNameSpace = null; |
| 62 | private String mName = null; |
| 63 | private String mRunner = null; |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 64 | private String mTestType = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 65 | private String mJarPath = null; |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 66 | private String mRunTimeArgs = null; |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 67 | private String mTestPackageName = null; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 68 | private String mDigest = null; |
Jed Estep | 6aab9db | 2015-10-20 10:19:39 -0700 | [diff] [blame^] | 69 | private long mRuntimeHint = 0; |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 70 | private IAbi mAbi = null; |
Guang Zhu | 7afa91e | 2015-02-06 00:44:34 -0800 | [diff] [blame] | 71 | private List<ITargetPreparer> mPreparers = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 72 | |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 73 | // use a LinkedHashSet for predictable iteration insertion-order, and fast |
| 74 | // lookups |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 75 | private Collection<TestIdentifier> mTests = new LinkedHashSet<TestIdentifier>(); |
| 76 | // also maintain an index of known test classes |
| 77 | private Collection<String> mTestClasses = new LinkedHashSet<String>(); |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 78 | // store instance arguments in order too for consistency |
| 79 | private Map<TestIdentifier, List<Map<String, String>>> mTestInstanceArguments = |
| 80 | new LinkedHashMap<>(); |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 81 | |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 82 | // dynamic options, not parsed from package xml |
| 83 | private String mClassName; |
| 84 | private String mMethodName; |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 85 | private TestFilter mTestFilter = new TestFilter(); |
Brett Chabot | 358dc56 | 2011-10-25 15:46:48 -0700 | [diff] [blame] | 86 | private String mTargetBinaryName; |
| 87 | private String mTargetNameSpace; |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 88 | // only timeout per package is supported. To change this to method granularity, |
| 89 | // test invocation should be done in method level. |
| 90 | // So for now, only max timeout for the package is used. |
| 91 | private int mTimeoutInMins = -1; |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 92 | |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public IAbi getAbi() { |
| 95 | return mAbi; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param abi the ABI to run this package on |
| 100 | */ |
| 101 | public void setAbi(IAbi abi) { |
| 102 | mAbi = abi; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @return unique id representing this test package for this ABI. |
| 107 | */ |
| 108 | @Override |
| 109 | public String getId() { |
| 110 | return AbiUtils.createId(getAbi().getName(), getAppPackageName()); |
| 111 | } |
| 112 | |
| 113 | void setAppPackageName(String appPackageName) { |
| 114 | mAppPackageName = appPackageName; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Nicholas Sauer | 6396d21 | 2014-11-05 20:10:21 -0800 | [diff] [blame] | 117 | String getAppPackageName() { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 118 | return mAppPackageName; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 121 | void setRunTimeArgs(String runTimeArgs) { |
| 122 | mRunTimeArgs = runTimeArgs; |
| 123 | } |
| 124 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 125 | void setAppNameSpace(String appNameSpace) { |
| 126 | mAppNameSpace = appNameSpace; |
| 127 | } |
| 128 | |
| 129 | String getAppNameSpace() { |
| 130 | return mAppNameSpace; |
| 131 | } |
| 132 | |
| 133 | void setName(String name) { |
| 134 | mName = name; |
| 135 | } |
| 136 | |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 137 | /** |
| 138 | * {@inheritDoc} |
| 139 | */ |
| 140 | @Override |
Jed Estep | 6aab9db | 2015-10-20 10:19:39 -0700 | [diff] [blame^] | 141 | public long getRuntimeHint() { |
| 142 | return mRuntimeHint; |
| 143 | } |
| 144 | |
| 145 | void setRuntimeHint(long runtimeHint) { |
| 146 | mRuntimeHint = runtimeHint; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * {@inheritDoc} |
| 151 | */ |
| 152 | @Override |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 153 | public String getName() { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 154 | return mName; |
| 155 | } |
| 156 | |
| 157 | void setRunner(String runnerName) { |
| 158 | mRunner = runnerName; |
| 159 | } |
| 160 | |
| 161 | String getRunner() { |
| 162 | return mRunner; |
| 163 | } |
| 164 | |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 165 | void setTestType(String testType) { |
| 166 | mTestType = testType; |
| 167 | } |
| 168 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 169 | String getTestType() { |
| 170 | return mTestType; |
| 171 | } |
| 172 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 173 | void setJarPath(String jarPath) { |
| 174 | mJarPath = jarPath; |
| 175 | } |
| 176 | |
| 177 | String getJarPath() { |
| 178 | return mJarPath; |
| 179 | } |
| 180 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 181 | void setTestPackageName(String testPackageName) { |
| 182 | mTestPackageName = testPackageName; |
| 183 | } |
| 184 | |
Brett Chabot | 358dc56 | 2011-10-25 15:46:48 -0700 | [diff] [blame] | 185 | void setTargetBinaryName(String targetBinaryName) { |
| 186 | mTargetBinaryName = targetBinaryName; |
| 187 | } |
| 188 | |
| 189 | void setTargetNameSpace(String targetNameSpace) { |
| 190 | mTargetNameSpace = targetNameSpace; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public String getTargetApkName() { |
| 195 | if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) { |
| 196 | return String.format("%s.apk", mTargetBinaryName); |
| 197 | } |
| 198 | return null; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public String getTargetPackageName() { |
| 203 | if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) { |
| 204 | return null; |
| 205 | } |
| 206 | return mTargetNameSpace; |
| 207 | } |
| 208 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 209 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 210 | * {@inheritDoc} |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 211 | */ |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 212 | @Override |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 213 | public void setTestFilter(TestFilter testFilter) { |
| 214 | mTestFilter = testFilter; |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * {@inheritDoc} |
| 219 | */ |
| 220 | @Override |
| 221 | public void setClassName(String className, String methodName) { |
| 222 | mClassName = className; |
| 223 | mMethodName = methodName; |
| 224 | } |
| 225 | |
| 226 | /** |
Guang Zhu | 7afa91e | 2015-02-06 00:44:34 -0800 | [diff] [blame] | 227 | * Setter for injecting a list of {@link ITargetPreparer}s as configured in module test config. |
| 228 | * @param preparers |
| 229 | */ |
| 230 | void setPackagePreparers(List<ITargetPreparer> preparers) { |
| 231 | mPreparers = preparers; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * {@inheritDoc} |
| 236 | */ |
| 237 | @Override |
| 238 | public List<ITargetPreparer> getPackagePreparers() { |
| 239 | return mPreparers; |
| 240 | } |
| 241 | |
| 242 | /** |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 243 | * {@inheritDoc} |
| 244 | */ |
| 245 | @Override |
| 246 | public IRemoteTest createTest(File testCaseDir) { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 247 | mTestFilter.setTestInclusion(mClassName, mMethodName); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 248 | mTests = filterTests(); |
| 249 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 250 | if (HOST_SIDE_ONLY_TEST.equals(mTestType)) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 251 | CLog.d("Creating host test for %s", mName); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 252 | JarHostTest hostTest = new JarHostTest(); |
Keun young Park | 14a5bfa | 2012-10-02 13:45:30 -0700 | [diff] [blame] | 253 | if (mTimeoutInMins >= 0) { |
| 254 | CLog.d("Setting new timeout to " + mTimeoutInMins + " mins"); |
| 255 | hostTest.setTimeout(mTimeoutInMins * 60 * 1000); |
| 256 | } |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 257 | hostTest.setRunName(mAppPackageName); |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 258 | hostTest.setJarFileName(mJarPath); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 259 | hostTest.setTests(mTests); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 260 | hostTest.setAbi(mAbi); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 261 | mDigest = generateDigest(testCaseDir, mJarPath); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 262 | return hostTest; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 263 | } else if (VM_HOST_TEST.equals(mTestType)) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 264 | CLog.d("Creating vm host test for %s", mName); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 265 | VMHostTest vmHostTest = new VMHostTest(); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 266 | vmHostTest.setRunName(mAppPackageName); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 267 | vmHostTest.setJarFileName(mJarPath); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 268 | vmHostTest.setTests(mTests); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 269 | vmHostTest.setAbi(mAbi); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 270 | mDigest = generateDigest(testCaseDir, mJarPath); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 271 | return vmHostTest; |
Mika Isojärvi | f1e23ee | 2014-08-25 13:17:58 +0300 | [diff] [blame] | 272 | } else if (DEQP_TEST.equals(mTestType)) { |
Jarkko Pöyry | 91d00e0 | 2015-01-15 18:02:15 -0800 | [diff] [blame] | 273 | DeqpTestRunner deqpTest = |
| 274 | new DeqpTestRunner(mAppPackageName, mName, mTests, mTestInstanceArguments); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 275 | deqpTest.setAbi(mAbi); |
| 276 | return deqpTest; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame] | 277 | } else if (NATIVE_TEST.equals(mTestType)) { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 278 | GeeTest geeTest = new GeeTest(mAppPackageName, mName); |
| 279 | geeTest.setAbi(mAbi); |
| 280 | return geeTest; |
Thomas Tafertshofer | b5385a6 | 2012-08-15 15:40:45 -0700 | [diff] [blame] | 281 | } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) { |
| 282 | CLog.d("Creating new wrapped native test for %s", mName); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 283 | WrappedGTest wrappedGeeTest = new WrappedGTest(mAppNameSpace, mAppPackageName, mName, mRunner); |
| 284 | wrappedGeeTest.setAbi(mAbi); |
| 285 | return wrappedGeeTest; |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 286 | } else if (UIAUTOMATOR_TEST.equals(mTestType)) { |
| 287 | UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest(); |
| 288 | return setUiAutomatorTest(uiautomatorTest); |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 289 | } else if (JUNIT_DEVICE_TEST.equals(mTestType)){ |
| 290 | CLog.d("Creating JUnit device test %s", mName); |
| 291 | JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest(); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 292 | jUnitDeviceTest.setRunName(mAppPackageName); |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 293 | jUnitDeviceTest.addTestJarFileName(mJarPath); |
| 294 | jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs); |
| 295 | jUnitDeviceTest.setTests(mTests); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 296 | jUnitDeviceTest.setAbi(mAbi); |
Tsu Chiang Chuang | d688d4b | 2014-06-26 11:12:37 -0700 | [diff] [blame] | 297 | mDigest = generateDigest(testCaseDir, mJarPath); |
| 298 | return jUnitDeviceTest; |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 299 | } else { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 300 | CLog.d("Creating instrumentation test for %s", mName); |
Stuart Scott | fef5e2c | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 301 | CtsInstrumentationApkTest instrTest = new CtsInstrumentationApkTest(); |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 302 | if (mTimeoutInMins >= 0) { |
| 303 | // as timeout cannot be set for each test, |
| 304 | // increase the time-out of the whole package |
| 305 | CLog.d("Setting new timeout to " + mTimeoutInMins + " mins"); |
| 306 | instrTest.setTestTimeout(mTimeoutInMins * 60 * 1000); |
| 307 | } |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 308 | return setInstrumentationTest(instrTest, testCaseDir); |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 311 | |
| 312 | /** |
Stuart Scott | fef5e2c | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 313 | * Populates given {@link CtsInstrumentationApkTest} with data from the package xml. |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 314 | * |
| 315 | * @param testCaseDir |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 316 | * @param instrTest |
| 317 | * @return the populated {@link InstrumentationTest} or <code>null</code> |
| 318 | */ |
Stuart Scott | fef5e2c | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 319 | private InstrumentationTest setInstrumentationTest(CtsInstrumentationApkTest instrTest, |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 320 | File testCaseDir) { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 321 | instrTest.setRunName(mAppPackageName); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 322 | instrTest.setPackageName(mAppNameSpace); |
| 323 | instrTest.setRunnerName(mRunner); |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 324 | instrTest.setAbi(mAbi); |
Nick Korostelev | 4bfd075 | 2014-08-06 14:56:08 -0700 | [diff] [blame] | 325 | instrTest.setTestsToRun(mTests, false |
| 326 | /* force batch mode off to always run using testFile */); |
| 327 | instrTest.setReRunUsingTestFile(true); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 328 | // mName means 'apk file name' for instrumentation tests |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 329 | instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 330 | mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName)); |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 331 | if (mTests.size() > 1000) { |
| 332 | // 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] | 333 | instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 334 | } |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 335 | return instrTest; |
| 336 | } |
| 337 | |
| 338 | /** |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 339 | * Populates given {@link UiAutomatorJarTest} with data from the package xml. |
| 340 | * |
| 341 | * @param uiautomatorTest |
| 342 | * @return the populated {@link UiAutomatorJarTest} or <code>null</code> |
| 343 | */ |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 344 | @SuppressWarnings("deprecation") |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 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 | } |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 356 | uiautomatorTest.setRunName(mAppPackageName); |
Brett Chabot | 87f4451 | 2012-11-16 16:00:02 -0800 | [diff] [blame] | 357 | uiautomatorTest.setCaptureLogs(false); |
| 358 | return uiautomatorTest; |
| 359 | } |
| 360 | |
| 361 | /** |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 362 | * 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] | 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() { |
Stuart Scott | e00a2b8 | 2014-09-05 17:37:18 -0700 | [diff] [blame] | 367 | mTestFilter.setTestInclusion(mClassName, mMethodName); |
| 368 | return mTestFilter.filter(mTests); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Nicholas Sauer | 6396d21 | 2014-11-05 20:10:21 -0800 | [diff] [blame] | 371 | boolean isKnownTestClass(String className) { |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 372 | return mTestClasses.contains(className); |
| 373 | } |
| 374 | |
| 375 | /** |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 376 | * Add a {@link TestIdentifier} to the list of tests in this package. |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 377 | * |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 378 | * @param testDef |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 379 | * @param timeout in mins |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 380 | */ |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 381 | void addTest(TestIdentifier testDef, int timeout) { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 382 | mTests.add(testDef); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 383 | mTestClasses.add(testDef.getClassName()); |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 384 | mTestInstanceArguments.put(testDef, new LinkedList<Map<String, String>>()); |
Keun young Park | 3424656 | 2012-09-18 11:18:20 -0700 | [diff] [blame] | 385 | // 0 means no timeout, so keep 0 if already is. |
| 386 | if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) { |
| 387 | mTimeoutInMins = timeout; |
| 388 | } |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 392 | * Add a test instance to an existing {@link TestIdentifier}. |
| 393 | */ |
| 394 | void addTestInstance(TestIdentifier testDef, Map<String, String> instanceArguments) { |
| 395 | if (!mTestInstanceArguments.containsKey(testDef)) { |
| 396 | throw new IllegalStateException("test id does not name an existing test"); |
| 397 | } |
| 398 | mTestInstanceArguments.get(testDef).add(instanceArguments); |
| 399 | } |
| 400 | |
| 401 | /** |
Stuart Scott | d606552 | 2014-10-02 10:34:27 -0700 | [diff] [blame] | 402 | * {@inheritDoc} |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 403 | */ |
Brett Chabot | 1dcb9a5 | 2011-02-10 20:26:57 -0800 | [diff] [blame] | 404 | @Override |
| 405 | public Collection<TestIdentifier> getTests() { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 406 | return mTests; |
| 407 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 408 | |
| 409 | /** |
Jarkko Pöyry | 0df3474 | 2015-01-14 10:46:41 -0800 | [diff] [blame] | 410 | * Get the instance argument map for tests. |
| 411 | * <p/> |
| 412 | * Exposed for unit testing. |
| 413 | */ |
| 414 | public Map<TestIdentifier, List<Map<String, String>>> getTestInstanceArguments() { |
| 415 | return mTestInstanceArguments; |
| 416 | } |
| 417 | |
| 418 | /** |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 419 | * {@inheritDoc} |
| 420 | */ |
| 421 | @Override |
| 422 | public String getDigest() { |
| 423 | return mDigest; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Generate a sha1sum digest for a file. |
| 428 | * <p/> |
| 429 | * Exposed for unit testing. |
| 430 | * |
| 431 | * @param fileDir the directory of the file |
| 432 | * @param fileName the name of the file |
| 433 | * @return a hex {@link String} of the digest |
| 434 | */ |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 435 | String generateDigest(File fileDir, String fileName) { |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 436 | final String algorithm = "SHA-1"; |
| 437 | InputStream fileStream = null; |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 438 | DigestInputStream d = null; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 439 | try { |
| 440 | fileStream = getFileStream(fileDir, fileName); |
| 441 | MessageDigest md = MessageDigest.getInstance(algorithm); |
| 442 | d = new DigestInputStream(fileStream, md); |
| 443 | byte[] buffer = new byte[8196]; |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 444 | while (d.read(buffer) != -1) { |
| 445 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 446 | return toHexString(md.digest()); |
| 447 | } catch (NoSuchAlgorithmException e) { |
| 448 | return algorithm + " not found"; |
| 449 | } catch (IOException e) { |
| 450 | CLog.e(e); |
| 451 | } finally { |
Guang Zhu | 7afa91e | 2015-02-06 00:44:34 -0800 | [diff] [blame] | 452 | StreamUtil.close(d); |
| 453 | StreamUtil.close(fileStream); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 454 | } |
| 455 | return "failed to generate digest"; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Retrieve an input stream for given file |
| 460 | * <p/> |
| 461 | * Exposed so unit tests can mock. |
| 462 | */ |
| 463 | InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException { |
| 464 | InputStream fileStream; |
| 465 | fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName))); |
| 466 | return fileStream; |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Convert the given byte array into a lowercase hex string. |
| 471 | * |
| 472 | * @param arr The array to convert. |
| 473 | * @return The hex encoded string. |
| 474 | */ |
| 475 | private String toHexString(byte[] arr) { |
Nicholas Sauer | 6396d21 | 2014-11-05 20:10:21 -0800 | [diff] [blame] | 476 | StringBuilder buf = new StringBuilder(arr.length * 2); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 477 | for (byte b : arr) { |
| 478 | buf.append(String.format("%02x", b & 0xFF)); |
| 479 | } |
| 480 | return buf.toString(); |
| 481 | } |
Nicholas Sauer | c176cac | 2014-10-23 15:36:49 -0700 | [diff] [blame] | 482 | |
| 483 | @Override |
| 484 | public int compareTo(ITestPackageDef testPackageDef) { |
| 485 | return getId().compareTo(testPackageDef.getId()); |
| 486 | } |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 487 | } |