The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -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 | |
| 17 | package android.test.mock; |
| 18 | |
Jeff Sharkey | 1307f42 | 2019-11-13 13:03:10 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.ContentProvider; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.IContentProvider; |
| 25 | import android.database.ContentObserver; |
| 26 | import android.net.Uri; |
| 27 | |
Paul Duffin | 8c5a24d | 2017-05-10 13:30:16 +0100 | [diff] [blame] | 28 | import java.util.HashMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import java.util.Map; |
| 30 | |
| 31 | /** |
Joe Malin | 7d433aa | 2010-05-31 14:37:28 -0700 | [diff] [blame] | 32 | * <p> |
| 33 | * An extension of {@link android.content.ContentResolver} that is designed for |
| 34 | * testing. |
| 35 | * </p> |
| 36 | * <p> |
| 37 | * MockContentResolver overrides Android's normal way of resolving providers by |
| 38 | * authority. To have access to a provider based on its authority, users of |
| 39 | * MockContentResolver first instantiate the provider and |
| 40 | * use {@link MockContentResolver#addProvider(String, ContentProvider)}. Resolution of an |
| 41 | * authority occurs entirely within MockContentResolver. |
| 42 | * </p> |
| 43 | * <p> |
| 44 | * Users can also set an authority's entry in the map to null, so that a provider is completely |
| 45 | * mocked out. |
| 46 | * </p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 47 | * |
| 48 | * <div class="special reference"> |
| 49 | * <h3>Developer Guides</h3> |
| 50 | * <p>For more information about application testing, read the |
| 51 | * <a href="{@docRoot}guide/topics/testing/index.html">Testing</a> developer guide.</p> |
| 52 | * </div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | */ |
| 54 | public class MockContentResolver extends ContentResolver { |
| 55 | Map<String, ContentProvider> mProviders; |
| 56 | |
Jeff Sharkey | 66a017b | 2013-01-17 18:18:22 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Creates a local map of providers. This map is used instead of the global |
| 59 | * map when an API call tries to acquire a provider. |
Joe Malin | 7d433aa | 2010-05-31 14:37:28 -0700 | [diff] [blame] | 60 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | public MockContentResolver() { |
Jeff Sharkey | 66a017b | 2013-01-17 18:18:22 -0800 | [diff] [blame] | 62 | this(null); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Creates a local map of providers. This map is used instead of the global |
| 67 | * map when an API call tries to acquire a provider. |
| 68 | */ |
| 69 | public MockContentResolver(Context context) { |
| 70 | super(context); |
Paul Duffin | 8c5a24d | 2017-05-10 13:30:16 +0100 | [diff] [blame] | 71 | mProviders = new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Joe Malin | 7d433aa | 2010-05-31 14:37:28 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Adds access to a provider based on its authority |
| 76 | * |
| 77 | * @param name The authority name associated with the provider. |
| 78 | * @param provider An instance of {@link android.content.ContentProvider} or one of its |
| 79 | * subclasses, or null. |
| 80 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | public void addProvider(String name, ContentProvider provider) { |
Joe Malin | 7d433aa | 2010-05-31 14:37:28 -0700 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * Maps the authority to the provider locally. |
| 85 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | mProviders.put(name, provider); |
| 87 | } |
| 88 | |
| 89 | /** @hide */ |
| 90 | @Override |
| 91 | protected IContentProvider acquireProvider(Context context, String name) { |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 92 | return acquireExistingProvider(context, name); |
| 93 | } |
| 94 | |
| 95 | /** @hide */ |
| 96 | @Override |
| 97 | protected IContentProvider acquireExistingProvider(Context context, String name) { |
Joe Malin | 7d433aa | 2010-05-31 14:37:28 -0700 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Gets the content provider from the local map |
| 101 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | final ContentProvider provider = mProviders.get(name); |
Joe Malin | 7d433aa | 2010-05-31 14:37:28 -0700 | [diff] [blame] | 103 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | if (provider != null) { |
| 105 | return provider.getIContentProvider(); |
| 106 | } else { |
| 107 | return null; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** @hide */ |
| 112 | @Override |
| 113 | public boolean releaseProvider(IContentProvider provider) { |
| 114 | return true; |
| 115 | } |
| 116 | |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 117 | /** @hide */ |
| 118 | @Override |
| 119 | protected IContentProvider acquireUnstableProvider(Context c, String name) { |
| 120 | return acquireProvider(c, name); |
| 121 | } |
| 122 | |
| 123 | /** @hide */ |
| 124 | @Override |
| 125 | public boolean releaseUnstableProvider(IContentProvider icp) { |
| 126 | return releaseProvider(icp); |
| 127 | } |
| 128 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 129 | /** @hide */ |
| 130 | @Override |
| 131 | public void unstableProviderDied(IContentProvider icp) { |
| 132 | } |
| 133 | |
Joe Malin | 7d433aa | 2010-05-31 14:37:28 -0700 | [diff] [blame] | 134 | /** |
Jeff Sharkey | 1307f42 | 2019-11-13 13:03:10 -0700 | [diff] [blame] | 135 | * Overrides the behavior from the parent class to completely ignore any |
| 136 | * content notifications sent to this object. This effectively hides clients |
| 137 | * from observers elsewhere in the system. |
Joe Malin | 7d433aa | 2010-05-31 14:37:28 -0700 | [diff] [blame] | 138 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | @Override |
Jeff Sharkey | 1307f42 | 2019-11-13 13:03:10 -0700 | [diff] [blame] | 140 | public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer) { |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Overrides the behavior from the parent class to completely ignore any |
| 145 | * content notifications sent to this object. This effectively hides clients |
| 146 | * from observers elsewhere in the system. |
| 147 | * |
| 148 | * @deprecated callers should consider migrating to |
| 149 | * {@link #notifyChange(Uri, ContentObserver, int)}, as it |
| 150 | * offers support for many more options than just |
| 151 | * {@link #NOTIFY_SYNC_TO_NETWORK}. |
| 152 | */ |
| 153 | @Override |
| 154 | @Deprecated |
| 155 | public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer, |
Joe Malin | 7d433aa | 2010-05-31 14:37:28 -0700 | [diff] [blame] | 156 | boolean syncToNetwork) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | } |
Jeff Sharkey | 1307f42 | 2019-11-13 13:03:10 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Overrides the behavior from the parent class to completely ignore any |
| 161 | * content notifications sent to this object. This effectively hides clients |
| 162 | * from observers elsewhere in the system. |
| 163 | */ |
| 164 | @Override |
| 165 | public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer, |
| 166 | @NotifyFlags int flags) { |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Overrides the behavior from the parent class to completely ignore any |
| 171 | * content notifications sent to this object. This effectively hides clients |
| 172 | * from observers elsewhere in the system. |
| 173 | */ |
| 174 | @Override |
| 175 | public void notifyChange(@NonNull Iterable<Uri> uris, @Nullable ContentObserver observer, |
| 176 | @NotifyFlags int flags) { |
| 177 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | } |