Chromium Code Reviews| Index: ui/views/controls/button/menu_button_unittest.cc |
| diff --git a/ui/views/controls/button/menu_button_unittest.cc b/ui/views/controls/button/menu_button_unittest.cc |
| index 3b153ad1f85992a0db6dd263f0c65e3f836dd3f5..62fd408badaa9120ed65f31691105fb45eb2aa3e 100644 |
| --- a/ui/views/controls/button/menu_button_unittest.cc |
| +++ b/ui/views/controls/button/menu_button_unittest.cc |
| @@ -6,10 +6,18 @@ |
| #include "base/memory/scoped_ptr.h" |
| #include "base/strings/utf_string_conversions.h" |
| +#include "ui/base/dragdrop/drag_drop_types.h" |
| #include "ui/events/test/event_generator.h" |
| #include "ui/views/controls/button/menu_button_listener.h" |
| +#include "ui/views/drag_controller.h" |
| #include "ui/views/test/views_test_base.h" |
| +#if defined(USE_AURA) |
| +#include "ui/events/event.h" |
| +#include "ui/events/event_handler.h" |
| +#include "ui/wm/public/drag_drop_client.h" |
| +#endif |
| + |
| using base::ASCIIToUTF16; |
| namespace views { |
| @@ -107,7 +115,9 @@ class TestButtonListener : public ButtonListener { |
| class TestMenuButtonListener : public MenuButtonListener { |
| public: |
| - TestMenuButtonListener() {} |
| + TestMenuButtonListener() |
| + : last_source_(NULL), |
|
sky
2014/10/23 17:25:31
NULL->nullptr
jonross
2014/10/23 22:18:00
Done.
|
| + last_source_state_(Button::STATE_NORMAL) {} |
| virtual ~TestMenuButtonListener() {} |
| virtual void OnMenuButtonClicked(View* source, |
| @@ -126,6 +136,124 @@ class TestMenuButtonListener : public MenuButtonListener { |
| Button::ButtonState last_source_state_; |
| }; |
| +// Basic implementation of a DragController, to test input behaviour for |
| +// MenuButtons that can be dragged. |
| +class TestDragController : public DragController { |
| + public: |
| + TestDragController() {} |
| + virtual ~TestDragController() {} |
|
sky
2014/10/23 17:25:31
no virtual, just override.
jonross
2014/10/23 22:18:00
Done.
|
| + |
| + virtual void WriteDragDataForView(View* sender, |
|
sky
2014/10/23 17:25:31
No virtual, just override (ya, style changed).
jonross
2014/10/23 22:18:01
Done.
|
| + const gfx::Point& press_pt, |
| + ui::OSExchangeData* data) override {} |
| + |
| + virtual int GetDragOperationsForView(View* sender, |
| + const gfx::Point& p) override { |
| + return ui::DragDropTypes::DRAG_MOVE; |
| + } |
| + |
| + virtual bool CanStartDragForView(View* sender, |
| + const gfx::Point& press_pt, |
| + const gfx::Point& p) override { |
| + return true; |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestDragController); |
| +}; |
| + |
| +#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(); |
| + virtual ~TestDragDropClient(); |
| + |
| + // aura::client::DragDropClient: |
| + virtual int StartDragAndDrop(const ui::OSExchangeData& data, |
| + aura::Window* root_window, |
| + aura::Window* source_window, |
| + const gfx::Point& root_location, |
| + int operation, |
| + ui::DragDropTypes::DragEventSource source) override; |
| + virtual void DragUpdate(aura::Window* target, |
| + const ui::LocatedEvent& event) override; |
| + virtual void Drop(aura::Window* target, |
| + const ui::LocatedEvent& event) override; |
| + virtual void DragCancel() override; |
| + virtual bool IsDragDropInProgress() override; |
| + |
| + // ui::EventHandler: |
| + virtual void OnMouseEvent(ui::MouseEvent* event) override; |
| + |
| + private: |
| + // True while receiving ui::LocatedEvents for drag operations. |
| + bool drag_in_progress_; |
| + |
| + // Target window where drag operations are occuring. |
| + aura::Window* target_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestDragDropClient); |
| +}; |
| + |
| +TestDragDropClient::TestDragDropClient() : drag_in_progress_(false), |
|
sky
2014/10/23 17:25:31
run git cl format.
jonross
2014/10/23 22:18:00
Done.
|
| + target_(NULL) { |
|
sky
2014/10/23 17:25:31
nullptr
jonross
2014/10/23 22:18:01
Done.
|
| +} |
| + |
| +TestDragDropClient::~TestDragDropClient() { |
| +} |
| + |
| +int TestDragDropClient::StartDragAndDrop(const ui::OSExchangeData& data, |
| + aura::Window* root_window, |
| + aura::Window* source_window, |
| + const gfx::Point& root_location, |
| + int operation, |
| + ui::DragDropTypes::DragEventSource source) { |
| + if (IsDragDropInProgress()) |
| + return 0; |
|
sky
2014/10/23 17:25:31
DRAG_NONE
jonross
2014/10/23 22:18:01
Done.
|
| + drag_in_progress_ = true; |
| + target_ = root_window; |
| + return operation; |
| +} |
| + |
| +void TestDragDropClient::DragUpdate(aura::Window* target, |
| + const ui::LocatedEvent& event) { |
| +} |
| + |
| +void TestDragDropClient::Drop(aura::Window* target, |
| + const ui::LocatedEvent& event) { |
| + drag_in_progress_ = false; |
| +} |
| + |
| +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::ET_MOUSE_DRAGGED: |
| + DragUpdate(target_, *event); |
| + event->StopPropagation(); |
| + break; |
| + case ui::ET_MOUSE_RELEASED: |
| + Drop(target_, *event); |
| + event->StopPropagation(); |
| + break; |
| + default: |
| + break; |
| + } |
| +} |
| +#endif // defined(USE_AURA) |
| + |
| // Tests if the listener is notified correctly, when a mouse click happens on a |
| // MenuButton that has a regular ButtonListener. |
| TEST_F(MenuButtonTest, ActivateNonDropDownOnMouseClick) { |
| @@ -230,4 +358,47 @@ TEST_F(MenuButtonTest, MenuButtonPressedLock) { |
| EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| } |
| +// Test that the MenuButton does not become pressed if it can be dragged, until |
| +// a release occurs. |
| +TEST_F(MenuButtonTest, DraggableMenuButtonActivatesOnRelease) { |
| + scoped_ptr<TestMenuButtonListener> menu_button_listener( |
|
sky
2014/10/23 17:25:31
No need to put in scoped_ptr, create on the stack.
jonross
2014/10/23 22:18:01
Done.
|
| + new TestMenuButtonListener); |
| + CreateMenuButtonWithMenuButtonListener(menu_button_listener.get()); |
| + TestDragController drag_controller; |
| + button()->set_drag_controller(&drag_controller); |
| + |
| + ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); |
| + |
| + generator.set_current_location(gfx::Point(10, 10)); |
| + generator.PressLeftButton(); |
| + EXPECT_EQ(NULL, menu_button_listener->last_source()); |
|
sky
2014/10/23 17:25:31
nullptr
jonross
2014/10/23 22:18:01
Done.
|
| + |
| + |
|
sky
2014/10/23 17:25:31
only one newline is needed here.
jonross
2014/10/23 22:18:01
Done.
|
| + generator.ReleaseLeftButton(); |
| + EXPECT_EQ(button(), menu_button_listener->last_source()); |
| + EXPECT_EQ(Button::STATE_PRESSED, menu_button_listener->last_source_state()); |
|
sky
2014/10/23 17:25:31
Why does the button stay pressed when the mouse is
jonross
2014/10/23 22:18:01
When a menu button activates to show the list of a
|
| +} |
| + |
| +#if defined(USE_AURA) |
| +// Tests that the MenuButton does not become pressed if it can be dragged, and a |
| +// DragDropClient is processing the events. |
| +TEST_F(MenuButtonTest, DraggableMenuButtonDoesNotActivateOnDrag) { |
| + scoped_ptr<TestMenuButtonListener> menu_button_listener( |
|
sky
2014/10/23 17:25:31
same comment about creating on the stack.
jonross
2014/10/23 22:18:01
Done.
|
| + new TestMenuButtonListener); |
| + CreateMenuButtonWithMenuButtonListener(menu_button_listener.get()); |
| + TestDragController drag_controller; |
| + button()->set_drag_controller(&drag_controller); |
| + |
| + TestDragDropClient drag_client; |
| + SetDragDropClient(GetContext(), &drag_client); |
| + button()->PrependPreTargetHandler(&drag_client); |
| + |
| + ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); |
| + generator.set_current_location(gfx::Point(10, 10)); |
| + generator.DragMouseBy(10,0); |
| + EXPECT_EQ(NULL, menu_button_listener->last_source()); |
| + EXPECT_EQ(Button::STATE_NORMAL, menu_button_listener->last_source_state()); |
| +} |
| +#endif |
| + |
| } // namespace views |