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