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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.ContextWrapper; |
| 21 | import android.content.ContentResolver; |
| 22 | import android.content.Intent; |
| 23 | import android.content.Context; |
| 24 | import android.content.ServiceConnection; |
| 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.content.pm.PackageManager; |
| 28 | import android.net.Uri; |
Paul Duffin | 20af1df | 2018-01-05 13:52:17 +0000 | [diff] [blame] | 29 | import android.test.mock.MockAccountManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
Ken Shirriff | 3b95f53 | 2009-07-06 10:45:38 -0700 | [diff] [blame] | 31 | import java.io.File; |
Paul Duffin | 8c5a24d | 2017-05-10 13:30:16 +0100 | [diff] [blame] | 32 | import java.util.ArrayList; |
Paul Westbrook | 69120a7 | 2010-02-26 18:21:15 -0800 | [diff] [blame] | 33 | import java.util.List; |
| 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 37 | * 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] | 38 | * 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] | 39 | * |
| 40 | * @deprecated New tests should be written using the |
| 41 | * <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] | 42 | */ |
Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 43 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | public class IsolatedContext extends ContextWrapper { |
| 45 | |
| 46 | private ContentResolver mResolver; |
Paul Duffin | 20af1df | 2018-01-05 13:52:17 +0000 | [diff] [blame] | 47 | private final AccountManager mMockAccountManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Paul Duffin | 8c5a24d | 2017-05-10 13:30:16 +0100 | [diff] [blame] | 49 | private List<Intent> mBroadcastIntents = new ArrayList<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| 51 | public IsolatedContext( |
| 52 | ContentResolver resolver, Context targetContext) { |
| 53 | super(targetContext); |
| 54 | mResolver = resolver; |
Paul Duffin | 20af1df | 2018-01-05 13:52:17 +0000 | [diff] [blame] | 55 | mMockAccountManager = MockAccountManager.newMockAccountManager(IsolatedContext.this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** Returns the list of intents that were broadcast since the last call to this method. */ |
| 59 | public List<Intent> getAndClearBroadcastIntents() { |
| 60 | List<Intent> intents = mBroadcastIntents; |
Paul Duffin | 8c5a24d | 2017-05-10 13:30:16 +0100 | [diff] [blame] | 61 | mBroadcastIntents = new ArrayList<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | return intents; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public ContentResolver getContentResolver() { |
| 67 | // We need to return the real resolver so that MailEngine.makeRight can get to the |
| 68 | // subscribed feeds provider. TODO: mock out subscribed feeds too. |
| 69 | return mResolver; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | @Override |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 78 | public boolean bindIsolatedService(Intent service, ServiceConnection conn, int flags, |
| 79 | String instanceName) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | @Override |
Jonas Schwertfeger | d672475 | 2010-09-30 14:04:09 +0200 | [diff] [blame] | 89 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 90 | // Ignore |
| 91 | } |
| 92 | |
| 93 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | public void sendBroadcast(Intent intent) { |
| 95 | mBroadcastIntents.add(intent); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void sendOrderedBroadcast(Intent intent, String receiverPermission) { |
| 100 | mBroadcastIntents.add(intent); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int checkUriPermission( |
| 105 | Uri uri, String readPermission, String writePermission, int pid, |
| 106 | int uid, int modeFlags) { |
| 107 | return PackageManager.PERMISSION_GRANTED; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
| 112 | return PackageManager.PERMISSION_GRANTED; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Object getSystemService(String name) { |
Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 117 | if (Context.ACCOUNT_SERVICE.equals(name)) { |
| 118 | return mMockAccountManager; |
| 119 | } |
| 120 | // No other services exist in this context. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | return null; |
| 122 | } |
| 123 | |
Ken Shirriff | 3b95f53 | 2009-07-06 10:45:38 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public File getFilesDir() { |
| 126 | return new File("/dev/null"); |
| 127 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | } |