The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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}. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 25 | * |
| 26 | * This launches the activity only once for the entire class instead of doing it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | * in every setup / teardown call. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 28 | * |
| 29 | * @deprecated Use |
| 30 | * <a href="{@docRoot}reference/android/support/test/rule/ActivityTestRule.html"> |
| 31 | * ActivityTestRule</a> instead. New tests should be written using the |
| 32 | * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | */ |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 34 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | public abstract class SingleLaunchActivityTestCase<T extends Activity> |
| 36 | extends InstrumentationTestCase { |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | String mPackage; |
| 39 | Class<T> mActivityClass; |
| 40 | private static int sTestCaseCounter = 0; |
| 41 | private static boolean sActivityLaunchedFlag = false; |
| 42 | |
| 43 | /** |
Andy Stadler | 72d5de7 | 2009-04-21 11:54:14 -0700 | [diff] [blame] | 44 | * <b>NOTE:</b> The parameter <i>pkg</i> must refer to the package identifier of the |
| 45 | * package hosting the activity to be launched, which is specified in the AndroidManifest.xml |
| 46 | * file. This is not necessarily the same as the java package name. |
| 47 | * |
| 48 | * @param pkg The package hosting the activity to be launched. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | * @param activityClass The activity to test. |
| 50 | */ |
| 51 | public SingleLaunchActivityTestCase(String pkg, Class<T> activityClass) { |
| 52 | mPackage = pkg; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 53 | mActivityClass = activityClass; |
| 54 | sTestCaseCounter ++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | } |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 56 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | /** |
| 58 | * The activity that will be set up for use in each test method. |
| 59 | */ |
| 60 | private static Activity sActivity; |
| 61 | |
| 62 | public T getActivity() { |
| 63 | return (T) sActivity; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected void setUp() throws Exception { |
| 68 | super.setUp(); |
| 69 | // If it is the first test case, launch the activity. |
| 70 | if (!sActivityLaunchedFlag) { |
| 71 | // by default, not in touch mode |
| 72 | getInstrumentation().setInTouchMode(false); |
| 73 | sActivity = launchActivity(mPackage, mActivityClass, null); |
| 74 | sActivityLaunchedFlag = true; |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 75 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected void tearDown() throws Exception { |
| 80 | // If it is the last test case, call finish on the activity. |
| 81 | sTestCaseCounter --; |
Mikael Andreen | c6bf407 | 2010-12-01 12:49:08 +0100 | [diff] [blame] | 82 | if (sTestCaseCounter == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | sActivity.finish(); |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 84 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | super.tearDown(); |
| 86 | } |
| 87 | |
| 88 | public void testActivityTestCaseSetUpProperly() throws Exception { |
| 89 | assertNotNull("activity should be launched successfully", sActivity); |
| 90 | } |
| 91 | } |