| Kenny Root | 10362ab | 2010-03-12 11:13:50 -0800 | [diff] [blame] | 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 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | package android.test; |
| 18 | |
| 19 | import android.content.ContentProvider; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| Ken Shirriff | 04cc0e1 | 2009-07-28 16:15:38 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.test.mock.MockContext; |
| 24 | import android.test.mock.MockContentResolver; |
| 25 | import android.database.DatabaseUtils; |
| 26 | |
| Paul Westbrook | 1048108 | 2010-02-11 10:33:12 -0800 | [diff] [blame] | 27 | import java.io.File; |
| 28 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | /** |
| Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame] | 30 | * This TestCase class provides a framework for isolated testing of a single |
| 31 | * ContentProvider. It uses a {@link android.test.mock.MockContentResolver} to |
| 32 | * access the provider, restricts the provider to an isolated area of the |
| 33 | * filesystem (for safely creating & modifying databases & files), and injects |
| 34 | * {@link android.test.IsolatedContext} to isolate the ContentProvider from the |
| 35 | * rest of the running system. |
| 36 | * |
| 37 | * <p>This environment is created automatically by {@link #setUp} and {@link |
| 38 | * #tearDown}. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | */ |
| 40 | public abstract class ProviderTestCase2<T extends ContentProvider> extends AndroidTestCase { |
| 41 | |
| 42 | Class<T> mProviderClass; |
| 43 | String mProviderAuthority; |
| 44 | |
| 45 | private IsolatedContext mProviderContext; |
| 46 | private MockContentResolver mResolver; |
| 47 | |
| Ken Shirriff | 04cc0e1 | 2009-07-28 16:15:38 -0700 | [diff] [blame] | 48 | private class MockContext2 extends MockContext { |
| 49 | |
| 50 | @Override |
| 51 | public Resources getResources() { |
| 52 | return getContext().getResources(); |
| 53 | } |
| Paul Westbrook | 1048108 | 2010-02-11 10:33:12 -0800 | [diff] [blame] | 54 | |
| 55 | @Override |
| 56 | public File getDir(String name, int mode) { |
| 57 | // name the directory so the directory will be seperated from |
| 58 | // one created through the regular Context |
| 59 | return getContext().getDir("mockcontext2_" + name, mode); |
| 60 | } |
| Ken Shirriff | 04cc0e1 | 2009-07-28 16:15:38 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | public ProviderTestCase2(Class<T> providerClass, String providerAuthority) { |
| 64 | mProviderClass = providerClass; |
| 65 | mProviderAuthority = providerAuthority; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * The content provider that will be set up for use in each test method. |
| 70 | */ |
| 71 | private T mProvider; |
| 72 | |
| 73 | public T getProvider() { |
| 74 | return mProvider; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void setUp() throws Exception { |
| 79 | super.setUp(); |
| 80 | |
| 81 | mResolver = new MockContentResolver(); |
| 82 | final String filenamePrefix = "test."; |
| 83 | RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( |
| Ken Shirriff | 04cc0e1 | 2009-07-28 16:15:38 -0700 | [diff] [blame] | 84 | new MockContext2(), // The context that most methods are delegated to |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | getContext(), // The context that file methods are delegated to |
| 86 | filenamePrefix); |
| 87 | mProviderContext = new IsolatedContext(mResolver, targetContextWrapper); |
| 88 | |
| 89 | mProvider = mProviderClass.newInstance(); |
| 90 | mProvider.attachInfo(mProviderContext, null); |
| 91 | assertNotNull(mProvider); |
| 92 | mResolver.addProvider(mProviderAuthority, getProvider()); |
| 93 | } |
| 94 | |
| 95 | public MockContentResolver getMockContentResolver() { |
| 96 | return mResolver; |
| 97 | } |
| 98 | |
| 99 | public IsolatedContext getMockContext() { |
| 100 | return mProviderContext; |
| 101 | } |
| 102 | |
| 103 | public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql( |
| 104 | Context targetContext, String filenamePrefix, Class<T> providerClass, String authority, |
| 105 | String databaseName, int databaseVersion, String sql) |
| 106 | throws IllegalAccessException, InstantiationException { |
| 107 | MockContentResolver resolver = new MockContentResolver(); |
| 108 | RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( |
| 109 | new MockContext(), // The context that most methods are delegated to |
| 110 | targetContext, // The context that file methods are delegated to |
| 111 | filenamePrefix); |
| 112 | Context context = new IsolatedContext(resolver, targetContextWrapper); |
| 113 | DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql); |
| 114 | |
| 115 | T provider = providerClass.newInstance(); |
| 116 | provider.attachInfo(context, null); |
| 117 | resolver.addProvider(authority, provider); |
| 118 | |
| 119 | return resolver; |
| 120 | } |
| Dianne Hackborn | 935ae46 | 2009-04-13 16:11:55 -0700 | [diff] [blame] | 121 | } |