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 17 package android.test.mock; 18 19 import android.content.ComponentName; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.content.BroadcastReceiver; 25 import android.content.IntentSender; 26 import android.content.ServiceConnection; 27 import android.content.SharedPreferences; 28 import android.content.pm.ApplicationInfo; 29 import android.content.pm.PackageManager; 30 import android.content.res.AssetManager; 31 import android.content.res.Configuration; 32 import android.content.res.Resources; 33 import android.database.DatabaseErrorHandler; 34 import android.database.sqlite.SQLiteDatabase; 35 import android.graphics.Bitmap; 36 import android.graphics.drawable.Drawable; 37 import android.net.Uri; 38 import android.os.Bundle; 39 import android.os.Handler; 40 import android.os.Looper; 41 import android.os.UserHandle; 42 import android.view.DisplayAdjustments; 43 import android.view.Display; 44 45 import java.io.File; 46 import java.io.FileInputStream; 47 import java.io.FileNotFoundException; 48 import java.io.FileOutputStream; 49 import java.io.IOException; 50 import java.io.InputStream; 51 52 /** 53 * A mock {@link android.content.Context} class. All methods are non-functional and throw 54 * {@link java.lang.UnsupportedOperationException}. You can use this to inject other dependencies, 55 * mocks, or monitors into the classes you are testing. 56 */ 57 public class MockContext extends Context { 58 59 @Override getAssets()60 public AssetManager getAssets() { 61 throw new UnsupportedOperationException(); 62 } 63 64 @Override getResources()65 public Resources getResources() { 66 throw new UnsupportedOperationException(); 67 } 68 69 @Override getPackageManager()70 public PackageManager getPackageManager() { 71 throw new UnsupportedOperationException(); 72 } 73 74 @Override getContentResolver()75 public ContentResolver getContentResolver() { 76 throw new UnsupportedOperationException(); 77 } 78 79 @Override getMainLooper()80 public Looper getMainLooper() { 81 throw new UnsupportedOperationException(); 82 } 83 84 @Override getApplicationContext()85 public Context getApplicationContext() { 86 throw new UnsupportedOperationException(); 87 } 88 89 @Override setTheme(int resid)90 public void setTheme(int resid) { 91 throw new UnsupportedOperationException(); 92 } 93 94 @Override getTheme()95 public Resources.Theme getTheme() { 96 throw new UnsupportedOperationException(); 97 } 98 99 @Override getClassLoader()100 public ClassLoader getClassLoader() { 101 throw new UnsupportedOperationException(); 102 } 103 104 @Override getPackageName()105 public String getPackageName() { 106 throw new UnsupportedOperationException(); 107 } 108 109 /** @hide */ 110 @Override getBasePackageName()111 public String getBasePackageName() { 112 throw new UnsupportedOperationException(); 113 } 114 115 /** @hide */ 116 @Override getOpPackageName()117 public String getOpPackageName() { 118 throw new UnsupportedOperationException(); 119 } 120 121 @Override getApplicationInfo()122 public ApplicationInfo getApplicationInfo() { 123 throw new UnsupportedOperationException(); 124 } 125 126 @Override getPackageResourcePath()127 public String getPackageResourcePath() { 128 throw new UnsupportedOperationException(); 129 } 130 131 /** @hide */ 132 @Override getSharedPrefsFile(String name)133 public File getSharedPrefsFile(String name) { 134 throw new UnsupportedOperationException(); 135 } 136 137 @Override getPackageCodePath()138 public String getPackageCodePath() { 139 throw new UnsupportedOperationException(); 140 } 141 142 @Override getSharedPreferences(String name, int mode)143 public SharedPreferences getSharedPreferences(String name, int mode) { 144 throw new UnsupportedOperationException(); 145 } 146 147 @Override openFileInput(String name)148 public FileInputStream openFileInput(String name) throws FileNotFoundException { 149 throw new UnsupportedOperationException(); 150 } 151 152 @Override openFileOutput(String name, int mode)153 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException { 154 throw new UnsupportedOperationException(); 155 } 156 157 @Override deleteFile(String name)158 public boolean deleteFile(String name) { 159 throw new UnsupportedOperationException(); 160 } 161 162 @Override getFileStreamPath(String name)163 public File getFileStreamPath(String name) { 164 throw new UnsupportedOperationException(); 165 } 166 167 @Override fileList()168 public String[] fileList() { 169 throw new UnsupportedOperationException(); 170 } 171 172 @Override getFilesDir()173 public File getFilesDir() { 174 throw new UnsupportedOperationException(); 175 } 176 177 @Override getNoBackupFilesDir()178 public File getNoBackupFilesDir() { 179 throw new UnsupportedOperationException(); 180 } 181 182 @Override getExternalFilesDir(String type)183 public File getExternalFilesDir(String type) { 184 throw new UnsupportedOperationException(); 185 } 186 187 @Override getObbDir()188 public File getObbDir() { 189 throw new UnsupportedOperationException(); 190 } 191 192 @Override getCacheDir()193 public File getCacheDir() { 194 throw new UnsupportedOperationException(); 195 } 196 197 @Override getCodeCacheDir()198 public File getCodeCacheDir() { 199 throw new UnsupportedOperationException(); 200 } 201 202 @Override getExternalCacheDir()203 public File getExternalCacheDir() { 204 throw new UnsupportedOperationException(); 205 } 206 207 @Override getDir(String name, int mode)208 public File getDir(String name, int mode) { 209 throw new UnsupportedOperationException(); 210 } 211 212 @Override openOrCreateDatabase(String file, int mode, SQLiteDatabase.CursorFactory factory)213 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 214 SQLiteDatabase.CursorFactory factory) { 215 throw new UnsupportedOperationException(); 216 } 217 218 @Override openOrCreateDatabase(String file, int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler)219 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 220 SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) { 221 throw new UnsupportedOperationException(); 222 } 223 224 @Override getDatabasePath(String name)225 public File getDatabasePath(String name) { 226 throw new UnsupportedOperationException(); 227 } 228 229 @Override databaseList()230 public String[] databaseList() { 231 throw new UnsupportedOperationException(); 232 } 233 234 @Override deleteDatabase(String name)235 public boolean deleteDatabase(String name) { 236 throw new UnsupportedOperationException(); 237 } 238 239 @Override getWallpaper()240 public Drawable getWallpaper() { 241 throw new UnsupportedOperationException(); 242 } 243 244 @Override peekWallpaper()245 public Drawable peekWallpaper() { 246 throw new UnsupportedOperationException(); 247 } 248 249 @Override getWallpaperDesiredMinimumWidth()250 public int getWallpaperDesiredMinimumWidth() { 251 throw new UnsupportedOperationException(); 252 } 253 254 @Override getWallpaperDesiredMinimumHeight()255 public int getWallpaperDesiredMinimumHeight() { 256 throw new UnsupportedOperationException(); 257 } 258 259 @Override setWallpaper(Bitmap bitmap)260 public void setWallpaper(Bitmap bitmap) throws IOException { 261 throw new UnsupportedOperationException(); 262 } 263 264 @Override setWallpaper(InputStream data)265 public void setWallpaper(InputStream data) throws IOException { 266 throw new UnsupportedOperationException(); 267 } 268 269 @Override clearWallpaper()270 public void clearWallpaper() { 271 throw new UnsupportedOperationException(); 272 } 273 274 @Override startActivity(Intent intent)275 public void startActivity(Intent intent) { 276 throw new UnsupportedOperationException(); 277 } 278 279 @Override startActivity(Intent intent, Bundle options)280 public void startActivity(Intent intent, Bundle options) { 281 startActivity(intent); 282 } 283 284 @Override startActivities(Intent[] intents)285 public void startActivities(Intent[] intents) { 286 throw new UnsupportedOperationException(); 287 } 288 289 @Override startActivities(Intent[] intents, Bundle options)290 public void startActivities(Intent[] intents, Bundle options) { 291 startActivities(intents); 292 } 293 294 @Override startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)295 public void startIntentSender(IntentSender intent, 296 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 297 throws IntentSender.SendIntentException { 298 throw new UnsupportedOperationException(); 299 } 300 301 @Override startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)302 public void startIntentSender(IntentSender intent, 303 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, 304 Bundle options) throws IntentSender.SendIntentException { 305 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags); 306 } 307 308 @Override sendBroadcast(Intent intent)309 public void sendBroadcast(Intent intent) { 310 throw new UnsupportedOperationException(); 311 } 312 313 @Override sendBroadcast(Intent intent, String receiverPermission)314 public void sendBroadcast(Intent intent, String receiverPermission) { 315 throw new UnsupportedOperationException(); 316 } 317 318 /** @hide */ 319 @Override sendBroadcast(Intent intent, String receiverPermission, int appOp)320 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) { 321 throw new UnsupportedOperationException(); 322 } 323 324 @Override sendOrderedBroadcast(Intent intent, String receiverPermission)325 public void sendOrderedBroadcast(Intent intent, 326 String receiverPermission) { 327 throw new UnsupportedOperationException(); 328 } 329 330 @Override sendOrderedBroadcast(Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)331 public void sendOrderedBroadcast(Intent intent, String receiverPermission, 332 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 333 Bundle initialExtras) { 334 throw new UnsupportedOperationException(); 335 } 336 337 /** @hide */ 338 @Override sendOrderedBroadcast(Intent intent, String receiverPermission, int appOp, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)339 public void sendOrderedBroadcast(Intent intent, String receiverPermission, int appOp, 340 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 341 Bundle initialExtras) { 342 throw new UnsupportedOperationException(); 343 } 344 345 @Override sendBroadcastAsUser(Intent intent, UserHandle user)346 public void sendBroadcastAsUser(Intent intent, UserHandle user) { 347 throw new UnsupportedOperationException(); 348 } 349 350 @Override sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission)351 public void sendBroadcastAsUser(Intent intent, UserHandle user, 352 String receiverPermission) { 353 throw new UnsupportedOperationException(); 354 } 355 356 @Override sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)357 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 358 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, 359 int initialCode, String initialData, Bundle initialExtras) { 360 throw new UnsupportedOperationException(); 361 } 362 363 /** @hide */ 364 @Override sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, int appOp, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)365 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 366 String receiverPermission, int appOp, BroadcastReceiver resultReceiver, 367 Handler scheduler, 368 int initialCode, String initialData, Bundle initialExtras) { 369 throw new UnsupportedOperationException(); 370 } 371 372 @Override 373 sendStickyBroadcast(Intent intent)374 public void sendStickyBroadcast(Intent intent) { 375 throw new UnsupportedOperationException(); 376 } 377 378 @Override sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)379 public void sendStickyOrderedBroadcast(Intent intent, 380 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 381 Bundle initialExtras) { 382 throw new UnsupportedOperationException(); 383 } 384 385 @Override removeStickyBroadcast(Intent intent)386 public void removeStickyBroadcast(Intent intent) { 387 throw new UnsupportedOperationException(); 388 } 389 390 @Override sendStickyBroadcastAsUser(Intent intent, UserHandle user)391 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { 392 throw new UnsupportedOperationException(); 393 } 394 395 @Override sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle user, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)396 public void sendStickyOrderedBroadcastAsUser(Intent intent, 397 UserHandle user, BroadcastReceiver resultReceiver, 398 Handler scheduler, int initialCode, String initialData, 399 Bundle initialExtras) { 400 throw new UnsupportedOperationException(); 401 } 402 403 @Override removeStickyBroadcastAsUser(Intent intent, UserHandle user)404 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) { 405 throw new UnsupportedOperationException(); 406 } 407 408 @Override registerReceiver(BroadcastReceiver receiver, IntentFilter filter)409 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 410 throw new UnsupportedOperationException(); 411 } 412 413 @Override registerReceiver(BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler)414 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, 415 String broadcastPermission, Handler scheduler) { 416 throw new UnsupportedOperationException(); 417 } 418 419 /** @hide */ 420 @Override registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, IntentFilter filter, String broadcastPermission, Handler scheduler)421 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, 422 IntentFilter filter, String broadcastPermission, Handler scheduler) { 423 throw new UnsupportedOperationException(); 424 } 425 426 @Override unregisterReceiver(BroadcastReceiver receiver)427 public void unregisterReceiver(BroadcastReceiver receiver) { 428 throw new UnsupportedOperationException(); 429 } 430 431 @Override startService(Intent service)432 public ComponentName startService(Intent service) { 433 throw new UnsupportedOperationException(); 434 } 435 436 @Override stopService(Intent service)437 public boolean stopService(Intent service) { 438 throw new UnsupportedOperationException(); 439 } 440 441 /** @hide */ 442 @Override startServiceAsUser(Intent service, UserHandle user)443 public ComponentName startServiceAsUser(Intent service, UserHandle user) { 444 throw new UnsupportedOperationException(); 445 } 446 447 /** @hide */ 448 @Override stopServiceAsUser(Intent service, UserHandle user)449 public boolean stopServiceAsUser(Intent service, UserHandle user) { 450 throw new UnsupportedOperationException(); 451 } 452 453 @Override bindService(Intent service, ServiceConnection conn, int flags)454 public boolean bindService(Intent service, ServiceConnection conn, int flags) { 455 throw new UnsupportedOperationException(); 456 } 457 458 /** @hide */ 459 @Override bindServiceAsUser(Intent service, ServiceConnection conn, int flags, UserHandle user)460 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, 461 UserHandle user) { 462 throw new UnsupportedOperationException(); 463 } 464 465 @Override unbindService(ServiceConnection conn)466 public void unbindService(ServiceConnection conn) { 467 throw new UnsupportedOperationException(); 468 } 469 470 @Override startInstrumentation(ComponentName className, String profileFile, Bundle arguments)471 public boolean startInstrumentation(ComponentName className, 472 String profileFile, Bundle arguments) { 473 throw new UnsupportedOperationException(); 474 } 475 476 @Override getSystemService(String name)477 public Object getSystemService(String name) { 478 throw new UnsupportedOperationException(); 479 } 480 481 @Override checkPermission(String permission, int pid, int uid)482 public int checkPermission(String permission, int pid, int uid) { 483 throw new UnsupportedOperationException(); 484 } 485 486 @Override checkCallingPermission(String permission)487 public int checkCallingPermission(String permission) { 488 throw new UnsupportedOperationException(); 489 } 490 491 @Override checkCallingOrSelfPermission(String permission)492 public int checkCallingOrSelfPermission(String permission) { 493 throw new UnsupportedOperationException(); 494 } 495 496 @Override enforcePermission( String permission, int pid, int uid, String message)497 public void enforcePermission( 498 String permission, int pid, int uid, String message) { 499 throw new UnsupportedOperationException(); 500 } 501 502 @Override enforceCallingPermission(String permission, String message)503 public void enforceCallingPermission(String permission, String message) { 504 throw new UnsupportedOperationException(); 505 } 506 507 @Override enforceCallingOrSelfPermission(String permission, String message)508 public void enforceCallingOrSelfPermission(String permission, String message) { 509 throw new UnsupportedOperationException(); 510 } 511 512 @Override grantUriPermission(String toPackage, Uri uri, int modeFlags)513 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { 514 throw new UnsupportedOperationException(); 515 } 516 517 @Override revokeUriPermission(Uri uri, int modeFlags)518 public void revokeUriPermission(Uri uri, int modeFlags) { 519 throw new UnsupportedOperationException(); 520 } 521 522 @Override checkUriPermission(Uri uri, int pid, int uid, int modeFlags)523 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 524 throw new UnsupportedOperationException(); 525 } 526 527 @Override checkCallingUriPermission(Uri uri, int modeFlags)528 public int checkCallingUriPermission(Uri uri, int modeFlags) { 529 throw new UnsupportedOperationException(); 530 } 531 532 @Override checkCallingOrSelfUriPermission(Uri uri, int modeFlags)533 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { 534 throw new UnsupportedOperationException(); 535 } 536 537 @Override checkUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags)538 public int checkUriPermission(Uri uri, String readPermission, 539 String writePermission, int pid, int uid, int modeFlags) { 540 throw new UnsupportedOperationException(); 541 } 542 543 @Override enforceUriPermission( Uri uri, int pid, int uid, int modeFlags, String message)544 public void enforceUriPermission( 545 Uri uri, int pid, int uid, int modeFlags, String message) { 546 throw new UnsupportedOperationException(); 547 } 548 549 @Override enforceCallingUriPermission( Uri uri, int modeFlags, String message)550 public void enforceCallingUriPermission( 551 Uri uri, int modeFlags, String message) { 552 throw new UnsupportedOperationException(); 553 } 554 555 @Override enforceCallingOrSelfUriPermission( Uri uri, int modeFlags, String message)556 public void enforceCallingOrSelfUriPermission( 557 Uri uri, int modeFlags, String message) { 558 throw new UnsupportedOperationException(); 559 } 560 enforceUriPermission( Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags, String message)561 public void enforceUriPermission( 562 Uri uri, String readPermission, String writePermission, 563 int pid, int uid, int modeFlags, String message) { 564 throw new UnsupportedOperationException(); 565 } 566 567 @Override createPackageContext(String packageName, int flags)568 public Context createPackageContext(String packageName, int flags) 569 throws PackageManager.NameNotFoundException { 570 throw new UnsupportedOperationException(); 571 } 572 573 /** {@hide} */ 574 @Override createApplicationContext(ApplicationInfo application, int flags)575 public Context createApplicationContext(ApplicationInfo application, int flags) 576 throws PackageManager.NameNotFoundException { 577 return null; 578 } 579 580 /** {@hide} */ 581 @Override createPackageContextAsUser(String packageName, int flags, UserHandle user)582 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user) 583 throws PackageManager.NameNotFoundException { 584 throw new UnsupportedOperationException(); 585 } 586 587 /** {@hide} */ 588 @Override getUserId()589 public int getUserId() { 590 throw new UnsupportedOperationException(); 591 } 592 593 @Override createConfigurationContext(Configuration overrideConfiguration)594 public Context createConfigurationContext(Configuration overrideConfiguration) { 595 throw new UnsupportedOperationException(); 596 } 597 598 @Override createDisplayContext(Display display)599 public Context createDisplayContext(Display display) { 600 throw new UnsupportedOperationException(); 601 } 602 603 @Override isRestricted()604 public boolean isRestricted() { 605 throw new UnsupportedOperationException(); 606 } 607 608 /** @hide */ 609 @Override getDisplayAdjustments(int displayId)610 public DisplayAdjustments getDisplayAdjustments(int displayId) { 611 throw new UnsupportedOperationException(); 612 } 613 614 @Override getExternalFilesDirs(String type)615 public File[] getExternalFilesDirs(String type) { 616 throw new UnsupportedOperationException(); 617 } 618 619 @Override getObbDirs()620 public File[] getObbDirs() { 621 throw new UnsupportedOperationException(); 622 } 623 624 @Override getExternalCacheDirs()625 public File[] getExternalCacheDirs() { 626 throw new UnsupportedOperationException(); 627 } 628 629 @Override getExternalMediaDirs()630 public File[] getExternalMediaDirs() { 631 throw new UnsupportedOperationException(); 632 } 633 } 634