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

Side by Side Diff: ui/views/controls/button/custom_button_unittest.cc

Issue 1411523009: Adds OnClickCanceled callback to views::Button (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adds NotifyReleasedWithoutClick callback to views::Button (comments) Created 5 years 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
« no previous file with comments | « ui/views/controls/button/custom_button.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/views/controls/button/custom_button.h" 5 #include "ui/views/controls/button/custom_button.h"
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/base/layout.h" 8 #include "ui/base/layout.h"
9 #include "ui/events/event_utils.h" 9 #include "ui/events/event_utils.h"
10 #include "ui/gfx/screen.h" 10 #include "ui/gfx/screen.h"
(...skipping 18 matching lines...) Expand all
29 29
30 class TestCustomButton : public CustomButton, public ButtonListener { 30 class TestCustomButton : public CustomButton, public ButtonListener {
31 public: 31 public:
32 explicit TestCustomButton() 32 explicit TestCustomButton()
33 : CustomButton(this) { 33 : CustomButton(this) {
34 } 34 }
35 35
36 ~TestCustomButton() override {} 36 ~TestCustomButton() override {}
37 37
38 void ButtonPressed(Button* sender, const ui::Event& event) override { 38 void ButtonPressed(Button* sender, const ui::Event& event) override {
39 notified_ = true; 39 pressed_ = true;
40 } 40 }
41 41
42 bool notified() { return notified_; } 42 void OnClickCanceled(const ui::Event& event) override {
43 canceled_ = true;
44 }
43 45
44 void Reset() { notified_ = false; } 46 bool pressed() { return pressed_; }
47 bool canceled() { return canceled_; }
48
49 void Reset() {
50 pressed_ = false;
51 canceled_ = false;
52 }
45 53
46 // CustomButton methods: 54 // CustomButton methods:
47 bool IsChildWidget() const override { return is_child_widget_; } 55 bool IsChildWidget() const override { return is_child_widget_; }
48 bool FocusInChildWidget() const override { return focus_in_child_widget_; } 56 bool FocusInChildWidget() const override { return focus_in_child_widget_; }
49 57
50 void set_child_widget(bool b) { is_child_widget_ = b; } 58 void set_child_widget(bool b) { is_child_widget_ = b; }
51 void set_focus_in_child_widget(bool b) { focus_in_child_widget_ = b; } 59 void set_focus_in_child_widget(bool b) { focus_in_child_widget_ = b; }
52 60
53 private: 61 private:
54 bool notified_ = false; 62 bool pressed_ = false;
63 bool canceled_ = false;
55 bool is_child_widget_ = false; 64 bool is_child_widget_ = false;
56 bool focus_in_child_widget_ = false; 65 bool focus_in_child_widget_ = false;
57 66
58 DISALLOW_COPY_AND_ASSIGN(TestCustomButton); 67 DISALLOW_COPY_AND_ASSIGN(TestCustomButton);
59 }; 68 };
60 69
61 class TestWidget : public Widget { 70 class TestWidget : public Widget {
62 public: 71 public:
63 TestWidget() : Widget() {} 72 TestWidget() : Widget() {}
64 73
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 204
196 // Tests the different types of NotifyActions. 205 // Tests the different types of NotifyActions.
197 TEST_F(CustomButtonTest, NotifyAction) { 206 TEST_F(CustomButtonTest, NotifyAction) {
198 gfx::Point center(10, 10); 207 gfx::Point center(10, 10);
199 208
200 // By default the button should notify its listener on mouse release. 209 // By default the button should notify its listener on mouse release.
201 button()->OnMousePressed(ui::MouseEvent( 210 button()->OnMousePressed(ui::MouseEvent(
202 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(), 211 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(),
203 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); 212 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
204 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state()); 213 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state());
205 EXPECT_FALSE(button()->notified()); 214 EXPECT_FALSE(button()->pressed());
206 215
207 button()->OnMouseReleased(ui::MouseEvent( 216 button()->OnMouseReleased(ui::MouseEvent(
208 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(), 217 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
209 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); 218 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
210 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state()); 219 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state());
211 EXPECT_TRUE(button()->notified()); 220 EXPECT_TRUE(button()->pressed());
212 221
213 // Set the notify action to its listener on mouse press. 222 // Set the notify action to its listener on mouse press.
214 button()->Reset(); 223 button()->Reset();
215 button()->set_notify_action(CustomButton::NOTIFY_ON_PRESS); 224 button()->set_notify_action(CustomButton::NOTIFY_ON_PRESS);
216 button()->OnMousePressed(ui::MouseEvent( 225 button()->OnMousePressed(ui::MouseEvent(
217 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(), 226 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(),
218 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); 227 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
219 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state()); 228 EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state());
220 EXPECT_TRUE(button()->notified()); 229 EXPECT_TRUE(button()->pressed());
221 230
222 // The button should no longer notify on mouse release. 231 // The button should no longer notify on mouse release.
223 button()->Reset(); 232 button()->Reset();
224 button()->OnMouseReleased(ui::MouseEvent( 233 button()->OnMouseReleased(ui::MouseEvent(
225 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(), 234 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
226 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); 235 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
227 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state()); 236 EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state());
228 EXPECT_FALSE(button()->notified()); 237 EXPECT_FALSE(button()->pressed());
229 } 238 }
230 239
231 TEST_F(CustomButtonTest, HandleAccelerator) { 240 TEST_F(CustomButtonTest, HandleAccelerator) {
232 // Child widgets shouldn't handle accelerators when they are not focused. 241 // Child widgets shouldn't handle accelerators when they are not focused.
233 EXPECT_FALSE(button()->IsChildWidget()); 242 EXPECT_FALSE(button()->IsChildWidget());
234 EXPECT_FALSE(button()->FocusInChildWidget()); 243 EXPECT_FALSE(button()->FocusInChildWidget());
235 EXPECT_FALSE(widget()->IsActive()); 244 EXPECT_FALSE(widget()->IsActive());
236 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)); 245 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
237 EXPECT_FALSE(button()->notified()); 246 EXPECT_FALSE(button()->pressed());
238 // Child without focus. 247 // Child without focus.
239 button()->set_child_widget(true); 248 button()->set_child_widget(true);
240 button()->set_focus_in_child_widget(false); 249 button()->set_focus_in_child_widget(false);
241 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)); 250 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
242 EXPECT_FALSE(button()->notified()); 251 EXPECT_FALSE(button()->pressed());
243 button()->Reset(); 252 button()->Reset();
244 // Child with focus. 253 // Child with focus.
245 button()->set_child_widget(true); 254 button()->set_child_widget(true);
246 button()->set_focus_in_child_widget(true); 255 button()->set_focus_in_child_widget(true);
247 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)); 256 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
248 EXPECT_TRUE(button()->notified()); 257 EXPECT_TRUE(button()->pressed());
249 button()->Reset(); 258 button()->Reset();
250 // Not a child, but active. 259 // Not a child, but active.
251 button()->set_child_widget(false); 260 button()->set_child_widget(false);
252 button()->set_focus_in_child_widget(true); 261 button()->set_focus_in_child_widget(true);
253 widget()->set_active(true); 262 widget()->set_active(true);
254 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)); 263 button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
255 EXPECT_TRUE(button()->notified()); 264 EXPECT_TRUE(button()->pressed());
265 }
266
267 // Tests that OnClickCanceled gets called when NotifyClick is not expected
268 // anymore.
269 TEST_F(CustomButtonTest, NotifyActionNoClick) {
270 gfx::Point center(10, 10);
271
272 // By default the button should notify its listener on mouse release.
273 button()->OnMousePressed(ui::MouseEvent(
274 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(),
275 ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
276 EXPECT_FALSE(button()->canceled());
277
278 button()->OnMouseReleased(ui::MouseEvent(
279 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
280 ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
281 EXPECT_TRUE(button()->canceled());
282
283 // Set the notify action to its listener on mouse press.
284 button()->Reset();
285 button()->set_notify_action(CustomButton::NOTIFY_ON_PRESS);
286 button()->OnMousePressed(ui::MouseEvent(
287 ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(),
288 ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
289 // OnClickCanceled is only sent on mouse release.
290 EXPECT_FALSE(button()->canceled());
291
292 // The button should no longer notify on mouse release.
293 button()->Reset();
294 button()->OnMouseReleased(ui::MouseEvent(
295 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
296 ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
297 EXPECT_FALSE(button()->canceled());
256 } 298 }
257 299
258 // No touch on desktop Mac. Tracked in http://crbug.com/445520. 300 // No touch on desktop Mac. Tracked in http://crbug.com/445520.
259 #if !defined(OS_MACOSX) || defined(USE_AURA) 301 #if !defined(OS_MACOSX) || defined(USE_AURA)
260 302
261 namespace { 303 namespace {
262 304
263 void PerformGesture(CustomButton* button, ui::EventType event_type) { 305 void PerformGesture(CustomButton* button, ui::EventType event_type) {
264 ui::GestureEventDetails gesture_details(event_type); 306 ui::GestureEventDetails gesture_details(event_type);
265 base::TimeDelta time_stamp = base::TimeDelta::FromMicroseconds(0); 307 base::TimeDelta time_stamp = base::TimeDelta::FromMicroseconds(0);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 EXPECT_FALSE(CustomButton::AsCustomButton(&label)); 353 EXPECT_FALSE(CustomButton::AsCustomButton(&label));
312 354
313 Link link(text); 355 Link link(text);
314 EXPECT_FALSE(CustomButton::AsCustomButton(&link)); 356 EXPECT_FALSE(CustomButton::AsCustomButton(&link));
315 357
316 Textfield textfield; 358 Textfield textfield;
317 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield)); 359 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield));
318 } 360 }
319 361
320 } // namespace views 362 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/button/custom_button.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698