• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 android.test;
18 
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24 
25 import java.util.List;
26 
27 public class TestCaseUtilTest extends TestCase {
28 
29     @SuppressWarnings("unchecked")
getTestCaseNames(Test test)30     private static List<String> getTestCaseNames(Test test) {
31         List<Test> tests = (List<Test>) TestCaseUtil.getTests(test, false);
32         List<String> testCaseNames = new ArrayList<>();
33         for (Test aTest : tests) {
34             testCaseNames.add(TestCaseUtil.getTestName(aTest));
35         }
36         return testCaseNames;
37     }
38 
testGetTests_ForTestSuiteWithSuiteMethod()39     public void testGetTests_ForTestSuiteWithSuiteMethod() throws Exception {
40         TestSuite testSuite = new TwoTestsInTestSuite();
41 
42         List<String> testCaseNames = getTestCaseNames(testSuite);
43 
44         assertEquals(0, testCaseNames.size());
45     }
46 
testGetTests_ForTestCaseWithSuiteMethod()47     public void testGetTests_ForTestCaseWithSuiteMethod() throws Exception {
48         TestCase testCase = new OneTestTestCaseWithSuite();
49 
50         List<String> testCaseNames = getTestCaseNames(testCase);
51 
52         assertEquals(1, testCaseNames.size());
53         assertTrue(testCaseNames.get(0).endsWith("testOne"));
54     }
55 
testInvokeSuiteMethodIfPossible_ForTestCase()56     public void testInvokeSuiteMethodIfPossible_ForTestCase() throws Exception {
57         Test test = TestCaseUtil.invokeSuiteMethodIfPossible(OneTestTestCase.class, new HashSet<>());
58         assertNull(test);
59     }
60 
testInvokeSuiteMethodIfPossible_ForTestSuiteWithSuiteMethod()61     public void testInvokeSuiteMethodIfPossible_ForTestSuiteWithSuiteMethod() throws Exception {
62         Test test = TestCaseUtil.invokeSuiteMethodIfPossible(TwoTestsInTestSuite.class, new HashSet<>());
63         assertNotNull(test);
64         assertEquals(2, test.countTestCases());
65     }
66 
testInvokeSuiteMethodIfPossible_ForTestCaseWithSuiteMethod()67     public void testInvokeSuiteMethodIfPossible_ForTestCaseWithSuiteMethod() throws Exception {
68         Test test = TestCaseUtil.invokeSuiteMethodIfPossible(OneTestTestCaseWithSuite.class, new HashSet<>());
69         assertNotNull(test);
70         assertEquals(1, test.countTestCases());
71     }
72 
testReturnEmptyStringForTestSuiteWithNoName()73     public void testReturnEmptyStringForTestSuiteWithNoName() throws Exception {
74         assertEquals("", TestCaseUtil.getTestName(new TestSuite()));
75     }
76 
77     public static class OneTestTestCase extends TestCase {
testOne()78         public void testOne() throws Exception {
79         }
80     }
81 
82     public static class OneTestTestCaseWithSuite extends TestCase {
suite()83         public static Test suite()  {
84             TestCase testCase = new OneTestTestCase();
85             testCase.setName("testOne");
86             return testCase;
87         }
88 
testOne()89         public void testOne() throws Exception {
90         }
91 
testTwo()92         public void testTwo() throws Exception {
93         }
94     }
95 
96     public static class OneTestTestSuite {
suite()97         public static Test suite() {
98             TestSuite suite = new TestSuite(OneTestTestSuite.class.getName());
99             suite.addTestSuite(OneTestTestCase.class);
100             return suite;
101         }
102     }
103 
104     public static class TwoTestsInTestSuite extends TestSuite {
suite()105         public static Test suite() {
106             TestSuite suite = new TestSuite(TwoTestsInTestSuite.class.getName());
107             suite.addTestSuite(OneTestTestCase.class);
108             suite.addTest(OneTestTestSuite.suite());
109             return suite;
110         }
111     }
112 }
113