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_box_view.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/app_list/test/app_list_test_view_delegate.h"
12 #include "ui/app_list/views/search_box_view_delegate.h"
13 #include "ui/views/controls/textfield/textfield.h"
14 #include "ui/views/test/widget_test.h"
19 class KeyPressCounterView
: public views::View
{
21 KeyPressCounterView() : count_(0) {}
22 ~KeyPressCounterView() override
{}
24 int GetCountAndReset() {
31 // Overridden from views::View:
32 bool OnKeyPressed(const ui::KeyEvent
& key_event
) override
{
33 if (!::isalnum(static_cast<int>(key_event
.key_code()))) {
41 DISALLOW_COPY_AND_ASSIGN(KeyPressCounterView
);
44 class SearchBoxViewTest
: public views::test::WidgetTest
,
45 public SearchBoxViewDelegate
{
47 SearchBoxViewTest() : query_changed_count_(0) {}
48 ~SearchBoxViewTest() override
{}
50 // Overridden from testing::Test:
51 void SetUp() override
{
52 views::test::WidgetTest::SetUp();
53 widget_
= CreateTopLevelPlatformWidget();
54 view_
= new SearchBoxView(this, &view_delegate_
);
55 counter_view_
= new KeyPressCounterView();
56 widget_
->GetContentsView()->AddChildView(view_
);
57 widget_
->GetContentsView()->AddChildView(counter_view_
);
58 view_
->set_contents_view(counter_view_
);
61 void TearDown() override
{
63 views::test::WidgetTest::TearDown();
67 SearchBoxView
* view() { return view_
; }
69 void SetLongAutoLaunchTimeout() {
70 // Sets a long timeout that lasts longer than the test run.
71 view_delegate_
.set_auto_launch_timeout(base::TimeDelta::FromDays(1));
74 base::TimeDelta
GetAutoLaunchTimeout() {
75 return view_delegate_
.GetAutoLaunchTimeout();
78 void ResetAutoLaunchTimeout() {
79 view_delegate_
.set_auto_launch_timeout(base::TimeDelta());
82 int GetContentsViewKeyPressCountAndReset() {
83 return counter_view_
->GetCountAndReset();
86 void KeyPress(ui::KeyboardCode key_code
) {
87 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, key_code
, ui::EF_NONE
);
88 view_
->search_box()->OnKeyPressed(event
);
89 // Emulates the input method.
90 if (::isalnum(static_cast<int>(key_code
))) {
91 base::char16 character
= ::tolower(static_cast<int>(key_code
));
92 view_
->search_box()->InsertText(base::string16(1, character
));
96 std::string
GetLastQueryAndReset() {
97 base::string16 query
= last_query_
;
99 return base::UTF16ToUTF8(query
);
102 int GetQueryChangedCountAndReset() {
103 int result
= query_changed_count_
;
104 query_changed_count_
= 0;
109 // Overridden from SearchBoxViewDelegate:
110 void QueryChanged(SearchBoxView
* sender
) override
{
111 ++query_changed_count_
;
112 last_query_
= sender
->search_box()->text();
115 void BackButtonPressed() override
{}
117 void SetSearchResultSelection(bool select
) override
{}
119 AppListTestViewDelegate view_delegate_
;
120 views::Widget
* widget_
;
121 SearchBoxView
* view_
;
122 KeyPressCounterView
* counter_view_
;
123 base::string16 last_query_
;
124 int query_changed_count_
;
126 DISALLOW_COPY_AND_ASSIGN(SearchBoxViewTest
);
129 TEST_F(SearchBoxViewTest
, Basic
) {
130 KeyPress(ui::VKEY_A
);
131 EXPECT_EQ("a", GetLastQueryAndReset());
132 EXPECT_EQ(1, GetQueryChangedCountAndReset());
133 EXPECT_EQ(0, GetContentsViewKeyPressCountAndReset());
135 KeyPress(ui::VKEY_DOWN
);
136 EXPECT_EQ(0, GetQueryChangedCountAndReset());
137 EXPECT_EQ(1, GetContentsViewKeyPressCountAndReset());
139 view()->ClearSearch();
140 EXPECT_EQ(1, GetQueryChangedCountAndReset());
141 EXPECT_TRUE(GetLastQueryAndReset().empty());
144 TEST_F(SearchBoxViewTest
, CancelAutoLaunch
) {
145 SetLongAutoLaunchTimeout();
146 ASSERT_NE(base::TimeDelta(), GetAutoLaunchTimeout());
148 // Normal key event cancels the timeout.
149 KeyPress(ui::VKEY_A
);
150 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
151 ResetAutoLaunchTimeout();
153 // Unusual key event doesn't cancel -- it will be canceled in
154 // SearchResultListView.
155 SetLongAutoLaunchTimeout();
156 KeyPress(ui::VKEY_DOWN
);
157 EXPECT_NE(base::TimeDelta(), GetAutoLaunchTimeout());
158 ResetAutoLaunchTimeout();
160 // Clearing search box also cancels.
161 SetLongAutoLaunchTimeout();
162 view()->ClearSearch();
163 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
167 } // namespace app_list