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