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 public 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     /**
38      * <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.
43      * @param activityClass The activity to test.
44      */
SingleLaunchActivityTestCase(String pkg, Class<T> activityClass)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 
getActivity()56     public T getActivity() {
57         return (T) sActivity;
58     }
59 
60     @Override
setUp()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
tearDown()73     protected void tearDown() throws Exception {
74         // If it is the last test case, call finish on the activity.
75         sTestCaseCounter --;
76         if (sTestCaseCounter == 0) {
77             sActivity.finish();
78         }
79         super.tearDown();
80     }
81 
testActivityTestCaseSetUpProperly()82     public void testActivityTestCaseSetUpProperly() throws Exception {
83         assertNotNull("activity should be launched successfully", sActivity);
84     }
85 }
86