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 | */ |
| 16 | package com.android.cts.tradefed.testtype; |
| 17 | |
| 18 | import com.android.ddmlib.Log; |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 19 | import com.android.ddmlib.testrunner.TestIdentifier; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 20 | import com.android.tradefed.testtype.IRemoteTest; |
| 21 | import com.android.tradefed.testtype.InstrumentationTest; |
| 22 | |
| 23 | import java.io.File; |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 24 | import java.util.ArrayList; |
| 25 | import java.util.Collection; |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 26 | import java.util.LinkedHashSet; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Container for CTS test info. |
| 30 | * <p/> |
| 31 | * Knows how to translate this info into a runnable {@link IRemoteTest}. |
| 32 | */ |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 33 | class TestPackageDef implements ITestPackageDef { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 34 | |
| 35 | private static final String LOG_TAG = "TestPackageDef"; |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 36 | private static final String SIGNATURE_TEST_METHOD = "testSignature"; |
| 37 | private static final String SIGNATURE_TEST_CLASS = "android.tests.sigtest.SimpleSignatureTest"; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 38 | |
| 39 | private String mUri = null; |
| 40 | private String mAppNameSpace = null; |
| 41 | private String mName = null; |
| 42 | private String mRunner = null; |
| 43 | private boolean mIsHostSideTest = false; |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 44 | private boolean mIsVMHostTest = false; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 45 | private String mJarPath = null; |
| 46 | private boolean mIsSignatureTest = false; |
| 47 | private boolean mIsReferenceAppTest = false; |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 48 | private String mPackageToTest = null; |
| 49 | private String mApkToTestName = null; |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame^] | 50 | private String mTestPackageName = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 51 | |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 52 | // use a LinkedHashSet for predictable iteration insertion-order, and fast lookups |
| 53 | private Collection<TestIdentifier> mTests = new LinkedHashSet<TestIdentifier>(); |
| 54 | // also maintain an index of known test classes |
| 55 | private Collection<String> mTestClasses = new LinkedHashSet<String>(); |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 56 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 57 | void setUri(String uri) { |
| 58 | mUri = uri; |
| 59 | } |
| 60 | |
| 61 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 62 | * {@inheritDoc} |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 63 | */ |
| 64 | public String getUri() { |
| 65 | return mUri; |
| 66 | } |
| 67 | |
| 68 | void setAppNameSpace(String appNameSpace) { |
| 69 | mAppNameSpace = appNameSpace; |
| 70 | } |
| 71 | |
| 72 | String getAppNameSpace() { |
| 73 | return mAppNameSpace; |
| 74 | } |
| 75 | |
| 76 | void setName(String name) { |
| 77 | mName = name; |
| 78 | } |
| 79 | |
| 80 | String getName() { |
| 81 | return mName; |
| 82 | } |
| 83 | |
| 84 | void setRunner(String runnerName) { |
| 85 | mRunner = runnerName; |
| 86 | } |
| 87 | |
| 88 | String getRunner() { |
| 89 | return mRunner; |
| 90 | } |
| 91 | |
| 92 | void setIsHostSideTest(boolean hostSideTest) { |
| 93 | mIsHostSideTest = hostSideTest; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | boolean isHostSideTest() { |
| 98 | return mIsHostSideTest; |
| 99 | } |
| 100 | |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 101 | void setIsVMHostTest(boolean vmHostTest) { |
| 102 | mIsVMHostTest = vmHostTest; |
| 103 | |
| 104 | } |
| 105 | |
| 106 | boolean isVMHostTest() { |
| 107 | return mIsVMHostTest; |
| 108 | } |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 109 | void setJarPath(String jarPath) { |
| 110 | mJarPath = jarPath; |
| 111 | } |
| 112 | |
| 113 | String getJarPath() { |
| 114 | return mJarPath; |
| 115 | } |
| 116 | |
| 117 | void setIsSignatureCheck(boolean isSignatureCheckTest) { |
| 118 | mIsSignatureTest = isSignatureCheckTest; |
| 119 | } |
| 120 | |
| 121 | boolean isSignatureCheck() { |
| 122 | return mIsSignatureTest; |
| 123 | } |
| 124 | |
| 125 | void setIsReferenceApp(boolean isReferenceApp) { |
| 126 | mIsReferenceAppTest = isReferenceApp; |
| 127 | } |
| 128 | |
| 129 | boolean isReferenceApp() { |
| 130 | return mIsReferenceAppTest; |
| 131 | } |
| 132 | |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 133 | void setPackageToTest(String packageName) { |
| 134 | mPackageToTest = packageName; |
| 135 | } |
| 136 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame^] | 137 | void setTestPackageName(String testPackageName) { |
| 138 | mTestPackageName = testPackageName; |
| 139 | } |
| 140 | |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 141 | void setApkToTest(String apkName) { |
| 142 | mApkToTestName = apkName; |
| 143 | } |
| 144 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 145 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 146 | * {@inheritDoc} |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 147 | */ |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 148 | public IRemoteTest createTest(File testCaseDir, String className, String methodName) { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 149 | if (mIsHostSideTest) { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 150 | Log.d(LOG_TAG, String.format("Creating host test for %s", mName)); |
| 151 | JarHostTest hostTest = new JarHostTest(); |
| 152 | hostTest.setRunName(mName); |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 153 | hostTest.setJarFileName(mJarPath); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 154 | hostTest.setTests(filterTests(mTests, className, methodName)); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 155 | return hostTest; |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 156 | } else if (mIsVMHostTest) { |
| 157 | Log.d(LOG_TAG, String.format("Creating vm host test for %s", mName)); |
| 158 | VMHostTest vmHostTest = new VMHostTest(); |
| 159 | vmHostTest.setRunName(mName); |
| 160 | vmHostTest.setJarFileName(mJarPath); |
| 161 | vmHostTest.setTests(filterTests(mTests, className, methodName)); |
| 162 | return vmHostTest; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 163 | } else if (mIsSignatureTest) { |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 164 | // TODO: hardcode the runner/class/method for now, since current package xml |
| 165 | // points to specialized instrumentation. Eventually this special case for signatureTest |
| 166 | // can be removed, and it can be treated just like a normal InstrumentationTest |
| 167 | Log.d(LOG_TAG, String.format("Creating signature test %s", mName)); |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 168 | InstrumentationApkTest instrTest = new InstrumentationApkTest(); |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 169 | instrTest.setPackageName(mAppNameSpace); |
| 170 | instrTest.setRunnerName("android.test.InstrumentationTestRunner"); |
| 171 | instrTest.setClassName(SIGNATURE_TEST_CLASS); |
| 172 | instrTest.setMethodName(SIGNATURE_TEST_METHOD); |
| 173 | // add signature test to list of known tests |
| 174 | addTest(new TestIdentifier(SIGNATURE_TEST_CLASS, SIGNATURE_TEST_METHOD)); |
| 175 | // mName means 'apk file name' for instrumentation tests |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 176 | instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace); |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 177 | return instrTest; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 178 | } else if (mIsReferenceAppTest) { |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 179 | // a reference app test is just a InstrumentationTest with one extra apk to install |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 180 | InstrumentationApkTest instrTest = new InstrumentationApkTest(); |
| 181 | instrTest.addInstallApk(String.format("%s.apk", mApkToTestName), mPackageToTest); |
| 182 | return setInstrumentationTest(className, methodName, instrTest); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 183 | } else { |
| 184 | Log.d(LOG_TAG, String.format("Creating instrumentation test for %s", mName)); |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 185 | InstrumentationApkTest instrTest = new InstrumentationApkTest(); |
| 186 | return setInstrumentationTest(className, methodName, instrTest); |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 189 | |
| 190 | /** |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 191 | * Populates given {@link InstrumentationApkTest} with data from the package xml |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 192 | * |
| 193 | * @param testCaseDir |
| 194 | * @param className |
| 195 | * @param methodName |
| 196 | * @param instrTest |
| 197 | * @return the populated {@link InstrumentationTest} or <code>null</code> |
| 198 | */ |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 199 | private InstrumentationApkTest setInstrumentationTest(String className, |
| 200 | String methodName, InstrumentationApkTest instrTest) { |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 201 | instrTest.setPackageName(mAppNameSpace); |
| 202 | instrTest.setRunnerName(mRunner); |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame^] | 203 | instrTest.setTestPackageName(mTestPackageName); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 204 | instrTest.setClassName(className); |
| 205 | instrTest.setMethodName(methodName); |
| 206 | // mName means 'apk file name' for instrumentation tests |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 207 | instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace); |
| 208 | if (mTests.size() > 1000) { |
| 209 | // TODO: hack, large test suites can take longer to collect tests, increase timeout |
| 210 | instrTest.setCollectsTestsShellTimeout(10*60*1000); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 211 | } |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 212 | return instrTest; |
| 213 | } |
| 214 | |
| 215 | /** |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 216 | * Filter the tests to run based on class and method name |
| 217 | * |
| 218 | * @param tests the full set of tests in package |
| 219 | * @param className the test class name filter. <code>null</code> to run all test classes |
| 220 | * @param methodName the test method name. <code>null</code> to run all test methods |
| 221 | * @return the filtered collection of tests |
| 222 | */ |
| 223 | private Collection<TestIdentifier> filterTests(Collection<TestIdentifier> tests, |
| 224 | String className, String methodName) { |
| 225 | Collection<TestIdentifier> filteredTests = new ArrayList<TestIdentifier>(tests.size()); |
| 226 | for (TestIdentifier test : tests) { |
| 227 | if (className == null || test.getClassName().equals(className)) { |
| 228 | if (methodName == null || test.getTestName().equals(methodName)) { |
| 229 | filteredTests.add(test); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | return filteredTests; |
| 234 | } |
| 235 | |
| 236 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 237 | * {@inheritDoc} |
| 238 | */ |
| 239 | public boolean isKnownTest(TestIdentifier testDef) { |
| 240 | return mTests.contains(testDef); |
| 241 | } |
| 242 | |
| 243 | /** |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 244 | * {@inheritDoc} |
| 245 | */ |
| 246 | public boolean isKnownTestClass(String className) { |
| 247 | return mTestClasses.contains(className); |
| 248 | } |
| 249 | |
| 250 | /** |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 251 | * Add a {@link TestDef} to the list of tests in this package. |
| 252 | * |
| 253 | * @param testdef |
| 254 | */ |
| 255 | void addTest(TestIdentifier testDef) { |
| 256 | mTests.add(testDef); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 257 | mTestClasses.add(testDef.getClassName()); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Get the collection of tests in this test package. |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 262 | */ |
Brett Chabot | 1dcb9a5 | 2011-02-10 20:26:57 -0800 | [diff] [blame] | 263 | @Override |
| 264 | public Collection<TestIdentifier> getTests() { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 265 | return mTests; |
| 266 | } |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 267 | } |