1 package android.test; 2 3 import com.google.android.collect.Lists; 4 5 import android.accounts.AccountManager; 6 import android.accounts.OnAccountsUpdateListener; 7 import android.accounts.Account; 8 import android.content.ContextWrapper; 9 import android.content.ContentResolver; 10 import android.content.Intent; 11 import android.content.Context; 12 import android.content.ServiceConnection; 13 import android.content.BroadcastReceiver; 14 import android.content.IntentFilter; 15 import android.content.pm.PackageManager; 16 import android.net.Uri; 17 import android.os.Handler; 18 19 import java.util.List; 20 import java.io.File; 21 22 /** 23 * A mock context which prevents its users from talking to the rest of the device while 24 * stubbing enough methods to satify code that tries to talk to other packages. 25 */ 26 public class IsolatedContext extends ContextWrapper { 27 28 private ContentResolver mResolver; 29 private final MockAccountManager mMockAccountManager; 30 31 private List<Intent> mBroadcastIntents = Lists.newArrayList(); 32 IsolatedContext( ContentResolver resolver, Context targetContext)33 public IsolatedContext( 34 ContentResolver resolver, Context targetContext) { 35 super(targetContext); 36 mResolver = resolver; 37 mMockAccountManager = new MockAccountManager(); 38 } 39 40 /** Returns the list of intents that were broadcast since the last call to this method. */ getAndClearBroadcastIntents()41 public List<Intent> getAndClearBroadcastIntents() { 42 List<Intent> intents = mBroadcastIntents; 43 mBroadcastIntents = Lists.newArrayList(); 44 return intents; 45 } 46 47 @Override getContentResolver()48 public ContentResolver getContentResolver() { 49 // We need to return the real resolver so that MailEngine.makeRight can get to the 50 // subscribed feeds provider. TODO: mock out subscribed feeds too. 51 return mResolver; 52 } 53 54 @Override bindService(Intent service, ServiceConnection conn, int flags)55 public boolean bindService(Intent service, ServiceConnection conn, int flags) { 56 return false; 57 } 58 59 @Override registerReceiver(BroadcastReceiver receiver, IntentFilter filter)60 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 61 return null; 62 } 63 64 @Override sendBroadcast(Intent intent)65 public void sendBroadcast(Intent intent) { 66 mBroadcastIntents.add(intent); 67 } 68 69 @Override sendOrderedBroadcast(Intent intent, String receiverPermission)70 public void sendOrderedBroadcast(Intent intent, String receiverPermission) { 71 mBroadcastIntents.add(intent); 72 } 73 74 @Override checkUriPermission( Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags)75 public int checkUriPermission( 76 Uri uri, String readPermission, String writePermission, int pid, 77 int uid, int modeFlags) { 78 return PackageManager.PERMISSION_GRANTED; 79 } 80 81 @Override checkUriPermission(Uri uri, int pid, int uid, int modeFlags)82 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 83 return PackageManager.PERMISSION_GRANTED; 84 } 85 86 @Override getSystemService(String name)87 public Object getSystemService(String name) { 88 if (Context.ACCOUNT_SERVICE.equals(name)) { 89 return mMockAccountManager; 90 } 91 // No other services exist in this context. 92 return null; 93 } 94 95 private class MockAccountManager extends AccountManager { MockAccountManager()96 public MockAccountManager() { 97 super(IsolatedContext.this, null /* IAccountManager */, null /* handler */); 98 } 99 addOnAccountsUpdatedListener(OnAccountsUpdateListener listener, Handler handler, boolean updateImmediately)100 public void addOnAccountsUpdatedListener(OnAccountsUpdateListener listener, 101 Handler handler, boolean updateImmediately) { 102 // do nothing 103 } 104 getAccounts()105 public Account[] getAccounts() { 106 return new Account[]{}; 107 } 108 } 109 @Override getFilesDir()110 public File getFilesDir() { 111 return new File("/dev/null"); 112 } 113 } 114