summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/bookmarkstore/tests/utils/ProviderTestCase3.java
blob: f8eb1614a57770fa6587e96d3a513c8eb60ba18d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bookmarkprovider.tests.utils;

import android.content.ContentProvider;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.test.IsolatedContext;
import android.test.RenamingDelegatingContext;
import android.test.mock.MockContext;

import java.io.File;

import android.content.pm.ProviderInfo;

/**
 * Replacement for ProviderTestCase2 that keeps calls to ContentResolver.notifyChanged
 * internal to observers registered with ProviderTestCase3.registerContentObserver
 */
public abstract class ProviderTestCase3<T extends ContentProvider> extends AndroidTestCase {

    public static final String FILENAME_PREFIX = "test.";

    private static final String BROWSER_AUTHORITIES = "com.android.browser;browser";

    Class<T> mProviderClass;
    String[] mProviderAuthority;

    private IsolatedContext mProviderContext;
    private MockContentResolver2 mResolver;

    private class MockContext2 extends MockContext {

        @Override
        public Resources getResources() {
            return getContext().getResources();
        }

        @Override
        public File getDir(String name, int mode) {
            // name the directory so the directory will be separated from
            // one created through the regular Context
            return getContext().getDir("mockcontext2_" + name, mode);
        }

        @Override
        public String getPackageName() {
            return getContext().getPackageName();
        }

        @Override
        public SharedPreferences getSharedPreferences(String name, int mode) {
            return getContext().getSharedPreferences("mockcontext2_" + name, mode);
        }

        @Override
        public Context getApplicationContext() {
            return this;
        }

        @Override
        public Object getSystemService(String name) {
            return null;
        }
    }
    /**
     * Constructor.
     *
     * @param providerClass The class name of the provider under test
     * @param providerAuthorities The provider's authority string
     */
    public ProviderTestCase3(Class<T> providerClass, String... providerAuthorities) {
        mProviderClass = providerClass;
        mProviderAuthority = providerAuthorities;
    }

    private T mProvider;

    /**
     * Returns the content provider created by this class in the {@link #setUp()} method.
     * @return T An instance of the provider class given as a parameter to the test case class.
     */
    public T getProvider() {
        return mProvider;
    }

    /**
     * Sets up the environment for the test fixture.
     * <p>
     * Creates a new
     * {@link com.android.bookmarkprovider.tests.utils.MockContentResolver2}, a new IsolatedContext
     * that isolates the provider's file operations, and a new instance of
     * the provider under test within the isolated environment.
     * </p>
     *
     * @throws Exception
     */
    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mResolver = new MockContentResolver2();
        RenamingDelegatingContext targetContextWrapper = new
                RenamingDelegatingContext(
                new MockContext2(), // The context that most methods are
                                    //delegated to
                getContext(), // The context that file methods are delegated to
                FILENAME_PREFIX);
        // The default IsolatedContext has a mock AccountManager that doesn't
        // work for us, so override getSystemService to always return null
        mProviderContext = new IsolatedContext(mResolver, targetContextWrapper) {

            @Override
            public Object getSystemService(String name) {
                return null;
            }
        };

        mProvider = mProviderClass.newInstance();
        ProviderInfo info = new ProviderInfo();
        info.authority = BROWSER_AUTHORITIES;
        mProvider.attachInfoForTesting(mProviderContext, info);
        assertNotNull(mProvider);
        for (String auth : mProviderAuthority) {
            mResolver.addProvider(auth, getProvider());
        }
    }

    /**
     * Tears down the environment for the test fixture.
     * <p>
     * Calls {@link android.content.ContentProvider#shutdown()} on the
     * {@link android.content.ContentProvider} represented by mProvider.
     */
    @Override
    protected void tearDown() throws Exception {
        mProvider.shutdown();
        super.tearDown();
    }

    /**
     * Gets the {@link MockContentResolver2} created by this class during initialization. You
     * must use the methods of this resolver to access the provider under test.
     *
     * @return A {@link MockContentResolver2} instance.
     */
    public MockContentResolver2 getMockContentResolver() {
        return mResolver;
    }

    /**
     * Gets the {@link IsolatedContext} created by this class during initialization.
     * @return The {@link IsolatedContext} instance
     */
    public IsolatedContext getMockContext() {
        return mProviderContext;
    }

    public void registerContentObserver(Uri uri, boolean notifyForDescendents,
            ContentObserver observer) {
        mResolver.safeRegisterContentObserver(uri, notifyForDescendents, observer);
    }

    public void unregisterContentObserver(ContentObserver observer) {
        mResolver.safeUnregisterContentObserver(observer);
    }

}