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