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