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.Document;
23import android.provider.DocumentsContract;
24import android.test.suitebuilder.annotation.MediumTest;
25
26import com.android.documentsui.model.DocumentInfo;
27import com.android.documentsui.model.DocumentStack;
28
29import java.util.List;
30
31@MediumTest
32public class MoveJobTest extends AbstractCopyJobTest<MoveJob> {
33
34    public void testMoveFiles() throws Exception {
35        runCopyFilesTest();
36
37        mDocs.assertChildCount(mSrcRoot, 0);
38    }
39
40    public void testMoveVirtualTypedFile() throws Exception {
41        mDocs.createFolder(mSrcRoot, "hello");
42        Uri testFile = mDocs.createVirtualFile(
43                mSrcRoot, "/hello/virtual.sth", "virtual/mime-type",
44                FRUITY_BYTES, "application/pdf", "text/html");
45        createJob(newArrayList(testFile)).run();
46
47        mJobListener.waitForFinished();
48
49        // Should have failed, source not deleted. Moving by bytes for virtual files
50        // is not supported.
51        mDocs.assertChildCount(mDestRoot, 0);
52        mDocs.assertChildCount(mSrcRoot, 1);
53    }
54
55    public void testMoveVirtualNonTypedFile() throws Exception {
56        runCopyVirtualNonTypedFileTest();
57
58        // Should have failed, source not deleted.
59        mDocs.assertChildCount(mSrcRoot, 1);
60    }
61
62    public void testMove_BackendSideVirtualTypedFile_Fallback() throws Exception {
63        Uri testFile = mDocs.createDocumentWithFlags(
64                mSrcRoot.documentId, "virtual/mime-type", "tokyo.sth",
65                Document.FLAG_VIRTUAL_DOCUMENT | Document.FLAG_SUPPORTS_COPY
66                        | Document.FLAG_SUPPORTS_MOVE, "application/pdf");
67
68        createJob(newArrayList(testFile)).run();
69        mJobListener.waitForFinished();
70
71        // Should have failed, source not deleted. Moving by bytes for virtual files
72        // is not supported.
73        mDocs.assertChildCount(mDestRoot, 0);
74        mDocs.assertChildCount(mSrcRoot, 1);
75    }
76
77    public void testMoveEmptyDir() throws Exception {
78        runCopyEmptyDirTest();
79
80        mDocs.assertChildCount(mSrcRoot, 0);
81    }
82
83    public void testMoveDirRecursively() throws Exception {
84        runCopyDirRecursivelyTest();
85
86        mDocs.assertChildCount(mSrcRoot, 0);
87    }
88
89    public void testNoMoveDirToSelf() throws Exception {
90        runNoCopyDirToSelfTest();
91
92        // should have failed, source not deleted
93        mDocs.assertChildCount(mSrcRoot, 1);
94    }
95
96    public void testNoMoveDirToDescendent() throws Exception {
97        runNoCopyDirToDescendentTest();
98
99        // should have failed, source not deleted
100        mDocs.assertChildCount(mSrcRoot, 1);
101    }
102
103    public void testMoveFileWithReadErrors() throws Exception {
104        runCopyFileWithReadErrorsTest();
105
106        // should have failed, source not deleted
107        mDocs.assertChildCount(mSrcRoot, 1);
108    }
109
110    // TODO: Add test cases for moving when multi-parented.
111
112    @Override
113    MoveJob createJob(List<DocumentInfo> srcs, DocumentInfo srcParent, DocumentStack stack)
114            throws Exception {
115        return new MoveJob(
116                mContext, mContext, mJobListener, FileOperations.createJobId(), stack, srcs,
117                srcParent);
118    }
119}
120