MockContext.java revision 756220bd1912535840388a6743830d2e59ad4964
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; 41 42import java.io.File; 43import java.io.FileInputStream; 44import java.io.FileNotFoundException; 45import java.io.FileOutputStream; 46import java.io.IOException; 47import java.io.InputStream; 48 49/** 50 * A mock {@link android.content.Context} class. All methods are non-functional and throw 51 * {@link java.lang.UnsupportedOperationException}. You can use this to inject other dependencies, 52 * mocks, or monitors into the classes you are testing. 53 */ 54public class MockContext extends Context { 55 56 @Override 57 public AssetManager getAssets() { 58 throw new UnsupportedOperationException(); 59 } 60 61 @Override 62 public Resources getResources() { 63 throw new UnsupportedOperationException(); 64 } 65 66 @Override 67 public PackageManager getPackageManager() { 68 throw new UnsupportedOperationException(); 69 } 70 71 @Override 72 public ContentResolver getContentResolver() { 73 throw new UnsupportedOperationException(); 74 } 75 76 @Override 77 public Looper getMainLooper() { 78 throw new UnsupportedOperationException(); 79 } 80 81 @Override 82 public Context getApplicationContext() { 83 throw new UnsupportedOperationException(); 84 } 85 86 @Override 87 public void setTheme(int resid) { 88 throw new UnsupportedOperationException(); 89 } 90 91 @Override 92 public Resources.Theme getTheme() { 93 throw new UnsupportedOperationException(); 94 } 95 96 @Override 97 public ClassLoader getClassLoader() { 98 throw new UnsupportedOperationException(); 99 } 100 101 @Override 102 public String getPackageName() { 103 throw new UnsupportedOperationException(); 104 } 105 106 @Override 107 public ApplicationInfo getApplicationInfo() { 108 throw new UnsupportedOperationException(); 109 } 110 111 @Override 112 public String getPackageResourcePath() { 113 throw new UnsupportedOperationException(); 114 } 115 116 /** @hide */ 117 @Override 118 public File getSharedPrefsFile(String name) { 119 throw new UnsupportedOperationException(); 120 } 121 122 @Override 123 public String getPackageCodePath() { 124 throw new UnsupportedOperationException(); 125 } 126 127 @Override 128 public SharedPreferences getSharedPreferences(String name, int mode) { 129 throw new UnsupportedOperationException(); 130 } 131 132 @Override 133 public FileInputStream openFileInput(String name) throws FileNotFoundException { 134 throw new UnsupportedOperationException(); 135 } 136 137 @Override 138 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException { 139 throw new UnsupportedOperationException(); 140 } 141 142 @Override 143 public boolean deleteFile(String name) { 144 throw new UnsupportedOperationException(); 145 } 146 147 @Override 148 public File getFileStreamPath(String name) { 149 throw new UnsupportedOperationException(); 150 } 151 152 @Override 153 public String[] fileList() { 154 throw new UnsupportedOperationException(); 155 } 156 157 @Override 158 public File getFilesDir() { 159 throw new UnsupportedOperationException(); 160 } 161 162 @Override 163 public File getExternalFilesDir(String type) { 164 throw new UnsupportedOperationException(); 165 } 166 167 @Override 168 public File getObbDir() { 169 throw new UnsupportedOperationException(); 170 } 171 172 @Override 173 public File getCacheDir() { 174 throw new UnsupportedOperationException(); 175 } 176 177 @Override 178 public File getExternalCacheDir() { 179 throw new UnsupportedOperationException(); 180 } 181 182 @Override 183 public File getDir(String name, int mode) { 184 throw new UnsupportedOperationException(); 185 } 186 187 @Override 188 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 189 SQLiteDatabase.CursorFactory factory) { 190 throw new UnsupportedOperationException(); 191 } 192 193 @Override 194 public SQLiteDatabase openOrCreateDatabase(String file, int mode, 195 SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) { 196 throw new UnsupportedOperationException(); 197 } 198 199 @Override 200 public File getDatabasePath(String name) { 201 throw new UnsupportedOperationException(); 202 } 203 204 @Override 205 public String[] databaseList() { 206 throw new UnsupportedOperationException(); 207 } 208 209 @Override 210 public boolean deleteDatabase(String name) { 211 throw new UnsupportedOperationException(); 212 } 213 214 @Override 215 public Drawable getWallpaper() { 216 throw new UnsupportedOperationException(); 217 } 218 219 @Override 220 public Drawable peekWallpaper() { 221 throw new UnsupportedOperationException(); 222 } 223 224 @Override 225 public int getWallpaperDesiredMinimumWidth() { 226 throw new UnsupportedOperationException(); 227 } 228 229 @Override 230 public int getWallpaperDesiredMinimumHeight() { 231 throw new UnsupportedOperationException(); 232 } 233 234 @Override 235 public void setWallpaper(Bitmap bitmap) throws IOException { 236 throw new UnsupportedOperationException(); 237 } 238 239 @Override 240 public void setWallpaper(InputStream data) throws IOException { 241 throw new UnsupportedOperationException(); 242 } 243 244 @Override 245 public void clearWallpaper() { 246 throw new UnsupportedOperationException(); 247 } 248 249 @Override 250 public void startActivity(Intent intent) { 251 throw new UnsupportedOperationException(); 252 } 253 254 @Override 255 public void startActivity(Intent intent, Bundle options) { 256 startActivity(intent); 257 } 258 259 @Override 260 public void startActivities(Intent[] intents) { 261 throw new UnsupportedOperationException(); 262 } 263 264 @Override 265 public void startActivities(Intent[] intents, Bundle options) { 266 startActivities(intents); 267 } 268 269 @Override 270 public void startIntentSender(IntentSender intent, 271 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 272 throws IntentSender.SendIntentException { 273 throw new UnsupportedOperationException(); 274 } 275 276 @Override 277 public void startIntentSender(IntentSender intent, 278 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, 279 Bundle options) throws IntentSender.SendIntentException { 280 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags); 281 } 282 283 @Override 284 public void sendBroadcast(Intent intent) { 285 throw new UnsupportedOperationException(); 286 } 287 288 @Override 289 public void sendBroadcast(Intent intent, String receiverPermission) { 290 throw new UnsupportedOperationException(); 291 } 292 293 @Override 294 public void sendOrderedBroadcast(Intent intent, 295 String receiverPermission) { 296 throw new UnsupportedOperationException(); 297 } 298 299 @Override 300 public void sendOrderedBroadcast(Intent intent, String receiverPermission, 301 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 302 Bundle initialExtras) { 303 throw new UnsupportedOperationException(); 304 } 305 306 @Override 307 public void sendBroadcastToUser(Intent intent, int userId) { 308 throw new UnsupportedOperationException(); 309 } 310 311 @Override 312 public void sendOrderedBroadcastToUser(Intent intent, int userId, 313 BroadcastReceiver resultReceiver, Handler scheduler, 314 int initialCode, String initialData, Bundle initialExtras) { 315 throw new UnsupportedOperationException(); 316 } 317 318 @Override 319 public void sendStickyBroadcast(Intent intent) { 320 throw new UnsupportedOperationException(); 321 } 322 323 @Override 324 public void sendStickyOrderedBroadcast(Intent intent, 325 BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, 326 Bundle initialExtras) { 327 throw new UnsupportedOperationException(); 328 } 329 330 @Override 331 public void removeStickyBroadcast(Intent intent) { 332 throw new UnsupportedOperationException(); 333 } 334 335 @Override 336 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 337 throw new UnsupportedOperationException(); 338 } 339 340 @Override 341 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, 342 String broadcastPermission, Handler scheduler) { 343 throw new UnsupportedOperationException(); 344 } 345 346 @Override 347 public void unregisterReceiver(BroadcastReceiver receiver) { 348 throw new UnsupportedOperationException(); 349 } 350 351 @Override 352 public ComponentName startService(Intent service) { 353 throw new UnsupportedOperationException(); 354 } 355 356 @Override 357 public boolean stopService(Intent service) { 358 throw new UnsupportedOperationException(); 359 } 360 361 @Override 362 public boolean bindService(Intent service, ServiceConnection conn, int flags) { 363 throw new UnsupportedOperationException(); 364 } 365 366 /** @hide */ 367 @Override 368 public boolean bindService(Intent service, ServiceConnection conn, int flags, int userId) { 369 throw new UnsupportedOperationException(); 370 } 371 372 @Override 373 public void unbindService(ServiceConnection conn) { 374 throw new UnsupportedOperationException(); 375 } 376 377 @Override 378 public boolean startInstrumentation(ComponentName className, 379 String profileFile, Bundle arguments) { 380 throw new UnsupportedOperationException(); 381 } 382 383 @Override 384 public Object getSystemService(String name) { 385 throw new UnsupportedOperationException(); 386 } 387 388 @Override 389 public int checkPermission(String permission, int pid, int uid) { 390 throw new UnsupportedOperationException(); 391 } 392 393 @Override 394 public int checkCallingPermission(String permission) { 395 throw new UnsupportedOperationException(); 396 } 397 398 @Override 399 public int checkCallingOrSelfPermission(String permission) { 400 throw new UnsupportedOperationException(); 401 } 402 403 @Override 404 public void enforcePermission( 405 String permission, int pid, int uid, String message) { 406 throw new UnsupportedOperationException(); 407 } 408 409 @Override 410 public void enforceCallingPermission(String permission, String message) { 411 throw new UnsupportedOperationException(); 412 } 413 414 @Override 415 public void enforceCallingOrSelfPermission(String permission, String message) { 416 throw new UnsupportedOperationException(); 417 } 418 419 @Override 420 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { 421 throw new UnsupportedOperationException(); 422 } 423 424 @Override 425 public void revokeUriPermission(Uri uri, int modeFlags) { 426 throw new UnsupportedOperationException(); 427 } 428 429 @Override 430 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 431 throw new UnsupportedOperationException(); 432 } 433 434 @Override 435 public int checkCallingUriPermission(Uri uri, int modeFlags) { 436 throw new UnsupportedOperationException(); 437 } 438 439 @Override 440 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { 441 throw new UnsupportedOperationException(); 442 } 443 444 @Override 445 public int checkUriPermission(Uri uri, String readPermission, 446 String writePermission, int pid, int uid, int modeFlags) { 447 throw new UnsupportedOperationException(); 448 } 449 450 @Override 451 public void enforceUriPermission( 452 Uri uri, int pid, int uid, int modeFlags, String message) { 453 throw new UnsupportedOperationException(); 454 } 455 456 @Override 457 public void enforceCallingUriPermission( 458 Uri uri, int modeFlags, String message) { 459 throw new UnsupportedOperationException(); 460 } 461 462 @Override 463 public void enforceCallingOrSelfUriPermission( 464 Uri uri, int modeFlags, String message) { 465 throw new UnsupportedOperationException(); 466 } 467 468 public void enforceUriPermission( 469 Uri uri, String readPermission, String writePermission, 470 int pid, int uid, int modeFlags, String message) { 471 throw new UnsupportedOperationException(); 472 } 473 474 @Override 475 public Context createPackageContext(String packageName, int flags) 476 throws PackageManager.NameNotFoundException { 477 throw new UnsupportedOperationException(); 478 } 479 480 @Override 481 public Context createConfigurationContext(Configuration overrideConfiguration) { 482 throw new UnsupportedOperationException(); 483 } 484 485 @Override 486 public boolean isRestricted() { 487 throw new UnsupportedOperationException(); 488 } 489} 490