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