• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 com.android.documentsui.testing;
18 
19 import android.content.pm.ProviderInfo;
20 import android.database.Cursor;
21 import android.database.MatrixCursor;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.os.CancellationSignal;
25 import android.os.ParcelFileDescriptor;
26 import android.provider.DocumentsContract.Document;
27 import android.provider.DocumentsProvider;
28 
29 import com.android.documentsui.base.DocumentInfo;
30 
31 import java.io.FileNotFoundException;
32 
33 /**
34  * Test doubles of {@link DocumentsProvider} to isolate document providers. This is not registered
35  * or exposed through AndroidManifest, but only used locally.
36  */
37 public class TestDocumentsProvider extends DocumentsProvider {
38 
39     private String[] DOCUMENTS_PROJECTION = new String[] {
40             Document.COLUMN_DOCUMENT_ID,
41             Document.COLUMN_MIME_TYPE,
42             Document.COLUMN_DISPLAY_NAME,
43             Document.COLUMN_LAST_MODIFIED,
44             Document.COLUMN_FLAGS,
45             Document.COLUMN_SUMMARY,
46             Document.COLUMN_SIZE,
47             Document.COLUMN_ICON
48     };
49 
50     private Cursor mNextChildDocuments;
51 
TestDocumentsProvider(String authority)52     public TestDocumentsProvider(String authority) {
53         ProviderInfo info = new ProviderInfo();
54         info.authority = authority;
55         attachInfoForTesting(null, info);
56     }
57 
58     @Override
refresh(Uri url, Bundle args, CancellationSignal signal)59     public boolean refresh(Uri url, Bundle args, CancellationSignal signal) {
60         return true;
61     }
62 
63     @Override
queryRoots(String[] projection)64     public Cursor queryRoots(String[] projection) throws FileNotFoundException {
65         return null;
66     }
67 
68     @Override
queryDocument(String documentId, String[] projection)69     public Cursor queryDocument(String documentId, String[] projection)
70             throws FileNotFoundException {
71         return null;
72     }
73 
74     @Override
queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder)75     public Cursor queryChildDocuments(String parentDocumentId, String[] projection,
76             String sortOrder) throws FileNotFoundException {
77         return mNextChildDocuments;
78     }
79 
80     @Override
openDocument(String documentId, String mode, CancellationSignal signal)81     public ParcelFileDescriptor openDocument(String documentId, String mode,
82             CancellationSignal signal) throws FileNotFoundException {
83         return null;
84     }
85 
86     @Override
onCreate()87     public boolean onCreate() {
88         return true;
89     }
90 
91     /**
92      * Sets the next return value for {@link #queryChildDocuments(String, String[], String)}.
93      * @param docs docs to return for next query.
94      */
setNextChildDocumentsReturns(DocumentInfo... docs)95     public void setNextChildDocumentsReturns(DocumentInfo... docs) {
96         mNextChildDocuments = createDocumentsCursor(docs);
97     }
98 
createDocumentsCursor(DocumentInfo... docs)99     private Cursor createDocumentsCursor(DocumentInfo... docs) {
100         MatrixCursor cursor = new MatrixCursor(DOCUMENTS_PROJECTION);
101         for (DocumentInfo doc : docs) {
102             cursor.newRow()
103                     .add(Document.COLUMN_DOCUMENT_ID, doc.documentId)
104                     .add(Document.COLUMN_MIME_TYPE, doc.mimeType)
105                     .add(Document.COLUMN_DISPLAY_NAME, doc.displayName)
106                     .add(Document.COLUMN_LAST_MODIFIED, doc.lastModified)
107                     .add(Document.COLUMN_FLAGS, doc.flags)
108                     .add(Document.COLUMN_SUMMARY, doc.summary)
109                     .add(Document.COLUMN_SIZE, doc.size)
110                     .add(Document.COLUMN_ICON, doc.icon);
111         }
112 
113         return cursor;
114     }
115 }
116