| Kenny Root | 10362ab | 2010-03-12 11:13:50 -0800 | [diff] [blame] | 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 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | package android.test; |
| 18 | |
| 19 | import com.google.android.collect.Lists; |
| 20 | |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 21 | import android.accounts.AccountManager; |
| Paul Westbrook | 69120a7 | 2010-02-26 18:21:15 -0800 | [diff] [blame] | 22 | import android.accounts.AccountManagerCallback; |
| 23 | import android.accounts.AccountManagerFuture; |
| 24 | import android.accounts.AuthenticatorException; |
| Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 25 | import android.accounts.OnAccountsUpdateListener; |
| Paul Westbrook | 69120a7 | 2010-02-26 18:21:15 -0800 | [diff] [blame] | 26 | import android.accounts.OperationCanceledException; |
| Cynthia Wong | 904de61 | 2009-09-03 10:06:55 -0700 | [diff] [blame] | 27 | import android.accounts.Account; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 37 | import android.os.Handler; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| Ken Shirriff | 3b95f53 | 2009-07-06 10:45:38 -0700 | [diff] [blame] | 39 | import java.io.File; |
| Paul Westbrook | 69120a7 | 2010-02-26 18:21:15 -0800 | [diff] [blame] | 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 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 54 | private final MockAccountManager mMockAccountManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
| 56 | private List<Intent> mBroadcastIntents = Lists.newArrayList(); |
| 57 | |
| 58 | public IsolatedContext( |
| 59 | ContentResolver resolver, Context targetContext) { |
| 60 | super(targetContext); |
| 61 | mResolver = resolver; |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 62 | mMockAccountManager = new MockAccountManager(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
| Jonas Schwertfeger | d672475 | 2010-09-30 14:04:09 +0200 | [diff] [blame] | 90 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 91 | // Ignore |
| 92 | } |
| 93 | |
| 94 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 118 | if (Context.ACCOUNT_SERVICE.equals(name)) { |
| 119 | return mMockAccountManager; |
| 120 | } |
| 121 | // No other services exist in this context. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | return null; |
| 123 | } |
| 124 | |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 125 | private class MockAccountManager extends AccountManager { |
| 126 | public MockAccountManager() { |
| 127 | super(IsolatedContext.this, null /* IAccountManager */, null /* handler */); |
| 128 | } |
| 129 | |
| Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 130 | public void addOnAccountsUpdatedListener(OnAccountsUpdateListener listener, |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 131 | Handler handler, boolean updateImmediately) { |
| 132 | // do nothing |
| 133 | } |
| Cynthia Wong | 904de61 | 2009-09-03 10:06:55 -0700 | [diff] [blame] | 134 | |
| 135 | public Account[] getAccounts() { |
| 136 | return new Account[]{}; |
| 137 | } |
| Paul Westbrook | 69120a7 | 2010-02-26 18:21:15 -0800 | [diff] [blame] | 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 | |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 188 | } |
| Paul Westbrook | 69120a7 | 2010-02-26 18:21:15 -0800 | [diff] [blame] | 189 | |
| Ken Shirriff | 3b95f53 | 2009-07-06 10:45:38 -0700 | [diff] [blame] | 190 | @Override |
| 191 | public File getFilesDir() { |
| 192 | return new File("/dev/null"); |
| 193 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | } |