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