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