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