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