| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| Paul Duffin | bd96f40 | 2017-06-22 13:29:26 +0100 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | import java.util.HashSet; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| Paul Duffin | bd96f40 | 2017-06-22 13:29:26 +0100 | [diff] [blame] | 29 | @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | TestSuite testSuite = new TwoTestsInTestSuite(); |
| 41 | |
| Paul Duffin | bd96f40 | 2017-06-22 13:29:26 +0100 | [diff] [blame] | 42 | List<String> testCaseNames = getTestCaseNames(testSuite); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| Paul Duffin | 2a637cf | 2017-06-22 12:52:18 +0100 | [diff] [blame] | 44 | assertEquals(0, testCaseNames.size()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| Paul Duffin | bd96f40 | 2017-06-22 13:29:26 +0100 | [diff] [blame] | 47 | public void testGetTests_ForTestCaseWithSuiteMethod() throws Exception { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | TestCase testCase = new OneTestTestCaseWithSuite(); |
| 49 | |
| Paul Duffin | bd96f40 | 2017-06-22 13:29:26 +0100 | [diff] [blame] | 50 | List<String> testCaseNames = getTestCaseNames(testCase); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
| 52 | assertEquals(1, testCaseNames.size()); |
| 53 | assertTrue(testCaseNames.get(0).endsWith("testOne")); |
| 54 | } |
| 55 | |
| Paul Duffin | bd96f40 | 2017-06-22 13:29:26 +0100 | [diff] [blame] | 56 | public void testInvokeSuiteMethodIfPossible_ForTestCase() throws Exception { |
| 57 | Test test = TestCaseUtil.invokeSuiteMethodIfPossible(OneTestTestCase.class, new HashSet<>()); |
| 58 | assertNull(test); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | } |
| Paul Duffin | bd96f40 | 2017-06-22 13:29:26 +0100 | [diff] [blame] | 60 | |
| 61 | public void testInvokeSuiteMethodIfPossible_ForTestSuiteWithSuiteMethod() throws Exception { |
| 62 | Test test = TestCaseUtil.invokeSuiteMethodIfPossible(TwoTestsInTestSuite.class, new HashSet<>()); |
| 63 | assertNotNull(test); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | assertEquals(2, test.countTestCases()); |
| 65 | } |
| 66 | |
| Paul Duffin | bd96f40 | 2017-06-22 13:29:26 +0100 | [diff] [blame] | 67 | public void testInvokeSuiteMethodIfPossible_ForTestCaseWithSuiteMethod() throws Exception { |
| 68 | Test test = TestCaseUtil.invokeSuiteMethodIfPossible(OneTestTestCaseWithSuite.class, new HashSet<>()); |
| 69 | assertNotNull(test); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | assertEquals(1, test.countTestCases()); |
| 71 | } |
| Paul Duffin | bd96f40 | 2017-06-22 13:29:26 +0100 | [diff] [blame] | 72 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | 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 | } |