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

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, 6 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 used for 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 ContinueSetUp(bool enable_fullscreen_app_list) {
vadimt 2017/06/22 00:53:05 This reminds me LongMnemonicFunctionName1. Please
newcomer 2017/06/22 16:17:21 I think the new one is better. LMK.
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_);
78 view_->UnitTestsRunning();
60 } 79 }
61 80
62 void TearDown() override { 81 void TearDown() override {
63 widget_->CloseNow(); 82 widget_->CloseNow();
64 views::test::WidgetTest::TearDown(); 83 views::test::WidgetTest::TearDown();
65 } 84 }
66 85
86 void EnableFullscreenAppList() {
87 scoped_feature_list_.InitAndEnableFeature(
88 app_list::features::kEnableFullscreenAppList);
89 }
90
67 protected: 91 protected:
68 SearchBoxView* view() { return view_; } 92 SearchBoxView* view() { return view_; }
69 93
70 void SetLongAutoLaunchTimeout() { 94 void SetLongAutoLaunchTimeout() {
71 // Sets a long timeout that lasts longer than the test run. 95 // Sets a long timeout that lasts longer than the test run.
72 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1)); 96 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1));
73 } 97 }
74 98
75 base::TimeDelta GetAutoLaunchTimeout() { 99 base::TimeDelta GetAutoLaunchTimeout() {
76 return view_delegate_.GetAutoLaunchTimeout(); 100 return view_delegate_.GetAutoLaunchTimeout();
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 139
116 void BackButtonPressed() override {} 140 void BackButtonPressed() override {}
117 141
118 void SetSearchResultSelection(bool select) override {} 142 void SetSearchResultSelection(bool select) override {}
119 143
120 AppListTestViewDelegate view_delegate_; 144 AppListTestViewDelegate view_delegate_;
121 views::Widget* widget_; 145 views::Widget* widget_;
122 SearchBoxView* view_; 146 SearchBoxView* view_;
123 KeyPressCounterView* counter_view_; 147 KeyPressCounterView* counter_view_;
124 base::string16 last_query_; 148 base::string16 last_query_;
149 base::test::ScopedFeatureList scoped_feature_list_;
125 int query_changed_count_; 150 int query_changed_count_;
126 151
127 DISALLOW_COPY_AND_ASSIGN(SearchBoxViewTest); 152 DISALLOW_COPY_AND_ASSIGN(SearchBoxViewTest);
128 }; 153 };
129 154
130 TEST_F(SearchBoxViewTest, Basic) { 155 TEST_F(SearchBoxViewTest, Basic) {
156 ContinueSetUp(false);
131 KeyPress(ui::VKEY_A); 157 KeyPress(ui::VKEY_A);
132 EXPECT_EQ("a", GetLastQueryAndReset()); 158 EXPECT_EQ("a", GetLastQueryAndReset());
133 EXPECT_EQ(1, GetQueryChangedCountAndReset()); 159 EXPECT_EQ(1, GetQueryChangedCountAndReset());
134 EXPECT_EQ(0, GetContentsViewKeyPressCountAndReset()); 160 EXPECT_EQ(0, GetContentsViewKeyPressCountAndReset());
135 161
136 KeyPress(ui::VKEY_DOWN); 162 KeyPress(ui::VKEY_DOWN);
137 EXPECT_EQ(0, GetQueryChangedCountAndReset()); 163 EXPECT_EQ(0, GetQueryChangedCountAndReset());
138 EXPECT_EQ(1, GetContentsViewKeyPressCountAndReset()); 164 EXPECT_EQ(1, GetContentsViewKeyPressCountAndReset());
139 165
140 view()->ClearSearch(); 166 view()->ClearSearch();
141 EXPECT_EQ(1, GetQueryChangedCountAndReset()); 167 EXPECT_EQ(1, GetQueryChangedCountAndReset());
142 EXPECT_TRUE(GetLastQueryAndReset().empty()); 168 EXPECT_TRUE(GetLastQueryAndReset().empty());
143 } 169 }
144 170
145 TEST_F(SearchBoxViewTest, CancelAutoLaunch) { 171 TEST_F(SearchBoxViewTest, CancelAutoLaunch) {
172 ContinueSetUp(false);
146 SetLongAutoLaunchTimeout(); 173 SetLongAutoLaunchTimeout();
147 ASSERT_NE(base::TimeDelta(), GetAutoLaunchTimeout()); 174 ASSERT_NE(base::TimeDelta(), GetAutoLaunchTimeout());
148 175
149 // Normal key event cancels the timeout. 176 // Normal key event cancels the timeout.
150 KeyPress(ui::VKEY_A); 177 KeyPress(ui::VKEY_A);
151 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout()); 178 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
152 ResetAutoLaunchTimeout(); 179 ResetAutoLaunchTimeout();
153 180
154 // Unusual key event doesn't cancel -- it will be canceled in 181 // Unusual key event doesn't cancel -- it will be canceled in
155 // SearchResultListView. 182 // SearchResultListView.
156 SetLongAutoLaunchTimeout(); 183 SetLongAutoLaunchTimeout();
157 KeyPress(ui::VKEY_DOWN); 184 KeyPress(ui::VKEY_DOWN);
158 EXPECT_NE(base::TimeDelta(), GetAutoLaunchTimeout()); 185 EXPECT_NE(base::TimeDelta(), GetAutoLaunchTimeout());
159 ResetAutoLaunchTimeout(); 186 ResetAutoLaunchTimeout();
160 187
161 // Clearing search box also cancels. 188 // Clearing search box also cancels.
162 SetLongAutoLaunchTimeout(); 189 SetLongAutoLaunchTimeout();
163 view()->ClearSearch(); 190 view()->ClearSearch();
164 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout()); 191 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
165 } 192 }
166 193
194 // Tests that a keypress enables the cursor and that an empty searchbox disables
195 // the cursor.
196 TEST_F(SearchBoxViewTest, KeyPressEnablesCursor) {
197 ContinueSetUp(true);
198
199 views::Textfield* search_box_text_field = view()->search_box();
200 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(),
201 gfx::Canvas::TEXT_ALIGN_CENTER);
202 EXPECT_EQ(search_box_text_field->placeholder_text_color(),
203 kDefaultSearchboxColor);
204 EXPECT_FALSE(view()->is_cursor_enabled());
205
206 // Press any key, the search box cursor should enable and the placeholder text
207 // should be aligned left
208 KeyPress(ui::VKEY_0);
209 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(),
210 gfx::Canvas::TEXT_ALIGN_LEFT);
211 EXPECT_EQ(search_box_text_field->placeholder_text_color(),
212 kZeroQuerySearchboxColor);
213 EXPECT_TRUE(view()->is_cursor_enabled());
214
215 // Delete the text, the cursor should return to its previous state with the
216 // placeholder text back in place.
217 view()->ClearSearch();
218 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(),
219 gfx::Canvas::TEXT_ALIGN_CENTER);
220 EXPECT_EQ(search_box_text_field->placeholder_text_color(),
221 kDefaultSearchboxColor);
222 EXPECT_FALSE(view()->is_cursor_enabled());
223 }
224
225 // Tests that the placeholder text and cursor are changed properly upon click.
226 TEST_F(SearchBoxViewTest, PlaceHolderTextTest) {
227 ContinueSetUp(true);
228
229 // Test that with no input the cursor is hidden and the text is center
230 // aligned.
231 views::Textfield* search_box_text_field = view()->search_box();
232 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(),
233 gfx::Canvas::TEXT_ALIGN_CENTER);
234 EXPECT_EQ(search_box_text_field->placeholder_text_color(),
235 kDefaultSearchboxColor);
236 EXPECT_FALSE(view()->is_cursor_enabled());
237
238 // click search box, text changes and aligns left but is still there
239 gfx::Point search_box_point =
240 search_box_text_field->GetBoundsInScreen().CenterPoint();
241 ui::MouseEvent mouse_event =
242 ui::MouseEvent(ui::ET_MOUSE_PRESSED, search_box_point, search_box_point,
243 base::TimeTicks::Now(), 0, 0);
244 EXPECT_TRUE(view()->PassMouseEventForTesting(mouse_event));
245 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(),
246 gfx::Canvas::TEXT_ALIGN_LEFT);
247 EXPECT_EQ(search_box_text_field->placeholder_text_color(),
248 kZeroQuerySearchboxColor);
249 EXPECT_TRUE(view()->is_cursor_enabled());
250 // Todo(newcomer): Add an EventGenerator for SearchBoxViewTest so we can pass
251 // gestures through the Textfield to SearchBoxView::HandleGestureEvent()
252 // crbug.com/735240
253 }
254
167 } // namespace test 255 } // namespace test
168 } // namespace app_list 256 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698