blob: c7b1d7017fc2554054fa7deeec72e338e261f372 [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;
Dmitri Plotnikovac77f4622011-01-07 12:06:47 -080022import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24/**
25 * This is common code used to support Activity test cases. For more useful classes, please see
Dmitri Plotnikovac77f4622011-01-07 12:06:47 -080026 * {@link android.test.ActivityUnitTestCase} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 * {@link android.test.ActivityInstrumentationTestCase}.
28 */
29public abstract class ActivityTestCase extends InstrumentationTestCase {
30
31 /**
32 * The activity that will be set up for use in each test method.
33 */
34 private Activity mActivity;
35
36 /**
37 * @return Returns the activity under test.
38 */
39 protected Activity getActivity() {
40 return mActivity;
41 }
Dmitri Plotnikovac77f4622011-01-07 12:06:47 -080042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 /**
44 * Set the activity under test.
45 * @param testActivity The activity under test
46 */
47 protected void setActivity(Activity testActivity) {
48 mActivity = testActivity;
49 }
Dmitri Plotnikovac77f4622011-01-07 12:06:47 -080050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 /**
52 * This function is called by various TestCase implementations, at tearDown() time, in order
53 * to scrub out any class variables. This protects against memory leaks in the case where a
54 * test case creates a non-static inner class (thus referencing the test case) and gives it to
55 * someone else to hold onto.
Dmitri Plotnikovac77f4622011-01-07 12:06:47 -080056 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 * @param testCaseClass The class of the derived TestCase implementation.
Dmitri Plotnikovac77f4622011-01-07 12:06:47 -080058 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 * @throws IllegalAccessException
60 */
61 protected void scrubClass(final Class<?> testCaseClass)
62 throws IllegalAccessException {
63 final Field[] fields = getClass().getDeclaredFields();
64 for (Field field : fields) {
65 final Class<?> fieldClass = field.getDeclaringClass();
Dmitri Plotnikovac77f4622011-01-07 12:06:47 -080066 if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive()
67 && (field.getModifiers() & Modifier.FINAL) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 try {
69 field.setAccessible(true);
70 field.set(this, null);
71 } catch (Exception e) {
72 android.util.Log.d("TestCase", "Error: Could not nullify field!");
73 }
74
75 if (field.get(this) != null) {
76 android.util.Log.d("TestCase", "Error: Could not nullify field!");
77 }
78 }
79 }
80 }
81
Dmitri Plotnikovac77f4622011-01-07 12:06:47 -080082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84}