MockContext.java revision 98365d7663cbd82979a5700faf0050220b01084d
1/* 2 * Copyright (C) 2007 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.mock; 18 19import android.content.ComponentName; 20import android.content.ContentResolver; 21import android.content.Context; 22import android.content.Intent; 23import android.content.IntentFilter; 24import android.content.BroadcastReceiver; 25import android.content.IntentSender; 26import android.content.ServiceConnection; 27import android.content.SharedPreferences; 28import android.content.pm.ApplicationInfo; 29import android.content.pm.PackageManager; 30import android.content.res.AssetManager; 31import android.content.res.Configuration; 32import android.content.res.Resources; 33import android.database.DatabaseErrorHandler; 34import android.database.sqlite.SQLiteDatabase; 35import android.graphics.Bitmap; 36import android.graphics.drawable.Drawable; 37import android.net.Uri; 38import android.os.Bundle; 39import android.os.Handler; 40import android.os.Looper; 41import android.os.UserHandle; 42import android.view.CompatibilityInfoHolder; 43 44import java.io.File; 45import java.io.FileInputStream; 46import java.io.FileNotFoundException; 47import java.io.FileOutputStream; 48import java.io.IOException; 49import java.io.InputStream; 50 51/** 52 * A mock {@link android.content.Context} class. All methods are non-functional and throw 53 * {@link java.lang.UnsupportedOperationException}. You can use this to inject other dependencies, 54 * mocks, or monitors into the classes you are testing. 55 */ 56public class MockContext extends Context { 57 58 @Override 59 public AssetManager getAssets() { 60 throw new UnsupportedOperationException(); 61 } 62 63 @Override 64 public Resources getResources() { 65 throw new UnsupportedOperationException(); 66 } 67 68 @Override 69 public PackageManager getPackageManager() { 70 throw new UnsupportedOperationException(); 71 } 72 73 @Override 74 public ContentResolver getContentResolver() { 75 throw new UnsupportedOperationException(); 76 } 77 78 @Override 79 public Looper getMainLooper() { 80 throw new UnsupportedOperationException(); 81 } 82 83 @Override 84 public Context getApplicationContext() { 85 throw new UnsupportedOperationException(); 86 } 87 88 @Override 89 public void setTheme(int resid) { 90 throw new UnsupportedOperationException(); 91 } 92 93 @Override 94 public Resources.Theme getTheme() { 95 throw new UnsupportedOperationException(); 96 } 97 98 @Override 99 public ClassLoader getClassLoader() { 100 throw new UnsupportedOperationException(); 101 } 102 103 @Override 104 public String getPackageName() { 105 throw new UnsupportedOperationException(); 106 } 107 108 @Override 109 public ApplicationInfo getApplicationInfo() { 110 throw new UnsupportedOperationException(); 111 } 112 113 @Override 114 public String getPackageResourcePath() { 115 throw new UnsupportedOperationException(); 116 } 117 118 /** @hide */ 119 @Override 120 public File getSharedPrefsFile(String name) { 121 throw new UnsupportedOperationException(); 122 } 123 124 @Override 125 public String getPackageCodePath() { 126 throw new UnsupportedOperationException(); 127 } 128 129 @Override 130 public SharedPreferences getSharedPreferences(String name, int mode) { 131 throw new UnsupportedOperationException(); 132 } 133 134 @Override 135 public FileInputStream openFileInput(String name) throws FileNotFoundException { 136 throw new UnsupportedOperationException(); 137 } 138 139 @Override 140 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException { 141 throw new UnsupportedOperationException(); 142 } 143 144 @Override 145 public boolean deleteFile(String name) { 146 throw new UnsupportedOperationException(); 147 } 148 149 @Override 150 public File getFileStreamPath(String name) { 151 throw new UnsupportedOperationException(); 152 } 153 154 @Override 155 public String[] fileList() { 156 throw new UnsupportedOperationException(); 157 } 158 159 @Override 160 public File getFilesDir() { 161 throw new UnsupportedOperationException(); 162 } 163 164 @Override 165 public File getExternalFilesDir(String type) { 166 throw new UnsupportedOperationException(); 167 } 168 169 @Override 170 public File getObbDir() { 171 throw new UnsupportedOperationException(); 172 } 173 174 @Override 175 public File getCacheDir() { 176 throw new UnsupportedOperationException(); 177 } 178 179 @Override 180 public File getExternalCacheDir() { 181 throw new UnsupportedOperationException(); 182 } 183 184 @Override 185 public File getDir(String name, int mode) { 186 throw new UnsupportedOperationException(); 187 } 188 189 @Override 190 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 191 SQLiteDatabase.CursorFactory factory) { 192 throw new UnsupportedOperationException(); 193 } 194 195 @Override 196 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 197 SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) { 198 throw new UnsupportedOperationException(); 199 } 200 201 @Override 202 public File getDatabasePath(String name) { 203 throw new UnsupportedOperationException(); 204 } 205 206 @Override 207 public String[] databaseList() { 208 throw new UnsupportedOperationException(); 209 } 210 211 @Override 212 public boolean deleteDatabase(String name) { 213 throw new UnsupportedOperationException(); 214 } 215 216 @Override 217 public Drawable getWallpaper() { 218 throw new UnsupportedOperationException(); 219 } 220 221 @Override 222 public Drawable peekWallpaper() { 223 throw new UnsupportedOperationException(); 224 } 225 226 @Override 227 public int getWallpaperDesiredMinimumWidth() { 228 throw new UnsupportedOperationException(); 229 } 230 231 @Override 232 public int getWallpaperDesiredMinimumHeight() { 233 throw new UnsupportedOperationException(); 234 } 235 236 @Override 237 public void setWallpaper(Bitmap bitmap) throws IOException { 238 throw new UnsupportedOperationException(); 239 } 240 241 @Override 242 public void setWallpaper(InputStream data) throws IOException { 243 throw new UnsupportedOperationException(); 244 } 245 246 @Override 247 public void clearWallpaper() { 248 throw new UnsupportedOperationException(); 249 } 250 251 @Override 252 public void startActivity(Intent intent) { 253 throw new UnsupportedOperationException(); 254 } 255 256 @Override 257 public void startActivity(Intent intent, Bundle options) { 258 startActivity(intent); 259 } 260 261 @Override 262 public void startActivities(Intent[] intents) { 263 throw new UnsupportedOperationException(); 264 } 265 266 @Override 267 public void startActivities(Intent[] intents, Bundle options) { 268 startActivities(intents); 269 } 270 271 @Override 272 public void startIntentSender(IntentSender intent, 273 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 274 throws IntentSender.SendIntentException { 275 throw new UnsupportedOperationException(); 276 } 277 278 @Override 279 public void startIntentSender(IntentSender intent, 280 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, 281 Bundle options) throws IntentSender.SendIntentException { 282 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags); 283 } 284 285 @Override 286 public void sendBroadcast(Intent intent) { 287 throw new UnsupportedOperationException(); 288 } 289 290 @Override 291 public void sendBroadcast(Intent intent, String receiverPermission) { 292 throw new UnsupportedOperationException(); 293 } 294 295 @Override 296 public void sendOrderedBroadcast(Intent intent, 297 String receiverPermission) { 298 throw new UnsupportedOperationException(); 299 } 300 301 @Override 302 public void sendOrderedBroadcast(Intent intent, String receiverPermission, 303 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 304 Bundle initialExtras) { 305 throw new UnsupportedOperationException(); 306 } 307 308 @Override 309 public void sendBroadcastAsUser(Intent intent, UserHandle user) { 310 throw new UnsupportedOperationException(); 311 } 312 313 @Override 314 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 315 BroadcastReceiver resultReceiver, Handler scheduler, 316 int initialCode, String initialData, Bundle initialExtras) { 317 throw new UnsupportedOperationException(); 318 } 319 320 @Override 321 public void sendStickyBroadcast(Intent intent) { 322 throw new UnsupportedOperationException(); 323 } 324 325 @Override 326 public void sendStickyOrderedBroadcast(Intent intent, 327 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 328 Bundle initialExtras) { 329 throw new UnsupportedOperationException(); 330 } 331 332 @Override 333 public void removeStickyBroadcast(Intent intent) { 334 throw new UnsupportedOperationException(); 335 } 336 337 @Override 338 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 339 throw new UnsupportedOperationException(); 340 } 341 342 @Override 343 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, 344 String broadcastPermission, Handler scheduler) { 345 throw new UnsupportedOperationException(); 346 } 347 348 @Override 349 public void unregisterReceiver(BroadcastReceiver receiver) { 350 throw new UnsupportedOperationException(); 351 } 352 353 @Override 354 public ComponentName startService(Intent service) { 355 throw new UnsupportedOperationException(); 356 } 357 358 @Override 359 public boolean stopService(Intent service) { 360 throw new UnsupportedOperationException(); 361 } 362 363 @Override 364 public boolean bindService(Intent service, ServiceConnection conn, int flags) { 365 throw new UnsupportedOperationException(); 366 } 367 368 /** @hide */ 369 @Override 370 public boolean bindService(Intent service, ServiceConnection conn, int flags, int userId) { 371 throw new UnsupportedOperationException(); 372 } 373 374 @Override 375 public void unbindService(ServiceConnection conn) { 376 throw new UnsupportedOperationException(); 377 } 378 379 @Override 380 public boolean startInstrumentation(ComponentName className, 381 String profileFile, Bundle arguments) { 382 throw new UnsupportedOperationException(); 383 } 384 385 @Override 386 public Object getSystemService(String name) { 387 throw new UnsupportedOperationException(); 388 } 389 390 @Override 391 public int checkPermission(String permission, int pid, int uid) { 392 throw new UnsupportedOperationException(); 393 } 394 395 @Override 396 public int checkCallingPermission(String permission) { 397 throw new UnsupportedOperationException(); 398 } 399 400 @Override 401 public int checkCallingOrSelfPermission(String permission) { 402 throw new UnsupportedOperationException(); 403 } 404 405 @Override 406 public void enforcePermission( 407 String permission, int pid, int uid, String message) { 408 throw new UnsupportedOperationException(); 409 } 410 411 @Override 412 public void enforceCallingPermission(String permission, String message) { 413 throw new UnsupportedOperationException(); 414 } 415 416 @Override 417 public void enforceCallingOrSelfPermission(String permission, String message) { 418 throw new UnsupportedOperationException(); 419 } 420 421 @Override 422 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { 423 throw new UnsupportedOperationException(); 424 } 425 426 @Override 427 public void revokeUriPermission(Uri uri, int modeFlags) { 428 throw new UnsupportedOperationException(); 429 } 430 431 @Override 432 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 433 throw new UnsupportedOperationException(); 434 } 435 436 @Override 437 public int checkCallingUriPermission(Uri uri, int modeFlags) { 438 throw new UnsupportedOperationException(); 439 } 440 441 @Override 442 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { 443 throw new UnsupportedOperationException(); 444 } 445 446 @Override 447 public int checkUriPermission(Uri uri, String readPermission, 448 String writePermission, int pid, int uid, int modeFlags) { 449 throw new UnsupportedOperationException(); 450 } 451 452 @Override 453 public void enforceUriPermission( 454 Uri uri, int pid, int uid, int modeFlags, String message) { 455 throw new UnsupportedOperationException(); 456 } 457 458 @Override 459 public void enforceCallingUriPermission( 460 Uri uri, int modeFlags, String message) { 461 throw new UnsupportedOperationException(); 462 } 463 464 @Override 465 public void enforceCallingOrSelfUriPermission( 466 Uri uri, int modeFlags, String message) { 467 throw new UnsupportedOperationException(); 468 } 469 470 public void enforceUriPermission( 471 Uri uri, String readPermission, String writePermission, 472 int pid, int uid, int modeFlags, String message) { 473 throw new UnsupportedOperationException(); 474 } 475 476 @Override 477 public Context createPackageContext(String packageName, int flags) 478 throws PackageManager.NameNotFoundException { 479 throw new UnsupportedOperationException(); 480 } 481 482 @Override 483 public Context createConfigurationContext(Configuration overrideConfiguration) { 484 throw new UnsupportedOperationException(); 485 } 486 487 @Override 488 public boolean isRestricted() { 489 throw new UnsupportedOperationException(); 490 } 491 492 /** @hide */ 493 @Override 494 public CompatibilityInfoHolder getCompatibilityInfo() { 495 throw new UnsupportedOperationException(); 496 } 497} 498