• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Iterator;
22 
23 /**
24  * Correspond to junit's test case, provide functions on
25  * storing and executing a test case from CTS test harness.
26  */
27 public class TestCase implements DeviceObserver {
28     private TestSuite mParentSuite;
29     private Collection<Test> mTests;
30     private String mName;
31     private String mPriority;
32 
33     private Test mCurrentTest;
34     private boolean mTestStop;
35 
TestCase(final TestSuite suite, final String name, final String priority)36     public TestCase(final TestSuite suite, final String name, final String priority) {
37         mParentSuite = suite;
38         mName = name;
39         mPriority = priority;
40         mTests = new ArrayList<Test>();
41 
42         mTestStop = false;
43         mCurrentTest = null;
44     }
45 
46     /**
47      * Get parent suite;
48      *
49      * @return Parent suite.
50      */
getParent()51     public TestSuite getParent() {
52         return mParentSuite;
53     }
54 
55     /**
56      * Get the case name of this case.
57      *
58      * @return The case name of this test case.
59      */
getName()60     public String getName() {
61         return mName;
62     }
63 
64     /**
65      * Get the full name of this test case.
66      *
67      * @return The full name of this test case.
68      */
getFullName()69     public String getFullName() {
70         TestSuite suite = getParent();
71         return suite.getFullName() + "." + getName();
72     }
73 
74     /**
75      * Get the priority of this test case.
76      *
77      * @return The priority of this test case.
78      */
getPriority()79     public String getPriority() {
80         return mPriority;
81     }
82 
83     /**
84      * Add a specific test.
85      *
86      * @param test The test to be added.
87      */
addTest(Test test)88     public void addTest(Test test) {
89         mTests.add(test);
90     }
91 
92     /**
93      * Get the tests under this test case.
94      *
95      * @return The tests under this test case.
96      */
getTests()97     public Collection<Test> getTests() {
98         return mTests;
99     }
100 
101     /**
102      * Get the excluded list according to the execution status of each test.
103      *
104      * @param resultType The result type to filter the tests.
105      * @return All excluded list.
106      */
getExcludedList(final String resultType)107     public ArrayList<String> getExcludedList(final String resultType) {
108         ArrayList<String> excludedList = new ArrayList<String>();
109 
110         for (Test test : getTests()) {
111             if (resultType == null) {
112                 //all result type except PASS will be excluded
113                 if (test.getResult().isPass()) {
114                     excludedList.add(test.getFullName());
115                 }
116             } else {
117                 //the result type given by resultType will be excluded
118                 if (!test.getResult().getResultString().equals(resultType)) {
119                     excludedList.add(test.getFullName());
120                 }
121             }
122         }
123 
124         if (excludedList.size() == getTests().size()) {
125             //the whole case is excluded, just need to add the full case name
126             excludedList.removeAll(excludedList);
127             excludedList.add(getFullName());
128         }
129         return excludedList;
130     }
131 
132     /**
133      * Get all test names contained in the test case.
134      *
135      * @return All test names.
136      */
getAllTestNames()137     public ArrayList<String> getAllTestNames() {
138         ArrayList<String> testNameList = new ArrayList<String>();
139         for (Test test : getTests()) {
140             testNameList.add(test.getFullName());
141         }
142         return testNameList;
143     }
144 
145     /**
146      * Search test in this test case.
147      *
148      * @param testName The test name to be searched against.
149      * @return null if not found, or return founded test
150      */
searchTest(final String testName)151     public Test searchTest(final String testName) {
152         String sName = mParentSuite.getFullName();
153         String caseFullName = sName + "." + mName;
154         int index = 0;
155         int testNameStartIndex = testName.lastIndexOf('#') + 1;
156 
157         Log.d("searchTest(): testName=" + testName + ",caseFullName=" + caseFullName);
158 
159         if (testName.substring(index).startsWith(caseFullName + Test.METHOD_SEPARATOR)) {
160             index += caseFullName.length() + 1;
161         } else {
162             return null;
163         }
164 
165         if (index == testNameStartIndex) {
166             String name = testName.substring(testNameStartIndex);
167             for (Test test : mTests) {
168                 if (test.getName().equals(name)) {
169                     return test;
170                 }
171             }
172         }
173 
174         return null;
175     }
176 
177     /**
178      * Set test stopped.
179      *
180      * @param testStopped If true, it's stopped. Else, still running.
181      */
setTestStopped(final boolean testStopped)182     public void setTestStopped(final boolean testStopped) {
183         mTestStop = testStopped;
184         if (mCurrentTest != null) {
185             mCurrentTest.setTestStopped(mTestStop);
186         }
187     }
188 
189     /**
190      * Run the test case over device given.
191      *
192      * @param device The device to run the test case over.
193      */
run(final TestDevice device)194     public void run(final TestDevice device) throws DeviceDisconnectedException,
195             ADBServerNeedRestartException {
196         mTestStop = false;
197         Iterator<Test> tests = getTests().iterator();
198         while (tests.hasNext() && (!mTestStop)) {
199             mCurrentTest = tests.next();
200             if (mCurrentTest.getResult().isNotExecuted()) {
201                 mCurrentTest.run(device);
202             }
203         }
204     }
205 
206     /**
207      * Run the the specific test contained in the test case over device given.
208      *
209      * @param device The device to run the test over.
210      * @param test The specific test to be run.
211      */
run(final TestDevice device, final Test test)212     public void run(final TestDevice device, final Test test)
213             throws DeviceDisconnectedException, ADBServerNeedRestartException {
214         mTestStop = false;
215         mCurrentTest = test;
216         mCurrentTest.run(device);
217     }
218 
219     /** {@inheritDoc} */
notifyInstallingComplete(final int resultCode)220     public void notifyInstallingComplete(final int resultCode) {
221         if (mCurrentTest != null) {
222             mCurrentTest.notifyInstallingComplete(resultCode);
223         }
224     }
225 
226     /** {@inheritDoc} */
notifyUninstallingComplete(final int resultCode)227     public void notifyUninstallingComplete(final int resultCode) {
228         if (mCurrentTest != null) {
229             mCurrentTest.notifyUninstallingComplete(resultCode);
230         }
231     }
232 
233     /** {@inheritDoc} */
notifyInstallingTimeout(final TestDevice testDevice)234     public void notifyInstallingTimeout(final TestDevice testDevice) {
235         if (mCurrentTest != null) {
236             mCurrentTest.notifyInstallingTimeout(testDevice);
237         }
238     }
239 
240     /** {@inheritDoc} */
notifyUninstallingTimeout(final TestDevice testDevice)241     public void notifyUninstallingTimeout(final TestDevice testDevice) {
242         if (mCurrentTest != null) {
243             mCurrentTest.notifyUninstallingTimeout(testDevice);
244         }
245     }
246 
247     /** {@inheritDoc} */
notifyTestingDeviceDisconnected()248     public void notifyTestingDeviceDisconnected() {
249         if (mCurrentTest != null) {
250             mCurrentTest.notifyTestingDeviceDisconnected();
251         }
252     }
253 }
254