DBTestHelper.java revision b8f31d5490eba7f307ee578426020bff2ee528ec
1/*
2 * Copyright (C) 2010 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.email;
18
19import com.android.email.provider.EmailContent;
20import com.android.email.provider.EmailProvider;
21
22import android.content.ContentProvider;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.res.Resources;
27import android.database.Cursor;
28import android.net.Uri;
29import android.test.IsolatedContext;
30import android.test.RenamingDelegatingContext;
31import android.test.mock.MockContentResolver;
32import android.test.mock.MockContext;
33import android.test.mock.MockCursor;
34
35import java.io.File;
36
37import junit.framework.Assert;
38
39/**
40 * Helper classes (and possibly methods) for database related tests.
41 */
42public final class DBTestHelper {
43    private DBTestHelper() { // Utility class.  No instantiation.
44    }
45
46    /**
47     * A simple {@link Context} that returns {@link MyProvider} as the email content provider.
48     */
49    public static class MyContext extends MockContext {
50        private final MockContentResolver mContentResolver;
51        private final MyProvider mProvider;
52
53        public MyContext() {
54            mProvider = new MyProvider();
55            mContentResolver = new MockContentResolver();
56            mContentResolver.addProvider(EmailProvider.EMAIL_AUTHORITY, mProvider);
57        }
58
59        @Override
60        public ContentResolver getContentResolver() {
61            return mContentResolver;
62        }
63
64        public MyProvider getMyProvider() {
65            return mProvider;
66        }
67    }
68
69    /**
70     * A simply {@link ContentProvider} to mock out {@link ContentProvider#query}.
71     */
72    public static class MyProvider extends ContentProvider {
73        public Cursor mQueryPresetResult;
74        public Uri mPassedUri;
75        public String[] mPassedProjection;
76        public String mPassedSelection;
77        public String[] mPassedSelectionArgs;
78        public String mPassedSortOrder;
79
80        public void reset() {
81            mQueryPresetResult = null;
82            mPassedUri = null;
83            mPassedProjection = null;
84            mPassedSelection = null;
85            mPassedSelectionArgs = null;
86            mPassedSortOrder = null;
87        }
88
89        @Override
90        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
91                String sortOrder) {
92            mPassedUri = uri;
93            mPassedProjection = projection;
94            mPassedSelection = selection;
95            mPassedSelectionArgs = selectionArgs;
96            mPassedSortOrder = sortOrder;
97            return mQueryPresetResult;
98        }
99
100        @Override
101        public int delete(Uri uri, String selection, String[] selectionArgs) {
102            throw new UnsupportedOperationException();
103        }
104
105        @Override
106        public String getType(Uri uri) {
107            throw new UnsupportedOperationException();
108        }
109
110        @Override
111        public Uri insert(Uri uri, ContentValues values) {
112            throw new UnsupportedOperationException();
113        }
114
115        @Override
116        public boolean onCreate() {
117            throw new UnsupportedOperationException();
118        }
119
120        @Override
121        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
122            throw new UnsupportedOperationException();
123        }
124    }
125
126    /**
127     * Simple {@link MockCursor} subclass that implements common methods.
128     */
129    public static class EasyMockCursor extends MockCursor {
130        public int mCount;
131        public boolean mClosed;
132
133        public EasyMockCursor(int count) {
134            mCount = count;
135        }
136
137        @Override
138        public int getCount() {
139            return mCount;
140        }
141
142        @Override
143        public void close() {
144            mClosed = true;
145        }
146    }
147
148    /**
149     * This class has only one method, that creats an isolated {@link Context} similar to what
150     * {@link android.test.ProviderTestCase2} provides.
151     *
152     * The method also creates a {@link ContentProvider} of {@code providerClass}, and add it to
153     * the context.  See the javadoc on android.test.ProviderTestCase2 for the details.
154     */
155    public static class ProviderContextSetupHelper {
156        // Based on ProviderTestCase2.MockContext2.
157        private static class MockContext2 extends MockContext {
158            private final Context mBaseContext;
159
160            public MockContext2(Context baseContext) {
161                mBaseContext = baseContext;
162            }
163
164            @Override
165            public Resources getResources() {
166                return mBaseContext.getResources();
167            }
168
169            @Override
170            public File getDir(String name, int mode) {
171                return mBaseContext.getDir("mockcontext2_" + name, mode);
172            }
173        }
174
175        /** {@link IsolatedContext} + getApplicationContext() */
176        private static class MyIsolatedContext extends IsolatedContext {
177            public MyIsolatedContext(ContentResolver resolver, Context targetContext) {
178                super(resolver, targetContext);
179            }
180
181            @Override
182            public Context getApplicationContext() {
183                return this;
184            }
185        }
186
187        // Based on ProviderTestCase2.setUp().
188        public static <T extends ContentProvider> Context getProviderContext(
189                Context context, Class<T> providerClass) throws Exception {
190            MockContentResolver resolver = new MockContentResolver();
191            final String filenamePrefix = "test.";
192            RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
193                    new MockContext2(context), // The context that most methods are delegated to
194                    context, // The context that file methods are delegated to
195                    filenamePrefix);
196            final Context providerContext = new MyIsolatedContext(resolver, targetContextWrapper);
197            providerContext.getContentResolver();
198
199            final T provider = providerClass.newInstance();
200            provider.attachInfo(providerContext, null);
201            Assert.assertNotNull(provider);
202            resolver.addProvider(EmailContent.AUTHORITY, provider);
203            return providerContext;
204        }
205    }
206}
207