MockContentResolver.java revision cca1f0e3476edd09cdd81b075a6b7780a2959b46
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
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/**
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
48public class MockContentResolver extends ContentResolver {
49    Map<String, ContentProvider> mProviders;
50
51    /*
52     * Creates a local map of providers. This map is used instead of the global map when an
53     * API call tries to acquire a provider.
54     */
55    public MockContentResolver() {
56        super(null);
57        mProviders = Maps.newHashMap();
58    }
59
60    /**
61     * Adds access to a provider based on its authority
62     *
63     * @param name The authority name associated with the provider.
64     * @param provider An instance of {@link android.content.ContentProvider} or one of its
65     * subclasses, or null.
66     */
67    public void addProvider(String name, ContentProvider provider) {
68
69        /*
70         * Maps the authority to the provider locally.
71         */
72        mProviders.put(name, provider);
73    }
74
75    /** @hide */
76    @Override
77    protected IContentProvider acquireProvider(Context context, String name) {
78        return acquireExistingProvider(context, name);
79    }
80
81    /** @hide */
82    @Override
83    protected IContentProvider acquireExistingProvider(Context context, String name) {
84
85        /*
86         * Gets the content provider from the local map
87         */
88        final ContentProvider provider = mProviders.get(name);
89
90        if (provider != null) {
91            return provider.getIContentProvider();
92        } else {
93            return null;
94        }
95    }
96
97    /** @hide */
98    @Override
99    public boolean releaseProvider(IContentProvider provider) {
100        return true;
101    }
102
103    /**
104     * Overrides {@link android.content.ContentResolver#notifyChange(Uri, ContentObserver, boolean)
105     * ContentResolver.notifChange(Uri, ContentObserver, boolean)}. All parameters are ignored.
106     * The method hides providers linked to MockContentResolver from other observers in the system.
107     *
108     * @param uri (Ignored) The uri of the content provider.
109     * @param observer (Ignored) The observer that originated the change.
110     * @param syncToNetwork (Ignored) If true, attempt to sync the change to the network.
111     */
112    @Override
113    public void notifyChange(Uri uri,
114            ContentObserver observer,
115            boolean syncToNetwork) {
116    }
117}
118