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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/views/controls/button/custom_button.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/controls/button/custom_button_unittest.cc
diff --git a/ui/views/controls/button/custom_button_unittest.cc b/ui/views/controls/button/custom_button_unittest.cc
index 7a079fe7bba05f22de2c3e0b5209d16579e94b1b..5704e734b8312219a8d9d6a664387595ae03d66c 100644
--- a/ui/views/controls/button/custom_button_unittest.cc
+++ b/ui/views/controls/button/custom_button_unittest.cc
@@ -36,12 +36,20 @@ class TestCustomButton : public CustomButton, public ButtonListener {
~TestCustomButton() override {}
void ButtonPressed(Button* sender, const ui::Event& event) override {
- notified_ = true;
+ pressed_ = true;
}
- bool notified() { return notified_; }
+ void OnClickCanceled(const ui::Event& event) override {
+ canceled_ = true;
+ }
+
+ bool pressed() { return pressed_; }
+ bool canceled() { return canceled_; }
- void Reset() { notified_ = false; }
+ void Reset() {
+ pressed_ = false;
+ canceled_ = false;
+ }
// CustomButton methods:
bool IsChildWidget() const override { return is_child_widget_; }
@@ -51,7 +59,8 @@ class TestCustomButton : public CustomButton, public ButtonListener {
void set_focus_in_child_widget(bool b) { focus_in_child_widget_ = b; }
private:
- bool notified_ = false;
+ bool pressed_ = false;
+ bool canceled_ = false;
bool is_child_widget_ = false;
bool focus_in_child_widget_ = false;
@@ -202,13 +211,13 @@ TEST_F(CustomButtonTest, NotifyAction) {
ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state());
- EXPECT_FALSE(button()->notified());
+ EXPECT_FALSE(button()->pressed());
button()->OnMouseReleased(ui::MouseEvent(
ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state());
- EXPECT_TRUE(button()->notified());
+ EXPECT_TRUE(button()->pressed());
// Set the notify action to its listener on mouse press.
button()->Reset();
@@ -217,7 +226,7 @@ TEST_F(CustomButtonTest, NotifyAction) {
ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
EXPECT_EQ(CustomButton::STATE_PRESSED, button()->state());
- EXPECT_TRUE(button()->notified());
+ EXPECT_TRUE(button()->pressed());
// The button should no longer notify on mouse release.
button()->Reset();
@@ -225,7 +234,7 @@ TEST_F(CustomButtonTest, NotifyAction) {
ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
EXPECT_EQ(CustomButton::STATE_HOVERED, button()->state());
- EXPECT_FALSE(button()->notified());
+ EXPECT_FALSE(button()->pressed());
}
TEST_F(CustomButtonTest, HandleAccelerator) {
@@ -234,25 +243,58 @@ TEST_F(CustomButtonTest, HandleAccelerator) {
EXPECT_FALSE(button()->FocusInChildWidget());
EXPECT_FALSE(widget()->IsActive());
button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
- EXPECT_FALSE(button()->notified());
+ EXPECT_FALSE(button()->pressed());
// Child without focus.
button()->set_child_widget(true);
button()->set_focus_in_child_widget(false);
button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
- EXPECT_FALSE(button()->notified());
+ EXPECT_FALSE(button()->pressed());
button()->Reset();
// Child with focus.
button()->set_child_widget(true);
button()->set_focus_in_child_widget(true);
button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
- EXPECT_TRUE(button()->notified());
+ EXPECT_TRUE(button()->pressed());
button()->Reset();
// Not a child, but active.
button()->set_child_widget(false);
button()->set_focus_in_child_widget(true);
widget()->set_active(true);
button()->AcceleratorPressed(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE));
- EXPECT_TRUE(button()->notified());
+ EXPECT_TRUE(button()->pressed());
+}
+
+// Tests that OnClickCanceled gets called when NotifyClick is not expected
+// anymore.
+TEST_F(CustomButtonTest, NotifyActionNoClick) {
+ gfx::Point center(10, 10);
+
+ // By default the button should notify its listener on mouse release.
+ button()->OnMousePressed(ui::MouseEvent(
+ ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(),
+ ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
+ EXPECT_FALSE(button()->canceled());
+
+ button()->OnMouseReleased(ui::MouseEvent(
+ ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
+ ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
+ EXPECT_TRUE(button()->canceled());
+
+ // Set the notify action to its listener on mouse press.
+ button()->Reset();
+ button()->set_notify_action(CustomButton::NOTIFY_ON_PRESS);
+ button()->OnMousePressed(ui::MouseEvent(
+ ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(),
+ ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
+ // OnClickCanceled is only sent on mouse release.
+ EXPECT_FALSE(button()->canceled());
+
+ // The button should no longer notify on mouse release.
+ button()->Reset();
+ button()->OnMouseReleased(ui::MouseEvent(
+ ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
+ ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_RIGHT_MOUSE_BUTTON));
+ EXPECT_FALSE(button()->canceled());
}
// No touch on desktop Mac. Tracked in http://crbug.com/445520.
« 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