Commit e70c716f authored by Toni Barzic's avatar Toni Barzic Committed by Chromium LUCI CQ

Update hover button state before calling press callback

Handling button press may delete the button view, so referecing the
hover button after button press callback is called is not safe.

BUG=1142568

Change-Id: I4b062cfc1ca632bf590df7892b58592680a38ab7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2618577
Commit-Queue: Toni Baržić <tbarzic@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842282}
parent 97639116
...@@ -58,9 +58,9 @@ void HoverButtonController::OnMouseReleased(const ui::MouseEvent& event) { ...@@ -58,9 +58,9 @@ void HoverButtonController::OnMouseReleased(const ui::MouseEvent& event) {
void HoverButtonController::OnGestureEvent(ui::GestureEvent* event) { void HoverButtonController::OnGestureEvent(ui::GestureEvent* event) {
if (event->type() == ui::ET_GESTURE_TAP) { if (event->type() == ui::ET_GESTURE_TAP) {
button()->SetState(views::Button::STATE_NORMAL);
if (callback_) if (callback_)
callback_.Run(*event); callback_.Run(*event);
button()->SetState(views::Button::STATE_NORMAL);
} else { } else {
ButtonController::OnGestureEvent(event); ButtonController::OnGestureEvent(event);
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/test/views/chrome_views_test_base.h" #include "chrome/test/views/chrome_views_test_base.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_enums.mojom.h" #include "ui/accessibility/ax_enums.mojom.h"
...@@ -159,4 +160,31 @@ TEST_F(HoverButtonTest, ActivatesOnMouseReleased) { ...@@ -159,4 +160,31 @@ TEST_F(HoverButtonTest, ActivatesOnMouseReleased) {
widget()->Close(); widget()->Close();
} }
// No touch on desktop Mac.
#if !defined(OS_MAC) || defined(USE_AURA)
// Tests that tapping hover button does not crash if the tap handler removes the
// button from views hierarchy.
TEST_F(HoverButtonTest, TapGestureThatDeletesTheButton) {
bool clicked = false;
HoverButton* button = widget()->SetContentsView(std::make_unique<HoverButton>(
base::BindRepeating(
[](bool* clicked, views::Widget* widget) {
*clicked = true;
// Update the widget contents view, which deletes the hover button.
widget->SetContentsView(std::make_unique<views::View>());
},
&clicked, widget()),
CreateIcon(), base::ASCIIToUTF16("Title"), base::string16()));
button->SetBoundsRect(gfx::Rect(100, 100, 200, 200));
widget()->Show();
generator()->GestureTapAt(gfx::Point(150, 150));
EXPECT_TRUE(clicked);
widget()->Close();
}
#endif // !defined(OS_MAC) || defined(USE_AURA)
} // namespace } // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment