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 import java.lang.reflect.Modifier; 23 24 /** 25 * This is common code used to support Activity test cases. For more useful classes, please see 26 * {@link android.test.ActivityUnitTestCase} and 27 * {@link android.test.ActivityInstrumentationTestCase}. 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>. 31 */ 32 @Deprecated 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 */ getActivity()43 protected Activity getActivity() { 44 return mActivity; 45 } 46 47 /** 48 * Set the activity under test. 49 * @param testActivity The activity under test 50 */ setActivity(Activity testActivity)51 protected void setActivity(Activity testActivity) { 52 mActivity = testActivity; 53 } 54 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. 60 * 61 * @param testCaseClass The class of the derived TestCase implementation. 62 * 63 * @throws IllegalAccessException 64 */ scrubClass(final Class<?> testCaseClass)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(); 70 if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive() 71 && (field.getModifiers() & Modifier.FINAL) == 0) { 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 86 87 88 } 89