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