| 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; |
| Paul Westbrook | 69120a7 | 2010-02-26 18:21:15 -0800 | [diff] [blame] | 42 | import java.util.List; |
| 43 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame^] | 46 | * A mock context which prevents its users from talking to the rest of the device while |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | * stubbing enough methods to satify code that tries to talk to other packages. |
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame^] | 48 | * |
| 49 | * @deprecated New tests should be written using the |
| 50 | * <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a>. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | */ |
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame^] | 52 | @Deprecated |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | public class IsolatedContext extends ContextWrapper { |
| 54 | |
| 55 | private ContentResolver mResolver; |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 56 | private final MockAccountManager mMockAccountManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | |
| 58 | private List<Intent> mBroadcastIntents = Lists.newArrayList(); |
| 59 | |
| 60 | public IsolatedContext( |
| 61 | ContentResolver resolver, Context targetContext) { |
| 62 | super(targetContext); |
| 63 | mResolver = resolver; |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 64 | mMockAccountManager = new MockAccountManager(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** Returns the list of intents that were broadcast since the last call to this method. */ |
| 68 | public List<Intent> getAndClearBroadcastIntents() { |
| 69 | List<Intent> intents = mBroadcastIntents; |
| 70 | mBroadcastIntents = Lists.newArrayList(); |
| 71 | return intents; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public ContentResolver getContentResolver() { |
| 76 | // We need to return the real resolver so that MailEngine.makeRight can get to the |
| 77 | // subscribed feeds provider. TODO: mock out subscribed feeds too. |
| 78 | return mResolver; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | @Override |
| Jonas Schwertfeger | d672475 | 2010-09-30 14:04:09 +0200 | [diff] [blame] | 92 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 93 | // Ignore |
| 94 | } |
| 95 | |
| 96 | @Override |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | public void sendBroadcast(Intent intent) { |
| 98 | mBroadcastIntents.add(intent); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void sendOrderedBroadcast(Intent intent, String receiverPermission) { |
| 103 | mBroadcastIntents.add(intent); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int checkUriPermission( |
| 108 | Uri uri, String readPermission, String writePermission, int pid, |
| 109 | int uid, int modeFlags) { |
| 110 | return PackageManager.PERMISSION_GRANTED; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
| 115 | return PackageManager.PERMISSION_GRANTED; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Object getSystemService(String name) { |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 120 | if (Context.ACCOUNT_SERVICE.equals(name)) { |
| 121 | return mMockAccountManager; |
| 122 | } |
| 123 | // No other services exist in this context. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | return null; |
| 125 | } |
| 126 | |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 127 | private class MockAccountManager extends AccountManager { |
| 128 | public MockAccountManager() { |
| 129 | super(IsolatedContext.this, null /* IAccountManager */, null /* handler */); |
| 130 | } |
| 131 | |
| Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 132 | public void addOnAccountsUpdatedListener(OnAccountsUpdateListener listener, |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 133 | Handler handler, boolean updateImmediately) { |
| 134 | // do nothing |
| 135 | } |
| Cynthia Wong | 904de61 | 2009-09-03 10:06:55 -0700 | [diff] [blame] | 136 | |
| 137 | public Account[] getAccounts() { |
| 138 | return new Account[]{}; |
| 139 | } |
| Paul Westbrook | 69120a7 | 2010-02-26 18:21:15 -0800 | [diff] [blame] | 140 | |
| 141 | public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures( |
| 142 | final String type, final String[] features, |
| 143 | AccountManagerCallback<Account[]> callback, Handler handler) { |
| 144 | return new MockAccountManagerFuture<Account[]>(new Account[0]); |
| 145 | } |
| 146 | |
| 147 | public String blockingGetAuthToken(Account account, String authTokenType, |
| 148 | boolean notifyAuthFailure) |
| 149 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * A very simple AccountManagerFuture class |
| 156 | * that returns what ever was passed in |
| 157 | */ |
| 158 | private class MockAccountManagerFuture<T> |
| 159 | implements AccountManagerFuture<T> { |
| 160 | |
| 161 | T mResult; |
| 162 | |
| 163 | public MockAccountManagerFuture(T result) { |
| 164 | mResult = result; |
| 165 | } |
| 166 | |
| 167 | public boolean cancel(boolean mayInterruptIfRunning) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | public boolean isCancelled() { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | public boolean isDone() { |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | public T getResult() |
| 180 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 181 | return mResult; |
| 182 | } |
| 183 | |
| 184 | public T getResult(long timeout, TimeUnit unit) |
| 185 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 186 | return getResult(); |
| 187 | } |
| 188 | } |
| 189 | |
| Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 190 | } |
| Paul Westbrook | 69120a7 | 2010-02-26 18:21:15 -0800 | [diff] [blame] | 191 | |
| Ken Shirriff | 3b95f53 | 2009-07-06 10:45:38 -0700 | [diff] [blame] | 192 | @Override |
| 193 | public File getFilesDir() { |
| 194 | return new File("/dev/null"); |
| 195 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } |