IsolatedContext.java revision 69120a73d0f7a1862d51e0a95ebd5e507fee8cd2
1package android.test; 2 3import com.google.android.collect.Lists; 4 5import android.accounts.AccountManager; 6import android.accounts.AccountManagerCallback; 7import android.accounts.AccountManagerFuture; 8import android.accounts.AuthenticatorException; 9import android.accounts.OnAccountsUpdateListener; 10import android.accounts.OperationCanceledException; 11import android.accounts.Account; 12import android.content.ContextWrapper; 13import android.content.ContentResolver; 14import android.content.Intent; 15import android.content.Context; 16import android.content.ServiceConnection; 17import android.content.BroadcastReceiver; 18import android.content.IntentFilter; 19import android.content.pm.PackageManager; 20import android.net.Uri; 21import android.os.Handler; 22 23import java.io.File; 24import java.io.IOException; 25import java.util.concurrent.TimeUnit; 26import java.util.concurrent.ExecutionException; 27import java.util.concurrent.TimeoutException; 28import java.util.List; 29 30 31/** 32 * A mock context which prevents its users from talking to the rest of the device while 33 * stubbing enough methods to satify code that tries to talk to other packages. 34 */ 35public class IsolatedContext extends ContextWrapper { 36 37 private ContentResolver mResolver; 38 private final MockAccountManager mMockAccountManager; 39 40 private List<Intent> mBroadcastIntents = Lists.newArrayList(); 41 42 public IsolatedContext( 43 ContentResolver resolver, Context targetContext) { 44 super(targetContext); 45 mResolver = resolver; 46 mMockAccountManager = new MockAccountManager(); 47 } 48 49 /** Returns the list of intents that were broadcast since the last call to this method. */ 50 public List<Intent> getAndClearBroadcastIntents() { 51 List<Intent> intents = mBroadcastIntents; 52 mBroadcastIntents = Lists.newArrayList(); 53 return intents; 54 } 55 56 @Override 57 public ContentResolver getContentResolver() { 58 // We need to return the real resolver so that MailEngine.makeRight can get to the 59 // subscribed feeds provider. TODO: mock out subscribed feeds too. 60 return mResolver; 61 } 62 63 @Override 64 public boolean bindService(Intent service, ServiceConnection conn, int flags) { 65 return false; 66 } 67 68 @Override 69 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 70 return null; 71 } 72 73 @Override 74 public void sendBroadcast(Intent intent) { 75 mBroadcastIntents.add(intent); 76 } 77 78 @Override 79 public void sendOrderedBroadcast(Intent intent, String receiverPermission) { 80 mBroadcastIntents.add(intent); 81 } 82 83 @Override 84 public int checkUriPermission( 85 Uri uri, String readPermission, String writePermission, int pid, 86 int uid, int modeFlags) { 87 return PackageManager.PERMISSION_GRANTED; 88 } 89 90 @Override 91 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 92 return PackageManager.PERMISSION_GRANTED; 93 } 94 95 @Override 96 public Object getSystemService(String name) { 97 if (Context.ACCOUNT_SERVICE.equals(name)) { 98 return mMockAccountManager; 99 } 100 // No other services exist in this context. 101 return null; 102 } 103 104 private class MockAccountManager extends AccountManager { 105 public MockAccountManager() { 106 super(IsolatedContext.this, null /* IAccountManager */, null /* handler */); 107 } 108 109 public void addOnAccountsUpdatedListener(OnAccountsUpdateListener listener, 110 Handler handler, boolean updateImmediately) { 111 // do nothing 112 } 113 114 public Account[] getAccounts() { 115 return new Account[]{}; 116 } 117 118 public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures( 119 final String type, final String[] features, 120 AccountManagerCallback<Account[]> callback, Handler handler) { 121 return new MockAccountManagerFuture<Account[]>(new Account[0]); 122 } 123 124 public String blockingGetAuthToken(Account account, String authTokenType, 125 boolean notifyAuthFailure) 126 throws OperationCanceledException, IOException, AuthenticatorException { 127 return null; 128 } 129 130 131 /** 132 * A very simple AccountManagerFuture class 133 * that returns what ever was passed in 134 */ 135 private class MockAccountManagerFuture<T> 136 implements AccountManagerFuture<T> { 137 138 T mResult; 139 140 public MockAccountManagerFuture(T result) { 141 mResult = result; 142 } 143 144 public boolean cancel(boolean mayInterruptIfRunning) { 145 return false; 146 } 147 148 public boolean isCancelled() { 149 return false; 150 } 151 152 public boolean isDone() { 153 return true; 154 } 155 156 public T getResult() 157 throws OperationCanceledException, IOException, AuthenticatorException { 158 return mResult; 159 } 160 161 public T getResult(long timeout, TimeUnit unit) 162 throws OperationCanceledException, IOException, AuthenticatorException { 163 return getResult(); 164 } 165 } 166 167 } 168 169 @Override 170 public File getFilesDir() { 171 return new File("/dev/null"); 172 } 173} 174