1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/button/menu_button.h"
#include <memory>
#include <utility>
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/test/event_generator.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/test/ink_drop_host_test_api.h"
#include "ui/views/animation/test/test_ink_drop.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/menu_button_controller.h"
#include "ui/views/drag_controller.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget_utils.h"
#if defined(USE_AURA)
#include "ui/aura/client/drag_drop_client.h"
#include "ui/aura/client/drag_drop_client_observer.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
#include "ui/events/event.h"
#include "ui/events/event_handler.h"
#endif
namespace views {
using ::base::ASCIIToUTF16;
using ::ui::mojom::DragOperation;
class TestMenuButton : public MenuButton {
public:
TestMenuButton()
: TestMenuButton(base::BindRepeating(&TestMenuButton::ButtonPressed,
base::Unretained(this))) {}
explicit TestMenuButton(PressedCallback callback)
: MenuButton(std::move(callback), std::u16string(u"button")) {}
TestMenuButton(const TestMenuButton&) = delete;
TestMenuButton& operator=(const TestMenuButton&) = delete;
~TestMenuButton() override = default;
bool clicked() const { return clicked_; }
Button::ButtonState last_state() const { return last_state_; }
ui::EventType last_event_type() const { return last_event_type_; }
void Reset() {
clicked_ = false;
last_state_ = Button::STATE_NORMAL;
last_event_type_ = ui::EventType::kUnknown;
}
private:
void ButtonPressed(const ui::Event& event) {
clicked_ = true;
last_state_ = GetState();
last_event_type_ = event.type();
}
bool clicked_ = false;
Button::ButtonState last_state_ = Button::STATE_NORMAL;
ui::EventType last_event_type_ = ui::EventType::kUnknown;
};
class MenuButtonTest : public ViewsTestBase {
public:
MenuButtonTest() = default;
MenuButtonTest(const MenuButtonTest&) = delete;
MenuButtonTest& operator=(const MenuButtonTest&) = delete;
~MenuButtonTest() override = default;
void TearDown() override {
generator_.reset();
widget_.reset();
ViewsTestBase::TearDown();
}
protected:
Widget* widget() { return widget_.get(); }
TestMenuButton* button() {
return static_cast<TestMenuButton*>(widget()->GetContentsView());
}
ui::test::EventGenerator* generator() { return generator_.get(); }
test::TestInkDrop* ink_drop() {
return static_cast<test::TestInkDrop*>(
test::InkDropHostTestApi(InkDrop::Get(button())).ink_drop());
}
gfx::Point GetOutOfButtonLocation() {
return gfx::Point(button()->x() - 1, button()->y() - 1);
}
void ConfigureMenuButton(std::unique_ptr<TestMenuButton> test_button) {
CHECK(!widget_);
widget_ = std::make_unique<Widget>();
Widget::InitParams params =
CreateParams(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.bounds = gfx::Rect(0, 0, 200, 200);
widget_->Init(std::move(params));
widget_->Show();
generator_ =
std::make_unique<ui::test::EventGenerator>(GetRootWindow(widget()));
// Set initial mouse location in a consistent way so that the menu button we
// are about to create initializes its hover state in a consistent manner.
generator_->set_current_screen_location(gfx::Point(10, 10));
widget_->SetContentsView(std::move(test_button));
button()->SetBoundsRect(gfx::Rect(0, 0, 200, 20));
auto ink_drop = std::make_unique<test::TestInkDrop>();
test::InkDropHostTestApi(InkDrop::Get(button()))
.SetInkDrop(std::move(ink_drop));
widget_->Show();
}
private:
std::unique_ptr<Widget> widget_;
std::unique_ptr<ui::test::EventGenerator> generator_;
};
// A Button that will acquire a PressedLock in the pressed callback and
// optionally release it as well.
class PressStateButton : public TestMenuButton {
public:
explicit PressStateButton(bool release_lock)
: TestMenuButton(base::BindRepeating(&PressStateButton::ButtonPressed,
base::Unretained(this))),
release_lock_(release_lock) {}
PressStateButton(const PressStateButton&) = delete;
PressStateButton& operator=(const PressStateButton&) = delete;
~PressStateButton() override = default;
void ReleasePressedLock() { pressed_lock_.reset(); }
private:
void ButtonPressed() {
pressed_lock_ = button_controller()->TakeLock();
if (release_lock_) {
ReleasePressedLock();
}
}
bool release_lock_;
std::unique_ptr<MenuButtonController::PressedLock> pressed_lock_;
};
// Basic implementation of a DragController, to test input behaviour for
// MenuButtons that can be dragged.
class TestDragController : public DragController {
public:
TestDragController() = default;
TestDragController(const TestDragController&) = delete;
TestDragController& operator=(const TestDragController&) = delete;
~TestDragController() override = default;
void WriteDragDataForView(View* sender,
const gfx::Point& press_pt,
ui::OSExchangeData* data) override {}
int GetDragOperationsForView(View* sender, const gfx::Point& p) override {
return ui::DragDropTypes::DRAG_MOVE;
}
bool CanStartDragForView(View* sender,
const gfx::Point& press_pt,
const gfx::Point& p) override {
return true;
}
};
#if defined(USE_AURA)
// Basic implementation of a DragDropClient, tracking the state of the drag
// operation. While dragging addition mouse events are consumed, preventing the
// target view from receiving them.
class TestDragDropClient : public aura::client::DragDropClient,
public ui::EventHandler {
public:
TestDragDropClient();
TestDragDropClient(const TestDragDropClient&) = delete;
TestDragDropClient& operator=(const TestDragDropClient&) = delete;
~TestDragDropClient() override;
// aura::client::DragDropClient:
DragOperation StartDragAndDrop(std::unique_ptr<ui::OSExchangeData> data,
aura::Window* root_window,
aura::Window* source_window,
const gfx::Point& screen_location,
int allowed_operations,
ui::mojom::DragEventSource source) override;
#if BUILDFLAG(IS_LINUX)
void UpdateDragImage(const gfx::ImageSkia& image,
const gfx::Vector2d& offset) override {}
#endif
void DragCancel() override;
bool IsDragDropInProgress() override;
void AddObserver(aura::client::DragDropClientObserver* observer) override {}
void RemoveObserver(aura::client::DragDropClientObserver* observer) override {
}
// ui::EventHandler:
void OnMouseEvent(ui::MouseEvent* event) override;
private:
// True while receiving ui::LocatedEvents for drag operations.
bool drag_in_progress_ = false;
// Target window where drag operations are occurring.
raw_ptr<aura::Window> target_ = nullptr;
};
TestDragDropClient::TestDragDropClient() = default;
TestDragDropClient::~TestDragDropClient() = default;
DragOperation TestDragDropClient::StartDragAndDrop(
std::unique_ptr<ui::OSExchangeData> data,
aura::Window* root_window,
aura::Window* source_window,
const gfx::Point& screen_location,
int allowed_operations,
ui::mojom::DragEventSource source) {
if (IsDragDropInProgress()) {
return DragOperation::kNone;
}
drag_in_progress_ = true;
target_ = root_window;
return ui::PreferredDragOperation(allowed_operations);
}
void TestDragDropClient::DragCancel() {
drag_in_progress_ = false;
}
bool TestDragDropClient::IsDragDropInProgress() {
return drag_in_progress_;
}
void TestDragDropClient::OnMouseEvent(ui::MouseEvent* event) {
if (!IsDragDropInProgress()) {
return;
}
switch (event->type()) {
case ui::EventType::kMouseDragged:
event->StopPropagation();
break;
case ui::EventType::kMouseReleased:
drag_in_progress_ = false;
event->StopPropagation();
break;
default:
break;
}
}
#endif // defined(USE_AURA)
// Tests if the callback is called correctly when a mouse click happens on a
// MenuButton.
TEST_F(MenuButtonTest, ActivateDropDownOnMouseClick) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
generator()->MoveMouseTo(button()->GetBoundsInScreen().CenterPoint());
generator()->ClickLeftButton();
EXPECT_TRUE(button()->clicked());
EXPECT_EQ(Button::STATE_HOVERED, button()->last_state());
}
TEST_F(MenuButtonTest, ActivateOnKeyPress) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
EXPECT_FALSE(button()->clicked());
button()->OnKeyPressed(ui::KeyEvent(ui::EventType::kKeyPressed,
ui::KeyboardCode::VKEY_SPACE,
ui::DomCode::SPACE, 0));
EXPECT_TRUE(button()->clicked());
button()->Reset();
EXPECT_FALSE(button()->clicked());
button()->OnKeyPressed(ui::KeyEvent(ui::EventType::kKeyPressed,
ui::KeyboardCode::VKEY_RETURN,
ui::DomCode::ENTER, 0));
EXPECT_EQ(PlatformStyle::kReturnClicksFocusedControl, button()->clicked());
}
// Tests that the ink drop center point is set from the mouse click point.
TEST_F(MenuButtonTest, InkDropCenterSetFromClick) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
const gfx::Point click_point = button()->GetBoundsInScreen().CenterPoint();
generator()->MoveMouseTo(click_point);
generator()->ClickLeftButton();
EXPECT_TRUE(button()->clicked());
gfx::Point inkdrop_center_point =
InkDrop::Get(button())->GetInkDropCenterBasedOnLastEvent();
View::ConvertPointToScreen(button(), &inkdrop_center_point);
EXPECT_EQ(click_point, inkdrop_center_point);
}
// Tests that the ink drop center point is set from the PressedLock constructor.
// TODO(crbug.com/40903656): Test flaky on MSAN ChromeOS builders.
#if BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
#define MAYBE_InkDropCenterSetFromClickWithPressedLock \
DISABLED_InkDropCenterSetFromClickWithPressedLock
#else
#define MAYBE_InkDropCenterSetFromClickWithPressedLock \
InkDropCenterSetFromClickWithPressedLock
#endif // BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
TEST_F(MenuButtonTest, MAYBE_InkDropCenterSetFromClickWithPressedLock) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
gfx::Point click_point(11, 7);
ui::MouseEvent click_event(ui::EventType::kMousePressed, click_point,
click_point, base::TimeTicks(), 0, 0);
MenuButtonController::PressedLock pressed_lock(button()->button_controller(),
false, &click_event);
EXPECT_EQ(Button::STATE_PRESSED, button()->GetState());
EXPECT_EQ(click_point,
InkDrop::Get(button())->GetInkDropCenterBasedOnLastEvent());
}
// Test that the MenuButton stays pressed while there are any PressedLocks.
// TODO(crbug.com/40903656): Test flaky on MSAN ChromeOS builders.
#if BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
#define MAYBE_ButtonStateForMenuButtonsWithPressedLocks \
DISABLED_ButtonStateForMenuButtonsWithPressedLocks
#else
#define MAYBE_ButtonStateForMenuButtonsWithPressedLocks \
ButtonStateForMenuButtonsWithPressedLocks
#endif // BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
TEST_F(MenuButtonTest, MAYBE_ButtonStateForMenuButtonsWithPressedLocks) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
const gfx::Rect button_bounds = button()->GetBoundsInScreen();
// Move the mouse over the button; the button should be in a hovered state.
generator()->MoveMouseTo(button_bounds.CenterPoint());
EXPECT_EQ(Button::STATE_HOVERED, button()->GetState());
// Introduce a PressedLock, which should make the button pressed.
auto pressed_lock1 = std::make_unique<MenuButtonController::PressedLock>(
button()->button_controller());
EXPECT_EQ(Button::STATE_PRESSED, button()->GetState());
// Even if we move the mouse outside of the button, it should remain pressed.
generator()->MoveMouseTo(button_bounds.bottom_right() + gfx::Vector2d(1, 1));
EXPECT_EQ(Button::STATE_PRESSED, button()->GetState());
// Creating a new lock should obviously keep the button pressed.
auto pressed_lock2 = std::make_unique<MenuButtonController::PressedLock>(
button()->button_controller());
EXPECT_EQ(Button::STATE_PRESSED, button()->GetState());
// The button should remain pressed while any locks are active.
pressed_lock1.reset();
EXPECT_EQ(Button::STATE_PRESSED, button()->GetState());
// Resetting the final lock should return the button's state to normal...
pressed_lock2.reset();
EXPECT_EQ(Button::STATE_NORMAL, button()->GetState());
// ...And it should respond to mouse movement again.
generator()->MoveMouseTo(button_bounds.CenterPoint());
EXPECT_EQ(Button::STATE_HOVERED, button()->GetState());
// Test that the button returns to the appropriate state after the press; if
// the mouse ends over the button, the button should be hovered.
pressed_lock1 = button()->button_controller()->TakeLock();
EXPECT_EQ(Button::STATE_PRESSED, button()->GetState());
pressed_lock1.reset();
EXPECT_EQ(Button::STATE_HOVERED, button()->GetState());
// If the button is disabled before the pressed lock, it should be disabled
// after the pressed lock.
button()->SetState(Button::STATE_DISABLED);
pressed_lock1 = button()->button_controller()->TakeLock();
EXPECT_EQ(Button::STATE_PRESSED, button()->GetState());
pressed_lock1.reset();
EXPECT_EQ(Button::STATE_DISABLED, button()->GetState());
generator()->MoveMouseTo(button_bounds.bottom_right() + gfx::Vector2d(1, 1));
// Edge case: the button is disabled, a pressed lock is added, and then the
// button is re-enabled. It should be enabled after the lock is removed.
pressed_lock1 = button()->button_controller()->TakeLock();
EXPECT_EQ(Button::STATE_PRESSED, button()->GetState());
button()->SetState(Button::STATE_NORMAL);
pressed_lock1.reset();
EXPECT_EQ(Button::STATE_NORMAL, button()->GetState());
}
// Test that the MenuButton does not become pressed if it can be dragged, until
// a release occurs.
// TODO(crbug.com/40903656): Test flaky on MSAN ChromeOS builders.
#if BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
#define MAYBE_DraggableMenuButtonActivatesOnRelease \
DISABLED_DraggableMenuButtonActivatesOnRelease
#else
#define MAYBE_DraggableMenuButtonActivatesOnRelease \
DraggableMenuButtonActivatesOnRelease
#endif // BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
TEST_F(MenuButtonTest, MAYBE_DraggableMenuButtonActivatesOnRelease) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
TestDragController drag_controller;
button()->set_drag_controller(&drag_controller);
generator()->MoveMouseTo(button()->GetBoundsInScreen().CenterPoint());
generator()->PressLeftButton();
EXPECT_FALSE(button()->clicked());
generator()->ReleaseLeftButton();
EXPECT_TRUE(button()->clicked());
EXPECT_EQ(Button::STATE_HOVERED, button()->last_state());
}
// TODO(crbug.com/40903656): Test flaky on MSAN ChromeOS builders.
#if BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
#define MAYBE_InkDropStateForMenuButtonActivationsWithoutCallback \
DISABLED_InkDropStateForMenuButtonActivationsWithoutCallback
#else
#define MAYBE_InkDropStateForMenuButtonActivationsWithoutCallback \
InkDropStateForMenuButtonActivationsWithoutCallback
#endif // BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
TEST_F(MenuButtonTest,
MAYBE_InkDropStateForMenuButtonActivationsWithoutCallback) {
ConfigureMenuButton(
std::make_unique<TestMenuButton>(Button::PressedCallback()));
ink_drop()->AnimateToState(InkDropState::ACTION_PENDING);
button()->Activate(nullptr);
EXPECT_EQ(InkDropState::HIDDEN, ink_drop()->GetTargetInkDropState());
}
// TODO(crbug.com/40903656): Test flaky on MSAN ChromeOS builders.
#if BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
#define MAYBE_InkDropStateForMenuButtonActivationsWithCallbackThatDoesntAcquireALock \
DISABLED_InkDropStateForMenuButtonActivationsWithCallbackThatDoesntAcquireALock
#else
#define MAYBE_InkDropStateForMenuButtonActivationsWithCallbackThatDoesntAcquireALock \
InkDropStateForMenuButtonActivationsWithCallbackThatDoesntAcquireALock
#endif // BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
TEST_F(
MenuButtonTest,
MAYBE_InkDropStateForMenuButtonActivationsWithCallbackThatDoesntAcquireALock) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
button()->Activate(nullptr);
EXPECT_EQ(InkDropState::ACTION_TRIGGERED,
ink_drop()->GetTargetInkDropState());
}
TEST_F(
MenuButtonTest,
InkDropStateForMenuButtonActivationsWithCallbackThatDoesntReleaseAllLocks) {
ConfigureMenuButton(std::make_unique<PressStateButton>(false));
button()->Activate(nullptr);
EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState());
}
TEST_F(MenuButtonTest,
InkDropStateForMenuButtonActivationsWithCallbackThatReleasesAllLocks) {
ConfigureMenuButton(std::make_unique<PressStateButton>(true));
button()->Activate(nullptr);
EXPECT_EQ(InkDropState::DEACTIVATED, ink_drop()->GetTargetInkDropState());
}
// TODO(crbug.com/40903656): Test flaky on MSAN ChromeOS builders.
#if BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
#define MAYBE_InkDropStateForMenuButtonsWithPressedLocks \
DISABLED_InkDropStateForMenuButtonsWithPressedLocks
#else
#define MAYBE_InkDropStateForMenuButtonsWithPressedLocks \
InkDropStateForMenuButtonsWithPressedLocks
#endif // BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
TEST_F(MenuButtonTest, MAYBE_InkDropStateForMenuButtonsWithPressedLocks) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
auto pressed_lock1 = std::make_unique<MenuButtonController::PressedLock>(
button()->button_controller());
EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState());
auto pressed_lock2 = std::make_unique<MenuButtonController::PressedLock>(
button()->button_controller());
EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState());
pressed_lock1.reset();
EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState());
pressed_lock2.reset();
EXPECT_EQ(InkDropState::DEACTIVATED, ink_drop()->GetTargetInkDropState());
}
// Verifies only one ink drop animation is triggered when multiple PressedLocks
// are attached to a MenuButton.
#if BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
#define MAYBE_OneInkDropAnimationForReentrantPressedLocks \
DISABLED_OneInkDropAnimationForReentrantPressedLocks
#else
#define MAYBE_OneInkDropAnimationForReentrantPressedLocks \
OneInkDropAnimationForReentrantPressedLocks
#endif // BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
TEST_F(MenuButtonTest, MAYBE_OneInkDropAnimationForReentrantPressedLocks) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
auto pressed_lock1 = std::make_unique<MenuButtonController::PressedLock>(
button()->button_controller());
EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState());
ink_drop()->AnimateToState(InkDropState::ACTION_PENDING);
auto pressed_lock2 = std::make_unique<MenuButtonController::PressedLock>(
button()->button_controller());
EXPECT_EQ(InkDropState::ACTION_PENDING, ink_drop()->GetTargetInkDropState());
}
// Verifies the InkDropState is left as ACTIVATED if a PressedLock is active
// before another Activation occurs.
// TODO(crbug.com/40903656): Test flaky on MSAN ChromeOS builders.
#if BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
#define MAYBE_InkDropStateForMenuButtonWithPressedLockBeforeActivation \
DISABLED_InkDropStateForMenuButtonWithPressedLockBeforeActivation
#else
#define MAYBE_InkDropStateForMenuButtonWithPressedLockBeforeActivation \
InkDropStateForMenuButtonWithPressedLockBeforeActivation
#endif // BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
TEST_F(MenuButtonTest,
MAYBE_InkDropStateForMenuButtonWithPressedLockBeforeActivation) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
MenuButtonController::PressedLock lock(button()->button_controller());
button()->Activate(nullptr);
EXPECT_EQ(InkDropState::ACTIVATED, ink_drop()->GetTargetInkDropState());
}
#if defined(USE_AURA)
// Tests that the MenuButton does not become pressed if it can be dragged, and a
// DragDropClient is processing the events.
// TODO(crbug.com/40903656): Test flaky on MSAN ChromeOS builders.
#if BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
#define MAYBE_DraggableMenuButtonDoesNotActivateOnDrag \
DISABLED_DraggableMenuButtonDoesNotActivateOnDrag
#else
#define MAYBE_DraggableMenuButtonDoesNotActivateOnDrag \
DraggableMenuButtonDoesNotActivateOnDrag
#endif // BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
TEST_F(MenuButtonTest, MAYBE_DraggableMenuButtonDoesNotActivateOnDrag) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
TestDragController drag_controller;
button()->set_drag_controller(&drag_controller);
TestDragDropClient drag_client;
SetDragDropClient(GetContext(), &drag_client);
button()->AddPreTargetHandler(&drag_client,
ui::EventTarget::Priority::kSystem);
generator()->DragMouseBy(10, 0);
EXPECT_FALSE(button()->clicked());
EXPECT_EQ(Button::STATE_NORMAL, button()->last_state());
button()->RemovePreTargetHandler(&drag_client);
}
#endif // USE_AURA
// No touch on desktop Mac. Tracked in http://crbug.com/445520.
#if !BUILDFLAG(IS_MAC) || defined(USE_AURA)
// Tests if the callback is notified correctly when a gesture tap happens on a
// MenuButton that has a callback.
TEST_F(MenuButtonTest, ActivateDropDownOnGestureTap) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
// 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 callback, while it was in pressed
// state.
EXPECT_TRUE(button()->clicked());
EXPECT_EQ(Button::STATE_HOVERED, button()->last_state());
// The button should go back to its normal state since the gesture ended.
EXPECT_EQ(Button::STATE_NORMAL, button()->GetState());
}
// Tests that the button enters a hovered state upon a tap down, before becoming
// pressed at activation.
TEST_F(MenuButtonTest, TouchFeedbackDuringTap) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
generator()->PressTouch();
EXPECT_EQ(Button::STATE_HOVERED, button()->GetState());
generator()->ReleaseTouch();
EXPECT_EQ(Button::STATE_HOVERED, button()->last_state());
}
// Tests that a move event that exits the button returns it to the normal state,
// and that the button did not activate the callback.
TEST_F(MenuButtonTest, TouchFeedbackDuringTapCancel) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
generator()->PressTouch();
EXPECT_EQ(Button::STATE_HOVERED, button()->GetState());
generator()->MoveTouch(gfx::Point(10, 30));
generator()->ReleaseTouch();
EXPECT_EQ(Button::STATE_NORMAL, button()->GetState());
EXPECT_FALSE(button()->clicked());
}
#endif // !BUILDFLAG(IS_MAC) || defined(USE_AURA)
TEST_F(MenuButtonTest, InkDropHoverWhenShowingMenu) {
ConfigureMenuButton(std::make_unique<PressStateButton>(false));
generator()->MoveMouseTo(GetOutOfButtonLocation());
EXPECT_FALSE(ink_drop()->is_hovered());
generator()->MoveMouseTo(button()->GetBoundsInScreen().CenterPoint());
EXPECT_TRUE(ink_drop()->is_hovered());
generator()->PressLeftButton();
EXPECT_FALSE(ink_drop()->is_hovered());
}
TEST_F(MenuButtonTest, InkDropIsHoveredAfterDismissingMenuWhenMouseOverButton) {
auto press_state_button = std::make_unique<PressStateButton>(false);
auto* test_button = press_state_button.get();
ConfigureMenuButton(std::move(press_state_button));
generator()->MoveMouseTo(button()->GetBoundsInScreen().CenterPoint());
generator()->PressLeftButton();
EXPECT_FALSE(ink_drop()->is_hovered());
test_button->ReleasePressedLock();
EXPECT_TRUE(ink_drop()->is_hovered());
}
TEST_F(MenuButtonTest,
InkDropIsntHoveredAfterDismissingMenuWhenMouseOutsideButton) {
auto press_state_button = std::make_unique<PressStateButton>(false);
auto* test_button = press_state_button.get();
ConfigureMenuButton(std::move(press_state_button));
generator()->MoveMouseTo(button()->GetBoundsInScreen().CenterPoint());
generator()->PressLeftButton();
generator()->MoveMouseTo(GetOutOfButtonLocation());
test_button->ReleasePressedLock();
EXPECT_FALSE(ink_drop()->is_hovered());
}
// This test ensures there isn't a UAF in MenuButton::OnGestureEvent() if the
// button callback deletes the MenuButton.
TEST_F(MenuButtonTest, DestroyButtonInGesture) {
std::unique_ptr<TestMenuButton> test_menu_button =
std::make_unique<TestMenuButton>(base::BindRepeating(
[](std::unique_ptr<TestMenuButton>* button) { button->reset(); },
&test_menu_button));
ConfigureMenuButton(std::move(test_menu_button));
ui::GestureEvent gesture_event(
0, 0, 0, base::TimeTicks::Now(),
ui::GestureEventDetails(ui::EventType::kGestureTap));
button()->OnGestureEvent(&gesture_event);
}
TEST_F(MenuButtonTest, AccessibleDefaultActionVerb) {
ConfigureMenuButton(std::make_unique<TestMenuButton>());
ui::AXNodeData data;
button()->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(data.GetDefaultActionVerb(), ax::mojom::DefaultActionVerb::kOpen);
data = ui::AXNodeData();
button()->SetEnabled(false);
button()->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_FALSE(
data.HasIntAttribute(ax::mojom::IntAttribute::kDefaultActionVerb));
data = ui::AXNodeData();
button()->SetEnabled(true);
button()->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(data.GetDefaultActionVerb(), ax::mojom::DefaultActionVerb::kOpen);
}
} // namespace views
|