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 "base/strings/utf_string_conversions.h"
10 #include "ui/app_list/app_list_model.h"
11 #include "ui/app_list/search_result.h"
12 #include "ui/app_list/test/app_list_test_view_delegate.h"
13 #include "ui/app_list/views/search_result_list_view_delegate.h"
14 #include "ui/app_list/views/search_result_view.h"
15 #include "ui/views/test/views_test_base.h"
21 int kDefaultSearchItems
= 5;
24 class SearchResultListViewTest
: public views::ViewsTestBase
,
25 public SearchResultListViewDelegate
{
27 SearchResultListViewTest() {}
28 virtual ~SearchResultListViewTest() {}
30 // Overridden from testing::Test:
31 virtual void SetUp() OVERRIDE
{
32 views::ViewsTestBase::SetUp();
33 view_
.reset(new SearchResultListView(this, &view_delegate_
));
34 view_
->SetResults(view_delegate_
.GetModel()->results());
35 view_
->SetSelectedIndex(0);
39 SearchResultListView
* view() { return view_
.get(); }
41 void SetLongAutoLaunchTimeout() {
42 // Sets a long timeout that lasts longer than the test run.
43 view_delegate_
.set_auto_launch_timeout(base::TimeDelta::FromDays(1));
46 base::TimeDelta
GetAutoLaunchTimeout() {
47 return view_delegate_
.GetAutoLaunchTimeout();
50 void SetUpSearchResults() {
51 AppListModel::SearchResults
* results
= view_delegate_
.GetModel()->results();
52 for (int i
= 0; i
< kDefaultSearchItems
; ++i
)
53 results
->Add(new SearchResult());
55 // Adding results will schedule Update().
59 int GetOpenResultCountAndReset(int ranking
) {
60 int result
= view_delegate_
.open_search_result_counts()[ranking
];
61 view_delegate_
.open_search_result_counts().clear();
65 int GetSearchResults() {
66 return view_
->last_visible_index_
+ 1;
69 int GetSelectedIndex() {
70 return view_
->selected_index_
;
73 void ResetSelectedIndex() {
74 view_
->SetSelectedIndex(0);
77 void AddTestResultAtIndex(int index
) {
78 view_delegate_
.GetModel()->results()->Add(new SearchResult());
81 void DeleteResultAt(int index
) {
82 view_delegate_
.GetModel()->results()->DeleteAt(index
);
85 bool KeyPress(ui::KeyboardCode key_code
) {
86 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, key_code
, ui::EF_NONE
);
87 return view_
->OnKeyPressed(event
);
90 bool IsAutoLaunching() {
91 return view_
->auto_launch_animation_
;
94 void ForceAutoLaunch() {
95 view_
->ForceAutoLaunchForTest();
98 void ExpectConsistent() {
99 // Adding results will schedule Update().
100 RunPendingMessages();
102 AppListModel::SearchResults
* results
= view_delegate_
.GetModel()->results();
103 for (size_t i
= 0; i
< results
->item_count(); ++i
) {
104 EXPECT_EQ(results
->GetItemAt(i
), view_
->GetResultViewAt(i
)->result());
109 virtual void OnResultInstalled(SearchResult
* result
) OVERRIDE
{}
110 virtual void OnResultUninstalled(SearchResult
* result
) OVERRIDE
{}
112 AppListTestViewDelegate view_delegate_
;
113 scoped_ptr
<SearchResultListView
> view_
;
115 DISALLOW_COPY_AND_ASSIGN(SearchResultListViewTest
);
118 TEST_F(SearchResultListViewTest
, Basic
) {
119 SetUpSearchResults();
121 const int results
= GetSearchResults();
122 EXPECT_EQ(kDefaultSearchItems
, results
);
123 EXPECT_EQ(0, GetSelectedIndex());
124 EXPECT_FALSE(IsAutoLaunching());
126 EXPECT_TRUE(KeyPress(ui::VKEY_RETURN
));
127 EXPECT_EQ(1, GetOpenResultCountAndReset(0));
129 for (int i
= 1; i
< results
; ++i
) {
130 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
131 EXPECT_EQ(i
, GetSelectedIndex());
134 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
135 EXPECT_EQ(results
- 1, GetSelectedIndex());
137 for (int i
= 1; i
< results
; ++i
) {
138 EXPECT_TRUE(KeyPress(ui::VKEY_UP
));
139 EXPECT_EQ(results
- i
- 1, GetSelectedIndex());
142 EXPECT_TRUE(KeyPress(ui::VKEY_UP
));
143 EXPECT_EQ(0, GetSelectedIndex());
144 ResetSelectedIndex();
146 for (int i
= 1; i
< results
; ++i
) {
147 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
));
148 EXPECT_EQ(i
, GetSelectedIndex());
151 EXPECT_TRUE(KeyPress(ui::VKEY_TAB
));
152 EXPECT_EQ(results
- 1, GetSelectedIndex());
155 TEST_F(SearchResultListViewTest
, AutoLaunch
) {
156 SetLongAutoLaunchTimeout();
157 SetUpSearchResults();
159 EXPECT_TRUE(IsAutoLaunching());
162 EXPECT_FALSE(IsAutoLaunching());
163 EXPECT_EQ(1, GetOpenResultCountAndReset(0));
165 // The timeout has to be cleared after the auto-launch, to prevent opening
166 // the search result twice. See the comment in AnimationEnded().
167 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
170 TEST_F(SearchResultListViewTest
, CancelAutoLaunch
) {
171 SetLongAutoLaunchTimeout();
172 SetUpSearchResults();
174 EXPECT_TRUE(IsAutoLaunching());
176 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
177 EXPECT_FALSE(IsAutoLaunching());
179 SetLongAutoLaunchTimeout();
180 view()->UpdateAutoLaunchState();
181 EXPECT_TRUE(IsAutoLaunching());
183 view()->SetVisible(false);
184 EXPECT_FALSE(IsAutoLaunching());
186 SetLongAutoLaunchTimeout();
187 view()->SetVisible(true);
188 EXPECT_TRUE(IsAutoLaunching());
191 TEST_F(SearchResultListViewTest
, ModelObservers
) {
192 SetUpSearchResults();
196 AddTestResultAtIndex(0);
200 DeleteResultAt(kDefaultSearchItems
);
204 AddTestResultAtIndex(kDefaultSearchItems
);
207 // Delete from start.
213 } // namespace app_list