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