blob: 72c93cef3fde6e29ef4cb4551be553a6242db7d8 [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
19import android.app.Activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
21/**
22 * If you would like to test a single activity with an
23 * {@link android.test.InstrumentationTestCase}, this provides some of the boiler plate to
24 * launch and finish the activity in {@link #setUp} and {@link #tearDown}.
25 *
26 * This launches the activity only once for the entire class instead of doing it
27 * in every setup / teardown call.
28 */
29public abstract class SingleLaunchActivityTestCase<T extends Activity>
30 extends InstrumentationTestCase {
31
32 String mPackage;
33 Class<T> mActivityClass;
34 private static int sTestCaseCounter = 0;
35 private static boolean sActivityLaunchedFlag = false;
36
37 /**
Andy Stadler72d5de72009-04-21 11:54:14 -070038 * <b>NOTE:</b> The parameter <i>pkg</i> must refer to the package identifier of the
39 * package hosting the activity to be launched, which is specified in the AndroidManifest.xml
40 * file. This is not necessarily the same as the java package name.
41 *
42 * @param pkg The package hosting the activity to be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 * @param activityClass The activity to test.
44 */
45 public SingleLaunchActivityTestCase(String pkg, Class<T> activityClass) {
46 mPackage = pkg;
47 mActivityClass = activityClass;
48 sTestCaseCounter ++;
49 }
50
51 /**
52 * The activity that will be set up for use in each test method.
53 */
54 private static Activity sActivity;
55
56 public T getActivity() {
57 return (T) sActivity;
58 }
59
60 @Override
61 protected void setUp() throws Exception {
62 super.setUp();
63 // If it is the first test case, launch the activity.
64 if (!sActivityLaunchedFlag) {
65 // by default, not in touch mode
66 getInstrumentation().setInTouchMode(false);
67 sActivity = launchActivity(mPackage, mActivityClass, null);
68 sActivityLaunchedFlag = true;
69 }
70 }
71
72 @Override
73 protected void tearDown() throws Exception {
74 // If it is the last test case, call finish on the activity.
75 sTestCaseCounter --;
Mikael Andreenc6bf4072010-12-01 12:49:08 +010076 if (sTestCaseCounter == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 sActivity.finish();
78 }
79 super.tearDown();
80 }
81
82 public void testActivityTestCaseSetUpProperly() throws Exception {
83 assertNotNull("activity should be launched successfully", sActivity);
84 }
85}