Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [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.services; |
| 18 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 19 | import static com.google.common.collect.Lists.newArrayList; |
| 20 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 21 | import android.net.Uri; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 22 | import android.provider.DocumentsContract; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 23 | import android.test.suitebuilder.annotation.MediumTest; |
| 24 | |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 25 | import com.android.documentsui.model.DocumentInfo; |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 26 | import com.android.documentsui.model.DocumentStack; |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 27 | |
| 28 | import java.util.List; |
| 29 | |
| 30 | @MediumTest |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 31 | public abstract class AbstractCopyJobTest<T extends CopyJob> extends AbstractJobTest<T> { |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 32 | |
| 33 | public void runCopyFilesTest() throws Exception { |
| 34 | Uri testFile1 = mDocs.createDocument(mSrcRoot, "text/plain", "test1.txt"); |
| 35 | mDocs.writeDocument(testFile1, HAM_BYTES); |
| 36 | |
| 37 | Uri testFile2 = mDocs.createDocument(mSrcRoot, "text/plain", "test2.txt"); |
| 38 | mDocs.writeDocument(testFile2, FRUITY_BYTES); |
| 39 | |
| 40 | createJob(newArrayList(testFile1, testFile2)).run(); |
| 41 | mJobListener.waitForFinished(); |
| 42 | |
| 43 | mDocs.assertChildCount(mDestRoot, 2); |
| 44 | mDocs.assertHasFile(mDestRoot, "test1.txt"); |
| 45 | mDocs.assertHasFile(mDestRoot, "test2.txt"); |
| 46 | mDocs.assertFileContents(mDestRoot.documentId, "test1.txt", HAM_BYTES); |
| 47 | mDocs.assertFileContents(mDestRoot.documentId, "test2.txt", FRUITY_BYTES); |
| 48 | } |
| 49 | |
| 50 | public void runCopyVirtualTypedFileTest() throws Exception { |
| 51 | Uri testFile = mDocs.createVirtualFile( |
| 52 | mSrcRoot, "/virtual.sth", "virtual/mime-type", |
| 53 | FRUITY_BYTES, "application/pdf", "text/html"); |
| 54 | |
| 55 | createJob(newArrayList(testFile)).run(); |
| 56 | |
| 57 | mJobListener.waitForFinished(); |
| 58 | |
| 59 | mDocs.assertChildCount(mDestRoot, 1); |
| 60 | mDocs.assertHasFile(mDestRoot, "virtual.sth.pdf"); // copy should convert file to PDF. |
| 61 | mDocs.assertFileContents(mDestRoot.documentId, "virtual.sth.pdf", FRUITY_BYTES); |
| 62 | } |
| 63 | |
| 64 | public void runCopyVirtualNonTypedFileTest() throws Exception { |
| 65 | Uri testFile = mDocs.createVirtualFile( |
| 66 | mSrcRoot, "/virtual.sth", "virtual/mime-type", |
| 67 | FRUITY_BYTES); |
| 68 | |
| 69 | createJob(newArrayList(testFile)).run(); |
| 70 | |
| 71 | mJobListener.waitForFinished(); |
| 72 | mJobListener.assertFailed(); |
| 73 | mJobListener.assertFilesFailed(newArrayList("virtual.sth")); |
| 74 | |
| 75 | mDocs.assertChildCount(mDestRoot, 0); |
| 76 | } |
| 77 | |
| 78 | public void runCopyEmptyDirTest() throws Exception { |
| 79 | Uri testDir = mDocs.createFolder(mSrcRoot, "emptyDir"); |
| 80 | |
| 81 | createJob(newArrayList(testDir)).run(); |
| 82 | mJobListener.waitForFinished(); |
| 83 | |
| 84 | mDocs.assertChildCount(mDestRoot, 1); |
| 85 | mDocs.assertHasDirectory(mDestRoot, "emptyDir"); |
| 86 | } |
| 87 | |
| 88 | public void runCopyDirRecursivelyTest() throws Exception { |
| 89 | |
| 90 | Uri testDir1 = mDocs.createFolder(mSrcRoot, "dir1"); |
| 91 | mDocs.createDocument(testDir1, "text/plain", "test1.txt"); |
| 92 | |
| 93 | Uri testDir2 = mDocs.createFolder(testDir1, "dir2"); |
| 94 | mDocs.createDocument(testDir2, "text/plain", "test2.txt"); |
| 95 | |
| 96 | createJob(newArrayList(testDir1)).run(); |
| 97 | mJobListener.waitForFinished(); |
| 98 | |
| 99 | DocumentInfo dir1Copy = mDocs.findDocument(mDestRoot.documentId, "dir1"); |
| 100 | |
| 101 | mDocs.assertChildCount(dir1Copy.derivedUri, 2); |
| 102 | mDocs.assertHasDirectory(dir1Copy.derivedUri, "dir2"); |
| 103 | mDocs.assertHasFile(dir1Copy.derivedUri, "test1.txt"); |
| 104 | |
| 105 | DocumentInfo dir2Copy = mDocs.findDocument(dir1Copy.documentId, "dir2"); |
| 106 | mDocs.assertChildCount(dir2Copy.derivedUri, 1); |
| 107 | mDocs.assertHasFile(dir2Copy.derivedUri, "test2.txt"); |
| 108 | } |
| 109 | |
| 110 | public void runNoCopyDirToSelfTest() throws Exception { |
| 111 | Uri testDir = mDocs.createFolder(mSrcRoot, "someDir"); |
| 112 | |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 113 | createJob(newArrayList(testDir), |
| 114 | DocumentsContract.buildDocumentUri(AUTHORITY, mSrcRoot.documentId), |
| 115 | testDir).run(); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 116 | |
| 117 | mJobListener.waitForFinished(); |
| 118 | mJobListener.assertFailed(); |
| 119 | mJobListener.assertFilesFailed(newArrayList("someDir")); |
| 120 | |
| 121 | mDocs.assertChildCount(mDestRoot, 0); |
| 122 | } |
| 123 | |
| 124 | public void runNoCopyDirToDescendentTest() throws Exception { |
| 125 | Uri testDir = mDocs.createFolder(mSrcRoot, "someDir"); |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 126 | Uri destDir = mDocs.createFolder(testDir, "theDescendent"); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 127 | |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 128 | createJob(newArrayList(testDir), |
| 129 | DocumentsContract.buildDocumentUri(AUTHORITY, mSrcRoot.documentId), |
| 130 | destDir).run(); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 131 | |
| 132 | mJobListener.waitForFinished(); |
| 133 | mJobListener.assertFailed(); |
| 134 | mJobListener.assertFilesFailed(newArrayList("someDir")); |
| 135 | |
| 136 | mDocs.assertChildCount(mDestRoot, 0); |
| 137 | } |
| 138 | |
| 139 | public void runCopyFileWithReadErrorsTest() throws Exception { |
| 140 | Uri testFile = mDocs.createDocument(mSrcRoot, "text/plain", "test1.txt"); |
| 141 | mDocs.writeDocument(testFile, HAM_BYTES); |
| 142 | |
| 143 | String testId = DocumentsContract.getDocumentId(testFile); |
| 144 | mClient.call("simulateReadErrorsForFile", testId, null); |
| 145 | |
| 146 | createJob(newArrayList(testFile)).run(); |
| 147 | |
| 148 | mJobListener.waitForFinished(); |
| 149 | mJobListener.assertFailed(); |
| 150 | mJobListener.assertFilesFailed(newArrayList("test1.txt")); |
| 151 | |
| 152 | mDocs.assertChildCount(mDestRoot, 0); |
| 153 | } |
| 154 | |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 155 | /** |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 156 | * Creates a job with a stack consisting to the default source and destination. |
| 157 | * TODO: Clean up, as mDestRoot.documentInfo may not really be the parent of |
| 158 | * srcs. |
Steve McKay | 3564543 | 2016-01-20 15:09:35 -0800 | [diff] [blame] | 159 | */ |
| 160 | final T createJob(List<Uri> srcs) throws Exception { |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 161 | Uri srcParent = DocumentsContract.buildDocumentUri(AUTHORITY, mSrcRoot.documentId); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 162 | Uri destination = DocumentsContract.buildDocumentUri(AUTHORITY, mDestRoot.documentId); |
Tomasz Mikolajewski | b8436af | 2016-01-25 16:20:15 +0900 | [diff] [blame] | 163 | return createJob(srcs, srcParent, destination); |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 164 | } |
Steve McKay | ecbf3c5 | 2016-01-13 17:17:39 -0800 | [diff] [blame] | 165 | } |