blob: af1448e556789a79a750ef093b94b83364add1db [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}.
Stephan Linznerb51617f2016-01-27 18:09:50 -080025 *
26 * This launches the activity only once for the entire class instead of doing it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 * in every setup / teardown call.
Stephan Linznerb51617f2016-01-27 18:09:50 -080028 *
29 * @deprecated Use
30 * <a href="{@docRoot}reference/android/support/test/rule/ActivityTestRule.html">
31 * ActivityTestRule</a> instead. New tests should be written using the
32 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 */
Stephan Linznerb51617f2016-01-27 18:09:50 -080034@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035public abstract class SingleLaunchActivityTestCase<T extends Activity>
36 extends InstrumentationTestCase {
Stephan Linznerb51617f2016-01-27 18:09:50 -080037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 String mPackage;
39 Class<T> mActivityClass;
40 private static int sTestCaseCounter = 0;
41 private static boolean sActivityLaunchedFlag = false;
42
43 /**
Andy Stadler72d5de72009-04-21 11:54:14 -070044 * <b>NOTE:</b> The parameter <i>pkg</i> must refer to the package identifier of the
45 * package hosting the activity to be launched, which is specified in the AndroidManifest.xml
46 * file. This is not necessarily the same as the java package name.
47 *
48 * @param pkg The package hosting the activity to be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 * @param activityClass The activity to test.
50 */
51 public SingleLaunchActivityTestCase(String pkg, Class<T> activityClass) {
52 mPackage = pkg;
Stephan Linznerb51617f2016-01-27 18:09:50 -080053 mActivityClass = activityClass;
54 sTestCaseCounter ++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 }
Stephan Linznerb51617f2016-01-27 18:09:50 -080056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 /**
58 * The activity that will be set up for use in each test method.
59 */
60 private static Activity sActivity;
61
62 public T getActivity() {
63 return (T) sActivity;
64 }
65
66 @Override
67 protected void setUp() throws Exception {
68 super.setUp();
69 // If it is the first test case, launch the activity.
70 if (!sActivityLaunchedFlag) {
71 // by default, not in touch mode
72 getInstrumentation().setInTouchMode(false);
73 sActivity = launchActivity(mPackage, mActivityClass, null);
74 sActivityLaunchedFlag = true;
Stephan Linznerb51617f2016-01-27 18:09:50 -080075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 }
77
78 @Override
79 protected void tearDown() throws Exception {
80 // If it is the last test case, call finish on the activity.
81 sTestCaseCounter --;
Mikael Andreenc6bf4072010-12-01 12:49:08 +010082 if (sTestCaseCounter == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 sActivity.finish();
Stephan Linznerb51617f2016-01-27 18:09:50 -080084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 super.tearDown();
86 }
87
88 public void testActivityTestCaseSetUpProperly() throws Exception {
89 assertNotNull("activity should be launched successfully", sActivity);
90 }
91}