MockContentResolver.java revision e70f61b1160e953e5e4d18d30a463fa9ba821779
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 17package android.test.mock; 18 19import android.content.ContentProvider; 20import android.content.ContentResolver; 21import android.content.Context; 22import android.content.IContentProvider; 23import android.database.ContentObserver; 24import android.net.Uri; 25 26import com.google.android.collect.Maps; 27 28import 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 */ 38public 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} 71