AndroidTestCase.java revision bedf9df706ef1fe352c1ba0734f50f9e8ad2533e
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 17package android.test; 18 19import android.content.ContentValues; 20import android.content.Context; 21import android.content.Intent; 22import android.net.Uri; 23import junit.framework.TestCase; 24 25import java.lang.reflect.Field; 26 27/** 28 * Extend this if you need to access Resources or other things that depend on Activity Context. 29 */ 30public class AndroidTestCase extends TestCase { 31 32 protected Context mContext; 33 34 @Override 35 protected void setUp() throws Exception { 36 super.setUp(); 37 } 38 39 @Override 40 protected void tearDown() throws Exception { 41 super.tearDown(); 42 } 43 44 public void testAndroidTestCaseSetupProperly() { 45 assertNotNull("Context is null. setContext should be called before tests are run", 46 mContext); 47 } 48 49 public void setContext(Context context) { 50 mContext = context; 51 } 52 53 public Context getContext() { 54 return mContext; 55 } 56 57 /** 58 * Asserts that launching a given activity is protected by a particular permission by 59 * attempting to start the activity and validating that a {@link SecurityException} 60 * is thrown that mentions the permission in its error message. 61 * 62 * Note that an instrumentation isn't needed because all we are looking for is a security error 63 * and we don't need to wait for the activity to launch and get a handle to the activity. 64 * 65 * @param packageName The package name of the activity to launch. 66 * @param className The class of the activity to launch. 67 * @param permission The name of the permission. 68 */ 69 public void assertActivityRequiresPermission( 70 String packageName, String className, String permission) { 71 final Intent intent = new Intent(); 72 intent.setClassName(packageName, className); 73 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 74 75 try { 76 getContext().startActivity(intent); 77 fail("expected security exception for " + permission); 78 } catch (SecurityException expected) { 79 assertNotNull("security exception's error message.", expected.getMessage()); 80 assertTrue("error message should contain " + permission + ".", 81 expected.getMessage().contains(permission)); 82 } 83 } 84 85 86 /** 87 * Asserts that reading from the content uri requires a particular permission by querying the 88 * uri and ensuring a {@link SecurityException} is thrown mentioning the particular permission. 89 * 90 * @param uri The uri that requires a permission to query. 91 * @param permission The permission that should be required. 92 */ 93 public void assertReadingContentUriRequiresPermission(Uri uri, String permission) { 94 try { 95 getContext().getContentResolver().query(uri, null, null, null, null); 96 fail("expected SecurityException requiring " + permission); 97 } catch (SecurityException expected) { 98 assertNotNull("security exception's error message.", expected.getMessage()); 99 assertTrue("error message should contain " + permission + ".", 100 expected.getMessage().contains(permission)); 101 } 102 } 103 104 /** 105 * Asserts that writing to the content uri requires a particular permission by inserting into 106 * the uri and ensuring a {@link SecurityException} is thrown mentioning the particular 107 * permission. 108 * 109 * @param uri The uri that requires a permission to query. 110 * @param permission The permission that should be required. 111 */ 112 public void assertWritingContentUriRequiresPermission(Uri uri, String permission) { 113 try { 114 getContext().getContentResolver().insert(uri, new ContentValues()); 115 fail("expected SecurityException requiring " + permission); 116 } catch (SecurityException expected) { 117 assertNotNull("security exception's error message.", expected.getMessage()); 118 assertTrue("error message should contain " + permission + ".", 119 expected.getMessage().contains(permission)); 120 } 121 } 122 123 /** 124 * This function is called by various TestCase implementations, at tearDown() time, in order 125 * to scrub out any class variables. This protects against memory leaks in the case where a 126 * test case creates a non-static inner class (thus referencing the test case) and gives it to 127 * someone else to hold onto. 128 * 129 * @param testCaseClass The class of the derived TestCase implementation. 130 * 131 * @throws IllegalAccessException 132 */ 133 protected void scrubClass(final Class<?> testCaseClass) 134 throws IllegalAccessException { 135 final Field[] fields = getClass().getDeclaredFields(); 136 for (Field field : fields) { 137 final Class<?> fieldClass = field.getDeclaringClass(); 138 if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive()) { 139 try { 140 field.setAccessible(true); 141 field.set(this, null); 142 } catch (Exception e) { 143 android.util.Log.d("TestCase", "Error: Could not nullify field!"); 144 } 145 146 if (field.get(this) != null) { 147 android.util.Log.d("TestCase", "Error: Could not nullify field!"); 148 } 149 } 150 } 151 } 152 153 154} 155