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