1 /* 2 * Copyright (C) 2008 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 17 package com.android.cts; 18 19 import java.io.IOException; 20 import java.util.ArrayList; 21 import java.util.Collection; 22 import java.util.Iterator; 23 24 /** 25 * Hold information for a suite of test case, provide functions 26 * on storing and executing a test suite from CTS test harness. 27 * 28 */ 29 public class TestSuite implements DeviceObserver { 30 private TestPackage mParentPackage; 31 private Collection<TestCase> mTestCases; 32 private String mName; 33 private String mFullName; 34 private Collection<TestSuite> mSubSuites; 35 36 private TestCase mCurrentTestCase; 37 private TestSuite mCurrentSubSuite; 38 private boolean mTestStop; 39 40 /** 41 * Construct a test suite. 42 * 43 * @param pkg TestPakcage as the reference to the parent package. 44 * @param suiteName The current suite name, not the full name. 45 * @param fullName The full suite name along the nested suite path. 46 */ 47 public TestSuite(final TestPackage pkg, final String suiteName, final String fullName) { 48 mParentPackage = pkg; 49 mName = suiteName; 50 mFullName = fullName; 51 mTestCases = new ArrayList<TestCase>(); 52 mSubSuites = new ArrayList<TestSuite>(); 53 54 mTestStop = false; 55 mCurrentTestCase = null; 56 mCurrentSubSuite = null; 57 } 58 59 /** 60 * Get parent package. 61 * 62 * @return Parent package. 63 */ 64 public TestPackage getParent() { 65 return mParentPackage; 66 } 67 68 /** 69 * Add a specific test case. 70 * 71 * @param tc The test case to be added. 72 */ 73 public void addTestCase(final TestCase tc) { 74 mTestCases.add(tc); 75 } 76 77 /** 78 * Add a specific test suite. 79 * 80 * @param suite The test suite to be added. 81 */ 82 public void addSubSuite(final TestSuite suite) { 83 mSubSuites.add(suite); 84 } 85 86 /** 87 * Get TestCases. 88 * 89 * @return TestCases 90 */ 91 public Collection<TestCase> getTestCases() { 92 return mTestCases; 93 } 94 95 /** 96 * Get the suite name of this TestSuite. 97 * 98 * @return The suite name of this TestCase. 99 */ 100 public String getName() { 101 return mName; 102 } 103 104 /** 105 * Get the full suite name of this TestSuite. 106 * 107 * @return The full suite name of this TestCase. 108 */ 109 public String getFullName() { 110 return mFullName; 111 } 112 113 /** 114 * Get the nested test suites of this test suite. 115 * 116 * @return The nested test suites. 117 */ 118 public Collection<TestSuite> getSubSuites() { 119 return mSubSuites; 120 } 121 122 /** 123 * Get all of the test suites contained in this test suite. 124 * 125 * @return All of the test suites. 126 */ 127 public Collection<TestSuite> getAllSuites() { 128 Collection<TestSuite> testSuites = new ArrayList<TestSuite>(); 129 testSuites.add(this); 130 for (TestSuite suite : mSubSuites) { 131 testSuites.addAll(suite.getAllSuites()); 132 } 133 return testSuites; 134 } 135 136 /** 137 * Search test in this test suite. 138 * 139 * @param testName The test name to be searched against. 140 * @return null if not found, or return founded test 141 */ 142 public Test searchTest(final String testName) { 143 Test test = null; 144 for (TestCase testCase : mTestCases) { 145 test = testCase.searchTest(testName); 146 if (test != null) { 147 return test; 148 } 149 } 150 151 if (mSubSuites.size() != 0) { 152 for (TestSuite subSuite : mSubSuites) { 153 test = subSuite.searchTest(testName); 154 if (test != null) { 155 return test; 156 } 157 } 158 } 159 160 return null; 161 } 162 163 /** 164 * Get the excluded list according to the execution status of each test. 165 * 166 * @param resultType The result type to filter the tests. 167 * @return All excluded list. 168 */ 169 public ArrayList<String> getExcludedList(final String resultType) { 170 ArrayList<String> excludedList = new ArrayList<String>(); 171 ArrayList<String> fullNameList = new ArrayList<String>(); 172 for (TestSuite suite : mSubSuites) { 173 fullNameList.add(suite.getFullName()); 174 ArrayList<String> list = suite.getExcludedList(resultType); 175 if ((list != null) && (list.size() > 0)) { 176 excludedList.addAll(list); 177 } 178 } 179 180 for (TestCase tc : mTestCases) { 181 fullNameList.add(tc.getFullName()); 182 ArrayList<String> list = tc.getExcludedList(resultType); 183 if ((list != null) && (list.size() > 0)) { 184 excludedList.addAll(list); 185 } 186 } 187 188 int count = 0; 189 for (String fullName : fullNameList) { 190 if (excludedList.contains(fullName)) { 191 count ++; 192 } 193 } 194 if (count == fullNameList.size()) { 195 //the whole suite is excluded, just need to add the full suite name 196 excludedList.removeAll(excludedList); 197 excludedList.add(getFullName()); 198 } 199 return excludedList; 200 } 201 202 /** 203 * Get all tests of this test suite. 204 * 205 * @return The tests of this suite. 206 */ 207 public Collection<Test> getTests() { 208 ArrayList<Test> tests = new ArrayList<Test>(); 209 for (TestSuite subSuite : mSubSuites) { 210 tests.addAll(subSuite.getTests()); 211 } 212 213 for (TestCase testCase : mTestCases) { 214 tests.addAll(testCase.getTests()); 215 } 216 217 return tests; 218 } 219 220 /** 221 * Get all test cases of this test suite. 222 * 223 * @return The test cases of this suite. 224 */ 225 public Collection<TestCase> getAllTestCases() { 226 ArrayList<TestCase> testCases = new ArrayList<TestCase>(); 227 testCases.addAll(mTestCases); 228 for (TestSuite subSuite : mSubSuites) { 229 testCases.addAll(subSuite.getAllTestCases()); 230 } 231 232 return testCases; 233 } 234 235 /** 236 * Get all test case names contained in the suite. 237 * 238 * @return All test case names. 239 */ 240 public ArrayList<String> getAllTestCaseNames() { 241 ArrayList<String> caseNameList = new ArrayList<String>(); 242 for (TestCase testCase : getAllTestCases()) { 243 caseNameList.add(testCase.getFullName()); 244 } 245 return caseNameList; 246 } 247 248 /** 249 * Set test stopped; 250 * 251 * @param testStopped If true, it's stopped. Else, still running. 252 */ 253 public void setTestStopped(final boolean testStopped) { 254 mTestStop = testStopped; 255 if (mCurrentTestCase != null) { 256 mCurrentTestCase.setTestStopped(mTestStop); 257 } 258 259 if (mCurrentSubSuite != null) { 260 mCurrentSubSuite.setTestStopped(mTestStop); 261 } 262 } 263 264 /** 265 * Run the this test suite or the specific java package contained 266 * in the test suite over device given. 267 * 268 * @param device The device to run the test over. 269 * @param javaPkgName The java package name. 270 */ 271 public void run(final TestDevice device, final String javaPkgName) 272 throws IOException, DeviceDisconnectedException, ADBServerNeedRestartException { 273 Iterator<TestSuite> subSuites = getSubSuites().iterator(); 274 Iterator<TestCase> testCases = getTestCases().iterator(); 275 276 mTestStop = false; 277 mCurrentTestCase = null; 278 mCurrentSubSuite = null; 279 280 while (subSuites.hasNext() && (!mTestStop)) { 281 mCurrentSubSuite = subSuites.next(); 282 mCurrentSubSuite.run(device, javaPkgName); 283 } 284 285 while (testCases.hasNext() && (!mTestStop)) { 286 mCurrentTestCase = testCases.next(); 287 String fullName = mFullName + "." + mCurrentTestCase.getName(); 288 if ((javaPkgName == null) || (javaPkgName.length() == 0) 289 || fullName.startsWith(javaPkgName)) { 290 mCurrentTestCase.run(device); 291 } 292 } 293 } 294 295 /** 296 * Run the specific test contained in the test suite over device given. 297 * 298 * @param device The device to run the test over. 299 * @param test The specific test to be run. 300 */ 301 public void run(final TestDevice device, final Test test) 302 throws DeviceDisconnectedException, ADBServerNeedRestartException { 303 mTestStop = false; 304 mCurrentTestCase = null; 305 mCurrentSubSuite = null; 306 307 mCurrentTestCase = test.getTestCase(); 308 mCurrentTestCase.run(device, test); 309 } 310 311 /** {@inheritDoc} */ 312 public void notifyInstallingComplete(final int resultCode) { 313 if (mCurrentTestCase != null) { 314 mCurrentTestCase.notifyInstallingComplete(resultCode); 315 } 316 317 if (mCurrentSubSuite != null) { 318 mCurrentSubSuite.notifyInstallingComplete(resultCode); 319 } 320 } 321 322 /** {@inheritDoc} */ 323 public void notifyUninstallingComplete(final int resultCode) { 324 if (mCurrentTestCase != null) { 325 mCurrentTestCase.notifyUninstallingComplete(resultCode); 326 } 327 328 if (mCurrentSubSuite != null) { 329 mCurrentSubSuite.notifyUninstallingComplete(resultCode); 330 } 331 } 332 333 /** {@inheritDoc} */ 334 public void notifyInstallingTimeout(final TestDevice testDevice) { 335 if (mCurrentTestCase != null) { 336 mCurrentTestCase.notifyInstallingTimeout(testDevice); 337 } 338 339 if (mCurrentSubSuite != null) { 340 mCurrentSubSuite.notifyInstallingTimeout(testDevice); 341 } 342 } 343 344 /** {@inheritDoc} */ 345 public void notifyUninstallingTimeout(final TestDevice testDevice) { 346 if (mCurrentTestCase != null) { 347 mCurrentTestCase.notifyUninstallingTimeout(testDevice); 348 } 349 350 if (mCurrentSubSuite != null) { 351 mCurrentSubSuite.notifyUninstallingTimeout(testDevice); 352 } 353 } 354 355 /** {@inheritDoc} */ 356 public void notifyTestingDeviceDisconnected() { 357 if (mCurrentTestCase != null) { 358 mCurrentTestCase.notifyTestingDeviceDisconnected(); 359 } 360 361 if (mCurrentSubSuite != null) { 362 mCurrentSubSuite.notifyTestingDeviceDisconnected(); 363 } 364 } 365 } 366