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