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"
9 #include "ui/app_list/app_list_model.h"
10 #include "ui/app_list/search_result.h"
11 #include "ui/app_list/test/app_list_test_view_delegate.h"
12 #include "ui/app_list/views/search_result_list_view_delegate.h"
13 #include "ui/views/test/views_test_base.h"
19 int kDefaultSearchItems = 5;
22 class SearchResultListViewTest : public views::ViewsTestBase,
23 public SearchResultListViewDelegate {
25 SearchResultListViewTest() {}
26 virtual ~SearchResultListViewTest() {}
28 // Overridden from testing::Test:
29 virtual void SetUp() OVERRIDE {
30 views::ViewsTestBase::SetUp();
31 view_.reset(new SearchResultListView(this, &view_delegate_));
32 view_->SetResults(view_delegate_.GetModel()->results());
33 view_->SetSelectedIndex(0);
37 SearchResultListView* view() { return view_.get(); }
39 void SetLongAutoLaunchTimeout() {
40 // Sets a long timeout that lasts longer than the test run.
41 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1));
44 base::TimeDelta GetAutoLaunchTimeout() {
45 return view_delegate_.GetAutoLaunchTimeout();
48 void SetUpSearchResults() {
49 AppListModel::SearchResults* results = view_delegate_.GetModel()->results();
50 for (int i = 0; i < kDefaultSearchItems; ++i)
51 results->Add(new SearchResult());
53 // Adding results will schedule Update().
57 int GetOpenResultCountAndReset(int ranking) {
58 int result = open_result_counts_[ranking];
59 open_result_counts_.clear();
63 int GetSearchResults() {
64 return view_->last_visible_index_ + 1;
67 int GetSelectedIndex() {
68 return view_->selected_index_;
71 void ResetSelectedIndex() {
72 view_->SetSelectedIndex(0);
75 bool KeyPress(ui::KeyboardCode key_code) {
76 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE, true);
77 return view_->OnKeyPressed(event);
80 bool IsAutoLaunching() {
81 return view_->auto_launch_animation_;
84 void ForceAutoLaunch() {
85 view_->ForceAutoLaunchForTest();
89 // Overridden from SearchResultListViewDelegate:
90 virtual void OpenResult(SearchResult* result,
92 int event_flags) OVERRIDE {
93 const AppListModel::SearchResults* results =
94 view_delegate_.GetModel()->results();
95 for (size_t i = 0; i < results->item_count(); ++i) {
96 if (results->GetItemAt(i) == result) {
97 open_result_counts_[i]++;
102 virtual void InvokeResultAction(SearchResult* result,
104 int event_flags) OVERRIDE {}
105 virtual void OnResultInstalled(SearchResult* result) OVERRIDE {}
106 virtual void OnResultUninstalled(SearchResult* result) OVERRIDE {}
108 AppListTestViewDelegate view_delegate_;
109 scoped_ptr<SearchResultListView> view_;
110 std::map<int, int> open_result_counts_;
112 DISALLOW_COPY_AND_ASSIGN(SearchResultListViewTest);
115 TEST_F(SearchResultListViewTest, Basic) {
116 SetUpSearchResults();
118 const int results = GetSearchResults();
119 EXPECT_EQ(kDefaultSearchItems, results);
120 EXPECT_EQ(0, GetSelectedIndex());
121 EXPECT_FALSE(IsAutoLaunching());
123 EXPECT_TRUE(KeyPress(ui::VKEY_RETURN));
124 EXPECT_EQ(1, GetOpenResultCountAndReset(0));
126 for (int i = 1; i < results; ++i) {
127 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
128 EXPECT_EQ(i, GetSelectedIndex());
131 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
132 EXPECT_EQ(results - 1, GetSelectedIndex());
134 for (int i = 1; i < results; ++i) {
135 EXPECT_TRUE(KeyPress(ui::VKEY_UP));
136 EXPECT_EQ(results - i - 1, GetSelectedIndex());
139 EXPECT_TRUE(KeyPress(ui::VKEY_UP));
140 EXPECT_EQ(0, GetSelectedIndex());
141 ResetSelectedIndex();
143 for (int i = 1; i < results; ++i) {
144 EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
145 EXPECT_EQ(i, GetSelectedIndex());
148 EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
149 EXPECT_EQ(results - 1, GetSelectedIndex());
152 TEST_F(SearchResultListViewTest, AutoLaunch) {
153 SetLongAutoLaunchTimeout();
154 SetUpSearchResults();
156 EXPECT_TRUE(IsAutoLaunching());
159 EXPECT_FALSE(IsAutoLaunching());
160 EXPECT_EQ(1, GetOpenResultCountAndReset(0));
162 // The timeout has to be cleared after the auto-launch, to prevent opening
163 // the search result twice. See the comment in AnimationEnded().
164 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
167 TEST_F(SearchResultListViewTest, CancelAutoLaunch) {
168 SetLongAutoLaunchTimeout();
169 SetUpSearchResults();
171 EXPECT_TRUE(IsAutoLaunching());
173 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
174 EXPECT_FALSE(IsAutoLaunching());
176 SetLongAutoLaunchTimeout();
177 view()->UpdateAutoLaunchState();
178 EXPECT_TRUE(IsAutoLaunching());
180 view()->SetVisible(false);
181 EXPECT_FALSE(IsAutoLaunching());
183 SetLongAutoLaunchTimeout();
184 view()->SetVisible(true);
185 EXPECT_TRUE(IsAutoLaunching());
189 } // namespace app_list