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