1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/app_list/views/search_result_page_view.h"
10 #include "base/command_line.h"
11 #include "ui/app_list/app_list_model.h"
12 #include "ui/app_list/app_list_switches.h"
13 #include "ui/app_list/test/app_list_test_view_delegate.h"
14 #include "ui/app_list/test/test_search_result.h"
15 #include "ui/app_list/views/search_result_list_view.h"
16 #include "ui/app_list/views/search_result_list_view_delegate.h"
17 #include "ui/app_list/views/search_result_tile_item_list_view.h"
18 #include "ui/app_list/views/search_result_view.h"
19 #include "ui/views/controls/textfield/textfield.h"
20 #include "ui/views/test/views_test_base.h"
25 class SearchResultPageViewTest
: public views::ViewsTestBase
,
26 public SearchResultListViewDelegate
{
28 SearchResultPageViewTest() {
29 base::CommandLine::ForCurrentProcess()->AppendSwitch(
30 switches::kEnableExperimentalAppList
);
32 ~SearchResultPageViewTest() override
{}
34 // Overridden from testing::Test:
35 void SetUp() override
{
36 views::ViewsTestBase::SetUp();
37 view_
.reset(new SearchResultPageView());
38 list_view_
= new SearchResultListView(this, &view_delegate_
);
39 view_
->AddSearchResultContainerView(GetResults(), list_view_
);
40 textfield_
.reset(new views::Textfield());
42 new SearchResultTileItemListView(textfield_
.get(), &view_delegate_
);
43 view_
->AddSearchResultContainerView(GetResults(), tile_list_view_
);
47 SearchResultPageView
* view() { return view_
.get(); }
49 SearchResultListView
* list_view() { return list_view_
; }
50 SearchResultTileItemListView
* tile_list_view() { return tile_list_view_
; }
52 AppListModel::SearchResults
* GetResults() {
53 return view_delegate_
.GetModel()->results();
56 void SetUpSearchResults(const std::vector
<
57 std::pair
<SearchResult::DisplayType
, int>> result_types
) {
58 AppListModel::SearchResults
* results
= GetResults();
60 double relevance
= result_types
.size();
61 for (const auto& data
: result_types
) {
62 // Set the relevance of the results in each group in decreasing order (so
63 // the earlier groups have higher relevance, and therefore appear first).
65 for (int i
= 0; i
< data
.second
; ++i
) {
66 TestSearchResult
* result
= new TestSearchResult();
67 result
->set_display_type(data
.first
);
68 result
->set_relevance(relevance
);
73 // Adding results will schedule Update().
77 int GetSelectedIndex() { return view_
->selected_index(); }
79 bool KeyPress(ui::KeyboardCode key_code
) { return KeyPress(key_code
, false); }
81 bool KeyPress(ui::KeyboardCode key_code
, bool shift_down
) {
82 int flags
= ui::EF_NONE
;
84 flags
|= ui::EF_SHIFT_DOWN
;
85 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, key_code
, flags
);
86 return view_
->OnKeyPressed(event
);
90 void OnResultInstalled(SearchResult
* result
) override
{}
92 SearchResultListView
* list_view_
;
93 SearchResultTileItemListView
* tile_list_view_
;
95 AppListTestViewDelegate view_delegate_
;
96 scoped_ptr
<SearchResultPageView
> view_
;
97 scoped_ptr
<views::Textfield
> textfield_
;
99 DISALLOW_COPY_AND_ASSIGN(SearchResultPageViewTest
);
102 TEST_F(SearchResultPageViewTest
, DirectionalMovement
) {
103 std::vector
<std::pair
<SearchResult::DisplayType
, int>> result_types
;
104 // 3 tile results, followed by 2 list results.
105 const int kTileResults
= 3;
106 const int kListResults
= 2;
107 const int kNoneResults
= 3;
108 result_types
.push_back(
109 std::make_pair(SearchResult::DISPLAY_TILE
, kTileResults
));
110 result_types
.push_back(
111 std::make_pair(SearchResult::DISPLAY_LIST
, kListResults
));
112 result_types
.push_back(
113 std::make_pair(SearchResult::DISPLAY_NONE
, kNoneResults
));
115 SetUpSearchResults(result_types
);
116 EXPECT_EQ(0, GetSelectedIndex());
117 EXPECT_EQ(0, tile_list_view()->selected_index());
119 // Navigate to the second tile in the tile group.
120 EXPECT_TRUE(KeyPress(ui::VKEY_RIGHT
));
121 EXPECT_EQ(0, GetSelectedIndex());
122 EXPECT_EQ(1, tile_list_view()->selected_index());
123 EXPECT_EQ(-1, list_view()->selected_index());
125 // Navigate to the list group.
126 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
127 EXPECT_EQ(1, GetSelectedIndex());
128 EXPECT_EQ(-1, tile_list_view()->selected_index());
129 EXPECT_EQ(0, list_view()->selected_index());
131 // Navigate to the second result in the list view.
132 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
133 EXPECT_EQ(1, GetSelectedIndex());
134 EXPECT_EQ(1, list_view()->selected_index());
136 // Attempt to navigate off bottom of list items.
137 EXPECT_FALSE(KeyPress(ui::VKEY_DOWN
));
138 EXPECT_EQ(1, GetSelectedIndex());
139 EXPECT_EQ(1, list_view()->selected_index());
141 // Navigate back to the tile group (should select the first tile result).
142 EXPECT_TRUE(KeyPress(ui::VKEY_UP
));
143 EXPECT_EQ(1, GetSelectedIndex());
144 EXPECT_EQ(0, list_view()->selected_index());
145 EXPECT_TRUE(KeyPress(ui::VKEY_UP
));
146 EXPECT_EQ(0, GetSelectedIndex());
147 EXPECT_EQ(0, tile_list_view()->selected_index());
148 EXPECT_EQ(-1, list_view()->selected_index());
150 // Navigate off top of list.
151 EXPECT_FALSE(KeyPress(ui::VKEY_UP
));
152 EXPECT_EQ(0, GetSelectedIndex());
153 EXPECT_EQ(0, tile_list_view()->selected_index());
156 TEST_F(SearchResultPageViewTest
, TabMovement
) {
157 std::vector
<std::pair
<SearchResult::DisplayType
, int>> result_types
;
158 // 3 tile results, followed by 2 list results.
159 const int kTileResults
= 3;
160 const int kListResults
= 2;
161 const int kNoneResults
= 3;
162 result_types
.push_back(
163 std::make_pair(SearchResult::DISPLAY_TILE
, kTileResults
));
164 result_types
.push_back(
165 std::make_pair(SearchResult::DISPLAY_LIST
, kListResults
));
166 result_types
.push_back(
167 std::make_pair(SearchResult::DISPLAY_NONE
, kNoneResults
));
169 SetUpSearchResults(result_types
);
170 EXPECT_EQ(0, GetSelectedIndex());
171 EXPECT_EQ(0, tile_list_view()->selected_index());
173 // Navigate to the second tile in the tile group.
174 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
));
175 EXPECT_EQ(0, GetSelectedIndex());
176 EXPECT_EQ(1, tile_list_view()->selected_index());
177 EXPECT_EQ(-1, list_view()->selected_index());
179 // Navigate to the list group.
180 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
));
181 EXPECT_EQ(0, GetSelectedIndex());
182 EXPECT_EQ(2, tile_list_view()->selected_index());
183 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
));
184 EXPECT_EQ(1, GetSelectedIndex());
185 EXPECT_EQ(-1, tile_list_view()->selected_index());
186 EXPECT_EQ(0, list_view()->selected_index());
188 // Navigate to the second result in the list view.
189 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
));
190 EXPECT_EQ(1, GetSelectedIndex());
191 EXPECT_EQ(1, list_view()->selected_index());
193 // Attempt to navigate off bottom of list items.
194 EXPECT_FALSE(KeyPress(ui::VKEY_TAB
));
195 EXPECT_EQ(1, GetSelectedIndex());
196 EXPECT_EQ(1, list_view()->selected_index());
198 // Navigate back to the tile group (should select the last tile result).
199 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
, true));
200 EXPECT_EQ(1, GetSelectedIndex());
201 EXPECT_EQ(0, list_view()->selected_index());
202 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
, true));
203 EXPECT_EQ(0, GetSelectedIndex());
204 EXPECT_EQ(2, tile_list_view()->selected_index());
205 EXPECT_EQ(-1, list_view()->selected_index());
207 // Navigate off top of list.
208 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
, true));
209 EXPECT_EQ(1, tile_list_view()->selected_index());
210 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
, true));
211 EXPECT_EQ(0, tile_list_view()->selected_index());
212 EXPECT_FALSE(KeyPress(ui::VKEY_TAB
, true));
213 EXPECT_EQ(0, GetSelectedIndex());
214 EXPECT_EQ(0, tile_list_view()->selected_index());
217 TEST_F(SearchResultPageViewTest
, ResultsSorted
) {
218 AppListModel::SearchResults
* results
= GetResults();
220 // Add 3 results and expect the tile list view to be the first result
222 TestSearchResult
* tile_result
= new TestSearchResult();
223 tile_result
->set_display_type(SearchResult::DISPLAY_TILE
);
224 tile_result
->set_relevance(1.0);
225 results
->Add(tile_result
);
227 TestSearchResult
* list_result
= new TestSearchResult();
228 list_result
->set_display_type(SearchResult::DISPLAY_LIST
);
229 list_result
->set_relevance(0.5);
230 results
->Add(list_result
);
233 TestSearchResult
* list_result
= new TestSearchResult();
234 list_result
->set_display_type(SearchResult::DISPLAY_LIST
);
235 list_result
->set_relevance(0.3);
236 results
->Add(list_result
);
239 // Adding results will schedule Update().
240 RunPendingMessages();
242 EXPECT_EQ(tile_list_view(), view()->result_container_views()[0]);
243 EXPECT_EQ(list_view(), view()->result_container_views()[1]);
245 // Change the relevance of the tile result and expect the list results to be
247 tile_result
->set_relevance(0.4);
249 results
->NotifyItemsChanged(0, 1);
250 RunPendingMessages();
252 EXPECT_EQ(list_view(), view()->result_container_views()[0]);
253 EXPECT_EQ(tile_list_view(), view()->result_container_views()[1]);
256 TEST_F(SearchResultPageViewTest
, UpdateWithSelection
) {
258 std::vector
<std::pair
<SearchResult::DisplayType
, int>> result_types
;
259 result_types
.push_back(std::make_pair(SearchResult::DISPLAY_TILE
, 3));
260 result_types
.push_back(std::make_pair(SearchResult::DISPLAY_LIST
, 2));
262 SetUpSearchResults(result_types
);
265 EXPECT_EQ(0, GetSelectedIndex());
266 EXPECT_EQ(0, tile_list_view()->selected_index());
267 EXPECT_EQ(-1, list_view()->selected_index());
269 // Navigate to the second result in the list group.
270 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
271 EXPECT_EQ(1, GetSelectedIndex());
272 EXPECT_EQ(-1, tile_list_view()->selected_index());
273 EXPECT_EQ(0, list_view()->selected_index());
275 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
276 EXPECT_EQ(1, GetSelectedIndex());
277 EXPECT_EQ(-1, tile_list_view()->selected_index());
278 EXPECT_EQ(1, list_view()->selected_index());
281 std::vector
<std::pair
<SearchResult::DisplayType
, int>> result_types
;
282 result_types
.push_back(std::make_pair(SearchResult::DISPLAY_TILE
, 3));
283 result_types
.push_back(std::make_pair(SearchResult::DISPLAY_LIST
, 3));
285 SetUpSearchResults(result_types
);
288 // The second list result should still be selected after the update.
289 EXPECT_EQ(1, GetSelectedIndex());
290 EXPECT_EQ(-1, tile_list_view()->selected_index());
291 EXPECT_EQ(1, list_view()->selected_index());
294 std::vector
<std::pair
<SearchResult::DisplayType
, int>> result_types
;
295 result_types
.push_back(std::make_pair(SearchResult::DISPLAY_TILE
, 3));
296 result_types
.push_back(std::make_pair(SearchResult::DISPLAY_LIST
, 1));
298 SetUpSearchResults(result_types
);
301 // The first list result should be selected after the update as the second
302 // result has vanished.
303 EXPECT_EQ(1, GetSelectedIndex());
304 EXPECT_EQ(-1, tile_list_view()->selected_index());
305 EXPECT_EQ(0, list_view()->selected_index());
308 std::vector
<std::pair
<SearchResult::DisplayType
, int>> result_types
;
309 result_types
.push_back(std::make_pair(SearchResult::DISPLAY_LIST
, 1));
310 result_types
.push_back(std::make_pair(SearchResult::DISPLAY_TILE
, 3));
312 SetUpSearchResults(result_types
);
315 // The tile container should be selected because we hold the selected
316 // container index constant.
317 EXPECT_EQ(1, GetSelectedIndex());
318 EXPECT_EQ(0, tile_list_view()->selected_index());
319 EXPECT_EQ(-1, list_view()->selected_index());
322 std::vector
<std::pair
<SearchResult::DisplayType
, int>> result_types
;
323 result_types
.push_back(std::make_pair(SearchResult::DISPLAY_LIST
, 3));
325 SetUpSearchResults(result_types
);
328 // The selected container has vanished so we reset the selection to 0.
329 EXPECT_EQ(0, GetSelectedIndex());
330 EXPECT_EQ(-1, tile_list_view()->selected_index());
331 EXPECT_EQ(0, list_view()->selected_index());
335 } // namespace app_list