The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.test; |
| 18 | |
Karl Rosaen | bedf9df | 2009-06-15 16:38:32 -0700 | [diff] [blame] | 19 | import android.content.ContentValues; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Karl Rosaen | bedf9df | 2009-06-15 16:38:32 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.net.Uri; |
Stuart Scott | 39aad68 | 2015-09-23 16:03:57 -0700 | [diff] [blame] | 23 | import android.test.suitebuilder.annotation.Suppress; |
Jeff Sharkey | dd86cb7 | 2013-02-14 16:02:05 -0800 | [diff] [blame] | 24 | |
Karl Rosaen | bedf9df | 2009-06-15 16:38:32 -0700 | [diff] [blame] | 25 | import junit.framework.TestCase; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | import java.lang.reflect.Field; |
Jeff Sharkey | dd86cb7 | 2013-02-14 16:02:05 -0800 | [diff] [blame] | 28 | import java.lang.reflect.Modifier; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Extend this if you need to access Resources or other things that depend on Activity Context. |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 32 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | */ |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 38 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | public class AndroidTestCase extends TestCase { |
| 40 | |
| 41 | protected Context mContext; |
Dmitri Plotnikov | 44a29dd | 2009-08-03 14:27:20 -0700 | [diff] [blame] | 42 | private Context mTestContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 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 Scott | 39aad68 | 2015-09-23 16:03:57 -0700 | [diff] [blame] | 54 | @Suppress |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | public void testAndroidTestCaseSetupProperly() { |
| 56 | assertNotNull("Context is null. setContext should be called before tests are run", |
Dmitri Plotnikov | 44a29dd | 2009-08-03 14:27:20 -0700 | [diff] [blame] | 57 | mContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public void setContext(Context context) { |
| 61 | mContext = context; |
| 62 | } |
| 63 | |
| 64 | public Context getContext() { |
| 65 | return mContext; |
| 66 | } |
| 67 | |
| 68 | /** |
Dmitri Plotnikov | 44a29dd | 2009-08-03 14:27:20 -0700 | [diff] [blame] | 69 | * 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 | * |
| 74 | * @hide |
| 75 | */ |
| 76 | public void setTestContext(Context context) { |
| 77 | mTestContext = context; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @hide |
| 82 | */ |
| 83 | public Context getTestContext() { |
| 84 | return mTestContext; |
| 85 | } |
| 86 | |
| 87 | /** |
Karl Rosaen | bedf9df | 2009-06-15 16:38:32 -0700 | [diff] [blame] | 88 | * Asserts that launching a given activity is protected by a particular permission by |
| 89 | * attempting to start the activity and validating that a {@link SecurityException} |
| 90 | * is thrown that mentions the permission in its error message. |
| 91 | * |
| 92 | * Note that an instrumentation isn't needed because all we are looking for is a security error |
| 93 | * and we don't need to wait for the activity to launch and get a handle to the activity. |
| 94 | * |
| 95 | * @param packageName The package name of the activity to launch. |
| 96 | * @param className The class of the activity to launch. |
| 97 | * @param permission The name of the permission. |
| 98 | */ |
| 99 | public void assertActivityRequiresPermission( |
| 100 | String packageName, String className, String permission) { |
| 101 | final Intent intent = new Intent(); |
| 102 | intent.setClassName(packageName, className); |
| 103 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 104 | |
| 105 | try { |
| 106 | getContext().startActivity(intent); |
| 107 | fail("expected security exception for " + permission); |
| 108 | } catch (SecurityException expected) { |
| 109 | assertNotNull("security exception's error message.", expected.getMessage()); |
| 110 | assertTrue("error message should contain " + permission + ".", |
| 111 | expected.getMessage().contains(permission)); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * Asserts that reading from the content uri requires a particular permission by querying the |
| 118 | * uri and ensuring a {@link SecurityException} is thrown mentioning the particular permission. |
| 119 | * |
| 120 | * @param uri The uri that requires a permission to query. |
| 121 | * @param permission The permission that should be required. |
| 122 | */ |
| 123 | public void assertReadingContentUriRequiresPermission(Uri uri, String permission) { |
| 124 | try { |
| 125 | getContext().getContentResolver().query(uri, null, null, null, null); |
| 126 | fail("expected SecurityException requiring " + permission); |
| 127 | } catch (SecurityException expected) { |
| 128 | assertNotNull("security exception's error message.", expected.getMessage()); |
| 129 | assertTrue("error message should contain " + permission + ".", |
| 130 | expected.getMessage().contains(permission)); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Asserts that writing to the content uri requires a particular permission by inserting into |
| 136 | * the uri and ensuring a {@link SecurityException} is thrown mentioning the particular |
| 137 | * permission. |
| 138 | * |
| 139 | * @param uri The uri that requires a permission to query. |
| 140 | * @param permission The permission that should be required. |
| 141 | */ |
| 142 | public void assertWritingContentUriRequiresPermission(Uri uri, String permission) { |
| 143 | try { |
| 144 | getContext().getContentResolver().insert(uri, new ContentValues()); |
| 145 | fail("expected SecurityException requiring " + permission); |
| 146 | } catch (SecurityException expected) { |
| 147 | assertNotNull("security exception's error message.", expected.getMessage()); |
Nick Kralevich | ff92aa7 | 2012-08-06 13:53:20 -0700 | [diff] [blame] | 148 | assertTrue("error message should contain \"" + permission + "\". Got: \"" |
| 149 | + expected.getMessage() + "\".", |
Karl Rosaen | bedf9df | 2009-06-15 16:38:32 -0700 | [diff] [blame] | 150 | expected.getMessage().contains(permission)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | * This function is called by various TestCase implementations, at tearDown() time, in order |
| 156 | * to scrub out any class variables. This protects against memory leaks in the case where a |
| 157 | * test case creates a non-static inner class (thus referencing the test case) and gives it to |
| 158 | * someone else to hold onto. |
Dmitri Plotnikov | 44a29dd | 2009-08-03 14:27:20 -0700 | [diff] [blame] | 159 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | * @param testCaseClass The class of the derived TestCase implementation. |
Dmitri Plotnikov | 44a29dd | 2009-08-03 14:27:20 -0700 | [diff] [blame] | 161 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | * @throws IllegalAccessException |
| 163 | */ |
| 164 | protected void scrubClass(final Class<?> testCaseClass) |
Jeff Sharkey | dd86cb7 | 2013-02-14 16:02:05 -0800 | [diff] [blame] | 165 | throws IllegalAccessException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | final Field[] fields = getClass().getDeclaredFields(); |
| 167 | for (Field field : fields) { |
Jeff Sharkey | dd86cb7 | 2013-02-14 16:02:05 -0800 | [diff] [blame] | 168 | if (!field.getType().isPrimitive() && |
| 169 | !Modifier.isStatic(field.getModifiers())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | try { |
| 171 | field.setAccessible(true); |
| 172 | field.set(this, null); |
| 173 | } catch (Exception e) { |
| 174 | android.util.Log.d("TestCase", "Error: Could not nullify field!"); |
| 175 | } |
| 176 | |
| 177 | if (field.get(this) != null) { |
| 178 | android.util.Log.d("TestCase", "Error: Could not nullify field!"); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |