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