blob: 4ca606cc641663f3040e090cf8bacb3c58a2f4e4 [file] [log] [blame]
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.documentsui;
import static com.android.documentsui.StubProvider.ROOT_0_ID;
import static com.android.documentsui.StubProvider.ROOT_1_ID;
import static org.junit.Assert.fail;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import androidx.test.filters.LargeTest;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.Shared;
import com.android.documentsui.files.FilesActivity;
import com.android.documentsui.filters.HugeLongTest;
import com.android.documentsui.rules.TestFilesRule;
import com.android.documentsui.sorting.SortDimension;
import com.android.documentsui.sorting.SortModel;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import java.util.List;
@LargeTest
public class FileManagementUiTest extends ActivityTestJunit4<FilesActivity> {
@Rule
public final TestFilesRule mTestFilesRule =
new TestFilesRule()
.createFolderInRoot(ROOT_0_ID, TestFilesRule.DIR_NAME_1)
.createFolderWithParent(TestFilesRule.DIR_NAME_1, "ChildDir1")
.createFileInRoot(ROOT_0_ID, "file0.log", "text/plain")
.createFileInRoot(ROOT_0_ID, "file1.png", "image/png")
.createFileInRoot(ROOT_0_ID, "file2.csv", "text/csv")
.createFileInRoot(ROOT_0_ID, "anotherFile0.log", "text/plain")
.createFileInRoot(ROOT_0_ID, "poodles.text", "text/plain");
@Ignore
@Test
public void testCreateDirectory() throws Exception {
bots.main.openOverflowMenu();
device.waitForIdle();
bots.main.clickToolbarOverflowItem("New folder");
device.waitForIdle();
bots.main.setDialogText("Kung Fu Panda");
device.waitForIdle();
bots.keyboard.pressEnter();
bots.directory.waitForDocument("Kung Fu Panda");
}
@Test
public void testDeleteDocument() throws Exception {
bots.directory.selectDocument("file1.png", 1);
device.waitForIdle();
bots.main.clickToolbarItem(R.id.action_menu_delete);
bots.main.clickDialogOkButton(/* closeSoftKeyboard */ false);
device.waitForIdle();
bots.directory.assertDocumentsAbsent("file1.png");
}
@HugeLongTest
@Test
public void testKeyboard_CutDocument() throws Exception {
bots.directory.selectDocument("file1.png", 1);
device.waitForIdle();
bots.keyboard.pressKey(KeyEvent.KEYCODE_X, KeyEvent.META_CTRL_ON);
device.waitForIdle();
bots.roots.openRoot(ROOT_1_ID);
bots.keyboard.pressKey(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON);
bots.directory.waitForDocument("file1.png");
bots.directory.assertDocumentsVisible("file1.png");
bots.roots.openRoot(ROOT_0_ID);
bots.directory.assertDocumentsAbsent("file1.png");
}
@HugeLongTest
@Test
public void testKeyboard_CopyDocument() throws Exception {
bots.directory.selectDocument("file1.png", 1);
device.waitForIdle();
bots.keyboard.pressKey(KeyEvent.KEYCODE_C, KeyEvent.META_CTRL_ON);
device.waitForIdle();
bots.roots.openRoot(ROOT_1_ID);
bots.keyboard.pressKey(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON);
bots.directory.waitForDocument("file1.png");
bots.roots.openRoot(ROOT_0_ID);
bots.directory.waitForDocument("file1.png");
}
@HugeLongTest
@Test
public void testKeyboard_PasteDocumentWhileSelectionActive() throws Exception {
bots.directory.selectDocument("file1.png", 1);
bots.keyboard.pressKey(KeyEvent.KEYCODE_C, KeyEvent.META_CTRL_ON);
device.waitForIdle();
bots.directory.openDocument("Dir1");
bots.directory.selectDocument("ChildDir1", 1);
bots.keyboard.pressKey(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON);
device.waitForIdle();
bots.directory.assertDocumentsVisible("file1.png");
}
@Test
public void testDeleteDocument_Cancel() throws Exception {
bots.directory.selectDocument("file1.png", 1);
device.waitForIdle();
bots.main.clickToolbarItem(R.id.action_menu_delete);
bots.main.clickDialogCancelButton(/* closeSoftKeyboard */ false);
bots.directory.waitForDocument("file1.png");
}
@HugeLongTest
@Test
public void testCopyLargeAmountOfFiles() throws Exception {
// Suppress root notification. We're gonna create tons of files and it will soon crash
// DocsUI because too many root refreshes are queued in an executor.
Bundle conf = new Bundle();
conf.putBoolean(StubProvider.EXTRA_ENABLE_ROOT_NOTIFICATION, false);
mDocsHelper.configure(null, conf);
final Uri test = mDocsHelper.createFolder(rootDir0, "test");
final Uri target = mDocsHelper.createFolder(rootDir0, "target");
String nameOfLastFile = "";
for (int i = 0; i <= Shared.MAX_DOCS_IN_INTENT; ++i) {
final String name = i + ".txt";
final Uri doc =
mDocsHelper.createDocument(test, "text/plain", name);
mDocsHelper.writeDocument(doc, Integer.toString(i).getBytes());
nameOfLastFile = nameOfLastFile.compareTo(name) < 0 ? name : nameOfLastFile;
}
bots.roots.openRoot(ROOT_0_ID);
bots.directory.openDocument("test");
bots.sort.sortBy(
SortModel.SORT_DIMENSION_ID_TITLE, SortDimension.SORT_DIRECTION_ASCENDING);
bots.directory.waitForDocument("0.txt");
bots.keyboard.pressKey(
KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON);
bots.keyboard.pressKey(
KeyEvent.KEYCODE_C, KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON);
bots.roots.openRoot(ROOT_0_ID);
bots.directory.openDocument("target");
bots.directory.pasteFilesFromClipboard();
// Use these 2 events as a signal that many files have already been copied. Only considering
// Android devices a more reliable way is to wait until notification goes away, but ARC++
// uses Chrome OS notifications so it isn't even an option.
bots.directory.waitForDocument("0.txt");
bots.directory.waitForDocument(nameOfLastFile);
final int expectedCount = Shared.MAX_DOCS_IN_INTENT + 1;
List<DocumentInfo> children = mDocsHelper.listChildren(target, -1);
if (children.size() == expectedCount) {
return;
}
// Files weren't copied fast enough, so gonna do some polling until they all arrive or copy
// seems stalled.
int maxRetries = 10;
int retries = 0;
while (retries++ <= maxRetries) {
Thread.sleep(200);
List<DocumentInfo> newChildren = mDocsHelper.listChildren(target, -1);
if (newChildren.size() == expectedCount) {
return;
}
if (newChildren.size() > expectedCount && retries >= maxRetries) {
// Should never happen
fail("Something wrong with this test case. Copied file count "
+ newChildren.size() + " exceeds expected number " + expectedCount);
}
if (newChildren.size() <= children.size() && retries >= maxRetries) {
fail("Only copied " + children.size()
+ " files, expected to copy " + expectedCount + " files.");
}
children = newChildren;
}
}
}