• 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 android.content.Intent;
20 
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23 
24 import java.util.Arrays;
25 import java.util.List;
26 
27 public class TestBrowserControllerImplTest extends TestCase {
28     private TestBrowserControllerImpl mTestBrowserController;
29     private TestBrowserViewStub mTestBrowserView;
30 
31     @Override
setUp()32     protected void setUp() throws Exception {
33         super.setUp();
34         mTestBrowserController = new TestBrowserControllerImpl();
35         mTestBrowserView = new TestBrowserViewStub();
36         mTestBrowserController.registerView(mTestBrowserView);
37     }
38 
testSetTestSuite()39     public void testSetTestSuite() throws Exception {
40         TestSuite testSuite = new TestSuite();
41         testSuite.addTestSuite(DummyTestCase.class);
42 
43         mTestBrowserController.setTestSuite(testSuite);
44 
45         verifyTestNames(Arrays.asList("Run All", DummyTestCase.class.getSimpleName()),
46                 mTestBrowserView.getTestNames());
47     }
48 
verifyTestNames(List<String> expectedTestNames, List<String> actualTestNames)49     private static void verifyTestNames(List<String> expectedTestNames,
50             List<String> actualTestNames) {
51         assertEquals(expectedTestNames.size(), actualTestNames.size());
52 
53         // We use endsWith instead of equals because the return value of
54         // class.getSimpleName(), when called on an inner class, varies
55         // from one vm to another.
56         // This allows the test to pass in multiple environments.
57         for (int i = 0; i < expectedTestNames.size(); i++) {
58             assertTrue(actualTestNames.get(i).endsWith(expectedTestNames.get(i)));
59         }
60     }
61 
testGetIntentForTestSuite()62     public void testGetIntentForTestSuite() throws Exception {
63         TestSuite testSuite = new TestSuite();
64         testSuite.addTestSuite(DummyTestCase.class);
65 
66         String targetBrowserActvityClassName = "com.android.bogus.DummyActivity";
67         String expectedTargetPackageName = "com.android.bogus";
68         mTestBrowserController.setTargetBrowserActivityClassName(targetBrowserActvityClassName);
69         mTestBrowserController.setTestSuite(testSuite);
70         mTestBrowserController.setTargetPackageName(expectedTargetPackageName);
71         Intent intent = mTestBrowserController.getIntentForTestAt(1);
72         verifyIntent(intent, DummyTestCase.class, expectedTargetPackageName);
73         assertEquals(targetBrowserActvityClassName, intent.getComponent().getClassName());
74     }
75 
testGetIntentForTestCase()76     public void testGetIntentForTestCase() throws Exception {
77         TestSuite testSuite = new TestSuite();
78         testSuite.addTest(new DummyTestCase());
79 
80         mTestBrowserController.setTestSuite(testSuite);
81         Intent intent = mTestBrowserController.getIntentForTestAt(1);
82         verifyIntent(intent, DummyTestCase.class, "com.android.testharness");
83         assertEquals(TestBrowserControllerImpl.TEST_RUNNER_ACTIVITY_CLASS_NAME,
84                 intent.getComponent().getClassName());
85         assertEquals("testDummyTest",
86                 intent.getStringExtra(TestBrowserController.BUNDLE_EXTRA_TEST_METHOD_NAME));
87     }
88 
testGetIntentForRunAll()89     public void testGetIntentForRunAll() throws Exception {
90         TestSuite testSuite = new DummyTestSuite();
91         testSuite.addTestSuite(DummyTestCase.class);
92 
93         mTestBrowserController.setTestSuite(testSuite);
94         Intent intent = mTestBrowserController.getIntentForTestAt(0);
95         verifyIntent(intent, DummyTestSuite.class, "com.android.testharness");
96     }
97 
verifyIntent(Intent intent, Class testClass, String expectedPackageName)98     private static void verifyIntent(Intent intent, Class testClass, String expectedPackageName) {
99         assertEquals(Intent.ACTION_RUN, intent.getAction());
100         assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK,
101                 intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK);
102         assertEquals(Intent.FLAG_ACTIVITY_MULTIPLE_TASK,
103                 intent.getFlags() & Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
104         assertEquals(testClass.getName(), intent.getData().toString());
105         assertEquals(expectedPackageName, intent.getComponent().getPackageName());
106     }
107 
108     private static class DummyTestSuite extends TestSuite {
DummyTestSuite()109         private DummyTestSuite() {
110             super(DummyTestSuite.class.getName());
111         }
112     }
113 
114     private static class DummyTestCase extends TestCase {
DummyTestCase()115         private DummyTestCase() {
116             super("testDummyTest");
117         }
118 
testDummyTest()119         public void testDummyTest() throws Exception {
120         }
121     }
122 
123     private class TestBrowserViewStub implements TestBrowserView {
124         private List<String> mTestNames;
125 
setTestNames(List<String> testNames)126         public void setTestNames(List<String> testNames) {
127             mTestNames = testNames;
128         }
129 
getTestNames()130         public List<String> getTestNames() {
131             return mTestNames;
132         }
133     }
134 }
135