Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: ui/app_list/views/search_result_list_view_unittest.cc

Issue 164193005: Refactors the auto-launch logic and adds tests for it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 }
xiyuan 2014/02/14 04:55:55 nit: // namespace
Jun Mukai 2014/02/14 19:17:27 Done.
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 SetUpAutoLaunchTimeout() {
40 // Sets a long timeout to make sure the timeout actualy happening in the
41 // test case.
xiyuan 2014/02/14 04:55:55 nit: Update comment and function name as the other
Jun Mukai 2014/02/14 19:17:27 Done.
42 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1));
43 }
44
45 void SetUpSearchResults() {
46 AppListModel::SearchResults* results = view_delegate_.GetModel()->results();
47 for (int i = 0; i < kDefaultSearchItems; ++i)
48 results->Add(new SearchResult());
49
50 // Adding results will schedule Update().
51 RunPendingMessages();
52 }
53
54 int GetOpenResultCountAndReset(int ranking) {
55 int result = open_result_counts_[ranking];
56 open_result_counts_.clear();
57 return result;
58 }
59
60 int GetSearchResults() {
61 return view_->last_visible_index_ + 1;
62 }
63
64 int GetSelectedIndex() {
65 return view_->selected_index_;
66 }
67
68 void ResetSelectedIndex() {
69 view_->SetSelectedIndex(0);
70 }
71
72 bool KeyPress(ui::KeyboardCode key_code) {
73 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE, true);
74 return view_->OnKeyPressed(event);
75 }
76
77 bool IsAutoLaunching() {
78 return view_->auto_launch_animation_;
79 }
80
81 void ForceAutoLaunch() {
82 view_->ForceAutoLaunchForTest();
83 }
84
85 private:
86 // Overridden from SearchResultListViewDelegate:
87 virtual void OpenResult(SearchResult* result, int event_flags) OVERRIDE {
88 const AppListModel::SearchResults* results =
89 view_delegate_.GetModel()->results();
90 for (size_t i = 0; i < results->item_count(); ++i) {
91 if (results->GetItemAt(i) == result) {
92 open_result_counts_[i]++;
93 break;
94 }
95 }
96 }
97 virtual void InvokeResultAction(SearchResult* result,
98 int action_index,
99 int event_flags) OVERRIDE {}
100 virtual void OnResultInstalled(SearchResult* result) OVERRIDE {}
101 virtual void OnResultUninstalled(SearchResult* result) OVERRIDE {}
102
103 AppListTestViewDelegate view_delegate_;
104 scoped_ptr<SearchResultListView> view_;
105 std::map<int, int> open_result_counts_;
106
107 DISALLOW_COPY_AND_ASSIGN(SearchResultListViewTest);
108 };
109
110 TEST_F(SearchResultListViewTest, Basic) {
111 SetUpSearchResults();
112
113 const int results = GetSearchResults();
114 EXPECT_EQ(kDefaultSearchItems, results);
115 EXPECT_EQ(0, GetSelectedIndex());
116 EXPECT_FALSE(IsAutoLaunching());
117
118 EXPECT_TRUE(KeyPress(ui::VKEY_RETURN));
119 EXPECT_EQ(1, GetOpenResultCountAndReset(0));
120
121 for (int i = 1; i < results; ++i) {
122 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
123 EXPECT_EQ(i, GetSelectedIndex());
124 }
125 // Doesn't rotate.
126 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
127 EXPECT_EQ(results - 1, GetSelectedIndex());
128
129 for (int i = 1; i < results; ++i) {
130 EXPECT_TRUE(KeyPress(ui::VKEY_UP));
131 EXPECT_EQ(results - i - 1, GetSelectedIndex());
132 }
133 // Doesn't rotate.
134 EXPECT_TRUE(KeyPress(ui::VKEY_UP));
135 EXPECT_EQ(0, GetSelectedIndex());
136 ResetSelectedIndex();
137
138 for (int i = 1; i < results; ++i) {
139 EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
140 EXPECT_EQ(i, GetSelectedIndex());
141 }
142 // Doesn't rotate.
143 EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
144 EXPECT_EQ(results - 1, GetSelectedIndex());
145 }
146
147 TEST_F(SearchResultListViewTest, AutoLaunch) {
148 SetUpAutoLaunchTimeout();
149 SetUpSearchResults();
150
151 EXPECT_TRUE(IsAutoLaunching());
152 ForceAutoLaunch();
153
154 EXPECT_FALSE(IsAutoLaunching());
155 EXPECT_EQ(1, GetOpenResultCountAndReset(0));
156 }
157
158 TEST_F(SearchResultListViewTest, CancelAutoLaunch) {
159 SetUpAutoLaunchTimeout();
160 SetUpSearchResults();
161
162 EXPECT_TRUE(IsAutoLaunching());
163
164 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
165 EXPECT_FALSE(IsAutoLaunching());
166
167 SetUpAutoLaunchTimeout();
168 view()->UpdateAutoLaunchState();
169 EXPECT_TRUE(IsAutoLaunching());
170
171 view()->SetVisible(false);
172 EXPECT_FALSE(IsAutoLaunching());
173
174 SetUpAutoLaunchTimeout();
175 view()->SetVisible(true);
176 EXPECT_TRUE(IsAutoLaunching());
177 }
178
179 } // namespace test
180 } // namespace app_list
OLDNEW
« ui/app_list/views/search_result_list_view.h ('K') | « ui/app_list/views/search_result_list_view.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698