Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / search_result_list_view_unittest.cc
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.
4
5 #include "ui/app_list/views/search_result_list_view.h"
6
7 #include <map>
8
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"
16
17 namespace app_list {
18 namespace test {
19
20 namespace {
21 int kDefaultSearchItems = 5;
22 }  // namespace
23
24 class SearchResultListViewTest : public views::ViewsTestBase,
25                                  public SearchResultListViewDelegate {
26  public:
27   SearchResultListViewTest() {}
28   virtual ~SearchResultListViewTest() {}
29
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);
36   }
37
38  protected:
39   SearchResultListView* view() { return view_.get(); }
40
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));
44   }
45
46   base::TimeDelta GetAutoLaunchTimeout() {
47     return view_delegate_.GetAutoLaunchTimeout();
48   }
49
50   void SetUpSearchResults() {
51     AppListModel::SearchResults* results = view_delegate_.GetModel()->results();
52     for (int i = 0; i < kDefaultSearchItems; ++i)
53       results->Add(new SearchResult());
54
55     // Adding results will schedule Update().
56     RunPendingMessages();
57   }
58
59   int GetOpenResultCountAndReset(int ranking) {
60     int result = view_delegate_.open_search_result_counts()[ranking];
61     view_delegate_.open_search_result_counts().clear();
62     return result;
63   }
64
65   int GetSearchResults() {
66     return view_->last_visible_index_ + 1;
67   }
68
69   int GetSelectedIndex() {
70     return view_->selected_index_;
71   }
72
73   void ResetSelectedIndex() {
74     view_->SetSelectedIndex(0);
75   }
76
77   void AddTestResultAtIndex(int index) {
78     view_delegate_.GetModel()->results()->Add(new SearchResult());
79   }
80
81   void DeleteResultAt(int index) {
82     view_delegate_.GetModel()->results()->DeleteAt(index);
83   }
84
85   bool KeyPress(ui::KeyboardCode key_code) {
86     ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE, true);
87     return view_->OnKeyPressed(event);
88   }
89
90   bool IsAutoLaunching() {
91     return view_->auto_launch_animation_;
92   }
93
94   void ForceAutoLaunch() {
95     view_->ForceAutoLaunchForTest();
96   }
97
98   void ExpectConsistent() {
99     // Adding results will schedule Update().
100     RunPendingMessages();
101
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());
105     }
106   }
107
108  private:
109   virtual void OnResultInstalled(SearchResult* result) OVERRIDE {}
110   virtual void OnResultUninstalled(SearchResult* result) OVERRIDE {}
111
112   AppListTestViewDelegate view_delegate_;
113   scoped_ptr<SearchResultListView> view_;
114
115   DISALLOW_COPY_AND_ASSIGN(SearchResultListViewTest);
116 };
117
118 TEST_F(SearchResultListViewTest, Basic) {
119   SetUpSearchResults();
120
121   const int results = GetSearchResults();
122   EXPECT_EQ(kDefaultSearchItems, results);
123   EXPECT_EQ(0, GetSelectedIndex());
124   EXPECT_FALSE(IsAutoLaunching());
125
126   EXPECT_TRUE(KeyPress(ui::VKEY_RETURN));
127   EXPECT_EQ(1, GetOpenResultCountAndReset(0));
128
129   for (int i = 1; i < results; ++i) {
130     EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
131     EXPECT_EQ(i, GetSelectedIndex());
132   }
133   // Doesn't rotate.
134   EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
135   EXPECT_EQ(results - 1, GetSelectedIndex());
136
137   for (int i = 1; i < results; ++i) {
138     EXPECT_TRUE(KeyPress(ui::VKEY_UP));
139     EXPECT_EQ(results - i - 1, GetSelectedIndex());
140   }
141   // Doesn't rotate.
142   EXPECT_TRUE(KeyPress(ui::VKEY_UP));
143   EXPECT_EQ(0, GetSelectedIndex());
144   ResetSelectedIndex();
145
146   for (int i = 1; i < results; ++i) {
147     EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
148     EXPECT_EQ(i, GetSelectedIndex());
149   }
150   // Doesn't rotate.
151   EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
152   EXPECT_EQ(results - 1, GetSelectedIndex());
153 }
154
155 TEST_F(SearchResultListViewTest, AutoLaunch) {
156   SetLongAutoLaunchTimeout();
157   SetUpSearchResults();
158
159   EXPECT_TRUE(IsAutoLaunching());
160   ForceAutoLaunch();
161
162   EXPECT_FALSE(IsAutoLaunching());
163   EXPECT_EQ(1, GetOpenResultCountAndReset(0));
164
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());
168 }
169
170 TEST_F(SearchResultListViewTest, CancelAutoLaunch) {
171   SetLongAutoLaunchTimeout();
172   SetUpSearchResults();
173
174   EXPECT_TRUE(IsAutoLaunching());
175
176   EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
177   EXPECT_FALSE(IsAutoLaunching());
178
179   SetLongAutoLaunchTimeout();
180   view()->UpdateAutoLaunchState();
181   EXPECT_TRUE(IsAutoLaunching());
182
183   view()->SetVisible(false);
184   EXPECT_FALSE(IsAutoLaunching());
185
186   SetLongAutoLaunchTimeout();
187   view()->SetVisible(true);
188   EXPECT_TRUE(IsAutoLaunching());
189 }
190
191 TEST_F(SearchResultListViewTest, ModelObservers) {
192   SetUpSearchResults();
193   ExpectConsistent();
194
195   // Insert at start.
196   AddTestResultAtIndex(0);
197   ExpectConsistent();
198
199   // Remove from end.
200   DeleteResultAt(kDefaultSearchItems);
201   ExpectConsistent();
202
203   // Insert at end.
204   AddTestResultAtIndex(kDefaultSearchItems);
205   ExpectConsistent();
206
207   // Delete from start.
208   DeleteResultAt(0);
209   ExpectConsistent();
210 }
211
212 }  // namespace test
213 }  // namespace app_list