The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | package android.test; |
| 2 | |
| 3 | import com.google.android.collect.Lists; |
| 4 | |
Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 5 | import android.accounts.AccountManager; |
| 6 | import android.accounts.OnAccountsUpdatedListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7 | import android.content.ContextWrapper; |
| 8 | import android.content.ContentResolver; |
| 9 | import android.content.Intent; |
| 10 | import android.content.Context; |
| 11 | import android.content.ServiceConnection; |
| 12 | import android.content.BroadcastReceiver; |
| 13 | import android.content.IntentFilter; |
| 14 | import android.content.pm.PackageManager; |
| 15 | import android.net.Uri; |
Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 16 | import android.os.Handler; |
| 17 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
| 19 | import java.util.List; |
| 20 | |
| 21 | /** |
| 22 | * A mock context which prevents its users from talking to the rest of the device while |
| 23 | * stubbing enough methods to satify code that tries to talk to other packages. |
| 24 | */ |
| 25 | public class IsolatedContext extends ContextWrapper { |
| 26 | |
| 27 | private ContentResolver mResolver; |
Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 28 | private final MockAccountManager mMockAccountManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | private List<Intent> mBroadcastIntents = Lists.newArrayList(); |
| 31 | |
| 32 | public IsolatedContext( |
| 33 | ContentResolver resolver, Context targetContext) { |
| 34 | super(targetContext); |
| 35 | mResolver = resolver; |
Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 36 | mMockAccountManager = new MockAccountManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /** Returns the list of intents that were broadcast since the last call to this method. */ |
| 40 | public List<Intent> getAndClearBroadcastIntents() { |
| 41 | List<Intent> intents = mBroadcastIntents; |
| 42 | mBroadcastIntents = Lists.newArrayList(); |
| 43 | return intents; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public ContentResolver getContentResolver() { |
| 48 | // We need to return the real resolver so that MailEngine.makeRight can get to the |
| 49 | // subscribed feeds provider. TODO: mock out subscribed feeds too. |
| 50 | return mResolver; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void sendBroadcast(Intent intent) { |
| 65 | mBroadcastIntents.add(intent); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void sendOrderedBroadcast(Intent intent, String receiverPermission) { |
| 70 | mBroadcastIntents.add(intent); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public int checkUriPermission( |
| 75 | Uri uri, String readPermission, String writePermission, int pid, |
| 76 | int uid, int modeFlags) { |
| 77 | return PackageManager.PERMISSION_GRANTED; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
| 82 | return PackageManager.PERMISSION_GRANTED; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Object getSystemService(String name) { |
Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 87 | if (Context.ACCOUNT_SERVICE.equals(name)) { |
| 88 | return mMockAccountManager; |
| 89 | } |
| 90 | // No other services exist in this context. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | return null; |
| 92 | } |
| 93 | |
Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 94 | private class MockAccountManager extends AccountManager { |
| 95 | public MockAccountManager() { |
| 96 | super(IsolatedContext.this, null /* IAccountManager */, null /* handler */); |
| 97 | } |
| 98 | |
| 99 | public void addOnAccountsUpdatedListener(OnAccountsUpdatedListener listener, |
| 100 | Handler handler, boolean updateImmediately) { |
| 101 | // do nothing |
| 102 | } |
| 103 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | } |