MockContext.java revision a492c3a7b2c18426fd0cb4d017eacbc368195dc5
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; 43import android.view.Display; 44 45import java.io.File; 46import java.io.FileInputStream; 47import java.io.FileNotFoundException; 48import java.io.FileOutputStream; 49import java.io.IOException; 50import 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 */ 57public 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 @Override 110 public ApplicationInfo getApplicationInfo() { 111 throw new UnsupportedOperationException(); 112 } 113 114 @Override 115 public String getPackageResourcePath() { 116 throw new UnsupportedOperationException(); 117 } 118 119 /** @hide */ 120 @Override 121 public File getSharedPrefsFile(String name) { 122 throw new UnsupportedOperationException(); 123 } 124 125 @Override 126 public String getPackageCodePath() { 127 throw new UnsupportedOperationException(); 128 } 129 130 @Override 131 public SharedPreferences getSharedPreferences(String name, int mode) { 132 throw new UnsupportedOperationException(); 133 } 134 135 @Override 136 public FileInputStream openFileInput(String name) throws FileNotFoundException { 137 throw new UnsupportedOperationException(); 138 } 139 140 @Override 141 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException { 142 throw new UnsupportedOperationException(); 143 } 144 145 @Override 146 public boolean deleteFile(String name) { 147 throw new UnsupportedOperationException(); 148 } 149 150 @Override 151 public File getFileStreamPath(String name) { 152 throw new UnsupportedOperationException(); 153 } 154 155 @Override 156 public String[] fileList() { 157 throw new UnsupportedOperationException(); 158 } 159 160 @Override 161 public File getFilesDir() { 162 throw new UnsupportedOperationException(); 163 } 164 165 @Override 166 public File getExternalFilesDir(String type) { 167 throw new UnsupportedOperationException(); 168 } 169 170 @Override 171 public File getObbDir() { 172 throw new UnsupportedOperationException(); 173 } 174 175 @Override 176 public File getCacheDir() { 177 throw new UnsupportedOperationException(); 178 } 179 180 @Override 181 public File getExternalCacheDir() { 182 throw new UnsupportedOperationException(); 183 } 184 185 @Override 186 public File getDir(String name, int mode) { 187 throw new UnsupportedOperationException(); 188 } 189 190 @Override 191 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 192 SQLiteDatabase.CursorFactory factory) { 193 throw new UnsupportedOperationException(); 194 } 195 196 @Override 197 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 198 SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) { 199 throw new UnsupportedOperationException(); 200 } 201 202 @Override 203 public File getDatabasePath(String name) { 204 throw new UnsupportedOperationException(); 205 } 206 207 @Override 208 public String[] databaseList() { 209 throw new UnsupportedOperationException(); 210 } 211 212 @Override 213 public boolean deleteDatabase(String name) { 214 throw new UnsupportedOperationException(); 215 } 216 217 @Override 218 public Drawable getWallpaper() { 219 throw new UnsupportedOperationException(); 220 } 221 222 @Override 223 public Drawable peekWallpaper() { 224 throw new UnsupportedOperationException(); 225 } 226 227 @Override 228 public int getWallpaperDesiredMinimumWidth() { 229 throw new UnsupportedOperationException(); 230 } 231 232 @Override 233 public int getWallpaperDesiredMinimumHeight() { 234 throw new UnsupportedOperationException(); 235 } 236 237 @Override 238 public void setWallpaper(Bitmap bitmap) throws IOException { 239 throw new UnsupportedOperationException(); 240 } 241 242 @Override 243 public void setWallpaper(InputStream data) throws IOException { 244 throw new UnsupportedOperationException(); 245 } 246 247 @Override 248 public void clearWallpaper() { 249 throw new UnsupportedOperationException(); 250 } 251 252 @Override 253 public void startActivity(Intent intent) { 254 throw new UnsupportedOperationException(); 255 } 256 257 @Override 258 public void startActivity(Intent intent, Bundle options) { 259 startActivity(intent); 260 } 261 262 @Override 263 public void startActivities(Intent[] intents) { 264 throw new UnsupportedOperationException(); 265 } 266 267 @Override 268 public void startActivities(Intent[] intents, Bundle options) { 269 startActivities(intents); 270 } 271 272 @Override 273 public void startIntentSender(IntentSender intent, 274 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 275 throws IntentSender.SendIntentException { 276 throw new UnsupportedOperationException(); 277 } 278 279 @Override 280 public void startIntentSender(IntentSender intent, 281 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, 282 Bundle options) throws IntentSender.SendIntentException { 283 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags); 284 } 285 286 @Override 287 public void sendBroadcast(Intent intent) { 288 throw new UnsupportedOperationException(); 289 } 290 291 @Override 292 public void sendBroadcast(Intent intent, String receiverPermission) { 293 throw new UnsupportedOperationException(); 294 } 295 296 @Override 297 public void sendOrderedBroadcast(Intent intent, 298 String receiverPermission) { 299 throw new UnsupportedOperationException(); 300 } 301 302 @Override 303 public void sendOrderedBroadcast(Intent intent, String receiverPermission, 304 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 305 Bundle initialExtras) { 306 throw new UnsupportedOperationException(); 307 } 308 309 @Override 310 public void sendBroadcastAsUser(Intent intent, UserHandle user) { 311 throw new UnsupportedOperationException(); 312 } 313 314 @Override 315 public void sendBroadcastAsUser(Intent intent, UserHandle user, 316 String receiverPermission) { 317 throw new UnsupportedOperationException(); 318 } 319 320 @Override 321 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, 322 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, 323 int initialCode, String initialData, Bundle initialExtras) { 324 throw new UnsupportedOperationException(); 325 } 326 327 @Override 328 public void sendStickyBroadcast(Intent intent) { 329 throw new UnsupportedOperationException(); 330 } 331 332 @Override 333 public void sendStickyOrderedBroadcast(Intent intent, 334 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 335 Bundle initialExtras) { 336 throw new UnsupportedOperationException(); 337 } 338 339 @Override 340 public void removeStickyBroadcast(Intent intent) { 341 throw new UnsupportedOperationException(); 342 } 343 344 @Override 345 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { 346 throw new UnsupportedOperationException(); 347 } 348 349 @Override 350 public void sendStickyOrderedBroadcastAsUser(Intent intent, 351 UserHandle user, BroadcastReceiver resultReceiver, 352 Handler scheduler, int initialCode, String initialData, 353 Bundle initialExtras) { 354 throw new UnsupportedOperationException(); 355 } 356 357 @Override 358 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) { 359 throw new UnsupportedOperationException(); 360 } 361 362 @Override 363 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 364 throw new UnsupportedOperationException(); 365 } 366 367 @Override 368 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, 369 String broadcastPermission, Handler scheduler) { 370 throw new UnsupportedOperationException(); 371 } 372 373 @Override 374 public void unregisterReceiver(BroadcastReceiver receiver) { 375 throw new UnsupportedOperationException(); 376 } 377 378 @Override 379 public ComponentName startService(Intent service) { 380 throw new UnsupportedOperationException(); 381 } 382 383 @Override 384 public boolean stopService(Intent service) { 385 throw new UnsupportedOperationException(); 386 } 387 388 /** @hide */ 389 @Override 390 public ComponentName startServiceAsUser(Intent service, UserHandle user) { 391 throw new UnsupportedOperationException(); 392 } 393 394 /** @hide */ 395 @Override 396 public boolean stopServiceAsUser(Intent service, UserHandle user) { 397 throw new UnsupportedOperationException(); 398 } 399 400 @Override 401 public boolean bindService(Intent service, ServiceConnection conn, int flags) { 402 throw new UnsupportedOperationException(); 403 } 404 405 /** @hide */ 406 @Override 407 public boolean bindService(Intent service, ServiceConnection conn, int flags, int userId) { 408 throw new UnsupportedOperationException(); 409 } 410 411 @Override 412 public void unbindService(ServiceConnection conn) { 413 throw new UnsupportedOperationException(); 414 } 415 416 @Override 417 public boolean startInstrumentation(ComponentName className, 418 String profileFile, Bundle arguments) { 419 throw new UnsupportedOperationException(); 420 } 421 422 @Override 423 public Object getSystemService(String name) { 424 throw new UnsupportedOperationException(); 425 } 426 427 @Override 428 public int checkPermission(String permission, int pid, int uid) { 429 throw new UnsupportedOperationException(); 430 } 431 432 @Override 433 public int checkCallingPermission(String permission) { 434 throw new UnsupportedOperationException(); 435 } 436 437 @Override 438 public int checkCallingOrSelfPermission(String permission) { 439 throw new UnsupportedOperationException(); 440 } 441 442 @Override 443 public void enforcePermission( 444 String permission, int pid, int uid, String message) { 445 throw new UnsupportedOperationException(); 446 } 447 448 @Override 449 public void enforceCallingPermission(String permission, String message) { 450 throw new UnsupportedOperationException(); 451 } 452 453 @Override 454 public void enforceCallingOrSelfPermission(String permission, String message) { 455 throw new UnsupportedOperationException(); 456 } 457 458 @Override 459 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { 460 throw new UnsupportedOperationException(); 461 } 462 463 @Override 464 public void revokeUriPermission(Uri uri, int modeFlags) { 465 throw new UnsupportedOperationException(); 466 } 467 468 @Override 469 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 470 throw new UnsupportedOperationException(); 471 } 472 473 @Override 474 public int checkCallingUriPermission(Uri uri, int modeFlags) { 475 throw new UnsupportedOperationException(); 476 } 477 478 @Override 479 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { 480 throw new UnsupportedOperationException(); 481 } 482 483 @Override 484 public int checkUriPermission(Uri uri, String readPermission, 485 String writePermission, int pid, int uid, int modeFlags) { 486 throw new UnsupportedOperationException(); 487 } 488 489 @Override 490 public void enforceUriPermission( 491 Uri uri, int pid, int uid, int modeFlags, String message) { 492 throw new UnsupportedOperationException(); 493 } 494 495 @Override 496 public void enforceCallingUriPermission( 497 Uri uri, int modeFlags, String message) { 498 throw new UnsupportedOperationException(); 499 } 500 501 @Override 502 public void enforceCallingOrSelfUriPermission( 503 Uri uri, int modeFlags, String message) { 504 throw new UnsupportedOperationException(); 505 } 506 507 public void enforceUriPermission( 508 Uri uri, String readPermission, String writePermission, 509 int pid, int uid, int modeFlags, String message) { 510 throw new UnsupportedOperationException(); 511 } 512 513 @Override 514 public Context createPackageContext(String packageName, int flags) 515 throws PackageManager.NameNotFoundException { 516 throw new UnsupportedOperationException(); 517 } 518 519 @Override 520 public Context createConfigurationContext(Configuration overrideConfiguration) { 521 throw new UnsupportedOperationException(); 522 } 523 524 @Override 525 public Context createDisplayContext(Display display) { 526 throw new UnsupportedOperationException(); 527 } 528 529 @Override 530 public boolean isRestricted() { 531 throw new UnsupportedOperationException(); 532 } 533 534 /** @hide */ 535 @Override 536 public CompatibilityInfoHolder getCompatibilityInfo(int displayId) { 537 throw new UnsupportedOperationException(); 538 } 539} 540