1/*
2 * Copyright (C) 2008 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;
20
21/**
22 * This class provides functional testing of a single activity.  The activity under test will
23 * be created using the system infrastructure (by calling InstrumentationTestCase.launchActivity())
24 * and you will then be able to manipulate your Activity directly.  Most of the work is handled
25 * automatically here by {@link #setUp} and {@link #tearDown}.
26 *
27 * <p>If you prefer an isolated unit test, see {@link android.test.ActivityUnitTestCase}.
28 *
29 * @deprecated new tests should be written using
30 * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for
31 * configuring the Activity under test
32 */
33@Deprecated
34public abstract class ActivityInstrumentationTestCase<T extends Activity>
35        extends ActivityTestCase {
36    String mPackage;
37    Class<T> mActivityClass;
38    boolean mInitialTouchMode = false;
39
40    /**
41     * Creates an {@link ActivityInstrumentationTestCase} in non-touch mode.
42     *
43     * @param pkg ignored - no longer in use.
44     * @param activityClass The activity to test. This must be a class in the instrumentation
45     * targetPackage specified in the AndroidManifest.xml
46     */
47    public ActivityInstrumentationTestCase(String pkg, Class<T> activityClass) {
48        this(pkg, activityClass, false);
49    }
50
51    /**
52     * Creates an {@link ActivityInstrumentationTestCase}.
53     *
54     * @param pkg ignored - no longer in use.
55     * @param activityClass The activity to test. This must be a class in the instrumentation
56     * targetPackage specified in the AndroidManifest.xml
57     * @param initialTouchMode true = in touch mode
58     */
59    public ActivityInstrumentationTestCase(String pkg, Class<T> activityClass,
60            boolean initialTouchMode) {
61        mActivityClass = activityClass;
62        mInitialTouchMode = initialTouchMode;
63    }
64
65    @Override
66    public T getActivity() {
67        return (T) super.getActivity();
68    }
69
70    @Override
71    protected void setUp() throws Exception {
72        super.setUp();
73        // set initial touch mode
74        getInstrumentation().setInTouchMode(mInitialTouchMode);
75        final String targetPackageName = getInstrumentation().getTargetContext().getPackageName();
76        setActivity(launchActivity(targetPackageName, mActivityClass, null));
77    }
78
79    @Override
80    protected void tearDown() throws Exception {
81        getActivity().finish();
82        setActivity(null);
83
84        // Scrub out members - protects against memory leaks in the case where someone
85        // creates a non-static inner class (thus referencing the test case) and gives it to
86        // someone else to hold onto
87        scrubClass(ActivityInstrumentationTestCase.class);
88
89        super.tearDown();
90    }
91
92    public void testActivityTestCaseSetUpProperly() throws Exception {
93        assertNotNull("activity should be launched successfully", getActivity());
94    }
95}
96