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 
19 import static com.android.documentsui.StubProvider.ROOT_0_ID;
20 import static com.android.documentsui.StubProvider.ROOT_1_ID;
21 
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.os.RemoteException;
25 import android.view.KeyEvent;
26 
27 import androidx.test.filters.LargeTest;
28 import androidx.test.filters.Suppress;
29 
30 import com.android.documentsui.base.DocumentInfo;
31 import com.android.documentsui.base.Shared;
32 import com.android.documentsui.files.FilesActivity;
33 import com.android.documentsui.filters.HugeLongTest;
34 import com.android.documentsui.sorting.SortDimension;
35 import com.android.documentsui.sorting.SortModel;
36 
37 import java.util.List;
38 
39 @LargeTest
40 public class FileManagementUiTest extends ActivityTest<FilesActivity> {
41 
FileManagementUiTest()42     public FileManagementUiTest() {
43         super(FilesActivity.class);
44     }
45 
46     @Override
setUp()47     public void setUp() throws Exception {
48         super.setUp();
49         initTestFiles();
50     }
51 
52     @Override
initTestFiles()53     public void initTestFiles() throws RemoteException {
54         Uri uri = mDocsHelper.createFolder(rootDir0, dirName1);
55         mDocsHelper.createFolder(uri, childDir1);
56 
57         mDocsHelper.createDocument(rootDir0, "text/plain", "file0.log");
58         mDocsHelper.createDocument(rootDir0, "image/png", "file1.png");
59         mDocsHelper.createDocument(rootDir0, "text/csv", "file2.csv");
60 
61         mDocsHelper.createDocument(rootDir1, "text/plain", "anotherFile0.log");
62         mDocsHelper.createDocument(rootDir1, "text/plain", "poodles.text");
63     }
64 
65     @Suppress
testCreateDirectory()66     public void testCreateDirectory() throws Exception {
67         bots.main.openOverflowMenu();
68         device.waitForIdle();
69 
70         bots.main.clickToolbarOverflowItem("New folder");
71         device.waitForIdle();
72 
73         bots.main.setDialogText("Kung Fu Panda");
74         device.waitForIdle();
75 
76         bots.keyboard.pressEnter();
77 
78         bots.directory.waitForDocument("Kung Fu Panda");
79     }
80 
testDeleteDocument()81     public void testDeleteDocument() throws Exception {
82         bots.directory.selectDocument("file1.png", 1);
83         device.waitForIdle();
84         bots.main.clickToolbarItem(R.id.action_menu_delete);
85 
86         bots.main.clickDialogOkButton();
87         device.waitForIdle();
88 
89         bots.directory.assertDocumentsAbsent("file1.png");
90     }
91 
92     @HugeLongTest
testKeyboard_CutDocument()93     public void testKeyboard_CutDocument() throws Exception {
94         bots.directory.selectDocument("file1.png", 1);
95         device.waitForIdle();
96         bots.keyboard.pressKey(KeyEvent.KEYCODE_X, KeyEvent.META_CTRL_ON);
97 
98         device.waitForIdle();
99 
100         bots.roots.openRoot(ROOT_1_ID);
101         bots.keyboard.pressKey(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON);
102 
103         bots.directory.waitForDocument("file1.png");
104         bots.directory.assertDocumentsPresent("file1.png");
105 
106         bots.roots.openRoot(ROOT_0_ID);
107         bots.directory.assertDocumentsAbsent("file1.png");
108     }
109 
110     @HugeLongTest
testKeyboard_CopyDocument()111     public void testKeyboard_CopyDocument() throws Exception {
112         bots.directory.selectDocument("file1.png", 1);
113         device.waitForIdle();
114         bots.keyboard.pressKey(KeyEvent.KEYCODE_C, KeyEvent.META_CTRL_ON);
115 
116         device.waitForIdle();
117 
118         bots.roots.openRoot(ROOT_1_ID);
119         bots.keyboard.pressKey(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON);
120 
121         bots.directory.waitForDocument("file1.png");
122 
123         bots.roots.openRoot(ROOT_0_ID);
124         bots.directory.waitForDocument("file1.png");
125     }
126 
testDeleteDocument_Cancel()127     public void testDeleteDocument_Cancel() throws Exception {
128         bots.directory.selectDocument("file1.png", 1);
129         device.waitForIdle();
130         bots.main.clickToolbarItem(R.id.action_menu_delete);
131 
132         bots.main.clickDialogCancelButton();
133 
134         bots.directory.waitForDocument("file1.png");
135     }
136 
137     @HugeLongTest
testCopyLargeAmountOfFiles()138     public void testCopyLargeAmountOfFiles() throws Exception {
139         // Suppress root notification. We're gonna create tons of files and it will soon crash
140         // DocsUI because too many root refreshes are queued in an executor.
141         Bundle conf = new Bundle();
142         conf.putBoolean(StubProvider.EXTRA_ENABLE_ROOT_NOTIFICATION, false);
143         mDocsHelper.configure(null, conf);
144 
145         final Uri test = mDocsHelper.createFolder(rootDir0, "test");
146         final Uri target = mDocsHelper.createFolder(rootDir0, "target");
147         String nameOfLastFile = "";
148         for (int i = 0; i <= Shared.MAX_DOCS_IN_INTENT; ++i) {
149             final String name = i + ".txt";
150             final Uri doc =
151                     mDocsHelper.createDocument(test, "text/plain", name);
152             mDocsHelper.writeDocument(doc, Integer.toString(i).getBytes());
153             nameOfLastFile = nameOfLastFile.compareTo(name) < 0 ? name : nameOfLastFile;
154         }
155 
156         bots.roots.openRoot(ROOT_0_ID);
157         bots.directory.openDocument("test");
158         bots.sort.sortBy(
159                 SortModel.SORT_DIMENSION_ID_TITLE, SortDimension.SORT_DIRECTION_ASCENDING);
160         bots.directory.waitForDocument("0.txt");
161         bots.keyboard.pressKey(
162                 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON);
163         bots.keyboard.pressKey(
164                 KeyEvent.KEYCODE_C, KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON);
165 
166         bots.roots.openRoot(ROOT_0_ID);
167         bots.directory.openDocument("target");
168         bots.directory.pasteFilesFromClipboard();
169 
170         // Use these 2 events as a signal that many files have already been copied. Only considering
171         // Android devices a more reliable way is to wait until notification goes away, but ARC++
172         // uses Chrome OS notifications so it isn't even an option.
173         bots.directory.waitForDocument("0.txt");
174         bots.directory.waitForDocument(nameOfLastFile);
175 
176         final int expectedCount = Shared.MAX_DOCS_IN_INTENT + 1;
177         List<DocumentInfo> children = mDocsHelper.listChildren(target, -1);
178         if (children.size() == expectedCount) {
179             return;
180         }
181 
182         // Files weren't copied fast enough, so gonna do some polling until they all arrive or copy
183         // seems stalled.
184         int maxRetries = 10;
185         int retries = 0;
186         while (retries++ <= maxRetries) {
187             Thread.sleep(200);
188             List<DocumentInfo> newChildren = mDocsHelper.listChildren(target, -1);
189             if (newChildren.size() == expectedCount) {
190                 return;
191             }
192 
193             if (newChildren.size() > expectedCount && retries >= maxRetries) {
194                 // Should never happen
195                 fail("Something wrong with this test case. Copied file count "
196                         + newChildren.size() + " exceeds expected number " + expectedCount);
197             }
198 
199             if (newChildren.size() <= children.size() && retries >= maxRetries) {
200                 fail("Only copied " + children.size()
201                         + " files, expected to copy " + expectedCount + " files.");
202             }
203 
204             children = newChildren;
205         }
206     }
207 }
208