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.annotation.SystemApi; 20import android.app.IApplicationThread; 21import android.app.IServiceConnection; 22import android.content.BroadcastReceiver; 23import android.content.ComponentName; 24import android.content.ContentResolver; 25import android.content.Context; 26import android.content.Intent; 27import android.content.IntentFilter; 28import android.content.IntentSender; 29import android.content.ServiceConnection; 30import android.content.SharedPreferences; 31import android.content.pm.ApplicationInfo; 32import android.content.pm.PackageManager; 33import android.content.res.AssetManager; 34import android.content.res.Configuration; 35import android.content.res.Resources; 36import android.database.DatabaseErrorHandler; 37import android.database.sqlite.SQLiteDatabase; 38import android.graphics.Bitmap; 39import android.graphics.drawable.Drawable; 40import android.net.Uri; 41import android.os.Bundle; 42import android.os.Handler; 43import android.os.IBinder; 44import android.os.Looper; 45import android.os.UserHandle; 46import android.view.Display; 47import android.view.DisplayAdjustments; 48 49import java.io.File; 50import java.io.FileInputStream; 51import java.io.FileNotFoundException; 52import java.io.FileOutputStream; 53import java.io.IOException; 54import java.io.InputStream; 55import java.util.concurrent.Executor; 56 57/** 58 * A mock {@link android.content.Context} class. All methods are non-functional and throw 59 * {@link java.lang.UnsupportedOperationException}. You can use this to inject other dependencies, 60 * mocks, or monitors into the classes you are testing. 61 */ 62public class MockContext extends Context { 63 64 @Override 65 public AssetManager getAssets() { 66 throw new UnsupportedOperationException(); 67 } 68 69 @Override 70 public Resources getResources() { 71 throw new UnsupportedOperationException(); 72 } 73 74 @Override 75 public PackageManager getPackageManager() { 76 throw new UnsupportedOperationException(); 77 } 78 79 @Override 80 public ContentResolver getContentResolver() { 81 throw new UnsupportedOperationException(); 82 } 83 84 @Override 85 public Looper getMainLooper() { 86 throw new UnsupportedOperationException(); 87 } 88 89 @Override 90 public Executor getMainExecutor() { 91 throw new UnsupportedOperationException(); 92 } 93 94 @Override 95 public Context getApplicationContext() { 96 throw new UnsupportedOperationException(); 97 } 98 99 @Override 100 public void setTheme(int resid) { 101 throw new UnsupportedOperationException(); 102 } 103 104 @Override 105 public Resources.Theme getTheme() { 106 throw new UnsupportedOperationException(); 107 } 108 109 @Override 110 public ClassLoader getClassLoader() { 111 throw new UnsupportedOperationException(); 112 } 113 114 @Override 115 public String getPackageName() { 116 throw new UnsupportedOperationException(); 117 } 118 119 /** @hide */ 120 @Override 121 public String getBasePackageName() { 122 throw new UnsupportedOperationException(); 123 } 124 125 /** @hide */ 126 @Override 127 public String getOpPackageName() { 128 throw new UnsupportedOperationException(); 129 } 130 131 @Override 132 public ApplicationInfo getApplicationInfo() { 133 throw new UnsupportedOperationException(); 134 } 135 136 @Override 137 public String getPackageResourcePath() { 138 throw new UnsupportedOperationException(); 139 } 140 141 @Override 142 public String getPackageCodePath() { 143 throw new UnsupportedOperationException(); 144 } 145 146 @Override 147 public SharedPreferences getSharedPreferences(String name, int mode) { 148 throw new UnsupportedOperationException(); 149 } 150 151 /** @removed */ 152 @Override 153 public SharedPreferences getSharedPreferences(File file, int mode) { 154 throw new UnsupportedOperationException(); 155 } 156 157 /** @hide */ 158 @Override 159 public void reloadSharedPreferences() { 160 throw new UnsupportedOperationException(); 161 } 162 163 @Override 164 public boolean moveSharedPreferencesFrom(Context sourceContext, String name) { 165 throw new UnsupportedOperationException(); 166 } 167 168 @Override 169 public boolean deleteSharedPreferences(String name) { 170 throw new UnsupportedOperationException(); 171 } 172 173 @Override 174 public FileInputStream openFileInput(String name) throws FileNotFoundException { 175 throw new UnsupportedOperationException(); 176 } 177 178 @Override 179 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException { 180 throw new UnsupportedOperationException(); 181 } 182 183 @Override 184 public boolean deleteFile(String name) { 185 throw new UnsupportedOperationException(); 186 } 187 188 @Override 189 public File getFileStreamPath(String name) { 190 throw new UnsupportedOperationException(); 191 } 192 193 /** @removed */ 194 @Override 195 public File getSharedPreferencesPath(String name) { 196 throw new UnsupportedOperationException(); 197 } 198 199 @Override 200 public String[] fileList() { 201 throw new UnsupportedOperationException(); 202 } 203 204 @Override 205 public File getDataDir() { 206 throw new UnsupportedOperationException(); 207 } 208 209 @Override 210 public File getFilesDir() { 211 throw new UnsupportedOperationException(); 212 } 213 214 @Override 215 public File getNoBackupFilesDir() { 216 throw new UnsupportedOperationException(); 217 } 218 219 @Override 220 public File getExternalFilesDir(String type) { 221 throw new UnsupportedOperationException(); 222 } 223 224 @Override 225 public File getObbDir() { 226 throw new UnsupportedOperationException(); 227 } 228 229 @Override 230 public File getCacheDir() { 231 throw new UnsupportedOperationException(); 232 } 233 234 @Override 235 public File getCodeCacheDir() { 236 throw new UnsupportedOperationException(); 237 } 238 239 @Override 240 public File getExternalCacheDir() { 241 throw new UnsupportedOperationException(); 242 } 243 244 @Override 245 public File getDir(String name, int mode) { 246 throw new UnsupportedOperationException(); 247 } 248 249 @Override 250 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 251 SQLiteDatabase.CursorFactory factory) { 252 throw new UnsupportedOperationException(); 253 } 254 255 @Override 256 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 257 SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) { 258 throw new UnsupportedOperationException(); 259 } 260 261 @Override 262 public File getDatabasePath(String name) { 263 throw new UnsupportedOperationException(); 264 } 265 266 @Override 267 public String[] databaseList() { 268 throw new UnsupportedOperationException(); 269 } 270 271 @Override 272 public boolean moveDatabaseFrom(Context sourceContext, String name) { 273 throw new UnsupportedOperationException(); 274 } 275 276 @Override 277 public boolean deleteDatabase(String name) { 278 throw new UnsupportedOperationException(); 279 } 280 281 @Override 282 public Drawable getWallpaper() { 283 throw new UnsupportedOperationException(); 284 } 285 286 @Override 287 public Drawable peekWallpaper() { 288 throw new UnsupportedOperationException(); 289 } 290 291 @Override 292 public int getWallpaperDesiredMinimumWidth() { 293 throw new UnsupportedOperationException(); 294 } 295 296 @Override 297 public int getWallpaperDesiredMinimumHeight() { 298 throw new UnsupportedOperationException(); 299 } 300 301 @Override 302 public void setWallpaper(Bitmap bitmap) throws IOException { 303 throw new UnsupportedOperationException(); 304 } 305 306 @Override 307 public void setWallpaper(InputStream data) throws IOException { 308 throw new UnsupportedOperationException(); 309 } 310 311 @Override 312 public void clearWallpaper() { 313 throw new UnsupportedOperationException(); 314 } 315 316 @Override 317 public void startActivity(Intent intent) { 318 throw new UnsupportedOperationException(); 319 } 320 321 @Override 322 public void startActivity(Intent intent, Bundle options) { 323 startActivity(intent); 324 } 325 326 @Override 327 public void startActivities(Intent[] intents) { 328 throw new UnsupportedOperationException(); 329 } 330 331 @Override 332 public void startActivities(Intent[] intents, Bundle options) { 333 startActivities(intents); 334 } 335 336 @Override 337 public void startIntentSender(IntentSender intent, 338 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 339 throws IntentSender.SendIntentException { 340 throw new UnsupportedOperationException(); 341 } 342 343 @Override 344 public void startIntentSender(IntentSender intent, 345 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, 346 Bundle options) throws IntentSender.SendIntentException { 347 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags); 348 } 349 350 @Override 351 public void sendBroadcast(Intent intent) { 352 throw new UnsupportedOperationException(); 353 } 354 355 @Override 356 public void sendBroadcast(Intent intent, String receiverPermission) { 357 throw new UnsupportedOperationException(); 358 } 359 360 /** @hide */ 361 @Override 362 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) { 363 throw new UnsupportedOperationException(); 364 } 365 366 /** @hide */ 367 @Override 368 public void sendBroadcastAsUserMultiplePermissions(Intent intent, UserHandle user, 369 String[] receiverPermissions) { 370 throw new UnsupportedOperationException(); 371 } 372 373 /** @hide */ 374 @SystemApi 375 @Override 376 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) { 377 throw new UnsupportedOperationException(); 378 } 379 380 /** @hide */ 381 @Override 382 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) { 383 throw new UnsupportedOperationException(); 384 } 385 386 @Override 387 public void sendOrderedBroadcast(Intent intent, 388 String receiverPermission) { 389 throw new UnsupportedOperationException(); 390 } 391 392 @Override 393 public void sendOrderedBroadcast(Intent intent, String receiverPermission, 394 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 395 Bundle initialExtras) { 396 throw new UnsupportedOperationException(); 397 } 398 399 /** @hide */ 400 @SystemApi 401 @Override 402 public void sendOrderedBroadcast(Intent intent, String receiverPermission, 403 Bundle options, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 404 Bundle initialExtras) { 405 throw new UnsupportedOperationException(); 406 } 407 408 /** @hide */ 409 @Override 410 public void sendOrderedBroadcast(Intent intent, String receiverPermission, int appOp, 411 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 412 Bundle initialExtras) { 413 throw new UnsupportedOperationException(); 414 } 415 416 @Override 417 public void sendBroadcastAsUser(Intent intent, UserHandle user) { 418 throw new UnsupportedOperationException(); 419 } 420 421 @Override 422 public void sendBroadcastAsUser(Intent intent, UserHandle user, 423 String receiverPermission) { 424 throw new UnsupportedOperationException(); 425 } 426 427 /** @hide */ 428 @SystemApi 429 @Override 430 public void sendBroadcastAsUser(Intent intent, UserHandle user, 431 String receiverPermission, Bundle options) { 432 throw new UnsupportedOperationException(); 433 } 434 435 /** @hide */ 436 @Override 437 public void sendBroadcastAsUser(Intent intent, UserHandle user, 438 String receiverPermission, int appOp) { 439 throw new UnsupportedOperationException(); 440 } 441 442 @Override 443 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 444 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, 445 int initialCode, String initialData, Bundle initialExtras) { 446 throw new UnsupportedOperationException(); 447 } 448 449 /** @hide */ 450 @Override 451 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 452 String receiverPermission, int appOp, BroadcastReceiver resultReceiver, 453 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) { 454 throw new UnsupportedOperationException(); 455 } 456 457 /** @hide */ 458 @Override 459 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 460 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver, 461 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) { 462 throw new UnsupportedOperationException(); 463 } 464 465 @Override 466 public void sendStickyBroadcast(Intent intent) { 467 throw new UnsupportedOperationException(); 468 } 469 470 @Override 471 public void sendStickyOrderedBroadcast(Intent intent, 472 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 473 Bundle initialExtras) { 474 throw new UnsupportedOperationException(); 475 } 476 477 @Override 478 public void removeStickyBroadcast(Intent intent) { 479 throw new UnsupportedOperationException(); 480 } 481 482 @Override 483 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { 484 throw new UnsupportedOperationException(); 485 } 486 487 /** @hide */ 488 @Override 489 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) { 490 throw new UnsupportedOperationException(); 491 } 492 493 @Override 494 public void sendStickyOrderedBroadcastAsUser(Intent intent, 495 UserHandle user, BroadcastReceiver resultReceiver, 496 Handler scheduler, int initialCode, String initialData, 497 Bundle initialExtras) { 498 throw new UnsupportedOperationException(); 499 } 500 501 @Override 502 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) { 503 throw new UnsupportedOperationException(); 504 } 505 506 @Override 507 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 508 throw new UnsupportedOperationException(); 509 } 510 511 @Override 512 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, 513 int flags) { 514 throw new UnsupportedOperationException(); 515 } 516 517 @Override 518 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, 519 String broadcastPermission, Handler scheduler) { 520 throw new UnsupportedOperationException(); 521 } 522 523 @Override 524 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, 525 String broadcastPermission, Handler scheduler, int flags) { 526 throw new UnsupportedOperationException(); 527 } 528 529 /** @hide */ 530 @Override 531 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, 532 IntentFilter filter, String broadcastPermission, Handler scheduler) { 533 throw new UnsupportedOperationException(); 534 } 535 536 @Override 537 public void unregisterReceiver(BroadcastReceiver receiver) { 538 throw new UnsupportedOperationException(); 539 } 540 541 @Override 542 public ComponentName startService(Intent service) { 543 throw new UnsupportedOperationException(); 544 } 545 546 @Override 547 public ComponentName startForegroundService(Intent service) { 548 throw new UnsupportedOperationException(); 549 } 550 551 @Override 552 public boolean stopService(Intent service) { 553 throw new UnsupportedOperationException(); 554 } 555 556 /** @hide */ 557 @Override 558 public ComponentName startServiceAsUser(Intent service, UserHandle user) { 559 throw new UnsupportedOperationException(); 560 } 561 562 /** @hide */ 563 @Override 564 public ComponentName startForegroundServiceAsUser(Intent service, UserHandle user) { 565 throw new UnsupportedOperationException(); 566 } 567 568 /** @hide */ 569 @Override 570 public boolean stopServiceAsUser(Intent service, UserHandle user) { 571 throw new UnsupportedOperationException(); 572 } 573 574 @Override 575 public boolean bindService(Intent service, ServiceConnection conn, int flags) { 576 throw new UnsupportedOperationException(); 577 } 578 579 /** @hide */ 580 @Override 581 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, 582 UserHandle user) { 583 throw new UnsupportedOperationException(); 584 } 585 586 @Override 587 public void unbindService(ServiceConnection conn) { 588 throw new UnsupportedOperationException(); 589 } 590 591 @Override 592 public boolean startInstrumentation(ComponentName className, 593 String profileFile, Bundle arguments) { 594 throw new UnsupportedOperationException(); 595 } 596 597 @Override 598 public Object getSystemService(String name) { 599 throw new UnsupportedOperationException(); 600 } 601 602 @Override 603 public String getSystemServiceName(Class<?> serviceClass) { 604 throw new UnsupportedOperationException(); 605 } 606 607 @Override 608 public int checkPermission(String permission, int pid, int uid) { 609 throw new UnsupportedOperationException(); 610 } 611 612 /** @hide */ 613 @Override 614 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) { 615 return checkPermission(permission, pid, uid); 616 } 617 618 @Override 619 public int checkCallingPermission(String permission) { 620 throw new UnsupportedOperationException(); 621 } 622 623 @Override 624 public int checkCallingOrSelfPermission(String permission) { 625 throw new UnsupportedOperationException(); 626 } 627 628 @Override 629 public int checkSelfPermission(String permission) { 630 throw new UnsupportedOperationException(); 631 } 632 633 @Override 634 public void enforcePermission( 635 String permission, int pid, int uid, String message) { 636 throw new UnsupportedOperationException(); 637 } 638 639 @Override 640 public void enforceCallingPermission(String permission, String message) { 641 throw new UnsupportedOperationException(); 642 } 643 644 @Override 645 public void enforceCallingOrSelfPermission(String permission, String message) { 646 throw new UnsupportedOperationException(); 647 } 648 649 @Override 650 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { 651 throw new UnsupportedOperationException(); 652 } 653 654 @Override 655 public void revokeUriPermission(Uri uri, int modeFlags) { 656 throw new UnsupportedOperationException(); 657 } 658 659 @Override 660 public void revokeUriPermission(String targetPackage, Uri uri, int modeFlags) { 661 throw new UnsupportedOperationException(); 662 } 663 664 @Override 665 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 666 throw new UnsupportedOperationException(); 667 } 668 669 /** @hide */ 670 @Override 671 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) { 672 return checkUriPermission(uri, pid, uid, modeFlags); 673 } 674 675 @Override 676 public int checkCallingUriPermission(Uri uri, int modeFlags) { 677 throw new UnsupportedOperationException(); 678 } 679 680 @Override 681 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { 682 throw new UnsupportedOperationException(); 683 } 684 685 @Override 686 public int checkUriPermission(Uri uri, String readPermission, 687 String writePermission, int pid, int uid, int modeFlags) { 688 throw new UnsupportedOperationException(); 689 } 690 691 @Override 692 public void enforceUriPermission( 693 Uri uri, int pid, int uid, int modeFlags, String message) { 694 throw new UnsupportedOperationException(); 695 } 696 697 @Override 698 public void enforceCallingUriPermission( 699 Uri uri, int modeFlags, String message) { 700 throw new UnsupportedOperationException(); 701 } 702 703 @Override 704 public void enforceCallingOrSelfUriPermission( 705 Uri uri, int modeFlags, String message) { 706 throw new UnsupportedOperationException(); 707 } 708 709 public void enforceUriPermission( 710 Uri uri, String readPermission, String writePermission, 711 int pid, int uid, int modeFlags, String message) { 712 throw new UnsupportedOperationException(); 713 } 714 715 @Override 716 public Context createPackageContext(String packageName, int flags) 717 throws PackageManager.NameNotFoundException { 718 throw new UnsupportedOperationException(); 719 } 720 721 /** {@hide} */ 722 @Override 723 public Context createApplicationContext(ApplicationInfo application, int flags) 724 throws PackageManager.NameNotFoundException { 725 return null; 726 } 727 728 /** @hide */ 729 @Override 730 public Context createContextForSplit(String splitName) 731 throws PackageManager.NameNotFoundException { 732 throw new UnsupportedOperationException(); 733 } 734 735 /** {@hide} */ 736 @Override 737 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user) 738 throws PackageManager.NameNotFoundException { 739 throw new UnsupportedOperationException(); 740 } 741 742 /** {@hide} */ 743 @Override 744 public int getUserId() { 745 throw new UnsupportedOperationException(); 746 } 747 748 @Override 749 public Context createConfigurationContext(Configuration overrideConfiguration) { 750 throw new UnsupportedOperationException(); 751 } 752 753 @Override 754 public Context createDisplayContext(Display display) { 755 throw new UnsupportedOperationException(); 756 } 757 758 @Override 759 public boolean isRestricted() { 760 throw new UnsupportedOperationException(); 761 } 762 763 /** @hide */ 764 @Override 765 public DisplayAdjustments getDisplayAdjustments(int displayId) { 766 throw new UnsupportedOperationException(); 767 } 768 769 /** @hide */ 770 @Override 771 public Display getDisplay() { 772 throw new UnsupportedOperationException(); 773 } 774 775 /** @hide */ 776 @Override 777 public void updateDisplay(int displayId) { 778 throw new UnsupportedOperationException(); 779 } 780 781 @Override 782 public File[] getExternalFilesDirs(String type) { 783 throw new UnsupportedOperationException(); 784 } 785 786 @Override 787 public File[] getObbDirs() { 788 throw new UnsupportedOperationException(); 789 } 790 791 @Override 792 public File[] getExternalCacheDirs() { 793 throw new UnsupportedOperationException(); 794 } 795 796 @Override 797 public File[] getExternalMediaDirs() { 798 throw new UnsupportedOperationException(); 799 } 800 801 /** @hide **/ 802 @Override 803 public File getPreloadsFileCache() { throw new UnsupportedOperationException(); } 804 805 @Override 806 public Context createDeviceProtectedStorageContext() { 807 throw new UnsupportedOperationException(); 808 } 809 810 /** {@hide} */ 811 @SystemApi 812 @Override 813 public Context createCredentialProtectedStorageContext() { 814 throw new UnsupportedOperationException(); 815 } 816 817 @Override 818 public boolean isDeviceProtectedStorage() { 819 throw new UnsupportedOperationException(); 820 } 821 822 /** {@hide} */ 823 @SystemApi 824 @Override 825 public boolean isCredentialProtectedStorage() { 826 throw new UnsupportedOperationException(); 827 } 828 829 /** {@hide} */ 830 @Override 831 public boolean canLoadUnsafeResources() { 832 throw new UnsupportedOperationException(); 833 } 834 835 /** {@hide} */ 836 @Override 837 public IBinder getActivityToken() { 838 throw new UnsupportedOperationException(); 839 } 840 841 /** {@hide} */ 842 @Override 843 public IServiceConnection getServiceDispatcher(ServiceConnection conn, Handler handler, 844 int flags) { 845 throw new UnsupportedOperationException(); 846 } 847 848 /** {@hide} */ 849 @Override 850 public IApplicationThread getIApplicationThread() { 851 throw new UnsupportedOperationException(); 852 } 853 854 /** {@hide} */ 855 @Override 856 public Handler getMainThreadHandler() { 857 throw new UnsupportedOperationException(); 858 } 859} 860