1 /* 2 * Copyright (C) 2016 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.provider; 18 19 import android.annotation.Nullable; 20 import android.app.AppOpsManager; 21 import android.content.Context; 22 import android.content.ContextWrapper; 23 import android.content.pm.ProviderInfo; 24 import android.database.Cursor; 25 import android.net.Uri; 26 import android.os.CancellationSignal; 27 import android.os.IBinder; 28 import android.os.ParcelFileDescriptor; 29 import android.provider.DocumentsContract.Path; 30 31 import org.mockito.Mockito; 32 33 import java.io.FileNotFoundException; 34 35 /** 36 * Provides a test double of {@link DocumentsProvider}. 37 */ 38 public class TestDocumentsProvider extends DocumentsProvider { 39 public static final String AUTHORITY = "android.provider.TestDocumentsProvider"; 40 41 public Path nextPath; 42 43 public boolean nextIsChildDocument; 44 45 public String lastDocumentId; 46 public String lastParentDocumentId; 47 48 @Override 49 public void attachInfoForTesting(Context context, ProviderInfo info) { 50 context = new TestContext(context); 51 super.attachInfoForTesting(context, info); 52 } 53 54 @Override 55 public boolean onCreate() { 56 return true; 57 } 58 59 @Override 60 public Cursor queryRoots(String[] projection) throws FileNotFoundException { 61 return null; 62 } 63 64 @Override 65 public Cursor queryDocument(String documentId, String[] projection) 66 throws FileNotFoundException { 67 return null; 68 } 69 70 @Override 71 public Cursor queryChildDocuments(String parentDocumentId, String[] projection, 72 String sortOrder) throws FileNotFoundException { 73 return null; 74 } 75 76 @Override 77 public ParcelFileDescriptor openDocument(String documentId, String mode, 78 CancellationSignal signal) throws FileNotFoundException { 79 return null; 80 } 81 82 @Override 83 public boolean isChildDocument(String parentDocumentId, String documentId) { 84 return nextIsChildDocument; 85 } 86 87 @Override 88 public Path findDocumentPath(@Nullable String parentDocumentId, String documentId) { 89 lastDocumentId = documentId; 90 lastParentDocumentId = parentDocumentId; 91 92 return nextPath; 93 } 94 95 @Override 96 protected int enforceReadPermissionInner(Uri uri, String callingPkg, IBinder callerToken) { 97 return AppOpsManager.MODE_ALLOWED; 98 } 99 100 @Override 101 protected int enforceWritePermissionInner(Uri uri, String callingPkg, IBinder callerToken) { 102 return AppOpsManager.MODE_ALLOWED; 103 } 104 105 private static class TestContext extends ContextWrapper { 106 107 private TestContext(Context context) { 108 super(context); 109 } 110 111 @Override 112 public void enforceCallingPermission(String permission, String message) { 113 // Always granted 114 } 115 116 @Override 117 public Object getSystemService(String name) { 118 if (Context.APP_OPS_SERVICE.equals(name)) { 119 return Mockito.mock(AppOpsManager.class); 120 } 121 122 return super.getSystemService(name); 123 } 124 } 125 } 126