• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
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>
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>
52  */
53 public class MockContentResolver extends ContentResolver {
54     Map<String, ContentProvider> mProviders;
55 
56     /**
57      * Creates a local map of providers. This map is used instead of the global
58      * map when an API call tries to acquire a provider.
59      */
MockContentResolver()60     public MockContentResolver() {
61         this(null);
62     }
63 
64     /**
65      * Creates a local map of providers. This map is used instead of the global
66      * map when an API call tries to acquire a provider.
67      */
MockContentResolver(Context context)68     public MockContentResolver(Context context) {
69         super(context);
70         mProviders = Maps.newHashMap();
71     }
72 
73     /**
74      * Adds access to a provider based on its authority
75      *
76      * @param name The authority name associated with the provider.
77      * @param provider An instance of {@link android.content.ContentProvider} or one of its
78      * subclasses, or null.
79      */
addProvider(String name, ContentProvider provider)80     public void addProvider(String name, ContentProvider provider) {
81 
82         /*
83          * Maps the authority to the provider locally.
84          */
85         mProviders.put(name, provider);
86     }
87 
88     /** @hide */
89     @Override
acquireProvider(Context context, String name)90     protected IContentProvider acquireProvider(Context context, String name) {
91         return acquireExistingProvider(context, name);
92     }
93 
94     /** @hide */
95     @Override
acquireExistingProvider(Context context, String name)96     protected IContentProvider acquireExistingProvider(Context context, String name) {
97 
98         /*
99          * Gets the content provider from the local map
100          */
101         final ContentProvider provider = mProviders.get(name);
102 
103         if (provider != null) {
104             return provider.getIContentProvider();
105         } else {
106             return null;
107         }
108     }
109 
110     /** @hide */
111     @Override
releaseProvider(IContentProvider provider)112     public boolean releaseProvider(IContentProvider provider) {
113         return true;
114     }
115 
116     /** @hide */
117     @Override
acquireUnstableProvider(Context c, String name)118     protected IContentProvider acquireUnstableProvider(Context c, String name) {
119         return acquireProvider(c, name);
120     }
121 
122     /** @hide */
123     @Override
releaseUnstableProvider(IContentProvider icp)124     public boolean releaseUnstableProvider(IContentProvider icp) {
125         return releaseProvider(icp);
126     }
127 
128     /** @hide */
129     @Override
unstableProviderDied(IContentProvider icp)130     public void unstableProviderDied(IContentProvider icp) {
131     }
132 
133     /**
134      * Overrides {@link android.content.ContentResolver#notifyChange(Uri, ContentObserver, boolean)
135      * ContentResolver.notifChange(Uri, ContentObserver, boolean)}. All parameters are ignored.
136      * The method hides providers linked to MockContentResolver from other observers in the system.
137      *
138      * @param uri (Ignored) The uri of the content provider.
139      * @param observer (Ignored) The observer that originated the change.
140      * @param syncToNetwork (Ignored) If true, attempt to sync the change to the network.
141      */
142     @Override
notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork)143     public void notifyChange(Uri uri,
144             ContentObserver observer,
145             boolean syncToNetwork) {
146     }
147 }
148