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.app.Activity;
20 
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  *
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>.
33  */
34 @Deprecated
35 public abstract class SingleLaunchActivityTestCase<T extends Activity>
36         extends InstrumentationTestCase {
37 
38     String mPackage;
39     Class<T> mActivityClass;
40     private static int sTestCaseCounter = 0;
41     private static boolean sActivityLaunchedFlag = false;
42 
43     /**
44      * <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.
49      * @param activityClass The activity to test.
50      */
SingleLaunchActivityTestCase(String pkg, Class<T> activityClass)51     public SingleLaunchActivityTestCase(String pkg, Class<T> activityClass) {
52         mPackage = pkg;
53         mActivityClass = activityClass;
54         sTestCaseCounter ++;
55     }
56 
57     /**
58      * The activity that will be set up for use in each test method.
59      */
60     private static Activity sActivity;
61 
getActivity()62     public T getActivity() {
63         return (T) sActivity;
64     }
65 
66     @Override
setUp()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;
75         }
76     }
77 
78     @Override
tearDown()79     protected void tearDown() throws Exception {
80         // If it is the last test case, call finish on the activity.
81         sTestCaseCounter --;
82         if (sTestCaseCounter == 0) {
83             sActivity.finish();
84         }
85         super.tearDown();
86     }
87 
testActivityTestCaseSetUpProperly()88     public void testActivityTestCaseSetUpProperly() throws Exception {
89         assertNotNull("activity should be launched successfully", sActivity);
90     }
91 }
92