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