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