blob: d3e896be0832814da3b385323a6815f6872dd25d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
Karl Rosaenbedf9df2009-06-15 16:38:32 -070019import android.content.ContentValues;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Karl Rosaenbedf9df2009-06-15 16:38:32 -070021import android.content.Intent;
22import android.net.Uri;
Stuart Scott39aad682015-09-23 16:03:57 -070023import android.test.suitebuilder.annotation.Suppress;
Jeff Sharkeydd86cb72013-02-14 16:02:05 -080024
Karl Rosaenbedf9df2009-06-15 16:38:32 -070025import junit.framework.TestCase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27import java.lang.reflect.Field;
Jeff Sharkeydd86cb72013-02-14 16:02:05 -080028import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030/**
31 * Extend this if you need to access Resources or other things that depend on Activity Context.
Stephan Linznerb51617f2016-01-27 18:09:50 -080032 *
33 * @deprecated Use
34 * <a href="{@docRoot}reference/android/support/test/InstrumentationRegistry.html">
35 * InstrumentationRegistry</a> instead. New tests should be written using the
36 * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 */
Stephan Linznerb51617f2016-01-27 18:09:50 -080038@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039public class AndroidTestCase extends TestCase {
40
41 protected Context mContext;
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -070042 private Context mTestContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44 @Override
45 protected void setUp() throws Exception {
46 super.setUp();
47 }
48
49 @Override
50 protected void tearDown() throws Exception {
51 super.tearDown();
52 }
53
Stuart Scott39aad682015-09-23 16:03:57 -070054 @Suppress
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 public void testAndroidTestCaseSetupProperly() {
56 assertNotNull("Context is null. setContext should be called before tests are run",
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -070057 mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 }
59
60 public void setContext(Context context) {
61 mContext = context;
62 }
63
64 public Context getContext() {
65 return mContext;
66 }
67
68 /**
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -070069 * Test context can be used to access resources from the test's own package
70 * as opposed to the resources from the test target package. Access to the
71 * latter is provided by the context set with the {@link #setContext}
72 * method.
73 *
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -070074 */
75 public void setTestContext(Context context) {
76 mTestContext = context;
77 }
78
79 /**
Jiyong Park04f520d2020-06-01 10:56:48 +090080 * Returns the test context that was set via {@link #setTestContext(Context)}.
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -070081 */
82 public Context getTestContext() {
83 return mTestContext;
84 }
85
86 /**
Karl Rosaenbedf9df2009-06-15 16:38:32 -070087 * Asserts that launching a given activity is protected by a particular permission by
88 * attempting to start the activity and validating that a {@link SecurityException}
89 * is thrown that mentions the permission in its error message.
90 *
91 * Note that an instrumentation isn't needed because all we are looking for is a security error
92 * and we don't need to wait for the activity to launch and get a handle to the activity.
93 *
94 * @param packageName The package name of the activity to launch.
95 * @param className The class of the activity to launch.
96 * @param permission The name of the permission.
97 */
98 public void assertActivityRequiresPermission(
99 String packageName, String className, String permission) {
100 final Intent intent = new Intent();
101 intent.setClassName(packageName, className);
102 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
103
104 try {
105 getContext().startActivity(intent);
106 fail("expected security exception for " + permission);
107 } catch (SecurityException expected) {
108 assertNotNull("security exception's error message.", expected.getMessage());
109 assertTrue("error message should contain " + permission + ".",
110 expected.getMessage().contains(permission));
111 }
112 }
113
114
115 /**
116 * Asserts that reading from the content uri requires a particular permission by querying the
117 * uri and ensuring a {@link SecurityException} is thrown mentioning the particular permission.
118 *
119 * @param uri The uri that requires a permission to query.
120 * @param permission The permission that should be required.
121 */
122 public void assertReadingContentUriRequiresPermission(Uri uri, String permission) {
123 try {
124 getContext().getContentResolver().query(uri, null, null, null, null);
125 fail("expected SecurityException requiring " + permission);
126 } catch (SecurityException expected) {
127 assertNotNull("security exception's error message.", expected.getMessage());
128 assertTrue("error message should contain " + permission + ".",
129 expected.getMessage().contains(permission));
130 }
131 }
132
133 /**
134 * Asserts that writing to the content uri requires a particular permission by inserting into
135 * the uri and ensuring a {@link SecurityException} is thrown mentioning the particular
136 * permission.
137 *
138 * @param uri The uri that requires a permission to query.
139 * @param permission The permission that should be required.
140 */
141 public void assertWritingContentUriRequiresPermission(Uri uri, String permission) {
142 try {
143 getContext().getContentResolver().insert(uri, new ContentValues());
144 fail("expected SecurityException requiring " + permission);
145 } catch (SecurityException expected) {
146 assertNotNull("security exception's error message.", expected.getMessage());
Nick Kralevichff92aa72012-08-06 13:53:20 -0700147 assertTrue("error message should contain \"" + permission + "\". Got: \""
148 + expected.getMessage() + "\".",
Karl Rosaenbedf9df2009-06-15 16:38:32 -0700149 expected.getMessage().contains(permission));
150 }
151 }
152
153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 * This function is called by various TestCase implementations, at tearDown() time, in order
155 * to scrub out any class variables. This protects against memory leaks in the case where a
156 * test case creates a non-static inner class (thus referencing the test case) and gives it to
157 * someone else to hold onto.
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -0700158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 * @param testCaseClass The class of the derived TestCase implementation.
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -0700160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * @throws IllegalAccessException
162 */
163 protected void scrubClass(final Class<?> testCaseClass)
Jeff Sharkeydd86cb72013-02-14 16:02:05 -0800164 throws IllegalAccessException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final Field[] fields = getClass().getDeclaredFields();
166 for (Field field : fields) {
Jeff Sharkeydd86cb72013-02-14 16:02:05 -0800167 if (!field.getType().isPrimitive() &&
168 !Modifier.isStatic(field.getModifiers())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 try {
170 field.setAccessible(true);
171 field.set(this, null);
172 } catch (Exception e) {
173 android.util.Log.d("TestCase", "Error: Could not nullify field!");
174 }
175
176 if (field.get(this) != null) {
177 android.util.Log.d("TestCase", "Error: Could not nullify field!");
178 }
179 }
180 }
181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182}