1 /* 2 * Copyright (C) 2009 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.AttributionSource; 22 import android.content.ContentProviderOperation; 23 import android.content.ContentProviderResult; 24 import android.content.ContentResolver; 25 import android.content.ContentValues; 26 import android.content.EntityIterator; 27 import android.content.IContentProvider; 28 import android.content.res.AssetFileDescriptor; 29 import android.database.Cursor; 30 import android.net.Uri; 31 import android.os.AsyncTask; 32 import android.os.Bundle; 33 import android.os.IBinder; 34 import android.os.ICancellationSignal; 35 import android.os.ParcelFileDescriptor; 36 import android.os.RemoteCallback; 37 import android.os.RemoteException; 38 39 import java.io.FileNotFoundException; 40 import java.util.ArrayList; 41 42 /** 43 * Mock implementation of IContentProvider. All methods are non-functional and throw 44 * {@link java.lang.UnsupportedOperationException}. Tests can extend this class to 45 * implement behavior needed for tests. 46 * 47 * @hide - @hide because this exposes bulkQuery() and call(), which must also be hidden. 48 */ 49 public class MockIContentProvider implements IContentProvider { 50 @Override bulkInsert(@onNull AttributionSource attributionSource, Uri url, ContentValues[] initialValues)51 public int bulkInsert(@NonNull AttributionSource attributionSource, Uri url, 52 ContentValues[] initialValues) { 53 throw new UnsupportedOperationException("unimplemented mock method"); 54 } 55 56 @Override 57 @SuppressWarnings("unused") delete(@onNull AttributionSource attributionSource, Uri url, Bundle extras)58 public int delete(@NonNull AttributionSource attributionSource, Uri url, 59 Bundle extras) throws RemoteException { 60 throw new UnsupportedOperationException("unimplemented mock method"); 61 } 62 63 @Override getType(@onNull AttributionSource attributionSource, Uri url)64 public String getType(@NonNull AttributionSource attributionSource, Uri url) { 65 throw new UnsupportedOperationException("unimplemented mock method"); 66 } 67 68 @Override 69 @SuppressWarnings("deprecation") getTypeAsync(@onNull AttributionSource attributionSource, Uri uri, RemoteCallback remoteCallback)70 public void getTypeAsync(@NonNull AttributionSource attributionSource, 71 Uri uri, RemoteCallback remoteCallback) { 72 AsyncTask.SERIAL_EXECUTOR.execute(() -> { 73 final Bundle bundle = new Bundle(); 74 bundle.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getType(attributionSource, 75 uri)); 76 remoteCallback.sendResult(bundle); 77 }); 78 } getTypeAnonymous(Uri url)79 public String getTypeAnonymous(Uri url) { 80 throw new UnsupportedOperationException("unimplemented mock method"); 81 } 82 @Override 83 @SuppressWarnings("deprecation") getTypeAnonymousAsync(Uri uri, RemoteCallback remoteCallback)84 public void getTypeAnonymousAsync(Uri uri, RemoteCallback remoteCallback) { 85 AsyncTask.SERIAL_EXECUTOR.execute(() -> { 86 final Bundle bundle = new Bundle(); 87 bundle.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getTypeAnonymous(uri)); 88 remoteCallback.sendResult(bundle); 89 }); 90 } 91 92 @Override 93 @SuppressWarnings("unused") insert(@onNull AttributionSource attributionSource, Uri url, ContentValues initialValues, Bundle extras)94 public Uri insert(@NonNull AttributionSource attributionSource, Uri url, 95 ContentValues initialValues, Bundle extras) throws RemoteException { 96 throw new UnsupportedOperationException("unimplemented mock method"); 97 } 98 99 @Override openFile(@onNull AttributionSource attributionSource, Uri url, String mode, ICancellationSignal signal)100 public ParcelFileDescriptor openFile(@NonNull AttributionSource attributionSource, 101 Uri url, String mode, ICancellationSignal signal) { 102 throw new UnsupportedOperationException("unimplemented mock method"); 103 } 104 105 @Override openAssetFile(@onNull AttributionSource attributionSource, Uri uri, String mode, ICancellationSignal signal)106 public AssetFileDescriptor openAssetFile(@NonNull AttributionSource attributionSource, 107 Uri uri, String mode, ICancellationSignal signal) { 108 throw new UnsupportedOperationException("unimplemented mock method"); 109 } 110 111 @Override applyBatch(@onNull AttributionSource attributionSource, String authority, ArrayList<ContentProviderOperation> operations)112 public ContentProviderResult[] applyBatch(@NonNull AttributionSource attributionSource, 113 String authority, ArrayList<ContentProviderOperation> operations) { 114 throw new UnsupportedOperationException("unimplemented mock method"); 115 } 116 117 @Override query(@onNull AttributionSource attributionSource, Uri url, @Nullable String[] projection, @Nullable Bundle queryArgs, @Nullable ICancellationSignal cancellationSignal)118 public Cursor query(@NonNull AttributionSource attributionSource, Uri url, 119 @Nullable String[] projection, @Nullable Bundle queryArgs, 120 @Nullable ICancellationSignal cancellationSignal) { 121 throw new UnsupportedOperationException("unimplemented mock method"); 122 } 123 queryEntities(Uri url, String selection, String[] selectionArgs, String sortOrder)124 public EntityIterator queryEntities(Uri url, String selection, String[] selectionArgs, 125 String sortOrder) { 126 throw new UnsupportedOperationException("unimplemented mock method"); 127 } 128 129 @Override update(@onNull AttributionSource attributionSource, Uri url, ContentValues values, Bundle extras)130 public int update(@NonNull AttributionSource attributionSource, Uri url, 131 ContentValues values, Bundle extras) throws RemoteException { 132 throw new UnsupportedOperationException("unimplemented mock method"); 133 } 134 135 @Override call(@onNull AttributionSource attributionSource, String authority, String method, String request, Bundle args)136 public Bundle call(@NonNull AttributionSource attributionSource, String authority, 137 String method, String request, Bundle args) throws RemoteException { 138 throw new UnsupportedOperationException("unimplemented mock method"); 139 } 140 141 @Override asBinder()142 public IBinder asBinder() { 143 throw new UnsupportedOperationException("unimplemented mock method"); 144 } 145 146 @Override getStreamTypes(@onNull AttributionSource attributionSource, Uri url, String mimeTypeFilter)147 public String[] getStreamTypes(@NonNull AttributionSource attributionSource, 148 Uri url, String mimeTypeFilter) throws RemoteException { 149 throw new UnsupportedOperationException("unimplemented mock method"); 150 } 151 152 @Override openTypedAssetFile(@onNull AttributionSource attributionSource, Uri url, String mimeType, Bundle opts, ICancellationSignal signal)153 public AssetFileDescriptor openTypedAssetFile(@NonNull AttributionSource attributionSource, 154 Uri url, String mimeType, Bundle opts, ICancellationSignal signal) 155 throws RemoteException, FileNotFoundException { 156 throw new UnsupportedOperationException("unimplemented mock method"); 157 } 158 159 @Override createCancellationSignal()160 public ICancellationSignal createCancellationSignal() throws RemoteException { 161 throw new UnsupportedOperationException("unimplemented mock method"); 162 } 163 164 @Override canonicalize(@onNull AttributionSource attributionSource, Uri uri)165 public Uri canonicalize(@NonNull AttributionSource attributionSource, Uri uri) { 166 throw new UnsupportedOperationException("unimplemented mock method"); 167 } 168 169 @Override 170 @SuppressWarnings("deprecation") canonicalizeAsync(@onNull AttributionSource attributionSource, Uri uri, RemoteCallback remoteCallback)171 public void canonicalizeAsync(@NonNull AttributionSource attributionSource, Uri uri, 172 RemoteCallback remoteCallback) { 173 AsyncTask.SERIAL_EXECUTOR.execute(() -> { 174 final Bundle bundle = new Bundle(); 175 bundle.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT, 176 canonicalize(attributionSource, uri)); 177 remoteCallback.sendResult(bundle); 178 }); 179 } 180 181 @Override uncanonicalize(@onNull AttributionSource attributionSource, Uri uri)182 public Uri uncanonicalize(@NonNull AttributionSource attributionSource, Uri uri) { 183 throw new UnsupportedOperationException("unimplemented mock method"); 184 } 185 186 @Override 187 @SuppressWarnings("deprecation") uncanonicalizeAsync(@onNull AttributionSource attributionSource, Uri uri, RemoteCallback remoteCallback)188 public void uncanonicalizeAsync(@NonNull AttributionSource attributionSource, Uri uri, 189 RemoteCallback remoteCallback) { 190 AsyncTask.SERIAL_EXECUTOR.execute(() -> { 191 final Bundle bundle = new Bundle(); 192 bundle.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT, 193 uncanonicalize(attributionSource, uri)); 194 remoteCallback.sendResult(bundle); 195 }); 196 } 197 198 @Override refresh(@onNull AttributionSource attributionSource, Uri url, Bundle args, ICancellationSignal cancellationSignal)199 public boolean refresh(@NonNull AttributionSource attributionSource, Uri url, Bundle args, 200 ICancellationSignal cancellationSignal) throws RemoteException { 201 throw new UnsupportedOperationException("unimplemented mock method"); 202 } 203 204 /** {@hide} */ 205 @Override checkUriPermission(@onNull AttributionSource attributionSource, Uri uri, int uid, int modeFlags)206 public int checkUriPermission(@NonNull AttributionSource attributionSource, Uri uri, int uid, 207 int modeFlags) { 208 throw new UnsupportedOperationException("unimplemented mock method call"); 209 } 210 } 211