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

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 (one more case handled) 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
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 21 matching lines...) Expand all
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 notified_ = true;
40 } 40 }
41 41
42 void OnClickCanceled(const ui::Event& event) override {
43 notified_no_click_ = true;
44 }
45
42 bool notified() { return notified_; } 46 bool notified() { return notified_; }
47 bool notified_no_click() { return notified_no_click_; }
43 48
44 void Reset() { notified_ = false; } 49 void Reset() {
50 notified_ = false;
51 notified_no_click_ = 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 notified_ = false;
63 bool notified_no_click_ = false;
Peter Kasting 2015/12/01 02:03:18 Why not call these |pressed_| and |canceled_|? Th
varkha 2015/12/01 19:23:29 Done.
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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 EXPECT_TRUE(button()->notified()); 257 EXPECT_TRUE(button()->notified());
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()->notified());
256 } 265 }
257 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()->notified_no_click());
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()->notified_no_click());
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 EXPECT_TRUE(button()->notified_no_click());
290
291 // The button should no longer notify on mouse release.
292 button()->Reset();
293 button()->OnMouseReleased(ui::MouseEvent(
294 ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
295 ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
296 EXPECT_FALSE(button()->notified_no_click());
297 }
298
258 // No touch on desktop Mac. Tracked in http://crbug.com/445520. 299 // No touch on desktop Mac. Tracked in http://crbug.com/445520.
259 #if !defined(OS_MACOSX) || defined(USE_AURA) 300 #if !defined(OS_MACOSX) || defined(USE_AURA)
260 301
261 namespace { 302 namespace {
262 303
263 void PerformGesture(CustomButton* button, ui::EventType event_type) { 304 void PerformGesture(CustomButton* button, ui::EventType event_type) {
264 ui::GestureEventDetails gesture_details(event_type); 305 ui::GestureEventDetails gesture_details(event_type);
265 base::TimeDelta time_stamp = base::TimeDelta::FromMicroseconds(0); 306 base::TimeDelta time_stamp = base::TimeDelta::FromMicroseconds(0);
266 ui::GestureEvent gesture_event(0, 0, 0, time_stamp, gesture_details); 307 ui::GestureEvent gesture_event(0, 0, 0, time_stamp, gesture_details);
267 button->OnGestureEvent(&gesture_event); 308 button->OnGestureEvent(&gesture_event);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 EXPECT_FALSE(CustomButton::AsCustomButton(&label)); 352 EXPECT_FALSE(CustomButton::AsCustomButton(&label));
312 353
313 Link link(text); 354 Link link(text);
314 EXPECT_FALSE(CustomButton::AsCustomButton(&link)); 355 EXPECT_FALSE(CustomButton::AsCustomButton(&link));
315 356
316 Textfield textfield; 357 Textfield textfield;
317 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield)); 358 EXPECT_FALSE(CustomButton::AsCustomButton(&textfield));
318 } 359 }
319 360
320 } // namespace views 361 } // namespace views
OLDNEW
« ui/views/controls/button/button.h ('K') | « 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