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