| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.test; |
| 18 |
|
| 19 |
import android.content.ContentValues; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.Intent; |
| 22 |
import android.net.Uri; |
| 23 |
import android.test.suitebuilder.annotation.Suppress; |
| 24 |
|
| 25 |
import junit.framework.TestCase; |
| 26 |
|
| 27 |
import java.lang.reflect.Field; |
| 28 |
import java.lang.reflect.Modifier; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
@Deprecated |
| 39 |
public class AndroidTestCase extends TestCase { |
| 40 |
|
| 41 |
protected Context mContext; |
| 42 |
private Context mTestContext; |
| 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 |
|
| 54 |
@Suppress |
| 55 |
public void testAndroidTestCaseSetupProperly() { |
| 56 |
assertNotNull("Context is null. setContext should be called before tests are run", |
| 57 |
mContext); |
| 58 |
} |
| 59 |
|
| 60 |
public void setContext(Context context) { |
| 61 |
mContext = context; |
| 62 |
} |
| 63 |
|
| 64 |
public Context getContext() { |
| 65 |
return mContext; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public void setTestContext(Context context) { |
| 76 |
mTestContext = context; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public Context getTestContext() { |
| 83 |
return mTestContext; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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()); |
| 147 |
assertTrue("error message should contain \"" + permission + "\". Got: \"" |
| 148 |
+ expected.getMessage() + "\".", |
| 149 |
expected.getMessage().contains(permission)); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
protected void scrubClass(final Class<?> testCaseClass) |
| 164 |
throws IllegalAccessException { |
| 165 |
final Field[] fields = getClass().getDeclaredFields(); |
| 166 |
for (Field field : fields) { |
| 167 |
if (!field.getType().isPrimitive() && |
| 168 |
!Modifier.isStatic(field.getModifiers())) { |
| 169 |
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 |
} |
| 182 |
} |