blob: aec6c772f3a5440b1c161856fe386157846a6e01 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.test.mock;
18
19import android.content.ContentProvider;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.content.IContentProvider;
23import android.database.ContentObserver;
24import android.net.Uri;
25
26import com.google.android.collect.Maps;
27
28import java.util.Map;
29
30/**
Joe Malin7d433aa2010-05-31 14:37:28 -070031 * <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 Fernandez3aef8e1d2011-12-20 10:38:34 -080046 *
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 Project9066cfe2009-03-03 19:31:44 -080052 */
Joe Malin7d433aa2010-05-31 14:37:28 -070053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054public class MockContentResolver extends ContentResolver {
55 Map<String, ContentProvider> mProviders;
56
Jeff Sharkey66a017b2013-01-17 18:18:22 -080057 /**
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 Malin7d433aa2010-05-31 14:37:28 -070060 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 public MockContentResolver() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -080062 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 mProviders = Maps.newHashMap();
72 }
73
Joe Malin7d433aa2010-05-31 14:37:28 -070074 /**
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 Project9066cfe2009-03-03 19:31:44 -080081 public void addProvider(String name, ContentProvider provider) {
Joe Malin7d433aa2010-05-31 14:37:28 -070082
83 /*
84 * Maps the authority to the provider locally.
85 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 mProviders.put(name, provider);
87 }
88
89 /** @hide */
90 @Override
91 protected IContentProvider acquireProvider(Context context, String name) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -070092 return acquireExistingProvider(context, name);
93 }
94
95 /** @hide */
96 @Override
97 protected IContentProvider acquireExistingProvider(Context context, String name) {
Joe Malin7d433aa2010-05-31 14:37:28 -070098
99 /*
100 * Gets the content provider from the local map
101 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 final ContentProvider provider = mProviders.get(name);
Joe Malin7d433aa2010-05-31 14:37:28 -0700103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 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 Hackborn652b6d12012-05-09 18:18:40 -0700117 /** @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 Hackborn6ae8d182012-05-23 13:12:42 -0700129 /** @hide */
130 @Override
131 public void unstableProviderDied(IContentProvider icp) {
132 }
133
Joe Malin7d433aa2010-05-31 14:37:28 -0700134 /**
135 * Overrides {@link android.content.ContentResolver#notifyChange(Uri, ContentObserver, boolean)
136 * ContentResolver.notifChange(Uri, ContentObserver, boolean)}. All parameters are ignored.
137 * The method hides providers linked to MockContentResolver from other observers in the system.
138 *
139 * @param uri (Ignored) The uri of the content provider.
140 * @param observer (Ignored) The observer that originated the change.
141 * @param syncToNetwork (Ignored) If true, attempt to sync the change to the network.
142 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 @Override
Joe Malin7d433aa2010-05-31 14:37:28 -0700144 public void notifyChange(Uri uri,
145 ContentObserver observer,
146 boolean syncToNetwork) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
148}