Skip to content

Commit

Permalink
[Views] Reset menu button state after activation
Browse files Browse the repository at this point in the history
MenuButton::Activate() sets the state to STATE_PRESSED, but never sets it back.
This is a problem with buttons that don't run menu controller code (which would
set the state) and gesture events (which means the view doesn't get mouse
entered/left events). Fix it, and add a few tests.

BUG=548149

Review URL: https://codereview.chromium.org/1425083002

Cr-Commit-Position: refs/heads/master@{#356950}
  • Loading branch information
rdcronin authored and Commit bot committed Oct 29, 2015
1 parent ae197fb commit 6c6f6aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions ui/views/controls/button/menu_button.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ MenuButton::~MenuButton() {
////////////////////////////////////////////////////////////////////////////////

bool MenuButton::Activate() {
SetState(STATE_PRESSED);
PressedLock pressed_lock(this);
if (listener_) {
gfx::Rect lb = GetLocalBounds();

Expand Down Expand Up @@ -219,10 +219,12 @@ void MenuButton::OnGestureEvent(ui::GestureEvent* event) {
if (switches::IsTouchFeedbackEnabled()) {
if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
event->SetHandled();
SetState(Button::STATE_HOVERED);
if (pressed_lock_count_ == 0)
SetState(Button::STATE_HOVERED);
} else if (state() == Button::STATE_HOVERED &&
(event->type() == ui::ET_GESTURE_TAP_CANCEL ||
event->type() == ui::ET_GESTURE_END)) {
event->type() == ui::ET_GESTURE_END) &&
pressed_lock_count_ == 0) {
SetState(Button::STATE_NORMAL);
}
}
Expand Down
18 changes: 18 additions & 0 deletions ui/views/controls/button/menu_button_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,22 @@ TEST_F(MenuButtonTest, ActivateNonDropDownOnGestureTap) {
CreateMenuButtonWithButtonListener(&button_listener);

ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow());

// Move the mouse outside the menu button so that it doesn't impact the
// button state.
generator.MoveMouseTo(400, 400);
EXPECT_FALSE(button()->IsMouseHovered());

generator.GestureTapAt(gfx::Point(10, 10));

// Check that MenuButton has notified the listener on gesture tap event, while
// it was in hovered state.
EXPECT_EQ(button(), button_listener.last_sender());
EXPECT_EQ(ui::ET_GESTURE_TAP, button_listener.last_event_type());
EXPECT_EQ(Button::STATE_HOVERED, button_listener.last_sender_state());

// The button should go back to it's normal state since the gesture ended.
EXPECT_EQ(Button::STATE_NORMAL, button()->state());
}

// Tests if the listener is notified correctly when a gesture tap happens on a
Expand All @@ -412,12 +421,21 @@ TEST_F(MenuButtonTest, ActivateDropDownOnGestureTap) {
CreateMenuButtonWithMenuButtonListener(&menu_button_listener);

ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow());

// Move the mouse outside the menu button so that it doesn't impact the
// button state.
generator.MoveMouseTo(400, 400);
EXPECT_FALSE(button()->IsMouseHovered());

generator.GestureTapAt(gfx::Point(10, 10));

// Check that MenuButton has notified the listener, while it was in pressed
// state.
EXPECT_EQ(button(), menu_button_listener.last_source());
EXPECT_EQ(Button::STATE_PRESSED, menu_button_listener.last_source_state());

// The button should go back to it's normal state since the gesture ended.
EXPECT_EQ(Button::STATE_NORMAL, button()->state());
}

// Tests that the button enters a hovered state upon a tap down, before becoming
Expand Down

0 comments on commit 6c6f6aa

Please sign in to comment.