Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/menu_button.h" | 5 #include "ui/views/controls/button/menu_button.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "ui/base/dragdrop/drag_drop_types.h" | 11 #include "ui/base/dragdrop/drag_drop_types.h" |
| 12 #include "ui/events/test/event_generator.h" | 12 #include "ui/events/test/event_generator.h" |
| 13 #include "ui/views/controls/button/menu_button_listener.h" | 13 #include "ui/views/controls/button/menu_button_listener.h" |
| 14 #include "ui/views/drag_controller.h" | 14 #include "ui/views/drag_controller.h" |
| 15 #include "ui/views/test/views_test_base.h" | 15 #include "ui/views/test/views_test_base.h" |
| 16 | 16 |
| 17 #if defined(USE_AURA) | 17 #if defined(USE_AURA) |
| 18 #include "ui/events/event.h" | 18 #include "ui/events/event.h" |
| 19 #include "ui/events/event_handler.h" | 19 #include "ui/events/event_handler.h" |
| 20 #include "ui/wm/public/drag_drop_client.h" | 20 #include "ui/wm/public/drag_drop_client.h" |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 using base::ASCIIToUTF16; | 23 using base::ASCIIToUTF16; |
| 24 | 24 |
| 25 namespace views { | 25 namespace views { |
| 26 | 26 |
| 27 class MenuButtonTest : public ViewsTestBase { | 27 class MenuButtonTest : public ViewsTestBase { |
| 28 public: | 28 public: |
| 29 MenuButtonTest() : widget_(nullptr), button_(nullptr) {} | 29 MenuButtonTest() : generator(nullptr), widget_(nullptr), button_(nullptr) {} |
|
tapted
2016/02/09 05:06:41
nit: doesn't need an initializer now that it's a s
Patti Lor
2016/02/12 04:42:20
Done.
| |
| 30 ~MenuButtonTest() override {} | 30 ~MenuButtonTest() override {} |
| 31 | 31 |
| 32 void TearDown() override { | 32 void TearDown() override { |
| 33 if (widget_ && !widget_->IsClosed()) | 33 if (widget_ && !widget_->IsClosed()) |
|
tapted
2016/02/09 05:06:41
nit: although it's not crashing.... since the gene
Patti Lor
2016/02/12 04:42:20
Done.
| |
| 34 widget_->Close(); | 34 widget_->Close(); |
| 35 | 35 |
| 36 ViewsTestBase::TearDown(); | 36 ViewsTestBase::TearDown(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 Widget* widget() { return widget_; } | 39 Widget* widget() { return widget_; } |
| 40 MenuButton* button() { return button_; } | 40 MenuButton* button() { return button_; } |
| 41 | 41 |
| 42 protected: | 42 protected: |
| 43 // Creates a MenuButton with no button listener. | 43 // Creates a MenuButton with no button listener. |
| 44 void CreateMenuButtonWithNoListener() { CreateMenuButton(nullptr, nullptr); } | 44 void CreateMenuButtonWithNoListener() { CreateMenuButton(nullptr, nullptr); } |
| 45 | 45 |
| 46 // Creates a MenuButton with a ButtonListener. In this case, the MenuButton | 46 // Creates a MenuButton with a ButtonListener. In this case, the MenuButton |
| 47 // acts like a regular button. | 47 // acts like a regular button. |
| 48 void CreateMenuButtonWithButtonListener(ButtonListener* button_listener) { | 48 void CreateMenuButtonWithButtonListener(ButtonListener* button_listener) { |
| 49 CreateMenuButton(button_listener, nullptr); | 49 CreateMenuButton(button_listener, nullptr); |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Creates a MenuButton with a MenuButtonListener. In this case, when the | 52 // Creates a MenuButton with a MenuButtonListener. In this case, when the |
| 53 // MenuButton is pushed, it notifies the MenuButtonListener to open a | 53 // MenuButton is pushed, it notifies the MenuButtonListener to open a |
| 54 // drop-down menu. | 54 // drop-down menu. |
| 55 void CreateMenuButtonWithMenuButtonListener( | 55 void CreateMenuButtonWithMenuButtonListener( |
| 56 MenuButtonListener* menu_button_listener) { | 56 MenuButtonListener* menu_button_listener) { |
| 57 CreateMenuButton(nullptr, menu_button_listener); | 57 CreateMenuButton(nullptr, menu_button_listener); |
| 58 } | 58 } |
| 59 | 59 |
| 60 scoped_ptr<ui::test::EventGenerator> generator; | |
|
tapted
2016/02/09 05:06:41
generator -> generator_
also, this should go back
Patti Lor
2016/02/12 04:42:20
Having the generator scoped_ptr be private means I
tapted
2016/02/12 04:44:54
returning generator_.get() is fine for this kind o
| |
| 61 | |
| 60 private: | 62 private: |
| 61 void CreateMenuButton(ButtonListener* button_listener, | 63 void CreateMenuButton(ButtonListener* button_listener, |
| 62 MenuButtonListener* menu_button_listener) { | 64 MenuButtonListener* menu_button_listener) { |
| 63 CreateWidget(); | 65 CreateWidget(); |
| 66 generator = make_scoped_ptr(new ui::test::EventGenerator(GetContext(), | |
|
tapted
2016/02/09 05:06:41
nit: make_scoped_ptr is used mostly after a `retur
Patti Lor
2016/02/12 04:42:20
Done.
| |
| 67 widget_->GetNativeWindow())); | |
| 64 | 68 |
| 65 const base::string16 label(ASCIIToUTF16("button")); | 69 const base::string16 label(ASCIIToUTF16("button")); |
| 66 button_ = | 70 button_ = |
| 67 new MenuButton(button_listener, label, menu_button_listener, false); | 71 new MenuButton(button_listener, label, menu_button_listener, false); |
| 68 button_->SetBoundsRect(gfx::Rect(0, 0, 200, 20)); | 72 button_->SetBoundsRect(gfx::Rect(0, 0, 200, 20)); |
| 69 widget_->SetContentsView(button_); | 73 widget_->SetContentsView(button_); |
| 70 | 74 |
| 71 widget_->Show(); | 75 widget_->Show(); |
| 72 } | 76 } |
| 73 | 77 |
| 74 void CreateWidget() { | 78 void CreateWidget() { |
| 75 DCHECK(!widget_); | 79 DCHECK(!widget_); |
| 76 | 80 |
| 77 widget_ = new Widget; | 81 widget_ = new Widget; |
| 78 Widget::InitParams params = | 82 Widget::InitParams params = |
| 79 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 83 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 80 params.bounds = gfx::Rect(0, 0, 200, 200); | 84 params.bounds = gfx::Rect(0, 0, 200, 200); |
| 81 widget_->Init(params); | 85 widget_->Init(params); |
| 82 } | 86 } |
| 83 | 87 |
| 84 Widget* widget_; | 88 Widget* widget_; |
| 85 MenuButton* button_; | 89 MenuButton* button_; |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(MenuButtonTest); | |
| 86 }; | 92 }; |
| 87 | 93 |
| 88 class TestButtonListener : public ButtonListener { | 94 class TestButtonListener : public ButtonListener { |
| 89 public: | 95 public: |
| 90 TestButtonListener() | 96 TestButtonListener() |
| 91 : last_sender_(nullptr), | 97 : last_sender_(nullptr), |
| 92 last_sender_state_(Button::STATE_NORMAL), | 98 last_sender_state_(Button::STATE_NORMAL), |
| 93 last_event_type_(ui::ET_UNKNOWN) {} | 99 last_event_type_(ui::ET_UNKNOWN) {} |
| 94 ~TestButtonListener() override {} | 100 ~TestButtonListener() override {} |
| 95 | 101 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 private: | 273 private: |
| 268 DISALLOW_COPY_AND_ASSIGN(TestShowSiblingButtonListener); | 274 DISALLOW_COPY_AND_ASSIGN(TestShowSiblingButtonListener); |
| 269 }; | 275 }; |
| 270 | 276 |
| 271 // Tests if the listener is notified correctly, when a mouse click happens on a | 277 // Tests if the listener is notified correctly, when a mouse click happens on a |
| 272 // MenuButton that has a regular ButtonListener. | 278 // MenuButton that has a regular ButtonListener. |
| 273 TEST_F(MenuButtonTest, ActivateNonDropDownOnMouseClick) { | 279 TEST_F(MenuButtonTest, ActivateNonDropDownOnMouseClick) { |
| 274 TestButtonListener button_listener; | 280 TestButtonListener button_listener; |
| 275 CreateMenuButtonWithButtonListener(&button_listener); | 281 CreateMenuButtonWithButtonListener(&button_listener); |
| 276 | 282 |
| 277 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 283 generator->set_current_location(gfx::Point(10, 10)); |
| 278 | 284 generator->ClickLeftButton(); |
| 279 generator.set_current_location(gfx::Point(10, 10)); | |
| 280 generator.ClickLeftButton(); | |
| 281 | 285 |
| 282 // Check that MenuButton has notified the listener on mouse-released event, | 286 // Check that MenuButton has notified the listener on mouse-released event, |
| 283 // while it was in hovered state. | 287 // while it was in hovered state. |
| 284 EXPECT_EQ(button(), button_listener.last_sender()); | 288 EXPECT_EQ(button(), button_listener.last_sender()); |
| 285 EXPECT_EQ(ui::ET_MOUSE_RELEASED, button_listener.last_event_type()); | 289 EXPECT_EQ(ui::ET_MOUSE_RELEASED, button_listener.last_event_type()); |
| 286 EXPECT_EQ(Button::STATE_HOVERED, button_listener.last_sender_state()); | 290 EXPECT_EQ(Button::STATE_HOVERED, button_listener.last_sender_state()); |
| 287 } | 291 } |
| 288 | 292 |
| 289 // Tests if the listener is notified correctly when a mouse click happens on a | 293 // Tests if the listener is notified correctly when a mouse click happens on a |
| 290 // MenuButton that has a MenuButtonListener. | 294 // MenuButton that has a MenuButtonListener. |
| 291 TEST_F(MenuButtonTest, ActivateDropDownOnMouseClick) { | 295 TEST_F(MenuButtonTest, ActivateDropDownOnMouseClick) { |
| 292 TestMenuButtonListener menu_button_listener; | 296 TestMenuButtonListener menu_button_listener; |
| 293 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 297 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
| 294 | 298 |
| 295 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 299 generator->set_current_location(gfx::Point(10, 10)); |
| 296 | 300 generator->ClickLeftButton(); |
| 297 generator.set_current_location(gfx::Point(10, 10)); | |
| 298 generator.ClickLeftButton(); | |
| 299 | 301 |
| 300 // Check that MenuButton has notified the listener, while it was in pressed | 302 // Check that MenuButton has notified the listener, while it was in pressed |
| 301 // state. | 303 // state. |
| 302 EXPECT_EQ(button(), menu_button_listener.last_source()); | 304 EXPECT_EQ(button(), menu_button_listener.last_source()); |
| 303 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); | 305 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); |
| 304 } | 306 } |
| 305 | 307 |
| 306 // Test that the MenuButton stays pressed while there are any PressedLocks. | 308 // Test that the MenuButton stays pressed while there are any PressedLocks. |
| 307 TEST_F(MenuButtonTest, MenuButtonPressedLock) { | 309 TEST_F(MenuButtonTest, MenuButtonPressedLock) { |
| 308 CreateMenuButtonWithNoListener(); | 310 CreateMenuButtonWithNoListener(); |
| 309 | 311 |
| 310 // Move the mouse over the button; the button should be in a hovered state. | 312 // Move the mouse over the button; the button should be in a hovered state. |
| 311 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 313 generator->MoveMouseTo(gfx::Point(10, 10)); |
| 312 generator.MoveMouseTo(gfx::Point(10, 10)); | |
| 313 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); | 314 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 314 | 315 |
| 315 // Introduce a PressedLock, which should make the button pressed. | 316 // Introduce a PressedLock, which should make the button pressed. |
| 316 scoped_ptr<MenuButton::PressedLock> pressed_lock1( | 317 scoped_ptr<MenuButton::PressedLock> pressed_lock1( |
| 317 new MenuButton::PressedLock(button())); | 318 new MenuButton::PressedLock(button())); |
| 318 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); | 319 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 319 | 320 |
| 320 // Even if we move the mouse outside of the button, it should remain pressed. | 321 // Even if we move the mouse outside of the button, it should remain pressed. |
| 321 generator.MoveMouseTo(gfx::Point(300, 10)); | 322 generator->MoveMouseTo(gfx::Point(300, 10)); |
| 322 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); | 323 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 323 | 324 |
| 324 // Creating a new lock should obviously keep the button pressed. | 325 // Creating a new lock should obviously keep the button pressed. |
| 325 scoped_ptr<MenuButton::PressedLock> pressed_lock2( | 326 scoped_ptr<MenuButton::PressedLock> pressed_lock2( |
| 326 new MenuButton::PressedLock(button())); | 327 new MenuButton::PressedLock(button())); |
| 327 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); | 328 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 328 | 329 |
| 329 // The button should remain pressed while any locks are active. | 330 // The button should remain pressed while any locks are active. |
| 330 pressed_lock1.reset(); | 331 pressed_lock1.reset(); |
| 331 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); | 332 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 332 | 333 |
| 333 // Reseting the final lock should return the button's state to normal... | 334 // Reseting the final lock should return the button's state to normal... |
| 334 pressed_lock2.reset(); | 335 pressed_lock2.reset(); |
| 335 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); | 336 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); |
| 336 | 337 |
| 337 // ...And it should respond to mouse movement again. | 338 // ...And it should respond to mouse movement again. |
| 338 generator.MoveMouseTo(gfx::Point(10, 10)); | 339 generator->MoveMouseTo(gfx::Point(10, 10)); |
| 339 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); | 340 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 340 | 341 |
| 341 // Test that the button returns to the appropriate state after the press; if | 342 // Test that the button returns to the appropriate state after the press; if |
| 342 // the mouse ends over the button, the button should be hovered. | 343 // the mouse ends over the button, the button should be hovered. |
| 343 pressed_lock1.reset(new MenuButton::PressedLock(button())); | 344 pressed_lock1.reset(new MenuButton::PressedLock(button())); |
| 344 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); | 345 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 345 pressed_lock1.reset(); | 346 pressed_lock1.reset(); |
| 346 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); | 347 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 347 | 348 |
| 348 // If the button is disabled before the pressed lock, it should be disabled | 349 // If the button is disabled before the pressed lock, it should be disabled |
| 349 // after the pressed lock. | 350 // after the pressed lock. |
| 350 button()->SetState(Button::STATE_DISABLED); | 351 button()->SetState(Button::STATE_DISABLED); |
| 351 pressed_lock1.reset(new MenuButton::PressedLock(button())); | 352 pressed_lock1.reset(new MenuButton::PressedLock(button())); |
| 352 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); | 353 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 353 pressed_lock1.reset(); | 354 pressed_lock1.reset(); |
| 354 EXPECT_EQ(Button::STATE_DISABLED, button()->state()); | 355 EXPECT_EQ(Button::STATE_DISABLED, button()->state()); |
| 355 | 356 |
| 356 generator.MoveMouseTo(gfx::Point(300, 10)); | 357 generator->MoveMouseTo(gfx::Point(300, 10)); |
| 357 | 358 |
| 358 // Edge case: the button is disabled, a pressed lock is added, and then the | 359 // Edge case: the button is disabled, a pressed lock is added, and then the |
| 359 // button is re-enabled. It should be enabled after the lock is removed. | 360 // button is re-enabled. It should be enabled after the lock is removed. |
| 360 pressed_lock1.reset(new MenuButton::PressedLock(button())); | 361 pressed_lock1.reset(new MenuButton::PressedLock(button())); |
| 361 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); | 362 EXPECT_EQ(Button::STATE_PRESSED, button()->state()); |
| 362 button()->SetState(Button::STATE_NORMAL); | 363 button()->SetState(Button::STATE_NORMAL); |
| 363 pressed_lock1.reset(); | 364 pressed_lock1.reset(); |
| 364 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); | 365 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); |
| 365 } | 366 } |
| 366 | 367 |
| 367 // Test that if a sibling menu is shown, the original menu button releases its | 368 // Test that if a sibling menu is shown, the original menu button releases its |
| 368 // PressedLock. | 369 // PressedLock. |
| 369 TEST_F(MenuButtonTest, PressedStateWithSiblingMenu) { | 370 TEST_F(MenuButtonTest, PressedStateWithSiblingMenu) { |
| 370 TestShowSiblingButtonListener listener; | 371 TestShowSiblingButtonListener listener; |
| 371 CreateMenuButtonWithMenuButtonListener(&listener); | 372 CreateMenuButtonWithMenuButtonListener(&listener); |
| 372 | 373 |
| 373 // Move the mouse over the button; the button should be in a hovered state. | 374 // Move the mouse over the button; the button should be in a hovered state. |
| 374 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 375 generator->MoveMouseTo(gfx::Point(10, 10)); |
| 375 generator.MoveMouseTo(gfx::Point(10, 10)); | |
| 376 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); | 376 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 377 generator.ClickLeftButton(); | 377 generator->ClickLeftButton(); |
| 378 // Test is continued in TestShowSiblingButtonListener::OnMenuButtonClicked(). | 378 // Test is continued in TestShowSiblingButtonListener::OnMenuButtonClicked(). |
| 379 } | 379 } |
| 380 | 380 |
| 381 // Test that the MenuButton does not become pressed if it can be dragged, until | 381 // Test that the MenuButton does not become pressed if it can be dragged, until |
| 382 // a release occurs. | 382 // a release occurs. |
| 383 TEST_F(MenuButtonTest, DraggableMenuButtonActivatesOnRelease) { | 383 TEST_F(MenuButtonTest, DraggableMenuButtonActivatesOnRelease) { |
| 384 TestMenuButtonListener menu_button_listener; | 384 TestMenuButtonListener menu_button_listener; |
| 385 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 385 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
| 386 TestDragController drag_controller; | 386 TestDragController drag_controller; |
| 387 button()->set_drag_controller(&drag_controller); | 387 button()->set_drag_controller(&drag_controller); |
| 388 | 388 |
| 389 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 389 generator->set_current_location(gfx::Point(10, 10)); |
| 390 | 390 generator->PressLeftButton(); |
| 391 generator.set_current_location(gfx::Point(10, 10)); | |
| 392 generator.PressLeftButton(); | |
| 393 EXPECT_EQ(nullptr, menu_button_listener.last_source()); | 391 EXPECT_EQ(nullptr, menu_button_listener.last_source()); |
| 394 | 392 |
| 395 generator.ReleaseLeftButton(); | 393 generator->ReleaseLeftButton(); |
| 396 EXPECT_EQ(button(), menu_button_listener.last_source()); | 394 EXPECT_EQ(button(), menu_button_listener.last_source()); |
| 397 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); | 395 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); |
| 398 } | 396 } |
| 399 | 397 |
| 400 #if defined(USE_AURA) | 398 #if defined(USE_AURA) |
| 401 | 399 |
| 402 // Tests that the MenuButton does not become pressed if it can be dragged, and a | 400 // Tests that the MenuButton does not become pressed if it can be dragged, and a |
| 403 // DragDropClient is processing the events. | 401 // DragDropClient is processing the events. |
| 404 TEST_F(MenuButtonTest, DraggableMenuButtonDoesNotActivateOnDrag) { | 402 TEST_F(MenuButtonTest, DraggableMenuButtonDoesNotActivateOnDrag) { |
| 405 TestMenuButtonListener menu_button_listener; | 403 TestMenuButtonListener menu_button_listener; |
| 406 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 404 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
| 407 TestDragController drag_controller; | 405 TestDragController drag_controller; |
| 408 button()->set_drag_controller(&drag_controller); | 406 button()->set_drag_controller(&drag_controller); |
| 409 | 407 |
| 410 TestDragDropClient drag_client; | 408 TestDragDropClient drag_client; |
| 411 SetDragDropClient(GetContext(), &drag_client); | 409 SetDragDropClient(GetContext(), &drag_client); |
| 412 button()->PrependPreTargetHandler(&drag_client); | 410 button()->PrependPreTargetHandler(&drag_client); |
| 413 | 411 |
| 414 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 412 generator->set_current_location(gfx::Point(10, 10)); |
| 415 generator.set_current_location(gfx::Point(10, 10)); | 413 generator->DragMouseBy(10, 0); |
| 416 generator.DragMouseBy(10, 0); | |
| 417 EXPECT_EQ(nullptr, menu_button_listener.last_source()); | 414 EXPECT_EQ(nullptr, menu_button_listener.last_source()); |
| 418 EXPECT_EQ(Button::STATE_NORMAL, menu_button_listener.last_source_state()); | 415 EXPECT_EQ(Button::STATE_NORMAL, menu_button_listener.last_source_state()); |
| 419 } | 416 } |
| 420 | 417 |
| 421 #endif // USE_AURA | 418 #endif // USE_AURA |
| 422 | 419 |
| 423 // No touch on desktop Mac. Tracked in http://crbug.com/445520. | 420 // No touch on desktop Mac. Tracked in http://crbug.com/445520. |
| 424 #if !defined(OS_MACOSX) || defined(USE_AURA) | 421 #if !defined(OS_MACOSX) || defined(USE_AURA) |
| 425 | 422 |
| 426 // Tests if the listener is notified correctly when a gesture tap happens on a | 423 // Tests if the listener is notified correctly when a gesture tap happens on a |
| 427 // MenuButton that has a regular ButtonListener. | 424 // MenuButton that has a regular ButtonListener. |
| 428 TEST_F(MenuButtonTest, ActivateNonDropDownOnGestureTap) { | 425 TEST_F(MenuButtonTest, ActivateNonDropDownOnGestureTap) { |
| 429 TestButtonListener button_listener; | 426 TestButtonListener button_listener; |
| 430 CreateMenuButtonWithButtonListener(&button_listener); | 427 CreateMenuButtonWithButtonListener(&button_listener); |
| 431 | 428 |
| 432 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | |
| 433 | |
| 434 // Move the mouse outside the menu button so that it doesn't impact the | 429 // Move the mouse outside the menu button so that it doesn't impact the |
| 435 // button state. | 430 // button state. |
| 436 generator.MoveMouseTo(400, 400); | 431 generator->MoveMouseTo(400, 400); |
| 437 EXPECT_FALSE(button()->IsMouseHovered()); | 432 EXPECT_FALSE(button()->IsMouseHovered()); |
| 438 | 433 |
| 439 generator.GestureTapAt(gfx::Point(10, 10)); | 434 generator->GestureTapAt(gfx::Point(10, 10)); |
| 440 | 435 |
| 441 // Check that MenuButton has notified the listener on gesture tap event, while | 436 // Check that MenuButton has notified the listener on gesture tap event, while |
| 442 // it was in hovered state. | 437 // it was in hovered state. |
| 443 EXPECT_EQ(button(), button_listener.last_sender()); | 438 EXPECT_EQ(button(), button_listener.last_sender()); |
| 444 EXPECT_EQ(ui::ET_GESTURE_TAP, button_listener.last_event_type()); | 439 EXPECT_EQ(ui::ET_GESTURE_TAP, button_listener.last_event_type()); |
| 445 EXPECT_EQ(Button::STATE_HOVERED, button_listener.last_sender_state()); | 440 EXPECT_EQ(Button::STATE_HOVERED, button_listener.last_sender_state()); |
| 446 | 441 |
| 447 // The button should go back to it's normal state since the gesture ended. | 442 // The button should go back to it's normal state since the gesture ended. |
| 448 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); | 443 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); |
| 449 } | 444 } |
| 450 | 445 |
| 451 // Tests if the listener is notified correctly when a gesture tap happens on a | 446 // Tests if the listener is notified correctly when a gesture tap happens on a |
| 452 // MenuButton that has a MenuButtonListener. | 447 // MenuButton that has a MenuButtonListener. |
| 453 TEST_F(MenuButtonTest, ActivateDropDownOnGestureTap) { | 448 TEST_F(MenuButtonTest, ActivateDropDownOnGestureTap) { |
| 454 TestMenuButtonListener menu_button_listener; | 449 TestMenuButtonListener menu_button_listener; |
| 455 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 450 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
| 456 | 451 |
| 457 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | |
| 458 | |
| 459 // Move the mouse outside the menu button so that it doesn't impact the | 452 // Move the mouse outside the menu button so that it doesn't impact the |
| 460 // button state. | 453 // button state. |
| 461 generator.MoveMouseTo(400, 400); | 454 generator->MoveMouseTo(400, 400); |
| 462 EXPECT_FALSE(button()->IsMouseHovered()); | 455 EXPECT_FALSE(button()->IsMouseHovered()); |
| 463 | 456 |
| 464 generator.GestureTapAt(gfx::Point(10, 10)); | 457 generator->GestureTapAt(gfx::Point(10, 10)); |
| 465 | 458 |
| 466 // Check that MenuButton has notified the listener, while it was in pressed | 459 // Check that MenuButton has notified the listener, while it was in pressed |
| 467 // state. | 460 // state. |
| 468 EXPECT_EQ(button(), menu_button_listener.last_source()); | 461 EXPECT_EQ(button(), menu_button_listener.last_source()); |
| 469 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); | 462 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); |
| 470 | 463 |
| 471 // The button should go back to it's normal state since the gesture ended. | 464 // The button should go back to it's normal state since the gesture ended. |
| 472 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); | 465 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); |
| 473 } | 466 } |
| 474 | 467 |
| 475 // Tests that the button enters a hovered state upon a tap down, before becoming | 468 // Tests that the button enters a hovered state upon a tap down, before becoming |
| 476 // pressed at activation. | 469 // pressed at activation. |
| 477 TEST_F(MenuButtonTest, TouchFeedbackDuringTap) { | 470 TEST_F(MenuButtonTest, TouchFeedbackDuringTap) { |
| 478 TestMenuButtonListener menu_button_listener; | 471 TestMenuButtonListener menu_button_listener; |
| 479 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 472 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
| 480 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 473 generator->set_current_location(gfx::Point(10, 10)); |
| 481 generator.set_current_location(gfx::Point(10, 10)); | 474 generator->PressTouch(); |
| 482 generator.PressTouch(); | |
| 483 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); | 475 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 484 | 476 |
| 485 generator.ReleaseTouch(); | 477 generator->ReleaseTouch(); |
| 486 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); | 478 EXPECT_EQ(Button::STATE_HOVERED, menu_button_listener.last_source_state()); |
| 487 } | 479 } |
| 488 | 480 |
| 489 // Tests that a move event that exits the button returns it to the normal state, | 481 // Tests that a move event that exits the button returns it to the normal state, |
| 490 // and that the button did not activate the listener. | 482 // and that the button did not activate the listener. |
| 491 TEST_F(MenuButtonTest, TouchFeedbackDuringTapCancel) { | 483 TEST_F(MenuButtonTest, TouchFeedbackDuringTapCancel) { |
| 492 TestMenuButtonListener menu_button_listener; | 484 TestMenuButtonListener menu_button_listener; |
| 493 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); | 485 CreateMenuButtonWithMenuButtonListener(&menu_button_listener); |
| 494 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 486 generator->set_current_location(gfx::Point(10, 10)); |
| 495 generator.set_current_location(gfx::Point(10, 10)); | 487 generator->PressTouch(); |
| 496 generator.PressTouch(); | |
| 497 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); | 488 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 498 | 489 |
| 499 generator.MoveTouch(gfx::Point(10, 30)); | 490 generator->MoveTouch(gfx::Point(10, 30)); |
| 500 generator.ReleaseTouch(); | 491 generator->ReleaseTouch(); |
| 501 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); | 492 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); |
| 502 EXPECT_EQ(nullptr, menu_button_listener.last_source()); | 493 EXPECT_EQ(nullptr, menu_button_listener.last_source()); |
| 503 } | 494 } |
| 504 | 495 |
| 505 #endif // !defined(OS_MACOSX) || defined(USE_AURA) | 496 #endif // !defined(OS_MACOSX) || defined(USE_AURA) |
| 506 | 497 |
| 507 } // namespace views | 498 } // namespace views |
| OLD | NEW |