The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
| 17 | package android.test.mock; |
| 18 | |
| 19 | import android.content.ContentProvider; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| 22 | import android.content.IContentProvider; |
| 23 | import android.database.ContentObserver; |
| 24 | import android.net.Uri; |
| 25 | |
| 26 | import com.google.android.collect.Maps; |
| 27 | |
| 28 | import java.util.Map; |
| 29 | |
| 30 | /** |
| 31 | * A mock {@link android.content.ContentResolver} class that isolates the test code from the real |
| 32 | * content system. All methods are non-functional and throw |
| 33 | * {@link java.lang.UnsupportedOperationException}. |
| 34 | * |
| 35 | * <p>This only isolates the test code in ways that have proven useful so far. More should be |
| 36 | * added as they become a problem. |
| 37 | */ |
| 38 | public class MockContentResolver extends ContentResolver { |
| 39 | Map<String, ContentProvider> mProviders; |
| 40 | |
| 41 | public MockContentResolver() { |
| 42 | super(null); |
| 43 | mProviders = Maps.newHashMap(); |
| 44 | } |
| 45 | |
| 46 | public void addProvider(String name, ContentProvider provider) { |
| 47 | mProviders.put(name, provider); |
| 48 | } |
| 49 | |
| 50 | /** @hide */ |
| 51 | @Override |
| 52 | protected IContentProvider acquireProvider(Context context, String name) { |
| 53 | final ContentProvider provider = mProviders.get(name); |
| 54 | if (provider != null) { |
| 55 | return provider.getIContentProvider(); |
| 56 | } else { |
| 57 | return null; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** @hide */ |
| 62 | @Override |
| 63 | public boolean releaseProvider(IContentProvider provider) { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) { |
| 69 | } |
| 70 | } |