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