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.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.ContentProvider; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.IContentProvider; 25 import android.database.ContentObserver; 26 import android.net.Uri; 27 28 import java.util.Collection; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 /** 33 * <p> 34 * An extension of {@link android.content.ContentResolver} that is designed for 35 * testing. 36 * </p> 37 * <p> 38 * MockContentResolver overrides Android's normal way of resolving providers by 39 * authority. To have access to a provider based on its authority, users of 40 * MockContentResolver first instantiate the provider and 41 * use {@link MockContentResolver#addProvider(String, ContentProvider)}. Resolution of an 42 * authority occurs entirely within MockContentResolver. 43 * </p> 44 * <p> 45 * Users can also set an authority's entry in the map to null, so that a provider is completely 46 * mocked out. 47 * </p> 48 * 49 * <div class="special reference"> 50 * <h3>Developer Guides</h3> 51 * <p>For more information about application testing, read the 52 * <a href="{@docRoot}guide/topics/testing/index.html">Testing</a> developer guide.</p> 53 * </div> 54 */ 55 public class MockContentResolver extends ContentResolver { 56 Map<String, ContentProvider> mProviders; 57 58 /** 59 * Creates a local map of providers. This map is used instead of the global 60 * map when an API call tries to acquire a provider. 61 */ MockContentResolver()62 public MockContentResolver() { 63 this(null); 64 } 65 66 /** 67 * Creates a local map of providers. This map is used instead of the global 68 * map when an API call tries to acquire a provider. 69 */ MockContentResolver(Context context)70 public MockContentResolver(Context context) { 71 super(context); 72 mProviders = new HashMap<>(); 73 } 74 75 /** 76 * Adds access to a provider based on its authority 77 * 78 * @param name The authority name associated with the provider. 79 * @param provider An instance of {@link android.content.ContentProvider} or one of its 80 * subclasses, or null. 81 */ addProvider(String name, ContentProvider provider)82 public void addProvider(String name, ContentProvider provider) { 83 84 /* 85 * Maps the authority to the provider locally. 86 */ 87 mProviders.put(name, provider); 88 } 89 90 /** @hide */ 91 @Override acquireProvider(Context context, String name)92 protected IContentProvider acquireProvider(Context context, String name) { 93 return acquireExistingProvider(context, name); 94 } 95 96 /** @hide */ 97 @Override acquireExistingProvider(Context context, String name)98 protected IContentProvider acquireExistingProvider(Context context, String name) { 99 100 /* 101 * Gets the content provider from the local map 102 */ 103 final ContentProvider provider = mProviders.get(name); 104 105 if (provider != null) { 106 return provider.getIContentProvider(); 107 } else { 108 return null; 109 } 110 } 111 112 /** @hide */ 113 @Override releaseProvider(IContentProvider provider)114 public boolean releaseProvider(IContentProvider provider) { 115 return true; 116 } 117 118 /** @hide */ 119 @Override acquireUnstableProvider(Context c, String name)120 protected IContentProvider acquireUnstableProvider(Context c, String name) { 121 return acquireProvider(c, name); 122 } 123 124 /** @hide */ 125 @Override releaseUnstableProvider(IContentProvider icp)126 public boolean releaseUnstableProvider(IContentProvider icp) { 127 return releaseProvider(icp); 128 } 129 130 /** @hide */ 131 @Override unstableProviderDied(IContentProvider icp)132 public void unstableProviderDied(IContentProvider icp) { 133 } 134 135 /** 136 * Overrides the behavior from the parent class to completely ignore any 137 * content notifications sent to this object. This effectively hides clients 138 * from observers elsewhere in the system. 139 */ 140 @Override notifyChange(@onNull Uri uri, @Nullable ContentObserver observer)141 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer) { 142 } 143 144 /** 145 * Overrides the behavior from the parent class to completely ignore any 146 * content notifications sent to this object. This effectively hides clients 147 * from observers elsewhere in the system. 148 * 149 * @deprecated callers should consider migrating to 150 * {@link #notifyChange(Uri, ContentObserver, int)}, as it 151 * offers support for many more options than just 152 * {@link #NOTIFY_SYNC_TO_NETWORK}. 153 */ 154 @Override 155 @Deprecated notifyChange(@onNull Uri uri, @Nullable ContentObserver observer, boolean syncToNetwork)156 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer, 157 boolean syncToNetwork) { 158 } 159 160 /** 161 * Overrides the behavior from the parent class to completely ignore any 162 * content notifications sent to this object. This effectively hides clients 163 * from observers elsewhere in the system. 164 */ 165 @Override notifyChange(@onNull Uri uri, @Nullable ContentObserver observer, @NotifyFlags int flags)166 public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer, 167 @NotifyFlags int flags) { 168 } 169 170 /** 171 * Overrides the behavior from the parent class to completely ignore any 172 * content notifications sent to this object. This effectively hides clients 173 * from observers elsewhere in the system. 174 */ 175 @Override notifyChange(@onNull Collection<Uri> uris, @Nullable ContentObserver observer, @NotifyFlags int flags)176 public void notifyChange(@NonNull Collection<Uri> uris, @Nullable ContentObserver observer, 177 @NotifyFlags int flags) { 178 } 179 } 180