1 /* 2 * Copyright (C) 2016 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 android.support.test.uiautomator.UiObjectNotFoundException; 20 21 import androidx.test.filters.LargeTest; 22 23 import com.android.documentsui.files.FilesActivity; 24 import com.android.documentsui.util.VersionUtils; 25 26 @LargeTest 27 public class RenameDocumentUiTest extends ActivityTest<FilesActivity> { 28 29 private final String newName = "kitties.log"; 30 RenameDocumentUiTest()31 public RenameDocumentUiTest() { 32 super(FilesActivity.class); 33 } 34 35 @Override setUp()36 public void setUp() throws Exception { 37 super.setUp(); 38 initTestFiles(); 39 bots.roots.closeDrawer(); 40 } 41 testRenameEnabled_SingleSelection()42 public void testRenameEnabled_SingleSelection() throws Exception { 43 bots.directory.selectDocument(fileName1, 1); 44 bots.main.openOverflowMenu(); 45 bots.main.assertMenuEnabled(R.string.menu_rename, true); 46 47 // Dismiss more options window 48 device.pressBack(); 49 } 50 testNoRenameSupport_SingleSelection()51 public void testNoRenameSupport_SingleSelection() throws Exception { 52 if (VersionUtils.isAtLeastR()) { 53 bots.directory.selectDocument(fileNameNoRename, 1); 54 bots.main.openOverflowMenu(); 55 bots.main.assertMenuEnabled(R.string.menu_rename, false); 56 57 // Dismiss more options window 58 device.pressBack(); 59 } 60 } 61 testOneHasRenameSupport_MultipleSelection()62 public void testOneHasRenameSupport_MultipleSelection() throws Exception { 63 if (VersionUtils.isAtLeastR()) { 64 bots.directory.selectDocument(fileName1, 1); 65 bots.directory.selectDocument(fileNameNoRename, 2); 66 bots.main.openOverflowMenu(); 67 bots.main.assertMenuEnabled(R.string.menu_rename, false); 68 69 // Dismiss more options window 70 device.pressBack(); 71 } 72 } 73 testRenameDisabled_MultipleSelection()74 public void testRenameDisabled_MultipleSelection() throws Exception { 75 if (VersionUtils.isAtLeastR()) { 76 bots.directory.selectDocument(fileName1, 1); 77 bots.directory.selectDocument(fileName2, 2); 78 bots.main.openOverflowMenu(); 79 bots.main.assertMenuEnabled(R.string.menu_rename, false); 80 81 // Dismiss more options window 82 device.pressBack(); 83 } 84 } 85 testRenameFile_OkButton()86 public void testRenameFile_OkButton() throws Exception { 87 bots.directory.selectDocument(fileName1, 1); 88 89 clickRename(); 90 91 device.waitForIdle(); 92 bots.main.setDialogText(newName); 93 94 device.waitForIdle(); 95 bots.main.clickDialogOkButton(); 96 97 bots.directory.waitForDocument(newName); 98 bots.directory.assertDocumentsAbsent(fileName1); 99 bots.directory.assertDocumentsCount(4); 100 } 101 testRenameFile_Enter()102 public void testRenameFile_Enter() throws Exception { 103 bots.directory.selectDocument(fileName1, 1); 104 105 clickRename(); 106 107 device.waitForIdle(); 108 bots.main.setDialogText(newName); 109 110 device.waitForIdle(); 111 bots.keyboard.pressEnter(); 112 113 bots.directory.waitForDocument(newName); 114 bots.directory.assertDocumentsAbsent(fileName1); 115 bots.directory.assertDocumentsCount(4); 116 } 117 testRenameWithoutChangeIsNoOp()118 public void testRenameWithoutChangeIsNoOp() throws Exception { 119 bots.directory.selectDocument(fileName1, 1); 120 121 clickRename(); 122 123 device.waitForIdle(); 124 bots.keyboard.pressEnter(); 125 126 bots.directory.waitForDocument(fileName1); 127 bots.directory.assertDocumentsCount(4); 128 } 129 testRenameFile_Cancel()130 public void testRenameFile_Cancel() throws Exception { 131 bots.directory.selectDocument(fileName1, 1); 132 133 clickRename(); 134 135 bots.main.setDialogText(newName); 136 137 bots.main.clickDialogCancelButton(); 138 139 bots.directory.assertDocumentsPresent(fileName1); 140 bots.directory.assertDocumentsAbsent(newName); 141 bots.directory.assertDocumentsCount(4); 142 143 bots.directory.assertSelection(1); 144 } 145 testRenameDir()146 public void testRenameDir() throws Exception { 147 String oldName = "Dir1"; 148 String newName = "Dir123"; 149 bots.directory.selectDocument(oldName, 1); 150 151 clickRename(); 152 153 bots.main.setDialogText(newName); 154 155 bots.keyboard.pressEnter(); 156 157 bots.directory.assertDocumentsAbsent(oldName); 158 bots.directory.assertDocumentsPresent(newName); 159 bots.directory.assertDocumentsCount(4); 160 } 161 testRename_NameExists()162 public void testRename_NameExists() throws Exception { 163 renameWithConflict(); 164 165 bots.main.clickDialogCancelButton(); 166 167 bots.directory.assertDocumentsPresent(fileName1); 168 bots.directory.assertDocumentsPresent(fileName2); 169 bots.directory.assertDocumentsCount(4); 170 } 171 testRename_RecoverAfterConflict()172 public void testRename_RecoverAfterConflict() throws Exception { 173 renameWithConflict(); 174 device.waitForIdle(); 175 176 bots.main.setDialogText(newName); 177 178 device.waitForIdle(); 179 bots.main.clickDialogOkButton(); 180 181 bots.directory.waitForDocument(newName); 182 bots.directory.assertDocumentsAbsent(fileName1); 183 bots.directory.assertDocumentsCount(4); 184 } 185 renameWithConflict()186 private void renameWithConflict() throws Exception { 187 // Check that document with the new name exists 188 bots.directory.assertDocumentsPresent(fileName2); 189 bots.directory.selectDocument(fileName1, 1); 190 191 clickRename(); 192 193 bots.main.assertDialogText(fileName1); 194 assertFalse(bots.main.findRenameErrorMessage().exists()); 195 bots.main.setDialogText(fileName2); 196 bots.keyboard.pressEnter(); 197 assertTrue(bots.main.findRenameErrorMessage().exists()); 198 } 199 clickRename()200 private void clickRename() throws UiObjectNotFoundException { 201 if (!bots.main.waitForActionModeBarToAppear()) { 202 throw new UiObjectNotFoundException("ActionMode bar not found"); 203 } 204 bots.main.clickActionbarOverflowItem("Rename"); 205 device.waitForIdle(); 206 } 207 }