blob: 8d43b732cf85747763521523c982da5c50a282f4 [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;
20import android.view.IWindowManager;
21import android.os.ServiceManager;
22
23/**
24 * If you would like to test a single activity with an
25 * {@link android.test.InstrumentationTestCase}, this provides some of the boiler plate to
26 * launch and finish the activity in {@link #setUp} and {@link #tearDown}.
27 *
28 * This launches the activity only once for the entire class instead of doing it
29 * in every setup / teardown call.
30 */
31public abstract class SingleLaunchActivityTestCase<T extends Activity>
32 extends InstrumentationTestCase {
33
34 String mPackage;
35 Class<T> mActivityClass;
36 private static int sTestCaseCounter = 0;
37 private static boolean sActivityLaunchedFlag = false;
38
39 /**
40 * @param pkg The package of the instrumentation.
41 * @param activityClass The activity to test.
42 */
43 public SingleLaunchActivityTestCase(String pkg, Class<T> activityClass) {
44 mPackage = pkg;
45 mActivityClass = activityClass;
46 sTestCaseCounter ++;
47 }
48
49 /**
50 * The activity that will be set up for use in each test method.
51 */
52 private static Activity sActivity;
53
54 public T getActivity() {
55 return (T) sActivity;
56 }
57
58 @Override
59 protected void setUp() throws Exception {
60 super.setUp();
61 // If it is the first test case, launch the activity.
62 if (!sActivityLaunchedFlag) {
63 // by default, not in touch mode
64 getInstrumentation().setInTouchMode(false);
65 sActivity = launchActivity(mPackage, mActivityClass, null);
66 sActivityLaunchedFlag = true;
67 }
68 }
69
70 @Override
71 protected void tearDown() throws Exception {
72 // If it is the last test case, call finish on the activity.
73 sTestCaseCounter --;
74 if (sTestCaseCounter == 1) {
75 sActivity.finish();
76 }
77 super.tearDown();
78 }
79
80 public void testActivityTestCaseSetUpProperly() throws Exception {
81 assertNotNull("activity should be launched successfully", sActivity);
82 }
83}