blob: fd896f2ee925a80d3c7592650cb80752dff1362b [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;
44 private String mJarPath = null;
45 private boolean mIsSignatureTest = false;
46 private boolean mIsReferenceAppTest = false;
Brett Chabota0f443c2011-02-04 13:57:55 -080047 private String mPackageToTest = null;
48 private String mApkToTestName = null;
Brett Chabot3add9162010-09-12 17:23:05 -070049
Brett Chabotef5a6042011-01-19 19:54:14 -080050 // 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 Chabot4f8143c2010-12-14 18:29:44 -080054
Brett Chabot3add9162010-09-12 17:23:05 -070055 void setUri(String uri) {
56 mUri = uri;
57 }
58
59 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -080060 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -070061 */
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 Chabota0f443c2011-02-04 13:57:55 -0800123 void setPackageToTest(String packageName) {
124 mPackageToTest = packageName;
125 }
126
127 void setApkToTest(String apkName) {
128 mApkToTestName = apkName;
129 }
130
Brett Chabot3add9162010-09-12 17:23:05 -0700131 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800132 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700133 */
Brett Chabotef5a6042011-01-19 19:54:14 -0800134 public IRemoteTest createTest(File testCaseDir, String className, String methodName) {
Brett Chabot3add9162010-09-12 17:23:05 -0700135 if (mIsHostSideTest) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700136 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 Chabotef5a6042011-01-19 19:54:14 -0800141 hostTest.setTests(filterTests(mTests, className, methodName));
Brett Chabot82d9daf2010-10-20 20:07:14 -0700142 return hostTest;
Brett Chabot3add9162010-09-12 17:23:05 -0700143 } else if (mIsSignatureTest) {
Brett Chabot584d2b92011-02-03 17:56:49 -0800144 // 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 Chabot3add9162010-09-12 17:23:05 -0700164 } else if (mIsReferenceAppTest) {
Brett Chabota0f443c2011-02-04 13:57:55 -0800165 // 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 Chabot3add9162010-09-12 17:23:05 -0700168 if (!apkFile.exists()) {
169 Log.w(LOG_TAG, String.format("Could not find apk file %s",
170 apkFile.getAbsolutePath()));
171 return null;
172 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800173 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 Chabot3add9162010-09-12 17:23:05 -0700179 }
180 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700181
182 /**
Brett Chabota0f443c2011-02-04 13:57:55 -0800183 * 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 Chabotef5a6042011-01-19 19:54:14 -0800209 * 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 Chabot4f8143c2010-12-14 18:29:44 -0800230 * {@inheritDoc}
231 */
232 public boolean isKnownTest(TestIdentifier testDef) {
233 return mTests.contains(testDef);
234 }
235
236 /**
Brett Chabotef5a6042011-01-19 19:54:14 -0800237 * {@inheritDoc}
238 */
239 public boolean isKnownTestClass(String className) {
240 return mTestClasses.contains(className);
241 }
242
243 /**
Brett Chabot82d9daf2010-10-20 20:07:14 -0700244 * 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 Chabotef5a6042011-01-19 19:54:14 -0800250 mTestClasses.add(testDef.getClassName());
Brett Chabot82d9daf2010-10-20 20:07:14 -0700251 }
252
253 /**
254 * Get the collection of tests in this test package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700255 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800256 @Override
257 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700258 return mTests;
259 }
Brett Chabot3add9162010-09-12 17:23:05 -0700260}