| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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}. | 
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 26 | * | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | * <p>If you prefer an isolated unit test, see {@link android.test.ActivityUnitTestCase}. | 
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 28 | * | 
|  | 29 | * @deprecated new tests should be written using | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | * {@link android.test.ActivityInstrumentationTestCase2}, which provides more options for | 
|  | 31 | * configuring the Activity under test | 
|  | 32 | */ | 
|  | 33 | @Deprecated | 
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 34 | public abstract class ActivityInstrumentationTestCase<T extends Activity> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | extends ActivityTestCase { | 
|  | 36 | String mPackage; | 
|  | 37 | Class<T> mActivityClass; | 
|  | 38 | boolean mInitialTouchMode = false; | 
|  | 39 |  | 
|  | 40 | /** | 
| Brett Chabot | 90762d3 | 2010-02-11 20:07:17 -0800 | [diff] [blame] | 41 | * Creates an {@link ActivityInstrumentationTestCase} in non-touch mode. | 
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 42 | * | 
| Brett Chabot | 90762d3 | 2010-02-11 20:07:17 -0800 | [diff] [blame] | 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 | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | */ | 
|  | 47 | public ActivityInstrumentationTestCase(String pkg, Class<T> activityClass) { | 
|  | 48 | this(pkg, activityClass, false); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | /** | 
| Brett Chabot | 90762d3 | 2010-02-11 20:07:17 -0800 | [diff] [blame] | 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 | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | * @param initialTouchMode true = in touch mode | 
|  | 58 | */ | 
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 59 | public ActivityInstrumentationTestCase(String pkg, Class<T> activityClass, | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | boolean initialTouchMode) { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); | 
| Brett Chabot | 90762d3 | 2010-02-11 20:07:17 -0800 | [diff] [blame] | 75 | final String targetPackageName = getInstrumentation().getTargetContext().getPackageName(); | 
|  | 76 | setActivity(launchActivity(targetPackageName, mActivityClass, null)); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | @Override | 
|  | 80 | protected void tearDown() throws Exception { | 
|  | 81 | getActivity().finish(); | 
|  | 82 | setActivity(null); | 
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 83 |  | 
|  | 84 | // Scrub out members - protects against memory leaks in the case where someone | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | } |