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());
41 tile_list_view_
= new SearchResultTileItemListView(textfield_
.get());
42 view_
->AddSearchResultContainerView(GetResults(), tile_list_view_
);
46 SearchResultPageView
* view() { return view_
.get(); }
48 SearchResultListView
* list_view() { return list_view_
; }
49 SearchResultTileItemListView
* tile_list_view() { return tile_list_view_
; }
51 AppListModel::SearchResults
* GetResults() {
52 return view_delegate_
.GetModel()->results();
55 void SetUpSearchResults(const std::vector
<
56 std::pair
<SearchResult::DisplayType
, int>> result_types
) {
57 AppListModel::SearchResults
* results
= GetResults();
58 double relevance
= result_types
.size();
59 for (const auto& data
: result_types
) {
60 // Set the relevance of the results in each group in decreasing order (so
61 // the earlier groups have higher relevance, and therefore appear first).
63 for (int i
= 0; i
< data
.second
; ++i
) {
64 TestSearchResult
* result
= new TestSearchResult();
65 result
->set_display_type(data
.first
);
66 result
->set_relevance(relevance
);
71 // Adding results will schedule Update().
75 int GetSelectedIndex() { return view_
->selected_index(); }
77 bool KeyPress(ui::KeyboardCode key_code
) { return KeyPress(key_code
, false); }
79 bool KeyPress(ui::KeyboardCode key_code
, bool shift_down
) {
80 int flags
= ui::EF_NONE
;
82 flags
|= ui::EF_SHIFT_DOWN
;
83 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, key_code
, flags
);
84 return view_
->OnKeyPressed(event
);
88 void OnResultInstalled(SearchResult
* result
) override
{}
90 SearchResultListView
* list_view_
;
91 SearchResultTileItemListView
* tile_list_view_
;
93 AppListTestViewDelegate view_delegate_
;
94 scoped_ptr
<SearchResultPageView
> view_
;
95 scoped_ptr
<views::Textfield
> textfield_
;
97 DISALLOW_COPY_AND_ASSIGN(SearchResultPageViewTest
);
100 TEST_F(SearchResultPageViewTest
, DirectionalMovement
) {
101 std::vector
<std::pair
<SearchResult::DisplayType
, int>> result_types
;
102 // 3 tile results, followed by 2 list results.
103 const int kTileResults
= 3;
104 const int kListResults
= 2;
105 const int kNoneResults
= 3;
106 result_types
.push_back(
107 std::make_pair(SearchResult::DISPLAY_TILE
, kTileResults
));
108 result_types
.push_back(
109 std::make_pair(SearchResult::DISPLAY_LIST
, kListResults
));
110 result_types
.push_back(
111 std::make_pair(SearchResult::DISPLAY_NONE
, kNoneResults
));
113 SetUpSearchResults(result_types
);
114 EXPECT_EQ(0, GetSelectedIndex());
115 EXPECT_EQ(0, tile_list_view()->selected_index());
117 // Navigate to the second tile in the tile group.
118 EXPECT_TRUE(KeyPress(ui::VKEY_RIGHT
));
119 EXPECT_EQ(0, GetSelectedIndex());
120 EXPECT_EQ(1, tile_list_view()->selected_index());
121 EXPECT_EQ(-1, list_view()->selected_index());
123 // Navigate to the list group.
124 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
125 EXPECT_EQ(1, GetSelectedIndex());
126 EXPECT_EQ(-1, tile_list_view()->selected_index());
127 EXPECT_EQ(0, list_view()->selected_index());
129 // Navigate to the second result in the list view.
130 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
131 EXPECT_EQ(1, GetSelectedIndex());
132 EXPECT_EQ(1, list_view()->selected_index());
134 // Attempt to navigate off bottom of list items.
135 EXPECT_FALSE(KeyPress(ui::VKEY_DOWN
));
136 EXPECT_EQ(1, GetSelectedIndex());
137 EXPECT_EQ(1, list_view()->selected_index());
139 // Navigate back to the tile group (should select the first tile result).
140 EXPECT_TRUE(KeyPress(ui::VKEY_UP
));
141 EXPECT_EQ(1, GetSelectedIndex());
142 EXPECT_EQ(0, list_view()->selected_index());
143 EXPECT_TRUE(KeyPress(ui::VKEY_UP
));
144 EXPECT_EQ(0, GetSelectedIndex());
145 EXPECT_EQ(0, tile_list_view()->selected_index());
146 EXPECT_EQ(-1, list_view()->selected_index());
148 // Navigate off top of list.
149 EXPECT_FALSE(KeyPress(ui::VKEY_UP
));
150 EXPECT_EQ(0, GetSelectedIndex());
151 EXPECT_EQ(0, tile_list_view()->selected_index());
154 TEST_F(SearchResultPageViewTest
, TabMovement
) {
155 std::vector
<std::pair
<SearchResult::DisplayType
, int>> result_types
;
156 // 3 tile results, followed by 2 list results.
157 const int kTileResults
= 3;
158 const int kListResults
= 2;
159 const int kNoneResults
= 3;
160 result_types
.push_back(
161 std::make_pair(SearchResult::DISPLAY_TILE
, kTileResults
));
162 result_types
.push_back(
163 std::make_pair(SearchResult::DISPLAY_LIST
, kListResults
));
164 result_types
.push_back(
165 std::make_pair(SearchResult::DISPLAY_NONE
, kNoneResults
));
167 SetUpSearchResults(result_types
);
168 EXPECT_EQ(0, GetSelectedIndex());
169 EXPECT_EQ(0, tile_list_view()->selected_index());
171 // Navigate to the second tile in the tile group.
172 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
));
173 EXPECT_EQ(0, GetSelectedIndex());
174 EXPECT_EQ(1, tile_list_view()->selected_index());
175 EXPECT_EQ(-1, list_view()->selected_index());
177 // Navigate to the list group.
178 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
));
179 EXPECT_EQ(0, GetSelectedIndex());
180 EXPECT_EQ(2, tile_list_view()->selected_index());
181 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
));
182 EXPECT_EQ(1, GetSelectedIndex());
183 EXPECT_EQ(-1, tile_list_view()->selected_index());
184 EXPECT_EQ(0, list_view()->selected_index());
186 // Navigate to the second result in the list view.
187 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
));
188 EXPECT_EQ(1, GetSelectedIndex());
189 EXPECT_EQ(1, list_view()->selected_index());
191 // Attempt to navigate off bottom of list items.
192 EXPECT_FALSE(KeyPress(ui::VKEY_TAB
));
193 EXPECT_EQ(1, GetSelectedIndex());
194 EXPECT_EQ(1, list_view()->selected_index());
196 // Navigate back to the tile group (should select the last tile result).
197 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
, true));
198 EXPECT_EQ(1, GetSelectedIndex());
199 EXPECT_EQ(0, list_view()->selected_index());
200 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
, true));
201 EXPECT_EQ(0, GetSelectedIndex());
202 EXPECT_EQ(2, tile_list_view()->selected_index());
203 EXPECT_EQ(-1, list_view()->selected_index());
205 // Navigate off top of list.
206 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
, true));
207 EXPECT_EQ(1, tile_list_view()->selected_index());
208 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
, true));
209 EXPECT_EQ(0, tile_list_view()->selected_index());
210 EXPECT_FALSE(KeyPress(ui::VKEY_TAB
, true));
211 EXPECT_EQ(0, GetSelectedIndex());
212 EXPECT_EQ(0, tile_list_view()->selected_index());
215 TEST_F(SearchResultPageViewTest
, ResultsSorted
) {
216 AppListModel::SearchResults
* results
= GetResults();
218 // Add 3 results and expect the tile list view to be the first result
220 TestSearchResult
* tile_result
= new TestSearchResult();
221 tile_result
->set_display_type(SearchResult::DISPLAY_TILE
);
222 tile_result
->set_relevance(1.0);
223 results
->Add(tile_result
);
225 TestSearchResult
* list_result
= new TestSearchResult();
226 list_result
->set_display_type(SearchResult::DISPLAY_LIST
);
227 list_result
->set_relevance(0.5);
228 results
->Add(list_result
);
231 TestSearchResult
* list_result
= new TestSearchResult();
232 list_result
->set_display_type(SearchResult::DISPLAY_LIST
);
233 list_result
->set_relevance(0.3);
234 results
->Add(list_result
);
237 // Adding results will schedule Update().
238 RunPendingMessages();
240 EXPECT_EQ(tile_list_view(), view()->result_container_views()[0]);
241 EXPECT_EQ(list_view(), view()->result_container_views()[1]);
243 // Change the relevance of the tile result and expect the list results to be
245 tile_result
->set_relevance(0.4);
247 results
->NotifyItemsChanged(0, 1);
248 RunPendingMessages();
250 EXPECT_EQ(list_view(), view()->result_container_views()[0]);
251 EXPECT_EQ(tile_list_view(), view()->result_container_views()[1]);
255 } // namespace app_list