blob: 2ecbfae6fede5f652a12d069f17f5f60cbd1b219 [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.
32 */
33public class AndroidTestCase extends TestCase {
34
35 protected Context mContext;
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -070036 private Context mTestContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38 @Override
39 protected void setUp() throws Exception {
40 super.setUp();
41 }
42
43 @Override
44 protected void tearDown() throws Exception {
45 super.tearDown();
46 }
47
Stuart Scott39aad682015-09-23 16:03:57 -070048 @Suppress
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 public void testAndroidTestCaseSetupProperly() {
50 assertNotNull("Context is null. setContext should be called before tests are run",
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -070051 mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 }
53
54 public void setContext(Context context) {
55 mContext = context;
56 }
57
58 public Context getContext() {
59 return mContext;
60 }
61
62 /**
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -070063 * Test context can be used to access resources from the test's own package
64 * as opposed to the resources from the test target package. Access to the
65 * latter is provided by the context set with the {@link #setContext}
66 * method.
67 *
68 * @hide
69 */
70 public void setTestContext(Context context) {
71 mTestContext = context;
72 }
73
74 /**
75 * @hide
76 */
77 public Context getTestContext() {
78 return mTestContext;
79 }
80
81 /**
Karl Rosaenbedf9df2009-06-15 16:38:32 -070082 * Asserts that launching a given activity is protected by a particular permission by
83 * attempting to start the activity and validating that a {@link SecurityException}
84 * is thrown that mentions the permission in its error message.
85 *
86 * Note that an instrumentation isn't needed because all we are looking for is a security error
87 * and we don't need to wait for the activity to launch and get a handle to the activity.
88 *
89 * @param packageName The package name of the activity to launch.
90 * @param className The class of the activity to launch.
91 * @param permission The name of the permission.
92 */
93 public void assertActivityRequiresPermission(
94 String packageName, String className, String permission) {
95 final Intent intent = new Intent();
96 intent.setClassName(packageName, className);
97 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
98
99 try {
100 getContext().startActivity(intent);
101 fail("expected security exception for " + permission);
102 } catch (SecurityException expected) {
103 assertNotNull("security exception's error message.", expected.getMessage());
104 assertTrue("error message should contain " + permission + ".",
105 expected.getMessage().contains(permission));
106 }
107 }
108
109
110 /**
111 * Asserts that reading from the content uri requires a particular permission by querying the
112 * uri and ensuring a {@link SecurityException} is thrown mentioning the particular permission.
113 *
114 * @param uri The uri that requires a permission to query.
115 * @param permission The permission that should be required.
116 */
117 public void assertReadingContentUriRequiresPermission(Uri uri, String permission) {
118 try {
119 getContext().getContentResolver().query(uri, null, null, null, null);
120 fail("expected SecurityException requiring " + permission);
121 } catch (SecurityException expected) {
122 assertNotNull("security exception's error message.", expected.getMessage());
123 assertTrue("error message should contain " + permission + ".",
124 expected.getMessage().contains(permission));
125 }
126 }
127
128 /**
129 * Asserts that writing to the content uri requires a particular permission by inserting into
130 * the uri and ensuring a {@link SecurityException} is thrown mentioning the particular
131 * permission.
132 *
133 * @param uri The uri that requires a permission to query.
134 * @param permission The permission that should be required.
135 */
136 public void assertWritingContentUriRequiresPermission(Uri uri, String permission) {
137 try {
138 getContext().getContentResolver().insert(uri, new ContentValues());
139 fail("expected SecurityException requiring " + permission);
140 } catch (SecurityException expected) {
141 assertNotNull("security exception's error message.", expected.getMessage());
Nick Kralevichff92aa72012-08-06 13:53:20 -0700142 assertTrue("error message should contain \"" + permission + "\". Got: \""
143 + expected.getMessage() + "\".",
Karl Rosaenbedf9df2009-06-15 16:38:32 -0700144 expected.getMessage().contains(permission));
145 }
146 }
147
148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * This function is called by various TestCase implementations, at tearDown() time, in order
150 * to scrub out any class variables. This protects against memory leaks in the case where a
151 * test case creates a non-static inner class (thus referencing the test case) and gives it to
152 * someone else to hold onto.
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -0700153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 * @param testCaseClass The class of the derived TestCase implementation.
Dmitri Plotnikov44a29dd2009-08-03 14:27:20 -0700155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 * @throws IllegalAccessException
157 */
158 protected void scrubClass(final Class<?> testCaseClass)
Jeff Sharkeydd86cb72013-02-14 16:02:05 -0800159 throws IllegalAccessException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 final Field[] fields = getClass().getDeclaredFields();
161 for (Field field : fields) {
Jeff Sharkeydd86cb72013-02-14 16:02:05 -0800162 if (!field.getType().isPrimitive() &&
163 !Modifier.isStatic(field.getModifiers())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 try {
165 field.setAccessible(true);
166 field.set(this, null);
167 } catch (Exception e) {
168 android.util.Log.d("TestCase", "Error: Could not nullify field!");
169 }
170
171 if (field.get(this) != null) {
172 android.util.Log.d("TestCase", "Error: Could not nullify field!");
173 }
174 }
175 }
176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177}