Upstream version 5.34.104.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 "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"
14
15 namespace app_list {
16 namespace test {
17
18 namespace {
19 int kDefaultSearchItems = 5;
20 }  // namespace
21
22 class SearchResultListViewTest : public views::ViewsTestBase,
23                                  public SearchResultListViewDelegate {
24  public:
25   SearchResultListViewTest() {}
26   virtual ~SearchResultListViewTest() {}
27
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);
34   }
35
36  protected:
37   SearchResultListView* view() { return view_.get(); }
38
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));
42   }
43
44   base::TimeDelta GetAutoLaunchTimeout() {
45     return view_delegate_.GetAutoLaunchTimeout();
46   }
47
48   void SetUpSearchResults() {
49     AppListModel::SearchResults* results = view_delegate_.GetModel()->results();
50     for (int i = 0; i < kDefaultSearchItems; ++i)
51       results->Add(new SearchResult());
52
53     // Adding results will schedule Update().
54     RunPendingMessages();
55   }
56
57   int GetOpenResultCountAndReset(int ranking) {
58     int result = open_result_counts_[ranking];
59     open_result_counts_.clear();
60     return result;
61   }
62
63   int GetSearchResults() {
64     return view_->last_visible_index_ + 1;
65   }
66
67   int GetSelectedIndex() {
68     return view_->selected_index_;
69   }
70
71   void ResetSelectedIndex() {
72     view_->SetSelectedIndex(0);
73   }
74
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);
78   }
79
80   bool IsAutoLaunching() {
81     return view_->auto_launch_animation_;
82   }
83
84   void ForceAutoLaunch() {
85     view_->ForceAutoLaunchForTest();
86   }
87
88  private:
89   // Overridden from SearchResultListViewDelegate:
90   virtual void OpenResult(SearchResult* result,
91                           bool auto_launch,
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]++;
98         break;
99       }
100     }
101   }
102   virtual void InvokeResultAction(SearchResult* result,
103                                   int action_index,
104                                   int event_flags) OVERRIDE {}
105   virtual void OnResultInstalled(SearchResult* result) OVERRIDE {}
106   virtual void OnResultUninstalled(SearchResult* result) OVERRIDE {}
107
108   AppListTestViewDelegate view_delegate_;
109   scoped_ptr<SearchResultListView> view_;
110   std::map<int, int> open_result_counts_;
111
112   DISALLOW_COPY_AND_ASSIGN(SearchResultListViewTest);
113 };
114
115 TEST_F(SearchResultListViewTest, Basic) {
116   SetUpSearchResults();
117
118   const int results = GetSearchResults();
119   EXPECT_EQ(kDefaultSearchItems, results);
120   EXPECT_EQ(0, GetSelectedIndex());
121   EXPECT_FALSE(IsAutoLaunching());
122
123   EXPECT_TRUE(KeyPress(ui::VKEY_RETURN));
124   EXPECT_EQ(1, GetOpenResultCountAndReset(0));
125
126   for (int i = 1; i < results; ++i) {
127     EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
128     EXPECT_EQ(i, GetSelectedIndex());
129   }
130   // Doesn't rotate.
131   EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
132   EXPECT_EQ(results - 1, GetSelectedIndex());
133
134   for (int i = 1; i < results; ++i) {
135     EXPECT_TRUE(KeyPress(ui::VKEY_UP));
136     EXPECT_EQ(results - i - 1, GetSelectedIndex());
137   }
138   // Doesn't rotate.
139   EXPECT_TRUE(KeyPress(ui::VKEY_UP));
140   EXPECT_EQ(0, GetSelectedIndex());
141   ResetSelectedIndex();
142
143   for (int i = 1; i < results; ++i) {
144     EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
145     EXPECT_EQ(i, GetSelectedIndex());
146   }
147   // Doesn't rotate.
148   EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
149   EXPECT_EQ(results - 1, GetSelectedIndex());
150 }
151
152 TEST_F(SearchResultListViewTest, AutoLaunch) {
153   SetLongAutoLaunchTimeout();
154   SetUpSearchResults();
155
156   EXPECT_TRUE(IsAutoLaunching());
157   ForceAutoLaunch();
158
159   EXPECT_FALSE(IsAutoLaunching());
160   EXPECT_EQ(1, GetOpenResultCountAndReset(0));
161
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());
165 }
166
167 TEST_F(SearchResultListViewTest, CancelAutoLaunch) {
168   SetLongAutoLaunchTimeout();
169   SetUpSearchResults();
170
171   EXPECT_TRUE(IsAutoLaunching());
172
173   EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
174   EXPECT_FALSE(IsAutoLaunching());
175
176   SetLongAutoLaunchTimeout();
177   view()->UpdateAutoLaunchState();
178   EXPECT_TRUE(IsAutoLaunching());
179
180   view()->SetVisible(false);
181   EXPECT_FALSE(IsAutoLaunching());
182
183   SetLongAutoLaunchTimeout();
184   view()->SetVisible(true);
185   EXPECT_TRUE(IsAutoLaunching());
186 }
187
188 }  // namespace test
189 }  // namespace app_list