Home | History | Annotate | Download | only in test
      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 import android.view.IWindowManager;
     21 import android.os.ServiceManager;
     22 
     23 /**
     24  * If you would like to test a single activity with an
     25  * {@link android.test.InstrumentationTestCase}, this provides some of the boiler plate to
     26  * launch and finish the activity in {@link #setUp} and {@link #tearDown}.
     27  *
     28  * This launches the activity only once for the entire class instead of doing it
     29  * in every setup / teardown call.
     30  */
     31 public abstract class SingleLaunchActivityTestCase<T extends Activity>
     32         extends InstrumentationTestCase {
     33 
     34     String mPackage;
     35     Class<T> mActivityClass;
     36     private static int sTestCaseCounter = 0;
     37     private static boolean sActivityLaunchedFlag = false;
     38 
     39     /**
     40      * <b>NOTE:</b> The parameter <i>pkg</i> must refer to the package identifier of the
     41      * package hosting the activity to be launched, which is specified in the AndroidManifest.xml
     42      * file.  This is not necessarily the same as the java package name.
     43      *
     44      * @param pkg The package hosting the activity to be launched.
     45      * @param activityClass The activity to test.
     46      */
     47     public SingleLaunchActivityTestCase(String pkg, Class<T> activityClass) {
     48         mPackage = pkg;
     49         mActivityClass = activityClass;
     50         sTestCaseCounter ++;
     51     }
     52 
     53     /**
     54      * The activity that will be set up for use in each test method.
     55      */
     56     private static Activity sActivity;
     57 
     58     public T getActivity() {
     59         return (T) sActivity;
     60     }
     61 
     62     @Override
     63     protected void setUp() throws Exception {
     64         super.setUp();
     65         // If it is the first test case, launch the activity.
     66         if (!sActivityLaunchedFlag) {
     67             // by default, not in touch mode
     68             getInstrumentation().setInTouchMode(false);
     69             sActivity = launchActivity(mPackage, mActivityClass, null);
     70             sActivityLaunchedFlag = true;
     71         }
     72     }
     73 
     74     @Override
     75     protected void tearDown() throws Exception {
     76         // If it is the last test case, call finish on the activity.
     77         sTestCaseCounter --;
     78         if (sTestCaseCounter == 1) {
     79             sActivity.finish();
     80         }
     81         super.tearDown();
     82     }
     83 
     84     public void testActivityTestCaseSetUpProperly() throws Exception {
     85         assertNotNull("activity should be launched successfully", sActivity);
     86     }
     87 }
     88