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

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

Issue 2952763002: SearchBoxView now enables/disables cursor based on user interaction. (Closed)
Patch Set: SearchBoxView now enables/disables cursor based on user interaction. Created 3 years, 5 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/app_list/views/search_box_view.h" 5 #include "ui/app_list/views/search_box_view.h"
6 6
7 #include <cctype> 7 #include <cctype>
8 #include <map> 8 #include <map>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/scoped_feature_list.h"
13 #include "base/time/time.h"
14 #include "ui/app_list/app_list_features.h"
12 #include "ui/app_list/test/app_list_test_view_delegate.h" 15 #include "ui/app_list/test/app_list_test_view_delegate.h"
13 #include "ui/app_list/views/search_box_view_delegate.h" 16 #include "ui/app_list/views/search_box_view_delegate.h"
17 #include "ui/events/event.h"
18 #include "ui/gfx/canvas.h"
14 #include "ui/views/controls/textfield/textfield.h" 19 #include "ui/views/controls/textfield/textfield.h"
15 #include "ui/views/test/widget_test.h" 20 #include "ui/views/test/widget_test.h"
16 21
17 namespace app_list { 22 namespace app_list {
18 namespace test { 23 namespace test {
19 24
25 constexpr SkColor kDefaultSearchboxColor =
26 SkColorSetARGBMacro(0xDE, 0x00, 0x00, 0x00);
27
28 // Color of placeholder text in zero query state.
29 constexpr SkColor kZeroQuerySearchboxColor =
30 SkColorSetARGBMacro(0x8A, 0x00, 0x00, 0x00);
31
20 class KeyPressCounterView : public views::View { 32 class KeyPressCounterView : public views::View {
21 public: 33 public:
22 KeyPressCounterView() : count_(0) {} 34 KeyPressCounterView() : count_(0) {}
23 ~KeyPressCounterView() override {} 35 ~KeyPressCounterView() override {}
24 36
25 int GetCountAndReset() { 37 int GetCountAndReset() {
26 int count = count_; 38 int count = count_;
27 count_ = 0; 39 count_ = 0;
28 return count; 40 return count;
29 } 41 }
(...skipping 15 matching lines...) Expand all
45 class SearchBoxViewTest : public views::test::WidgetTest, 57 class SearchBoxViewTest : public views::test::WidgetTest,
46 public SearchBoxViewDelegate { 58 public SearchBoxViewDelegate {
47 public: 59 public:
48 SearchBoxViewTest() : query_changed_count_(0) {} 60 SearchBoxViewTest() : query_changed_count_(0) {}
49 ~SearchBoxViewTest() override {} 61 ~SearchBoxViewTest() override {}
50 62
51 // Overridden from testing::Test: 63 // Overridden from testing::Test:
52 void SetUp() override { 64 void SetUp() override {
53 views::test::WidgetTest::SetUp(); 65 views::test::WidgetTest::SetUp();
54 widget_ = CreateTopLevelPlatformWidget(); 66 widget_ = CreateTopLevelPlatformWidget();
67 }
68
69 void InitializeViewWithFeatureFlags(bool enable_fullscreen_app_list) {
70 if (enable_fullscreen_app_list)
71 EnableFullscreenAppList();
72
55 view_ = new SearchBoxView(this, &view_delegate_); 73 view_ = new SearchBoxView(this, &view_delegate_);
56 counter_view_ = new KeyPressCounterView(); 74 counter_view_ = new KeyPressCounterView();
57 widget_->GetContentsView()->AddChildView(view_); 75 widget_->GetContentsView()->AddChildView(view_);
58 widget_->GetContentsView()->AddChildView(counter_view_); 76 widget_->GetContentsView()->AddChildView(counter_view_);
59 view_->set_contents_view(counter_view_); 77 view_->set_contents_view(counter_view_);
60 } 78 }
61 79
62 void TearDown() override { 80 void TearDown() override {
63 widget_->CloseNow(); 81 widget_->CloseNow();
64 views::test::WidgetTest::TearDown(); 82 views::test::WidgetTest::TearDown();
65 } 83 }
66 84
85 void EnableFullscreenAppList() {
86 scoped_feature_list_.InitAndEnableFeature(
87 app_list::features::kEnableFullscreenAppList);
88 }
89
67 protected: 90 protected:
68 SearchBoxView* view() { return view_; } 91 SearchBoxView* view() { return view_; }
69 92
70 void SetLongAutoLaunchTimeout() { 93 void SetLongAutoLaunchTimeout() {
71 // Sets a long timeout that lasts longer than the test run. 94 // Sets a long timeout that lasts longer than the test run.
72 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1)); 95 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1));
73 } 96 }
74 97
75 base::TimeDelta GetAutoLaunchTimeout() { 98 base::TimeDelta GetAutoLaunchTimeout() {
76 return view_delegate_.GetAutoLaunchTimeout(); 99 return view_delegate_.GetAutoLaunchTimeout();
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 138
116 void BackButtonPressed() override {} 139 void BackButtonPressed() override {}
117 140
118 void SetSearchResultSelection(bool select) override {} 141 void SetSearchResultSelection(bool select) override {}
119 142
120 AppListTestViewDelegate view_delegate_; 143 AppListTestViewDelegate view_delegate_;
121 views::Widget* widget_; 144 views::Widget* widget_;
122 SearchBoxView* view_; 145 SearchBoxView* view_;
123 KeyPressCounterView* counter_view_; 146 KeyPressCounterView* counter_view_;
124 base::string16 last_query_; 147 base::string16 last_query_;
148 base::test::ScopedFeatureList scoped_feature_list_;
125 int query_changed_count_; 149 int query_changed_count_;
126 150
127 DISALLOW_COPY_AND_ASSIGN(SearchBoxViewTest); 151 DISALLOW_COPY_AND_ASSIGN(SearchBoxViewTest);
128 }; 152 };
129 153
130 TEST_F(SearchBoxViewTest, Basic) { 154 TEST_F(SearchBoxViewTest, Basic) {
155 InitializeViewWithFeatureFlags(false);
131 KeyPress(ui::VKEY_A); 156 KeyPress(ui::VKEY_A);
132 EXPECT_EQ("a", GetLastQueryAndReset()); 157 EXPECT_EQ("a", GetLastQueryAndReset());
133 EXPECT_EQ(1, GetQueryChangedCountAndReset()); 158 EXPECT_EQ(1, GetQueryChangedCountAndReset());
134 EXPECT_EQ(0, GetContentsViewKeyPressCountAndReset()); 159 EXPECT_EQ(0, GetContentsViewKeyPressCountAndReset());
135 160
136 KeyPress(ui::VKEY_DOWN); 161 KeyPress(ui::VKEY_DOWN);
137 EXPECT_EQ(0, GetQueryChangedCountAndReset()); 162 EXPECT_EQ(0, GetQueryChangedCountAndReset());
138 EXPECT_EQ(1, GetContentsViewKeyPressCountAndReset()); 163 EXPECT_EQ(1, GetContentsViewKeyPressCountAndReset());
139 164
140 view()->ClearSearch(); 165 view()->ClearSearch();
141 EXPECT_EQ(1, GetQueryChangedCountAndReset()); 166 EXPECT_EQ(1, GetQueryChangedCountAndReset());
142 EXPECT_TRUE(GetLastQueryAndReset().empty()); 167 EXPECT_TRUE(GetLastQueryAndReset().empty());
143 } 168 }
144 169
145 TEST_F(SearchBoxViewTest, CancelAutoLaunch) { 170 TEST_F(SearchBoxViewTest, CancelAutoLaunch) {
171 InitializeViewWithFeatureFlags(false);
146 SetLongAutoLaunchTimeout(); 172 SetLongAutoLaunchTimeout();
147 ASSERT_NE(base::TimeDelta(), GetAutoLaunchTimeout()); 173 ASSERT_NE(base::TimeDelta(), GetAutoLaunchTimeout());
148 174
149 // Normal key event cancels the timeout. 175 // Normal key event cancels the timeout.
150 KeyPress(ui::VKEY_A); 176 KeyPress(ui::VKEY_A);
151 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout()); 177 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
152 ResetAutoLaunchTimeout(); 178 ResetAutoLaunchTimeout();
153 179
154 // Unusual key event doesn't cancel -- it will be canceled in 180 // Unusual key event doesn't cancel -- it will be canceled in
155 // SearchResultListView. 181 // SearchResultListView.
156 SetLongAutoLaunchTimeout(); 182 SetLongAutoLaunchTimeout();
157 KeyPress(ui::VKEY_DOWN); 183 KeyPress(ui::VKEY_DOWN);
158 EXPECT_NE(base::TimeDelta(), GetAutoLaunchTimeout()); 184 EXPECT_NE(base::TimeDelta(), GetAutoLaunchTimeout());
159 ResetAutoLaunchTimeout(); 185 ResetAutoLaunchTimeout();
160 186
161 // Clearing search box also cancels. 187 // Clearing search box also cancels.
162 SetLongAutoLaunchTimeout(); 188 SetLongAutoLaunchTimeout();
163 view()->ClearSearch(); 189 view()->ClearSearch();
164 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout()); 190 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
165 } 191 }
166 192
193 // Tests that a keypress enables the cursor and that an empty searchbox disables
194 // the cursor.
195 TEST_F(SearchBoxViewTest, KeyPressEnablesCursor) {
196 InitializeViewWithFeatureFlags(true);
197
198 views::Textfield* search_box_text_field = view()->search_box();
199 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(),
200 gfx::Canvas::TEXT_ALIGN_CENTER);
201 EXPECT_EQ(search_box_text_field->placeholder_text_color(),
202 kDefaultSearchboxColor);
203 EXPECT_FALSE(view()->is_cursor_enabled());
204
205 // Press any key, the search box cursor should enable and the placeholder text
206 // should be aligned left
207 KeyPress(ui::VKEY_0);
208 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(),
209 gfx::Canvas::TEXT_ALIGN_LEFT);
210 EXPECT_EQ(search_box_text_field->placeholder_text_color(),
211 kZeroQuerySearchboxColor);
212 EXPECT_TRUE(view()->is_cursor_enabled());
213
214 // Delete the text, the cursor should return to its previous state with the
215 // placeholder text back in place.
216 view()->ClearSearch();
217 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(),
218 gfx::Canvas::TEXT_ALIGN_CENTER);
219 EXPECT_EQ(search_box_text_field->placeholder_text_color(),
220 kDefaultSearchboxColor);
221 EXPECT_FALSE(view()->is_cursor_enabled());
222 }
223
224 // Tests that the placeholder text and cursor are changed properly upon click.
225 TEST_F(SearchBoxViewTest, PlaceHolderTextTest) {
226 InitializeViewWithFeatureFlags(true);
227
228 // Test that with no input the cursor is hidden and the text is center
229 // aligned.
230 views::Textfield* search_box_text_field = view()->search_box();
231 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(),
232 gfx::Canvas::TEXT_ALIGN_CENTER);
233 EXPECT_EQ(search_box_text_field->placeholder_text_color(),
234 kDefaultSearchboxColor);
235 EXPECT_FALSE(view()->is_cursor_enabled());
236
237 // click search box, text changes and aligns left but is still there
238 gfx::Point search_box_point =
239 search_box_text_field->GetBoundsInScreen().CenterPoint();
240 ui::MouseEvent mouse_event =
241 ui::MouseEvent(ui::ET_MOUSE_PRESSED, search_box_point, search_box_point,
242 base::TimeTicks::Now(), 0, 0);
243 EXPECT_TRUE(view()->OnMouseEvent(*mouse_event));
244 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(),
245 gfx::Canvas::TEXT_ALIGN_LEFT);
246 EXPECT_EQ(search_box_text_field->placeholder_text_color(),
247 kZeroQuerySearchboxColor);
248 EXPECT_TRUE(view()->is_cursor_enabled());
249 }
250
167 } // namespace test 251 } // namespace test
168 } // namespace app_list 252 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698