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