Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / ui / app_list / views / search_result_page_view_unittest.cc
bloba198a7fdbb46ad431060a25e3190a263e99759e9
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"
7 #include <utility>
8 #include <vector>
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"
22 namespace app_list {
23 namespace test {
25 class SearchResultPageViewTest : public views::ViewsTestBase,
26 public SearchResultListViewDelegate {
27 public:
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_ =
42 new SearchResultTileItemListView(textfield_.get(), &view_delegate_);
43 view_->AddSearchResultContainerView(GetResults(), tile_list_view_);
46 protected:
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();
59 double relevance = result_types.size();
60 for (const auto& data : result_types) {
61 // Set the relevance of the results in each group in decreasing order (so
62 // the earlier groups have higher relevance, and therefore appear first).
63 relevance -= 1.0;
64 for (int i = 0; i < data.second; ++i) {
65 TestSearchResult* result = new TestSearchResult();
66 result->set_display_type(data.first);
67 result->set_relevance(relevance);
68 results->Add(result);
72 // Adding results will schedule Update().
73 RunPendingMessages();
76 int GetSelectedIndex() { return view_->selected_index(); }
78 bool KeyPress(ui::KeyboardCode key_code) { return KeyPress(key_code, false); }
80 bool KeyPress(ui::KeyboardCode key_code, bool shift_down) {
81 int flags = ui::EF_NONE;
82 if (shift_down)
83 flags |= ui::EF_SHIFT_DOWN;
84 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, flags);
85 return view_->OnKeyPressed(event);
88 private:
89 void OnResultInstalled(SearchResult* result) override {}
91 SearchResultListView* list_view_;
92 SearchResultTileItemListView* tile_list_view_;
94 AppListTestViewDelegate view_delegate_;
95 scoped_ptr<SearchResultPageView> view_;
96 scoped_ptr<views::Textfield> textfield_;
98 DISALLOW_COPY_AND_ASSIGN(SearchResultPageViewTest);
101 TEST_F(SearchResultPageViewTest, DirectionalMovement) {
102 std::vector<std::pair<SearchResult::DisplayType, int>> result_types;
103 // 3 tile results, followed by 2 list results.
104 const int kTileResults = 3;
105 const int kListResults = 2;
106 const int kNoneResults = 3;
107 result_types.push_back(
108 std::make_pair(SearchResult::DISPLAY_TILE, kTileResults));
109 result_types.push_back(
110 std::make_pair(SearchResult::DISPLAY_LIST, kListResults));
111 result_types.push_back(
112 std::make_pair(SearchResult::DISPLAY_NONE, kNoneResults));
114 SetUpSearchResults(result_types);
115 EXPECT_EQ(0, GetSelectedIndex());
116 EXPECT_EQ(0, tile_list_view()->selected_index());
118 // Navigate to the second tile in the tile group.
119 EXPECT_TRUE(KeyPress(ui::VKEY_RIGHT));
120 EXPECT_EQ(0, GetSelectedIndex());
121 EXPECT_EQ(1, tile_list_view()->selected_index());
122 EXPECT_EQ(-1, list_view()->selected_index());
124 // Navigate to the list group.
125 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
126 EXPECT_EQ(1, GetSelectedIndex());
127 EXPECT_EQ(-1, tile_list_view()->selected_index());
128 EXPECT_EQ(0, list_view()->selected_index());
130 // Navigate to the second result in the list view.
131 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
132 EXPECT_EQ(1, GetSelectedIndex());
133 EXPECT_EQ(1, list_view()->selected_index());
135 // Attempt to navigate off bottom of list items.
136 EXPECT_FALSE(KeyPress(ui::VKEY_DOWN));
137 EXPECT_EQ(1, GetSelectedIndex());
138 EXPECT_EQ(1, list_view()->selected_index());
140 // Navigate back to the tile group (should select the first tile result).
141 EXPECT_TRUE(KeyPress(ui::VKEY_UP));
142 EXPECT_EQ(1, GetSelectedIndex());
143 EXPECT_EQ(0, list_view()->selected_index());
144 EXPECT_TRUE(KeyPress(ui::VKEY_UP));
145 EXPECT_EQ(0, GetSelectedIndex());
146 EXPECT_EQ(0, tile_list_view()->selected_index());
147 EXPECT_EQ(-1, list_view()->selected_index());
149 // Navigate off top of list.
150 EXPECT_FALSE(KeyPress(ui::VKEY_UP));
151 EXPECT_EQ(0, GetSelectedIndex());
152 EXPECT_EQ(0, tile_list_view()->selected_index());
155 TEST_F(SearchResultPageViewTest, TabMovement) {
156 std::vector<std::pair<SearchResult::DisplayType, int>> result_types;
157 // 3 tile results, followed by 2 list results.
158 const int kTileResults = 3;
159 const int kListResults = 2;
160 const int kNoneResults = 3;
161 result_types.push_back(
162 std::make_pair(SearchResult::DISPLAY_TILE, kTileResults));
163 result_types.push_back(
164 std::make_pair(SearchResult::DISPLAY_LIST, kListResults));
165 result_types.push_back(
166 std::make_pair(SearchResult::DISPLAY_NONE, kNoneResults));
168 SetUpSearchResults(result_types);
169 EXPECT_EQ(0, GetSelectedIndex());
170 EXPECT_EQ(0, tile_list_view()->selected_index());
172 // Navigate to the second tile in the tile group.
173 EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
174 EXPECT_EQ(0, GetSelectedIndex());
175 EXPECT_EQ(1, tile_list_view()->selected_index());
176 EXPECT_EQ(-1, list_view()->selected_index());
178 // Navigate to the list group.
179 EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
180 EXPECT_EQ(0, GetSelectedIndex());
181 EXPECT_EQ(2, tile_list_view()->selected_index());
182 EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
183 EXPECT_EQ(1, GetSelectedIndex());
184 EXPECT_EQ(-1, tile_list_view()->selected_index());
185 EXPECT_EQ(0, list_view()->selected_index());
187 // Navigate to the second result in the list view.
188 EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
189 EXPECT_EQ(1, GetSelectedIndex());
190 EXPECT_EQ(1, list_view()->selected_index());
192 // Attempt to navigate off bottom of list items.
193 EXPECT_FALSE(KeyPress(ui::VKEY_TAB));
194 EXPECT_EQ(1, GetSelectedIndex());
195 EXPECT_EQ(1, list_view()->selected_index());
197 // Navigate back to the tile group (should select the last tile result).
198 EXPECT_TRUE(KeyPress(ui::VKEY_TAB, true));
199 EXPECT_EQ(1, GetSelectedIndex());
200 EXPECT_EQ(0, list_view()->selected_index());
201 EXPECT_TRUE(KeyPress(ui::VKEY_TAB, true));
202 EXPECT_EQ(0, GetSelectedIndex());
203 EXPECT_EQ(2, tile_list_view()->selected_index());
204 EXPECT_EQ(-1, list_view()->selected_index());
206 // Navigate off top of list.
207 EXPECT_TRUE(KeyPress(ui::VKEY_TAB, true));
208 EXPECT_EQ(1, tile_list_view()->selected_index());
209 EXPECT_TRUE(KeyPress(ui::VKEY_TAB, true));
210 EXPECT_EQ(0, tile_list_view()->selected_index());
211 EXPECT_FALSE(KeyPress(ui::VKEY_TAB, true));
212 EXPECT_EQ(0, GetSelectedIndex());
213 EXPECT_EQ(0, tile_list_view()->selected_index());
216 TEST_F(SearchResultPageViewTest, ResultsSorted) {
217 AppListModel::SearchResults* results = GetResults();
219 // Add 3 results and expect the tile list view to be the first result
220 // container view.
221 TestSearchResult* tile_result = new TestSearchResult();
222 tile_result->set_display_type(SearchResult::DISPLAY_TILE);
223 tile_result->set_relevance(1.0);
224 results->Add(tile_result);
226 TestSearchResult* list_result = new TestSearchResult();
227 list_result->set_display_type(SearchResult::DISPLAY_LIST);
228 list_result->set_relevance(0.5);
229 results->Add(list_result);
232 TestSearchResult* list_result = new TestSearchResult();
233 list_result->set_display_type(SearchResult::DISPLAY_LIST);
234 list_result->set_relevance(0.3);
235 results->Add(list_result);
238 // Adding results will schedule Update().
239 RunPendingMessages();
241 EXPECT_EQ(tile_list_view(), view()->result_container_views()[0]);
242 EXPECT_EQ(list_view(), view()->result_container_views()[1]);
244 // Change the relevance of the tile result and expect the list results to be
245 // displayed first.
246 tile_result->set_relevance(0.4);
248 results->NotifyItemsChanged(0, 1);
249 RunPendingMessages();
251 EXPECT_EQ(list_view(), view()->result_container_views()[0]);
252 EXPECT_EQ(tile_list_view(), view()->result_container_views()[1]);
255 } // namespace test
256 } // namespace app_list