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 static com.android.documentsui.StubProvider.ROOT_0_ID;
20 import static com.android.documentsui.StubProvider.ROOT_1_ID;
21 
22 import androidx.test.filters.LargeTest;
23 import androidx.test.filters.Suppress;
24 
25 import com.android.documentsui.files.FilesActivity;
26 import com.android.documentsui.filters.HugeLongTest;
27 
28 @LargeTest
29 public class SearchViewUiTest extends ActivityTest<FilesActivity> {
30 
SearchViewUiTest()31     public SearchViewUiTest() {
32         super(FilesActivity.class);
33     }
34 
35     @Override
setUp()36     public void setUp() throws Exception {
37       super.setUp();
38       initTestFiles();
39       // Drawer interferes with a lot of search action; going to try to close any opened ones
40       bots.roots.closeDrawer();
41 
42       // wait for a file to be present in default dir.
43       bots.directory.waitForDocument(fileName1);
44     }
45 
46     @Override
tearDown()47     public void tearDown() throws Exception {
48         // manually close activity to avoid SearchFragment show when Activity close. ref b/142840883
49         device.waitForIdle();
50         device.pressBack();
51         device.pressBack();
52         device.pressBack();
53         super.tearDown();
54     }
55 
testSearchIconVisible()56     public void testSearchIconVisible() throws Exception {
57         // The default root (root 0) supports search
58         bots.search.assertInputExists(false);
59         bots.search.assertIconVisible(true);
60     }
61 
62     @HugeLongTest
testSearchIconHidden()63     public void testSearchIconHidden() throws Exception {
64         bots.roots.openRoot(ROOT_1_ID);  // root 1 doesn't support search
65 
66         bots.search.assertIconVisible(false);
67         bots.search.assertInputExists(false);
68     }
69 
testSearchView_ExpandsOnClick()70     public void testSearchView_ExpandsOnClick() throws Exception {
71         bots.search.clickIcon();
72         device.waitForIdle();
73 
74         bots.search.assertInputExists(true);
75         bots.search.assertInputFocused(true);
76 
77         // FIXME: Matchers fail the not-present check if we've ever clicked this.
78         // bots.search.assertIconVisible(false);
79     }
80 
testSearchView_ShouldHideOptionMenuOnExpanding()81     public void testSearchView_ShouldHideOptionMenuOnExpanding() throws Exception {
82         bots.search.clickIcon();
83         device.waitForIdle();
84 
85         bots.search.assertInputExists(true);
86         bots.search.assertInputFocused(true);
87         device.waitForIdle();
88 
89         assertFalse(bots.menu.hasMenuItem("Grid view"));
90         assertFalse(bots.menu.hasMenuItem("List view"));
91         assertFalse(bots.menu.hasMenuItemByDesc("More options"));
92     }
93 
testSearchView_CollapsesOnBack()94     public void testSearchView_CollapsesOnBack() throws Exception {
95         bots.search.clickIcon();
96         device.pressBack();
97 
98         bots.search.assertIconVisible(true);
99         bots.search.assertInputExists(false);
100     }
101 
testSearchFragment_DismissedOnCloseAfterCancel()102     public void testSearchFragment_DismissedOnCloseAfterCancel() throws Exception {
103         bots.search.clickIcon();
104         bots.search.setInputText("query text");
105 
106         // Cancel search
107         device.pressBack();
108         device.waitForIdle();
109 
110         // Close search
111         device.pressBack();
112         device.waitForIdle();
113 
114         bots.search.assertIconVisible(true);
115         bots.search.assertInputExists(false);
116         bots.search.assertSearchHistoryVisible(false);
117     }
118 
testSearchView_ClearsTextOnBack()119     public void testSearchView_ClearsTextOnBack() throws Exception {
120         bots.search.clickIcon();
121         bots.search.setInputText("file2");
122 
123         device.pressBack();
124         device.pressBack();
125 
126         // Wait for a file in the default directory to be listed.
127         bots.directory.waitForDocument(dirName1);
128 
129         bots.search.assertIconVisible(true);
130         bots.search.assertInputExists(false);
131     }
132 
testSearchView_ClearsSearchOnBack()133     public void testSearchView_ClearsSearchOnBack() throws Exception {
134         bots.search.clickIcon();
135         bots.search.setInputText("file1");
136         bots.keyboard.pressEnter();
137         device.waitForIdle();
138 
139         device.pressBack();
140 
141         bots.search.assertIconVisible(true);
142         bots.search.assertInputExists(false);
143     }
144 
testSearchView_ClearsAutoSearchOnBack()145     public void testSearchView_ClearsAutoSearchOnBack() throws Exception {
146         bots.search.clickIcon();
147         bots.search.setInputText("chocolate");
148         //Wait for auto search result, it should be no results and show holder message.
149         bots.directory.waitForHolderMessage();
150 
151         device.pressBack();
152         device.pressBack();
153 
154         bots.search.assertIconVisible(true);
155         bots.search.assertInputExists(false);
156     }
157 
testSearchView_StateAfterSearch()158     public void testSearchView_StateAfterSearch() throws Exception {
159         bots.search.clickIcon();
160         bots.search.setInputText("file1");
161         bots.keyboard.pressEnter();
162         device.waitForIdle();
163 
164         bots.search.assertInputEquals("file1");
165     }
166 
testSearch_ResultsFound()167     public void testSearch_ResultsFound() throws Exception {
168         bots.search.clickIcon();
169         bots.search.setInputText("file1");
170         bots.keyboard.pressEnter();
171 
172         bots.directory.assertDocumentsCountOnList(true, 2);
173         bots.directory.assertDocumentsPresent(fileName1, fileName2);
174     }
175 
testSearch_NoResults()176     public void testSearch_NoResults() throws Exception {
177         bots.search.clickIcon();
178         bots.search.setInputText("chocolate");
179 
180         bots.keyboard.pressEnter();
181 
182         String msg = String.valueOf(context.getString(R.string.no_results));
183         bots.directory.assertPlaceholderMessageText(String.format(msg, "TEST_ROOT_0"));
184     }
185 
186     @Suppress
testSearchResultsFound_ClearsOnBack()187     public void testSearchResultsFound_ClearsOnBack() throws Exception {
188         bots.search.clickIcon();
189         bots.search.setInputText(fileName1);
190 
191         bots.keyboard.pressEnter();
192         device.pressBack();
193         device.waitForIdle();
194 
195         assertDefaultContentOfTestDir0();
196     }
197 
198     @Suppress
testSearchNoResults_ClearsOnBack()199     public void testSearchNoResults_ClearsOnBack() throws Exception {
200         bots.search.clickIcon();
201         bots.search.setInputText("chocolate bunny");
202 
203         bots.keyboard.pressEnter();
204         device.pressBack();
205         device.waitForIdle();
206 
207         assertDefaultContentOfTestDir0();
208     }
209 
210     @Suppress
testSearchResultsFound_ClearsOnDirectoryChange()211     public void testSearchResultsFound_ClearsOnDirectoryChange() throws Exception {
212         // Skipping this test for phones since currently there's no way to open the drawer on
213         // phones after doing a search (it's a back button instead of a hamburger button)
214         if (!bots.main.inFixedLayout()) {
215             return;
216         }
217 
218         bots.search.clickIcon();
219 
220         bots.search.setInputText(fileName1);
221 
222         bots.keyboard.pressEnter();
223 
224         bots.roots.openRoot(ROOT_1_ID);
225         device.waitForIdle();
226         assertDefaultContentOfTestDir1();
227 
228         bots.roots.openRoot(ROOT_0_ID);
229         device.waitForIdle();
230 
231         assertDefaultContentOfTestDir0();
232     }
233 
testSearchHistory_showAfterSearchViewClear()234     public void testSearchHistory_showAfterSearchViewClear() throws Exception {
235         bots.search.clickIcon();
236         bots.search.setInputText("chocolate");
237 
238         bots.keyboard.pressEnter();
239         device.waitForIdle();
240 
241         bots.search.clickSearchViewClearButton();
242         device.waitForIdle();
243 
244         bots.search.assertInputExists(true);
245         bots.search.assertInputFocused(true);
246         bots.search.assertSearchHistoryVisible(true);
247     }
248 
testSearchView_focusClearedAfterSelectingSearchHistory()249     public void testSearchView_focusClearedAfterSelectingSearchHistory() throws Exception {
250         String queryText = "history";
251         bots.search.clickIcon();
252         bots.search.setInputText(queryText);
253         bots.keyboard.pressEnter();
254         device.waitForIdle();
255 
256         bots.search.clickSearchViewClearButton();
257         device.waitForIdle();
258         bots.search.assertInputFocused(true);
259         bots.search.assertSearchHistoryVisible(true);
260 
261         bots.search.clickSearchHistory(queryText);
262         bots.search.assertInputFocused(false);
263         bots.search.assertSearchHistoryVisible(false);
264     }
265 }
266