| 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 | |
| 21 | import java.lang.reflect.Field; |
| Dmitri Plotnikov | ac77f462 | 2011-01-07 12:06:47 -0800 | [diff] [blame] | 22 | import java.lang.reflect.Modifier; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * This is common code used to support Activity test cases. For more useful classes, please see |
| Dmitri Plotnikov | ac77f462 | 2011-01-07 12:06:47 -0800 | [diff] [blame] | 26 | * {@link android.test.ActivityUnitTestCase} and |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | * {@link android.test.ActivityInstrumentationTestCase}. |
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame^] | 28 | * |
| 29 | * @deprecated New tests should be written using the |
| 30 | * <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] | 31 | */ |
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame^] | 32 | @Deprecated |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | public abstract class ActivityTestCase extends InstrumentationTestCase { |
| 34 | |
| 35 | /** |
| 36 | * The activity that will be set up for use in each test method. |
| 37 | */ |
| 38 | private Activity mActivity; |
| 39 | |
| 40 | /** |
| 41 | * @return Returns the activity under test. |
| 42 | */ |
| 43 | protected Activity getActivity() { |
| 44 | return mActivity; |
| 45 | } |
| Dmitri Plotnikov | ac77f462 | 2011-01-07 12:06:47 -0800 | [diff] [blame] | 46 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | /** |
| 48 | * Set the activity under test. |
| 49 | * @param testActivity The activity under test |
| 50 | */ |
| 51 | protected void setActivity(Activity testActivity) { |
| 52 | mActivity = testActivity; |
| 53 | } |
| Dmitri Plotnikov | ac77f462 | 2011-01-07 12:06:47 -0800 | [diff] [blame] | 54 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | /** |
| 56 | * This function is called by various TestCase implementations, at tearDown() time, in order |
| 57 | * to scrub out any class variables. This protects against memory leaks in the case where a |
| 58 | * test case creates a non-static inner class (thus referencing the test case) and gives it to |
| 59 | * someone else to hold onto. |
| Dmitri Plotnikov | ac77f462 | 2011-01-07 12:06:47 -0800 | [diff] [blame] | 60 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | * @param testCaseClass The class of the derived TestCase implementation. |
| Dmitri Plotnikov | ac77f462 | 2011-01-07 12:06:47 -0800 | [diff] [blame] | 62 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | * @throws IllegalAccessException |
| 64 | */ |
| 65 | protected void scrubClass(final Class<?> testCaseClass) |
| 66 | throws IllegalAccessException { |
| 67 | final Field[] fields = getClass().getDeclaredFields(); |
| 68 | for (Field field : fields) { |
| 69 | final Class<?> fieldClass = field.getDeclaringClass(); |
| Dmitri Plotnikov | ac77f462 | 2011-01-07 12:06:47 -0800 | [diff] [blame] | 70 | if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive() |
| 71 | && (field.getModifiers() & Modifier.FINAL) == 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | try { |
| 73 | field.setAccessible(true); |
| 74 | field.set(this, null); |
| 75 | } catch (Exception e) { |
| 76 | android.util.Log.d("TestCase", "Error: Could not nullify field!"); |
| 77 | } |
| 78 | |
| 79 | if (field.get(this) != null) { |
| 80 | android.util.Log.d("TestCase", "Error: Could not nullify field!"); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| Dmitri Plotnikov | ac77f462 | 2011-01-07 12:06:47 -0800 | [diff] [blame] | 86 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
| 88 | } |