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 junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22 
23 import java.util.List;
24 
25 public class TestCaseUtilTest extends TestCase {
26 
testGetTestCaseNamesForTestSuiteWithSuiteMethod()27     public void testGetTestCaseNamesForTestSuiteWithSuiteMethod() throws Exception {
28         TestSuite testSuite = new TwoTestsInTestSuite();
29 
30         List<String> testCaseNames = TestCaseUtil.getTestCaseNames(testSuite, false);
31 
32         assertEquals(2, testCaseNames.size());
33         assertTrue(testCaseNames.get(0).endsWith("OneTestTestCase"));
34         assertTrue(testCaseNames.get(1).endsWith("OneTestTestSuite"));
35     }
36 
testGetTestCaseNamesForTestCaseWithSuiteMethod()37     public void testGetTestCaseNamesForTestCaseWithSuiteMethod() throws Exception {
38         TestCase testCase = new OneTestTestCaseWithSuite();
39 
40         List<String> testCaseNames = TestCaseUtil.getTestCaseNames(testCase, false);
41 
42         assertEquals(1, testCaseNames.size());
43         assertTrue(testCaseNames.get(0).endsWith("testOne"));
44     }
45 
testCreateTestForTestCase()46     public void testCreateTestForTestCase() throws Exception {
47         Test test = TestCaseUtil.createTestSuite(OneTestTestCase.class);
48         assertEquals(1, test.countTestCases());
49     }
50 
testCreateTestForTestSuiteWithSuiteMethod()51     public void testCreateTestForTestSuiteWithSuiteMethod() throws Exception {
52         Test test = TestCaseUtil.createTestSuite(TwoTestsInTestSuite.class);
53         assertEquals(2, test.countTestCases());
54     }
55 
testCreateTestForTestCaseWithSuiteMethod()56     public void testCreateTestForTestCaseWithSuiteMethod() throws Exception {
57         Test test = TestCaseUtil.createTestSuite(OneTestTestCaseWithSuite.class);
58         assertEquals(1, test.countTestCases());
59     }
60 
testReturnEmptyStringForTestSuiteWithNoName()61     public void testReturnEmptyStringForTestSuiteWithNoName() throws Exception {
62         assertEquals("", TestCaseUtil.getTestName(new TestSuite()));
63     }
64 
65     public static class OneTestTestCase extends TestCase {
testOne()66         public void testOne() throws Exception {
67         }
68     }
69 
70     public static class OneTestTestCaseWithSuite extends TestCase {
suite()71         public static Test suite()  {
72             TestCase testCase = new OneTestTestCase();
73             testCase.setName("testOne");
74             return testCase;
75         }
76 
testOne()77         public void testOne() throws Exception {
78         }
79 
testTwo()80         public void testTwo() throws Exception {
81         }
82     }
83 
84     public static class OneTestTestSuite {
suite()85         public static Test suite() {
86             TestSuite suite = new TestSuite(OneTestTestSuite.class.getName());
87             suite.addTestSuite(OneTestTestCase.class);
88             return suite;
89         }
90     }
91 
92     public static class TwoTestsInTestSuite extends TestSuite {
suite()93         public static Test suite() {
94             TestSuite suite = new TestSuite(TwoTestsInTestSuite.class.getName());
95             suite.addTestSuite(OneTestTestCase.class);
96             suite.addTest(OneTestTestSuite.suite());
97             return suite;
98         }
99     }
100 }
101