Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / app_list / views / search_result_list_view_unittest.cc
blob3d857ecf14df9db1526e28da016ae6f77ee4f66b
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"
7 #include <map>
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/app_list/app_list_model.h"
12 #include "ui/app_list/test/app_list_test_view_delegate.h"
13 #include "ui/app_list/test/test_search_result.h"
14 #include "ui/app_list/views/progress_bar_view.h"
15 #include "ui/app_list/views/search_result_list_view_delegate.h"
16 #include "ui/app_list/views/search_result_view.h"
17 #include "ui/views/test/views_test_base.h"
19 namespace app_list {
20 namespace test {
22 namespace {
23 int kDefaultSearchItems = 5;
24 } // namespace
26 class SearchResultListViewTest : public views::ViewsTestBase,
27 public SearchResultListViewDelegate {
28 public:
29 SearchResultListViewTest() {}
30 ~SearchResultListViewTest() override {}
32 // Overridden from testing::Test:
33 void SetUp() override {
34 views::ViewsTestBase::SetUp();
35 view_.reset(new SearchResultListView(this, &view_delegate_));
36 view_->SetResults(view_delegate_.GetModel()->results());
39 protected:
40 SearchResultListView* view() { return view_.get(); }
42 SearchResultView* GetResultViewAt(int index) {
43 return view_->GetResultViewAt(index);
46 AppListModel::SearchResults* GetResults() {
47 return view_delegate_.GetModel()->results();
50 void SetLongAutoLaunchTimeout() {
51 // Sets a long timeout that lasts longer than the test run.
52 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1));
55 base::TimeDelta GetAutoLaunchTimeout() {
56 return view_delegate_.GetAutoLaunchTimeout();
59 void SetUpSearchResults() {
60 AppListModel::SearchResults* results = GetResults();
61 for (int i = 0; i < kDefaultSearchItems; ++i) {
62 TestSearchResult* result = new TestSearchResult();
63 result->set_display_type(SearchResult::DISPLAY_LIST);
64 result->set_title(base::UTF8ToUTF16(base::StringPrintf("Result %d", i)));
65 if (i < 2)
66 result->set_details(base::ASCIIToUTF16("Detail"));
67 results->Add(result);
70 // Adding results will schedule Update().
71 RunPendingMessages();
72 view_->OnContainerSelected(false, false);
75 int GetOpenResultCountAndReset(int ranking) {
76 int result = view_delegate_.open_search_result_counts()[ranking];
77 view_delegate_.open_search_result_counts().clear();
78 return result;
81 int GetResultCount() { return view_->num_results(); }
83 int GetSelectedIndex() { return view_->selected_index(); }
85 void ResetSelectedIndex() {
86 view_->SetSelectedIndex(0);
89 void AddTestResultAtIndex(int index) {
90 GetResults()->Add(new TestSearchResult());
93 void DeleteResultAt(int index) { GetResults()->DeleteAt(index); }
95 bool KeyPress(ui::KeyboardCode key_code) {
96 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE);
97 return view_->OnKeyPressed(event);
100 bool IsAutoLaunching() {
101 return view_->auto_launch_animation_;
104 void ForceAutoLaunch() {
105 view_->ForceAutoLaunchForTest();
108 void ExpectConsistent() {
109 // Adding results will schedule Update().
110 RunPendingMessages();
112 AppListModel::SearchResults* results = GetResults();
113 for (size_t i = 0; i < results->item_count(); ++i) {
114 EXPECT_EQ(results->GetItemAt(i), GetResultViewAt(i)->result());
118 ProgressBarView* GetProgressBarAt(size_t index) {
119 return GetResultViewAt(index)->progress_bar_;
122 private:
123 void OnResultInstalled(SearchResult* result) override {}
125 AppListTestViewDelegate view_delegate_;
126 scoped_ptr<SearchResultListView> view_;
128 DISALLOW_COPY_AND_ASSIGN(SearchResultListViewTest);
131 TEST_F(SearchResultListViewTest, Basic) {
132 SetUpSearchResults();
134 const int results = GetResultCount();
135 EXPECT_EQ(kDefaultSearchItems, results);
136 EXPECT_EQ(0, GetSelectedIndex());
137 EXPECT_FALSE(IsAutoLaunching());
139 EXPECT_TRUE(KeyPress(ui::VKEY_RETURN));
140 EXPECT_EQ(1, GetOpenResultCountAndReset(0));
142 for (int i = 1; i < results; ++i) {
143 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
144 EXPECT_EQ(i, GetSelectedIndex());
146 // When navigating off the end of the list, pass the event to the parent to
147 // handle.
148 EXPECT_FALSE(KeyPress(ui::VKEY_DOWN));
149 EXPECT_EQ(results - 1, GetSelectedIndex());
151 for (int i = 1; i < results; ++i) {
152 EXPECT_TRUE(KeyPress(ui::VKEY_UP));
153 EXPECT_EQ(results - i - 1, GetSelectedIndex());
155 // Navigate off top of list.
156 EXPECT_FALSE(KeyPress(ui::VKEY_UP));
157 EXPECT_EQ(0, GetSelectedIndex());
158 ResetSelectedIndex();
160 for (int i = 1; i < results; ++i) {
161 EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
162 EXPECT_EQ(i, GetSelectedIndex());
164 // Navigate off bottom of list.
165 EXPECT_FALSE(KeyPress(ui::VKEY_TAB));
166 EXPECT_EQ(results - 1, GetSelectedIndex());
169 TEST_F(SearchResultListViewTest, AutoLaunch) {
170 SetLongAutoLaunchTimeout();
171 SetUpSearchResults();
173 EXPECT_TRUE(IsAutoLaunching());
174 ForceAutoLaunch();
176 EXPECT_FALSE(IsAutoLaunching());
177 EXPECT_EQ(1, GetOpenResultCountAndReset(0));
179 // The timeout has to be cleared after the auto-launch, to prevent opening
180 // the search result twice. See the comment in AnimationEnded().
181 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
184 TEST_F(SearchResultListViewTest, CancelAutoLaunch) {
185 SetLongAutoLaunchTimeout();
186 SetUpSearchResults();
188 EXPECT_TRUE(IsAutoLaunching());
190 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
191 EXPECT_FALSE(IsAutoLaunching());
193 SetLongAutoLaunchTimeout();
194 view()->UpdateAutoLaunchState();
195 EXPECT_TRUE(IsAutoLaunching());
197 view()->SetVisible(false);
198 EXPECT_FALSE(IsAutoLaunching());
200 SetLongAutoLaunchTimeout();
201 view()->SetVisible(true);
202 EXPECT_TRUE(IsAutoLaunching());
205 TEST_F(SearchResultListViewTest, SpokenFeedback) {
206 SetUpSearchResults();
208 // Result 0 has a detail text. Expect that the detail is appended to the
209 // accessibility name.
210 EXPECT_EQ(base::ASCIIToUTF16("Result 0, Detail"),
211 GetResultViewAt(0)->ComputeAccessibleName());
213 // Result 2 has no detail text.
214 EXPECT_EQ(base::ASCIIToUTF16("Result 2"),
215 GetResultViewAt(2)->ComputeAccessibleName());
218 TEST_F(SearchResultListViewTest, ModelObservers) {
219 SetUpSearchResults();
220 ExpectConsistent();
222 // Insert at start.
223 AddTestResultAtIndex(0);
224 ExpectConsistent();
226 // Remove from end.
227 DeleteResultAt(kDefaultSearchItems);
228 ExpectConsistent();
230 // Insert at end.
231 AddTestResultAtIndex(kDefaultSearchItems);
232 ExpectConsistent();
234 // Delete from start.
235 DeleteResultAt(0);
236 ExpectConsistent();
239 // Regression test for http://crbug.com/402859 to ensure ProgressBar is
240 // initialized properly in SearchResultListView::SetResult().
241 TEST_F(SearchResultListViewTest, ProgressBar) {
242 SetUpSearchResults();
244 GetResults()->GetItemAt(0)->SetIsInstalling(true);
245 EXPECT_EQ(0.0f, GetProgressBarAt(0)->current_value());
246 GetResults()->GetItemAt(0)->SetPercentDownloaded(10);
248 DeleteResultAt(0);
249 RunPendingMessages();
250 EXPECT_EQ(0.0f, GetProgressBarAt(0)->current_value());
253 } // namespace test
254 } // namespace app_list