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(Uri url)64 public String getType(Uri url) { 65 throw new UnsupportedOperationException("unimplemented mock method"); 66 } 67 68 @Override 69 @SuppressWarnings("deprecation") getTypeAsync(Uri uri, RemoteCallback remoteCallback)70 public void getTypeAsync(Uri uri, RemoteCallback remoteCallback) { 71 AsyncTask.SERIAL_EXECUTOR.execute(() -> { 72 final Bundle bundle = new Bundle(); 73 bundle.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getType(uri)); 74 remoteCallback.sendResult(bundle); 75 }); 76 } 77 78 @Override 79 @SuppressWarnings("unused") insert(@onNull AttributionSource attributionSource, Uri url, ContentValues initialValues, Bundle extras)80 public Uri insert(@NonNull AttributionSource attributionSource, Uri url, 81 ContentValues initialValues, Bundle extras) throws RemoteException { 82 throw new UnsupportedOperationException("unimplemented mock method"); 83 } 84 85 @Override openFile(@onNull AttributionSource attributionSource, Uri url, String mode, ICancellationSignal signal)86 public ParcelFileDescriptor openFile(@NonNull AttributionSource attributionSource, 87 Uri url, String mode, ICancellationSignal signal) { 88 throw new UnsupportedOperationException("unimplemented mock method"); 89 } 90 91 @Override openAssetFile(@onNull AttributionSource attributionSource, Uri uri, String mode, ICancellationSignal signal)92 public AssetFileDescriptor openAssetFile(@NonNull AttributionSource attributionSource, 93 Uri uri, String mode, ICancellationSignal signal) { 94 throw new UnsupportedOperationException("unimplemented mock method"); 95 } 96 97 @Override applyBatch(@onNull AttributionSource attributionSource, String authority, ArrayList<ContentProviderOperation> operations)98 public ContentProviderResult[] applyBatch(@NonNull AttributionSource attributionSource, 99 String authority, ArrayList<ContentProviderOperation> operations) { 100 throw new UnsupportedOperationException("unimplemented mock method"); 101 } 102 103 @Override query(@onNull AttributionSource attributionSource, Uri url, @Nullable String[] projection, @Nullable Bundle queryArgs, @Nullable ICancellationSignal cancellationSignal)104 public Cursor query(@NonNull AttributionSource attributionSource, Uri url, 105 @Nullable String[] projection, @Nullable Bundle queryArgs, 106 @Nullable ICancellationSignal cancellationSignal) { 107 throw new UnsupportedOperationException("unimplemented mock method"); 108 } 109 queryEntities(Uri url, String selection, String[] selectionArgs, String sortOrder)110 public EntityIterator queryEntities(Uri url, String selection, String[] selectionArgs, 111 String sortOrder) { 112 throw new UnsupportedOperationException("unimplemented mock method"); 113 } 114 115 @Override update(@onNull AttributionSource attributionSource, Uri url, ContentValues values, Bundle extras)116 public int update(@NonNull AttributionSource attributionSource, Uri url, 117 ContentValues values, Bundle extras) throws RemoteException { 118 throw new UnsupportedOperationException("unimplemented mock method"); 119 } 120 121 @Override call(@onNull AttributionSource attributionSource, String authority, String method, String request, Bundle args)122 public Bundle call(@NonNull AttributionSource attributionSource, String authority, 123 String method, String request, Bundle args) throws RemoteException { 124 throw new UnsupportedOperationException("unimplemented mock method"); 125 } 126 127 @Override asBinder()128 public IBinder asBinder() { 129 throw new UnsupportedOperationException("unimplemented mock method"); 130 } 131 132 @Override getStreamTypes(Uri url, String mimeTypeFilter)133 public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException { 134 throw new UnsupportedOperationException("unimplemented mock method"); 135 } 136 137 @Override openTypedAssetFile(@onNull AttributionSource attributionSource, Uri url, String mimeType, Bundle opts, ICancellationSignal signal)138 public AssetFileDescriptor openTypedAssetFile(@NonNull AttributionSource attributionSource, 139 Uri url, String mimeType, Bundle opts, ICancellationSignal signal) 140 throws RemoteException, FileNotFoundException { 141 throw new UnsupportedOperationException("unimplemented mock method"); 142 } 143 144 @Override createCancellationSignal()145 public ICancellationSignal createCancellationSignal() throws RemoteException { 146 throw new UnsupportedOperationException("unimplemented mock method"); 147 } 148 149 @Override canonicalize(@onNull AttributionSource attributionSource, Uri uri)150 public Uri canonicalize(@NonNull AttributionSource attributionSource, Uri uri) { 151 throw new UnsupportedOperationException("unimplemented mock method"); 152 } 153 154 @Override 155 @SuppressWarnings("deprecation") canonicalizeAsync(@onNull AttributionSource attributionSource, Uri uri, RemoteCallback remoteCallback)156 public void canonicalizeAsync(@NonNull AttributionSource attributionSource, Uri uri, 157 RemoteCallback remoteCallback) { 158 AsyncTask.SERIAL_EXECUTOR.execute(() -> { 159 final Bundle bundle = new Bundle(); 160 bundle.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT, 161 canonicalize(attributionSource, uri)); 162 remoteCallback.sendResult(bundle); 163 }); 164 } 165 166 @Override uncanonicalize(@onNull AttributionSource attributionSource, Uri uri)167 public Uri uncanonicalize(@NonNull AttributionSource attributionSource, Uri uri) { 168 throw new UnsupportedOperationException("unimplemented mock method"); 169 } 170 171 @Override 172 @SuppressWarnings("deprecation") uncanonicalizeAsync(@onNull AttributionSource attributionSource, Uri uri, RemoteCallback remoteCallback)173 public void uncanonicalizeAsync(@NonNull AttributionSource attributionSource, Uri uri, 174 RemoteCallback remoteCallback) { 175 AsyncTask.SERIAL_EXECUTOR.execute(() -> { 176 final Bundle bundle = new Bundle(); 177 bundle.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT, 178 uncanonicalize(attributionSource, uri)); 179 remoteCallback.sendResult(bundle); 180 }); 181 } 182 183 @Override refresh(@onNull AttributionSource attributionSource, Uri url, Bundle args, ICancellationSignal cancellationSignal)184 public boolean refresh(@NonNull AttributionSource attributionSource, Uri url, Bundle args, 185 ICancellationSignal cancellationSignal) throws RemoteException { 186 throw new UnsupportedOperationException("unimplemented mock method"); 187 } 188 189 /** {@hide} */ 190 @Override checkUriPermission(@onNull AttributionSource attributionSource, Uri uri, int uid, int modeFlags)191 public int checkUriPermission(@NonNull AttributionSource attributionSource, Uri uri, int uid, 192 int modeFlags) { 193 throw new UnsupportedOperationException("unimplemented mock method call"); 194 } 195 } 196