blob: eb8a061833b26710dc1cc041819dac84b09182ae [file] [log] [blame]
Steve McKayecbf3c52016-01-13 17:17:39 -08001/*
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.services;
18
Steve McKayecbf3c52016-01-13 17:17:39 -080019import static com.google.common.collect.Lists.newArrayList;
20
Steve McKayecbf3c52016-01-13 17:17:39 -080021import android.net.Uri;
Steve McKayecbf3c52016-01-13 17:17:39 -080022import android.provider.DocumentsContract;
Steve McKayecbf3c52016-01-13 17:17:39 -080023import android.test.suitebuilder.annotation.MediumTest;
24
Steve McKayecbf3c52016-01-13 17:17:39 -080025import com.android.documentsui.model.DocumentInfo;
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +090026import com.android.documentsui.model.DocumentStack;
Steve McKayecbf3c52016-01-13 17:17:39 -080027
28import java.util.List;
29
30@MediumTest
Steve McKay35645432016-01-20 15:09:35 -080031public abstract class AbstractCopyJobTest<T extends CopyJob> extends AbstractJobTest<T> {
Steve McKayecbf3c52016-01-13 17:17:39 -080032
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 Mikolajewskib8436af2016-01-25 16:20:15 +0900113 createJob(newArrayList(testDir),
114 DocumentsContract.buildDocumentUri(AUTHORITY, mSrcRoot.documentId),
115 testDir).run();
Steve McKayecbf3c52016-01-13 17:17:39 -0800116
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 McKay35645432016-01-20 15:09:35 -0800126 Uri destDir = mDocs.createFolder(testDir, "theDescendent");
Steve McKayecbf3c52016-01-13 17:17:39 -0800127
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900128 createJob(newArrayList(testDir),
129 DocumentsContract.buildDocumentUri(AUTHORITY, mSrcRoot.documentId),
130 destDir).run();
Steve McKayecbf3c52016-01-13 17:17:39 -0800131
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 McKay35645432016-01-20 15:09:35 -0800155 /**
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900156 * 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 McKay35645432016-01-20 15:09:35 -0800159 */
160 final T createJob(List<Uri> srcs) throws Exception {
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900161 Uri srcParent = DocumentsContract.buildDocumentUri(AUTHORITY, mSrcRoot.documentId);
Steve McKayecbf3c52016-01-13 17:17:39 -0800162 Uri destination = DocumentsContract.buildDocumentUri(AUTHORITY, mDestRoot.documentId);
Tomasz Mikolajewskib8436af2016-01-25 16:20:15 +0900163 return createJob(srcs, srcParent, destination);
Steve McKayecbf3c52016-01-13 17:17:39 -0800164 }
Steve McKayecbf3c52016-01-13 17:17:39 -0800165}