Add support for providers called when the omnibox is focused.
[chromium-blink-merge.git] / ui / app_list / views / search_result_list_view_unittest.cc
blobf5beab623a768f8a82a5f82f2fb13bd52a681649
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_list_view.h"
7 #include <map>
9 #include "base/strings/utf_string_conversions.h"
10 #include "ui/app_list/app_list_model.h"
11 #include "ui/app_list/test/app_list_test_view_delegate.h"
12 #include "ui/app_list/test/test_search_result.h"
13 #include "ui/app_list/views/progress_bar_view.h"
14 #include "ui/app_list/views/search_result_list_view_delegate.h"
15 #include "ui/app_list/views/search_result_view.h"
16 #include "ui/views/test/views_test_base.h"
18 namespace app_list {
19 namespace test {
21 namespace {
22 int kDefaultSearchItems = 5;
23 } // namespace
25 class SearchResultListViewTest : public views::ViewsTestBase,
26 public SearchResultListViewDelegate {
27 public:
28 SearchResultListViewTest() {}
29 ~SearchResultListViewTest() override {}
31 // Overridden from testing::Test:
32 void SetUp() override {
33 views::ViewsTestBase::SetUp();
34 view_.reset(new SearchResultListView(this, &view_delegate_));
35 view_->SetResults(view_delegate_.GetModel()->results());
38 protected:
39 SearchResultListView* view() { return view_.get(); }
41 AppListModel::SearchResults* GetResults() {
42 return view_delegate_.GetModel()->results();
45 void SetLongAutoLaunchTimeout() {
46 // Sets a long timeout that lasts longer than the test run.
47 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1));
50 base::TimeDelta GetAutoLaunchTimeout() {
51 return view_delegate_.GetAutoLaunchTimeout();
54 void SetUpSearchResults() {
55 AppListModel::SearchResults* results = GetResults();
56 for (int i = 0; i < kDefaultSearchItems; ++i) {
57 TestSearchResult* result = new TestSearchResult();
58 result->SetDisplayType(SearchResult::DISPLAY_LIST);
59 results->Add(result);
62 // Adding results will schedule Update().
63 RunPendingMessages();
64 view_->OnContainerSelected(false);
67 int GetOpenResultCountAndReset(int ranking) {
68 int result = view_delegate_.open_search_result_counts()[ranking];
69 view_delegate_.open_search_result_counts().clear();
70 return result;
73 int GetResultCount() { return view_->num_results(); }
75 int GetSelectedIndex() { return view_->selected_index(); }
77 void ResetSelectedIndex() {
78 view_->SetSelectedIndex(0);
81 void AddTestResultAtIndex(int index) {
82 GetResults()->Add(new TestSearchResult());
85 void DeleteResultAt(int index) { GetResults()->DeleteAt(index); }
87 bool KeyPress(ui::KeyboardCode key_code) {
88 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE);
89 return view_->OnKeyPressed(event);
92 bool IsAutoLaunching() {
93 return view_->auto_launch_animation_;
96 void ForceAutoLaunch() {
97 view_->ForceAutoLaunchForTest();
100 void ExpectConsistent() {
101 // Adding results will schedule Update().
102 RunPendingMessages();
104 AppListModel::SearchResults* results = GetResults();
105 for (size_t i = 0; i < results->item_count(); ++i) {
106 EXPECT_EQ(results->GetItemAt(i), view_->GetResultViewAt(i)->result());
110 ProgressBarView* GetProgressBarAt(size_t index) {
111 return view()->GetResultViewAt(index)->progress_bar_;
114 private:
115 void OnResultInstalled(SearchResult* result) override {}
117 AppListTestViewDelegate view_delegate_;
118 scoped_ptr<SearchResultListView> view_;
120 DISALLOW_COPY_AND_ASSIGN(SearchResultListViewTest);
123 TEST_F(SearchResultListViewTest, Basic) {
124 SetUpSearchResults();
126 const int results = GetResultCount();
127 EXPECT_EQ(kDefaultSearchItems, results);
128 EXPECT_EQ(0, GetSelectedIndex());
129 EXPECT_FALSE(IsAutoLaunching());
131 EXPECT_TRUE(KeyPress(ui::VKEY_RETURN));
132 EXPECT_EQ(1, GetOpenResultCountAndReset(0));
134 for (int i = 1; i < results; ++i) {
135 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
136 EXPECT_EQ(i, GetSelectedIndex());
138 // When navigating off the end of the list, pass the event to the parent to
139 // handle.
140 EXPECT_FALSE(KeyPress(ui::VKEY_DOWN));
141 EXPECT_EQ(results - 1, GetSelectedIndex());
143 for (int i = 1; i < results; ++i) {
144 EXPECT_TRUE(KeyPress(ui::VKEY_UP));
145 EXPECT_EQ(results - i - 1, GetSelectedIndex());
147 // Navigate off top of list.
148 EXPECT_FALSE(KeyPress(ui::VKEY_UP));
149 EXPECT_EQ(0, GetSelectedIndex());
150 ResetSelectedIndex();
152 for (int i = 1; i < results; ++i) {
153 EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
154 EXPECT_EQ(i, GetSelectedIndex());
156 // Navigate off bottom of list.
157 EXPECT_FALSE(KeyPress(ui::VKEY_TAB));
158 EXPECT_EQ(results - 1, GetSelectedIndex());
161 TEST_F(SearchResultListViewTest, AutoLaunch) {
162 SetLongAutoLaunchTimeout();
163 SetUpSearchResults();
165 EXPECT_TRUE(IsAutoLaunching());
166 ForceAutoLaunch();
168 EXPECT_FALSE(IsAutoLaunching());
169 EXPECT_EQ(1, GetOpenResultCountAndReset(0));
171 // The timeout has to be cleared after the auto-launch, to prevent opening
172 // the search result twice. See the comment in AnimationEnded().
173 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
176 TEST_F(SearchResultListViewTest, CancelAutoLaunch) {
177 SetLongAutoLaunchTimeout();
178 SetUpSearchResults();
180 EXPECT_TRUE(IsAutoLaunching());
182 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
183 EXPECT_FALSE(IsAutoLaunching());
185 SetLongAutoLaunchTimeout();
186 view()->UpdateAutoLaunchState();
187 EXPECT_TRUE(IsAutoLaunching());
189 view()->SetVisible(false);
190 EXPECT_FALSE(IsAutoLaunching());
192 SetLongAutoLaunchTimeout();
193 view()->SetVisible(true);
194 EXPECT_TRUE(IsAutoLaunching());
197 TEST_F(SearchResultListViewTest, ModelObservers) {
198 SetUpSearchResults();
199 ExpectConsistent();
201 // Insert at start.
202 AddTestResultAtIndex(0);
203 ExpectConsistent();
205 // Remove from end.
206 DeleteResultAt(kDefaultSearchItems);
207 ExpectConsistent();
209 // Insert at end.
210 AddTestResultAtIndex(kDefaultSearchItems);
211 ExpectConsistent();
213 // Delete from start.
214 DeleteResultAt(0);
215 ExpectConsistent();
218 // Regression test for http://crbug.com/402859 to ensure ProgressBar is
219 // initialized properly in SearchResultListView::SetResult().
220 TEST_F(SearchResultListViewTest, ProgressBar) {
221 SetUpSearchResults();
223 GetResults()->GetItemAt(0)->SetIsInstalling(true);
224 EXPECT_EQ(0.0f, GetProgressBarAt(0)->current_value());
225 GetResults()->GetItemAt(0)->SetPercentDownloaded(10);
227 DeleteResultAt(0);
228 RunPendingMessages();
229 EXPECT_EQ(0.0f, GetProgressBarAt(0)->current_value());
232 } // namespace test
233 } // namespace app_list