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 17package android.test; 18 19import com.google.android.collect.Lists; 20 21import android.accounts.AccountManager; 22import android.accounts.AccountManagerCallback; 23import android.accounts.AccountManagerFuture; 24import android.accounts.AuthenticatorException; 25import android.accounts.OnAccountsUpdateListener; 26import android.accounts.OperationCanceledException; 27import android.accounts.Account; 28import android.content.ContextWrapper; 29import android.content.ContentResolver; 30import android.content.Intent; 31import android.content.Context; 32import android.content.ServiceConnection; 33import android.content.BroadcastReceiver; 34import android.content.IntentFilter; 35import android.content.pm.PackageManager; 36import android.net.Uri; 37import android.os.Handler; 38 39import java.io.File; 40import java.io.IOException; 41import java.util.concurrent.TimeUnit; 42import java.util.concurrent.ExecutionException; 43import java.util.concurrent.TimeoutException; 44import 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 */ 51public 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 sendBroadcast(Intent intent) { 91 mBroadcastIntents.add(intent); 92 } 93 94 @Override 95 public void sendOrderedBroadcast(Intent intent, String receiverPermission) { 96 mBroadcastIntents.add(intent); 97 } 98 99 @Override 100 public int checkUriPermission( 101 Uri uri, String readPermission, String writePermission, int pid, 102 int uid, int modeFlags) { 103 return PackageManager.PERMISSION_GRANTED; 104 } 105 106 @Override 107 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 108 return PackageManager.PERMISSION_GRANTED; 109 } 110 111 @Override 112 public Object getSystemService(String name) { 113 if (Context.ACCOUNT_SERVICE.equals(name)) { 114 return mMockAccountManager; 115 } 116 // No other services exist in this context. 117 return null; 118 } 119 120 private class MockAccountManager extends AccountManager { 121 public MockAccountManager() { 122 super(IsolatedContext.this, null /* IAccountManager */, null /* handler */); 123 } 124 125 public void addOnAccountsUpdatedListener(OnAccountsUpdateListener listener, 126 Handler handler, boolean updateImmediately) { 127 // do nothing 128 } 129 130 public Account[] getAccounts() { 131 return new Account[]{}; 132 } 133 134 public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures( 135 final String type, final String[] features, 136 AccountManagerCallback<Account[]> callback, Handler handler) { 137 return new MockAccountManagerFuture<Account[]>(new Account[0]); 138 } 139 140 public String blockingGetAuthToken(Account account, String authTokenType, 141 boolean notifyAuthFailure) 142 throws OperationCanceledException, IOException, AuthenticatorException { 143 return null; 144 } 145 146 147 /** 148 * A very simple AccountManagerFuture class 149 * that returns what ever was passed in 150 */ 151 private class MockAccountManagerFuture<T> 152 implements AccountManagerFuture<T> { 153 154 T mResult; 155 156 public MockAccountManagerFuture(T result) { 157 mResult = result; 158 } 159 160 public boolean cancel(boolean mayInterruptIfRunning) { 161 return false; 162 } 163 164 public boolean isCancelled() { 165 return false; 166 } 167 168 public boolean isDone() { 169 return true; 170 } 171 172 public T getResult() 173 throws OperationCanceledException, IOException, AuthenticatorException { 174 return mResult; 175 } 176 177 public T getResult(long timeout, TimeUnit unit) 178 throws OperationCanceledException, IOException, AuthenticatorException { 179 return getResult(); 180 } 181 } 182 183 } 184 185 @Override 186 public File getFilesDir() { 187 return new File("/dev/null"); 188 } 189} 190