DocumentsProviderHelper.java revision 2ed9f813b0870cfa11032aa9c9c10be0bee4b64b
1/* 2 * Copyright (C) 2015 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 17package com.android.documentsui; 18 19import static android.provider.DocumentsContract.buildChildDocumentsUri; 20import static android.provider.DocumentsContract.buildDocumentUri; 21import static android.provider.DocumentsContract.buildRootsUri; 22import static com.android.documentsui.model.DocumentInfo.getCursorString; 23import static com.android.internal.util.Preconditions.checkArgument; 24import static junit.framework.Assert.assertEquals; 25import static junit.framework.Assert.assertNotNull; 26import static junit.framework.Assert.fail; 27 28import android.content.ContentProviderClient; 29import android.database.Cursor; 30import android.net.Uri; 31import android.os.Bundle; 32import android.os.ParcelFileDescriptor; 33import android.os.ParcelFileDescriptor.AutoCloseInputStream; 34import android.os.ParcelFileDescriptor.AutoCloseOutputStream; 35import android.os.RemoteException; 36import android.provider.DocumentsContract; 37import android.provider.DocumentsContract.Document; 38import android.provider.DocumentsContract.Root; 39import android.support.annotation.Nullable; 40import android.test.MoreAsserts; 41import android.text.TextUtils; 42 43import com.android.documentsui.model.DocumentInfo; 44import com.android.documentsui.model.RootInfo; 45 46import com.google.android.collect.Lists; 47 48import libcore.io.IoUtils; 49import libcore.io.Streams; 50 51import java.io.IOException; 52import java.util.ArrayList; 53import java.util.List; 54 55/** 56 * Provides support for creation of documents in a test settings. 57 */ 58public class DocumentsProviderHelper { 59 60 private final String mAuthority; 61 private final ContentProviderClient mClient; 62 63 public DocumentsProviderHelper(String authority, ContentProviderClient client) { 64 checkArgument(!TextUtils.isEmpty(authority)); 65 mAuthority = authority; 66 mClient = client; 67 } 68 69 public RootInfo getRoot(String documentId) throws RemoteException { 70 final Uri rootsUri = buildRootsUri(mAuthority); 71 Cursor cursor = null; 72 try { 73 cursor = mClient.query(rootsUri, null, null, null, null); 74 while (cursor.moveToNext()) { 75 if (documentId.equals(getCursorString(cursor, Root.COLUMN_ROOT_ID))) { 76 return RootInfo.fromRootsCursor(mAuthority, cursor); 77 } 78 } 79 throw new IllegalArgumentException("Can't find matching root for id=" + documentId); 80 } catch (Exception e) { 81 throw new RuntimeException("Can't load root for id=" + documentId , e); 82 } finally { 83 IoUtils.closeQuietly(cursor); 84 } 85 } 86 87 public Uri createDocument(Uri parentUri, String mimeType, String name) { 88 if (name.contains("/")) { 89 throw new IllegalArgumentException("Name and mimetype probably interposed."); 90 } 91 try { 92 Uri uri = DocumentsContract.createDocument(mClient, parentUri, mimeType, name); 93 return uri; 94 } catch (RemoteException e) { 95 throw new RuntimeException("Couldn't create document: " + name + " with mimetype " + mimeType, e); 96 } 97 } 98 99 public Uri createDocument(String parentId, String mimeType, String name) { 100 Uri parentUri = buildDocumentUri(mAuthority, parentId); 101 return createDocument(parentUri, mimeType, name); 102 } 103 104 public Uri createDocument(RootInfo root, String mimeType, String name) { 105 return createDocument(root.documentId, mimeType, name); 106 } 107 108 public Uri createFolder(Uri parentUri, String name) { 109 return createDocument(parentUri, Document.MIME_TYPE_DIR, name); 110 } 111 112 public Uri createFolder(String parentId, String name) { 113 Uri parentUri = buildDocumentUri(mAuthority, parentId); 114 return createDocument(parentUri, Document.MIME_TYPE_DIR, name); 115 } 116 117 public Uri createFolder(RootInfo root, String name) { 118 return createDocument(root, Document.MIME_TYPE_DIR, name); 119 } 120 121 public void writeDocument(Uri documentUri, byte[] contents) 122 throws RemoteException, IOException { 123 ParcelFileDescriptor file = mClient.openFile(documentUri, "w", null); 124 try (AutoCloseOutputStream out = new AutoCloseOutputStream(file)) { 125 out.write(contents, 0, contents.length); 126 } 127 } 128 129 public byte[] readDocument(Uri documentUri) throws RemoteException, IOException { 130 ParcelFileDescriptor file = mClient.openFile(documentUri, "r", null); 131 byte[] buf = null; 132 try (AutoCloseInputStream in = new AutoCloseInputStream(file)) { 133 buf = Streams.readFully(in); 134 } 135 return buf; 136 } 137 138 public void assertChildCount(Uri parentUri, int expected) throws Exception { 139 List<DocumentInfo> children = listChildren(parentUri); 140 assertEquals("Incorrect file count after copy", expected, children.size()); 141 } 142 143 public void assertChildCount(String parentId, int expected) throws Exception { 144 List<DocumentInfo> children = listChildren(parentId); 145 assertEquals("Incorrect file count after copy", expected, children.size()); 146 } 147 148 public void assertChildCount(RootInfo root, int expected) throws Exception { 149 assertChildCount(root.documentId, expected); 150 } 151 152 public void assertHasFile(Uri parentUri, String name) throws Exception { 153 List<DocumentInfo> children = listChildren(parentUri); 154 for (DocumentInfo child : children) { 155 if (name.equals(child.displayName) && !child.isDirectory()) { 156 return; 157 } 158 } 159 fail("Could not find file named=" + name + " in children " + children); 160 } 161 162 public void assertHasFile(String parentId, String name) throws Exception { 163 Uri parentUri = buildDocumentUri(mAuthority, parentId); 164 assertHasFile(parentUri, name); 165 } 166 167 public void assertHasFile(RootInfo root, String name) throws Exception { 168 assertHasFile(root.documentId, name); 169 } 170 171 public void assertHasDirectory(Uri parentUri, String name) throws Exception { 172 List<DocumentInfo> children = listChildren(parentUri); 173 for (DocumentInfo child : children) { 174 if (name.equals(child.displayName) && child.isDirectory()) { 175 return; 176 } 177 } 178 fail("Could not find name=" + name + " in children " + children); 179 } 180 181 public void assertHasDirectory(String parentId, String name) throws Exception { 182 Uri parentUri = buildDocumentUri(mAuthority, parentId); 183 assertHasDirectory(parentUri, name); 184 } 185 186 public void assertHasDirectory(RootInfo root, String name) throws Exception { 187 assertHasDirectory(root.documentId, name); 188 } 189 190 public void assertDoesNotExist(Uri parentUri, String name) throws Exception { 191 List<DocumentInfo> children = listChildren(parentUri); 192 for (DocumentInfo child : children) { 193 if (name.equals(child.displayName)) { 194 fail("Found name=" + name + " in children " + children); 195 } 196 } 197 } 198 199 public void assertDoesNotExist(String parentId, String name) throws Exception { 200 Uri parentUri = buildDocumentUri(mAuthority, parentId); 201 assertDoesNotExist(parentUri, name); 202 } 203 204 public void assertDoesNotExist(RootInfo root, String name) throws Exception { 205 assertDoesNotExist(root.getUri(), name); 206 } 207 208 public @Nullable DocumentInfo findFile(String parentId, String name) 209 throws Exception { 210 List<DocumentInfo> children = listChildren(parentId); 211 for (DocumentInfo child : children) { 212 if (name.equals(child.displayName)) { 213 return child; 214 } 215 } 216 return null; 217 } 218 219 public DocumentInfo findDocument(String parentId, String name) throws Exception { 220 List<DocumentInfo> children = listChildren(parentId); 221 for (DocumentInfo child : children) { 222 if (name.equals(child.displayName)) { 223 return child; 224 } 225 } 226 return null; 227 } 228 229 public DocumentInfo findDocument(Uri parentUri, String name) throws Exception { 230 List<DocumentInfo> children = listChildren(parentUri); 231 for (DocumentInfo child : children) { 232 if (name.equals(child.displayName)) { 233 return child; 234 } 235 } 236 return null; 237 } 238 239 public List<DocumentInfo> listChildren(Uri parentUri) throws Exception { 240 String id = DocumentsContract.getDocumentId(parentUri); 241 return listChildren(id); 242 } 243 244 public List<DocumentInfo> listChildren(String documentId) throws Exception { 245 Uri uri = buildChildDocumentsUri(mAuthority, documentId); 246 List<DocumentInfo> children = new ArrayList<>(); 247 try (Cursor cursor = mClient.query(uri, null, null, null, null, null)) { 248 Cursor wrapper = new RootCursorWrapper(mAuthority, "totally-fake", cursor, 100); 249 while (wrapper.moveToNext()) { 250 children.add(DocumentInfo.fromDirectoryCursor(wrapper)); 251 } 252 } 253 return children; 254 } 255 256 public void assertFileContents(Uri documentUri, byte[] expected) throws Exception { 257 MoreAsserts.assertEquals( 258 "Copied file contents differ", 259 expected, readDocument(documentUri)); 260 } 261 262 public void assertFileContents(String parentId, String fileName, byte[] expected) 263 throws Exception { 264 DocumentInfo file = findFile(parentId, fileName); 265 assertNotNull(file); 266 assertFileContents(file.derivedUri, expected); 267 } 268 269 /** 270 * A helper method for StubProvider only. Won't work with other providers. 271 * @throws RemoteException 272 */ 273 public Uri createVirtualFile( 274 RootInfo root, String path, String mimeType, byte[] content, String... streamTypes) 275 throws RemoteException { 276 277 Bundle args = new Bundle(); 278 args.putString(StubProvider.EXTRA_ROOT, root.rootId); 279 args.putString(StubProvider.EXTRA_PATH, path); 280 args.putString(Document.COLUMN_MIME_TYPE, mimeType); 281 args.putStringArrayList(StubProvider.EXTRA_STREAM_TYPES, Lists.newArrayList(streamTypes)); 282 args.putByteArray(StubProvider.EXTRA_CONTENT, content); 283 284 Bundle result = mClient.call("createVirtualFile", null, args); 285 String documentId = result.getString(Document.COLUMN_DOCUMENT_ID); 286 287 return DocumentsContract.buildDocumentUri(mAuthority, documentId); 288 } 289} 290