blob: 6d424b08dfb14a2a4357e5e3c38755ae85159d63 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.test;
18
Paul Duffinbd96f402017-06-22 13:29:26 +010019import java.util.ArrayList;
20import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import junit.framework.Test;
22import junit.framework.TestCase;
23import junit.framework.TestSuite;
24
25import java.util.List;
26
27public class TestCaseUtilTest extends TestCase {
28
Paul Duffinbd96f402017-06-22 13:29:26 +010029 @SuppressWarnings("unchecked")
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
39 public void testGetTests_ForTestSuiteWithSuiteMethod() throws Exception {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 TestSuite testSuite = new TwoTestsInTestSuite();
41
Paul Duffinbd96f402017-06-22 13:29:26 +010042 List<String> testCaseNames = getTestCaseNames(testSuite);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Paul Duffin2a637cf2017-06-22 12:52:18 +010044 assertEquals(0, testCaseNames.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 }
46
Paul Duffinbd96f402017-06-22 13:29:26 +010047 public void testGetTests_ForTestCaseWithSuiteMethod() throws Exception {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 TestCase testCase = new OneTestTestCaseWithSuite();
49
Paul Duffinbd96f402017-06-22 13:29:26 +010050 List<String> testCaseNames = getTestCaseNames(testCase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52 assertEquals(1, testCaseNames.size());
53 assertTrue(testCaseNames.get(0).endsWith("testOne"));
54 }
55
Paul Duffinbd96f402017-06-22 13:29:26 +010056 public void testInvokeSuiteMethodIfPossible_ForTestCase() throws Exception {
57 Test test = TestCaseUtil.invokeSuiteMethodIfPossible(OneTestTestCase.class, new HashSet<>());
58 assertNull(test);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 }
Paul Duffinbd96f402017-06-22 13:29:26 +010060
61 public void testInvokeSuiteMethodIfPossible_ForTestSuiteWithSuiteMethod() throws Exception {
62 Test test = TestCaseUtil.invokeSuiteMethodIfPossible(TwoTestsInTestSuite.class, new HashSet<>());
63 assertNotNull(test);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 assertEquals(2, test.countTestCases());
65 }
66
Paul Duffinbd96f402017-06-22 13:29:26 +010067 public void testInvokeSuiteMethodIfPossible_ForTestCaseWithSuiteMethod() throws Exception {
68 Test test = TestCaseUtil.invokeSuiteMethodIfPossible(OneTestTestCaseWithSuite.class, new HashSet<>());
69 assertNotNull(test);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 assertEquals(1, test.countTestCases());
71 }
Paul Duffinbd96f402017-06-22 13:29:26 +010072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 public void testReturnEmptyStringForTestSuiteWithNoName() throws Exception {
74 assertEquals("", TestCaseUtil.getTestName(new TestSuite()));
75 }
76
77 public static class OneTestTestCase extends TestCase {
78 public void testOne() throws Exception {
79 }
80 }
81
82 public static class OneTestTestCaseWithSuite extends TestCase {
83 public static Test suite() {
84 TestCase testCase = new OneTestTestCase();
85 testCase.setName("testOne");
86 return testCase;
87 }
88
89 public void testOne() throws Exception {
90 }
91
92 public void testTwo() throws Exception {
93 }
94 }
95
96 public static class OneTestTestSuite {
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 {
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}