blob: 51dd3ef8c35b2398a4a2ef8fd5cd638931f43625 [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}.
Stephan Linznerb51617f2016-01-27 18:09:50 -080028 *
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 Project9066cfe2009-03-03 19:31:44 -080031 */
Stephan Linznerb51617f2016-01-27 18:09:50 -080032@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033public 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 Plotnikovac77f4622011-01-07 12:06:47 -080046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 /**
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 Plotnikovac77f4622011-01-07 12:06:47 -080054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 /**
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 Plotnikovac77f4622011-01-07 12:06:47 -080060 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 * @param testCaseClass The class of the derived TestCase implementation.
Dmitri Plotnikovac77f4622011-01-07 12:06:47 -080062 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 * @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 Plotnikovac77f4622011-01-07 12:06:47 -080070 if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive()
71 && (field.getModifiers() & Modifier.FINAL) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 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 Plotnikovac77f4622011-01-07 12:06:47 -080086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88}