1/*
2 * Copyright (C) 2011 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 com.android.browser.tests.utils;
18
19import com.google.android.collect.Maps;
20
21import android.content.ContentProvider;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.IContentProvider;
25import android.database.ContentObserver;
26import android.net.Uri;
27
28import java.util.Map;
29
30public class MockContentResolver2 extends ContentResolver {
31
32    Map<String, ContentProvider> mProviders;
33    private final MockObserverNode mRootNode = new MockObserverNode("");
34
35    /*
36     * Creates a local map of providers. This map is used instead of the global map when an
37     * API call tries to acquire a provider.
38     */
39    public MockContentResolver2() {
40        super(null);
41        mProviders = Maps.newHashMap();
42    }
43
44    /**
45     * Adds access to a provider based on its authority
46     *
47     * @param name The authority name associated with the provider.
48     * @param provider An instance of {@link android.content.ContentProvider} or one of its
49     * subclasses, or null.
50     */
51    public void addProvider(String name, ContentProvider provider) {
52
53        /*
54         * Maps the authority to the provider locally.
55         */
56        mProviders.put(name, provider);
57    }
58
59    /** @hide */
60    @Override
61    protected IContentProvider acquireProvider(Context context, String name) {
62        return acquireExistingProvider(context, name);
63    }
64
65    /** @hide */
66    @Override
67    protected IContentProvider acquireExistingProvider(Context context, String name) {
68
69        /*
70         * Gets the content provider from the local map
71         */
72        final ContentProvider provider = mProviders.get(name);
73
74        if (provider != null) {
75            return provider.getIContentProvider();
76        } else {
77            return null;
78        }
79    }
80
81    /** @hide */
82    @Override
83    public boolean releaseProvider(IContentProvider provider) {
84        return true;
85    }
86
87    /** @hide */
88    protected IContentProvider acquireUnstableProvider(Context c, String name) {
89        return acquireProvider(c, name);
90    }
91
92    /** @hide */
93    public boolean releaseUnstableProvider(IContentProvider icp) {
94        return releaseProvider(icp);
95    }
96
97    /** @hide */
98    public void unstableProviderDied(IContentProvider icp) {
99    }
100
101    @Override
102    public void notifyChange(Uri uri, ContentObserver observer,
103            boolean syncToNetwork) {
104        mRootNode.notifyMyObservers(uri, 0, observer, false);
105    }
106
107    public void safeRegisterContentObserver(Uri uri, boolean notifyForDescendents,
108            ContentObserver observer) {
109        mRootNode.addObserver(uri, observer, notifyForDescendents);
110    }
111
112    public void safeUnregisterContentObserver(ContentObserver observer) {
113        mRootNode.removeObserver(observer);
114    }
115
116}
117