1/*
2 * Copyright (C) 2007 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;
18
19import android.content.ContentProvider;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.test.mock.MockContext;
23import android.test.mock.MockContentResolver;
24import android.database.DatabaseUtils;
25
26/**
27 * If you would like to test a single content provider with an
28 * {@link InstrumentationTestCase}, this provides some of the boiler plate in {@link #setUp} and
29 * {@link #tearDown}.
30 *
31 * @deprecated this class extends InstrumentationTestCase but should extend AndroidTestCase. Use
32 * ProviderTestCase2, which corrects this problem, instead.
33 */
34@Deprecated
35public abstract class ProviderTestCase<T extends ContentProvider>
36       extends InstrumentationTestCase {
37
38    Class<T> mProviderClass;
39    String mProviderAuthority;
40
41    private IsolatedContext mProviderContext;
42    private MockContentResolver mResolver;
43
44    public ProviderTestCase(Class<T> providerClass, String providerAuthority) {
45        mProviderClass = providerClass;
46        mProviderAuthority = providerAuthority;
47    }
48
49    /**
50     * The content provider that will be set up for use in each test method.
51     */
52    private T mProvider;
53
54    public T getProvider() {
55        return mProvider;
56    }
57
58    @Override
59    protected void setUp() throws Exception {
60        super.setUp();
61
62        mResolver = new MockContentResolver();
63        final String filenamePrefix = "test.";
64        RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
65                new MockContext(), // The context that most methods are delegated to
66                getInstrumentation().getTargetContext(), // The context that file methods are delegated to
67                filenamePrefix);
68        mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
69
70        mProvider = mProviderClass.newInstance();
71        mProvider.attachInfo(mProviderContext, null);
72        assertNotNull(mProvider);
73        mResolver.addProvider(mProviderAuthority, getProvider());
74    }
75
76    public MockContentResolver getMockContentResolver() {
77        return mResolver;
78    }
79
80    public IsolatedContext getMockContext() {
81        return mProviderContext;
82    }
83
84    public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql(
85            Context targetContext, Class<T> providerClass, String authority,
86            String databaseName, int databaseVersion, String sql)
87            throws IllegalAccessException, InstantiationException {
88        final String filenamePrefix = "test.";
89        MockContentResolver resolver = new MockContentResolver();
90        RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
91                new MockContext(), // The context that most methods are delegated to
92                targetContext, // The context that file methods are delegated to
93                filenamePrefix);
94        Context context = new IsolatedContext(
95                resolver, targetContextWrapper);
96        DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
97
98        T provider = providerClass.newInstance();
99        provider.attachInfo(context, null);
100        resolver.addProvider(authority, provider);
101
102        return resolver;
103    }
104}
105