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 package com.android.documentsui.testing; 17 18 import static junit.framework.Assert.assertEquals; 19 20 import android.net.Uri; 21 import android.os.RemoteException; 22 import android.provider.DocumentsContract; 23 import android.provider.DocumentsContract.Path; 24 import android.util.Pair; 25 26 import com.android.documentsui.DocumentsAccess; 27 import com.android.documentsui.base.DocumentInfo; 28 import com.android.documentsui.base.RootInfo; 29 import com.android.documentsui.base.UserId; 30 31 import java.util.List; 32 33 import javax.annotation.Nullable; 34 35 public class TestDocumentsAccess implements DocumentsAccess { 36 37 public @Nullable DocumentInfo nextRootDocument; 38 public @Nullable DocumentInfo nextDocument; 39 public @Nullable List<DocumentInfo> nextDocuments; 40 41 public boolean nextIsDocumentsUri; 42 public @Nullable Path nextPath; 43 44 public TestEventHandler<Uri> lastUri = new TestEventHandler<>(); 45 46 private Pair<DocumentInfo, DocumentInfo> mLastCreatedDoc; 47 48 @Override getRootDocument(RootInfo root)49 public DocumentInfo getRootDocument(RootInfo root) { 50 return nextRootDocument; 51 } 52 53 @Override getDocument(Uri uri, UserId userId)54 public DocumentInfo getDocument(Uri uri, UserId userId) { 55 return nextDocument; 56 } 57 58 @Override getDocuments(UserId userId, String authority, List<String> docIds)59 public List<DocumentInfo> getDocuments(UserId userId, String authority, List<String> docIds) { 60 return nextDocuments; 61 } 62 63 @Override createDocument(DocumentInfo parentDoc, String mimeType, String displayName)64 public Uri createDocument(DocumentInfo parentDoc, String mimeType, String displayName) { 65 final DocumentInfo child = new DocumentInfo(); 66 child.authority = parentDoc.authority; 67 child.mimeType = mimeType; 68 child.displayName = displayName; 69 child.documentId = displayName; 70 child.derivedUri = DocumentsContract.buildDocumentUri(child.authority, displayName); 71 72 mLastCreatedDoc = Pair.create(parentDoc, child); 73 74 return child.derivedUri; 75 } 76 77 @Override getArchiveDocument(Uri uri, UserId userId)78 public DocumentInfo getArchiveDocument(Uri uri, UserId userId) { 79 return nextDocument; 80 } 81 82 @Override isDocumentUri(Uri uri)83 public boolean isDocumentUri(Uri uri) { 84 return nextIsDocumentsUri; 85 } 86 87 @Override findDocumentPath(Uri docUri, UserId userId)88 public Path findDocumentPath(Uri docUri, UserId userId) throws RemoteException { 89 lastUri.accept(docUri); 90 return nextPath; 91 } 92 assertCreatedDocument(DocumentInfo parent, String mimeType, String displayName)93 public void assertCreatedDocument(DocumentInfo parent, String mimeType, String displayName) { 94 assertEquals(parent, mLastCreatedDoc.first); 95 assertEquals(mimeType, mLastCreatedDoc.second.mimeType); 96 assertEquals(displayName, mLastCreatedDoc.second.displayName); 97 } 98 getLastCreatedDocumentUri()99 public @Nullable Uri getLastCreatedDocumentUri() { 100 return mLastCreatedDoc.second.derivedUri; 101 } 102 } 103