blob: 20396f7f9f0afc7e46af871d60f183249ddac0f6 [file] [log] [blame]
Brett Chabot3add9162010-09-12 17:23:05 -07001/*
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 */
16package com.android.cts.tradefed.testtype;
17
18import com.android.ddmlib.Log;
Brett Chabot82d9daf2010-10-20 20:07:14 -070019import com.android.ddmlib.testrunner.TestIdentifier;
Brett Chabot3add9162010-09-12 17:23:05 -070020import com.android.tradefed.testtype.IRemoteTest;
21import com.android.tradefed.testtype.InstrumentationTest;
22
23import java.io.File;
Brett Chabot82d9daf2010-10-20 20:07:14 -070024import java.util.ArrayList;
25import java.util.Collection;
Brett Chabotef5a6042011-01-19 19:54:14 -080026import java.util.LinkedHashSet;
Brett Chabot3add9162010-09-12 17:23:05 -070027
28/**
29 * Container for CTS test info.
30 * <p/>
31 * Knows how to translate this info into a runnable {@link IRemoteTest}.
32 */
Brett Chabot4f8143c2010-12-14 18:29:44 -080033class TestPackageDef implements ITestPackageDef {
Brett Chabot3add9162010-09-12 17:23:05 -070034
35 private static final String LOG_TAG = "TestPackageDef";
Brett Chabot584d2b92011-02-03 17:56:49 -080036 private static final String SIGNATURE_TEST_METHOD = "testSignature";
37 private static final String SIGNATURE_TEST_CLASS = "android.tests.sigtest.SimpleSignatureTest";
Brett Chabot3add9162010-09-12 17:23:05 -070038
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 Chuang9a223d72011-04-27 17:19:46 -070044 private boolean mIsVMHostTest = false;
Brett Chabot3add9162010-09-12 17:23:05 -070045 private String mJarPath = null;
46 private boolean mIsSignatureTest = false;
47 private boolean mIsReferenceAppTest = false;
Brett Chabota0f443c2011-02-04 13:57:55 -080048 private String mPackageToTest = null;
49 private String mApkToTestName = null;
Brian Carlstrom022aff42011-05-17 23:16:51 -070050 private String mTestPackageName = null;
Brett Chabot3add9162010-09-12 17:23:05 -070051
Brett Chabotef5a6042011-01-19 19:54:14 -080052 // 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 Chabot4f8143c2010-12-14 18:29:44 -080056
Brett Chabot3add9162010-09-12 17:23:05 -070057 void setUri(String uri) {
58 mUri = uri;
59 }
60
61 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -080062 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -070063 */
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 Chuang9a223d72011-04-27 17:19:46 -0700101 void setIsVMHostTest(boolean vmHostTest) {
102 mIsVMHostTest = vmHostTest;
103
104 }
105
106 boolean isVMHostTest() {
107 return mIsVMHostTest;
108 }
Brett Chabot3add9162010-09-12 17:23:05 -0700109 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 Chabota0f443c2011-02-04 13:57:55 -0800133 void setPackageToTest(String packageName) {
134 mPackageToTest = packageName;
135 }
136
Brian Carlstrom022aff42011-05-17 23:16:51 -0700137 void setTestPackageName(String testPackageName) {
138 mTestPackageName = testPackageName;
139 }
140
Brett Chabota0f443c2011-02-04 13:57:55 -0800141 void setApkToTest(String apkName) {
142 mApkToTestName = apkName;
143 }
144
Brett Chabot3add9162010-09-12 17:23:05 -0700145 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800146 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700147 */
Brett Chabotef5a6042011-01-19 19:54:14 -0800148 public IRemoteTest createTest(File testCaseDir, String className, String methodName) {
Brett Chabot3add9162010-09-12 17:23:05 -0700149 if (mIsHostSideTest) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700150 Log.d(LOG_TAG, String.format("Creating host test for %s", mName));
151 JarHostTest hostTest = new JarHostTest();
152 hostTest.setRunName(mName);
Brett Chabotf4bec732011-04-19 17:31:06 -0700153 hostTest.setJarFileName(mJarPath);
Brett Chabotef5a6042011-01-19 19:54:14 -0800154 hostTest.setTests(filterTests(mTests, className, methodName));
Brett Chabot82d9daf2010-10-20 20:07:14 -0700155 return hostTest;
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700156 } 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 Chabot3add9162010-09-12 17:23:05 -0700163 } else if (mIsSignatureTest) {
Brett Chabot584d2b92011-02-03 17:56:49 -0800164 // 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 Chabotf4bec732011-04-19 17:31:06 -0700168 InstrumentationApkTest instrTest = new InstrumentationApkTest();
Brett Chabot584d2b92011-02-03 17:56:49 -0800169 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 Chabotf4bec732011-04-19 17:31:06 -0700176 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot584d2b92011-02-03 17:56:49 -0800177 return instrTest;
Brett Chabot3add9162010-09-12 17:23:05 -0700178 } else if (mIsReferenceAppTest) {
Brett Chabota0f443c2011-02-04 13:57:55 -0800179 // a reference app test is just a InstrumentationTest with one extra apk to install
Brett Chabot4263db42011-04-15 13:51:48 -0700180 InstrumentationApkTest instrTest = new InstrumentationApkTest();
181 instrTest.addInstallApk(String.format("%s.apk", mApkToTestName), mPackageToTest);
182 return setInstrumentationTest(className, methodName, instrTest);
Brett Chabota0f443c2011-02-04 13:57:55 -0800183 } else {
184 Log.d(LOG_TAG, String.format("Creating instrumentation test for %s", mName));
Brett Chabot4263db42011-04-15 13:51:48 -0700185 InstrumentationApkTest instrTest = new InstrumentationApkTest();
186 return setInstrumentationTest(className, methodName, instrTest);
Brett Chabot3add9162010-09-12 17:23:05 -0700187 }
188 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700189
190 /**
Brett Chabot4263db42011-04-15 13:51:48 -0700191 * Populates given {@link InstrumentationApkTest} with data from the package xml
Brett Chabota0f443c2011-02-04 13:57:55 -0800192 *
193 * @param testCaseDir
194 * @param className
195 * @param methodName
196 * @param instrTest
197 * @return the populated {@link InstrumentationTest} or <code>null</code>
198 */
Brett Chabot4263db42011-04-15 13:51:48 -0700199 private InstrumentationApkTest setInstrumentationTest(String className,
200 String methodName, InstrumentationApkTest instrTest) {
Brett Chabota0f443c2011-02-04 13:57:55 -0800201 instrTest.setPackageName(mAppNameSpace);
202 instrTest.setRunnerName(mRunner);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700203 instrTest.setTestPackageName(mTestPackageName);
Brett Chabota0f443c2011-02-04 13:57:55 -0800204 instrTest.setClassName(className);
205 instrTest.setMethodName(methodName);
206 // mName means 'apk file name' for instrumentation tests
Brett Chabot4263db42011-04-15 13:51:48 -0700207 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 Chabota0f443c2011-02-04 13:57:55 -0800211 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800212 return instrTest;
213 }
214
215 /**
Brett Chabotef5a6042011-01-19 19:54:14 -0800216 * 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 Chabot4f8143c2010-12-14 18:29:44 -0800237 * {@inheritDoc}
238 */
239 public boolean isKnownTest(TestIdentifier testDef) {
240 return mTests.contains(testDef);
241 }
242
243 /**
Brett Chabotef5a6042011-01-19 19:54:14 -0800244 * {@inheritDoc}
245 */
246 public boolean isKnownTestClass(String className) {
247 return mTestClasses.contains(className);
248 }
249
250 /**
Brett Chabot82d9daf2010-10-20 20:07:14 -0700251 * 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 Chabotef5a6042011-01-19 19:54:14 -0800257 mTestClasses.add(testDef.getClassName());
Brett Chabot82d9daf2010-10-20 20:07:14 -0700258 }
259
260 /**
261 * Get the collection of tests in this test package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700262 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800263 @Override
264 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700265 return mTests;
266 }
Brett Chabot3add9162010-09-12 17:23:05 -0700267}