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/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "ui/base/dragdrop/drag_drop_types.h" | |
| 9 #include "ui/events/test/event_generator.h" | 10 #include "ui/events/test/event_generator.h" |
| 10 #include "ui/views/controls/button/menu_button_listener.h" | 11 #include "ui/views/controls/button/menu_button_listener.h" |
| 12 #include "ui/views/drag_controller.h" | |
| 11 #include "ui/views/test/views_test_base.h" | 13 #include "ui/views/test/views_test_base.h" |
| 12 | 14 |
| 15 #if defined(USE_AURA) | |
| 16 #include "ui/events/event.h" | |
| 17 #include "ui/events/event_handler.h" | |
| 18 #include "ui/wm/public/drag_drop_client.h" | |
| 19 #endif | |
| 20 | |
| 13 using base::ASCIIToUTF16; | 21 using base::ASCIIToUTF16; |
| 14 | 22 |
| 15 namespace views { | 23 namespace views { |
| 16 | 24 |
| 17 class MenuButtonTest : public ViewsTestBase { | 25 class MenuButtonTest : public ViewsTestBase { |
| 18 public: | 26 public: |
| 19 MenuButtonTest() : widget_(NULL), button_(NULL) {} | 27 MenuButtonTest() : widget_(NULL), button_(NULL) {} |
| 20 virtual ~MenuButtonTest() {} | 28 virtual ~MenuButtonTest() {} |
| 21 | 29 |
| 22 virtual void TearDown() override { | 30 virtual void TearDown() override { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 private: | 108 private: |
| 101 Button* last_sender_; | 109 Button* last_sender_; |
| 102 Button::ButtonState last_sender_state_; | 110 Button::ButtonState last_sender_state_; |
| 103 ui::EventType last_event_type_; | 111 ui::EventType last_event_type_; |
| 104 | 112 |
| 105 DISALLOW_COPY_AND_ASSIGN(TestButtonListener); | 113 DISALLOW_COPY_AND_ASSIGN(TestButtonListener); |
| 106 }; | 114 }; |
| 107 | 115 |
| 108 class TestMenuButtonListener : public MenuButtonListener { | 116 class TestMenuButtonListener : public MenuButtonListener { |
| 109 public: | 117 public: |
| 110 TestMenuButtonListener() {} | 118 TestMenuButtonListener() |
| 119 : last_source_(NULL), | |
|
sky
2014/10/23 17:25:31
NULL->nullptr
jonross
2014/10/23 22:18:00
Done.
| |
| 120 last_source_state_(Button::STATE_NORMAL) {} | |
| 111 virtual ~TestMenuButtonListener() {} | 121 virtual ~TestMenuButtonListener() {} |
| 112 | 122 |
| 113 virtual void OnMenuButtonClicked(View* source, | 123 virtual void OnMenuButtonClicked(View* source, |
| 114 const gfx::Point& /*point*/) override { | 124 const gfx::Point& /*point*/) override { |
| 115 last_source_ = source; | 125 last_source_ = source; |
| 116 CustomButton* custom_button = CustomButton::AsCustomButton(source); | 126 CustomButton* custom_button = CustomButton::AsCustomButton(source); |
| 117 DCHECK(custom_button); | 127 DCHECK(custom_button); |
| 118 last_source_state_ = custom_button->state(); | 128 last_source_state_ = custom_button->state(); |
| 119 } | 129 } |
| 120 | 130 |
| 121 View* last_source() { return last_source_; } | 131 View* last_source() { return last_source_; } |
| 122 Button::ButtonState last_source_state() { return last_source_state_; } | 132 Button::ButtonState last_source_state() { return last_source_state_; } |
| 123 | 133 |
| 124 private: | 134 private: |
| 125 View* last_source_; | 135 View* last_source_; |
| 126 Button::ButtonState last_source_state_; | 136 Button::ButtonState last_source_state_; |
| 127 }; | 137 }; |
| 128 | 138 |
| 139 // Basic implementation of a DragController, to test input behaviour for | |
| 140 // MenuButtons that can be dragged. | |
| 141 class TestDragController : public DragController { | |
| 142 public: | |
| 143 TestDragController() {} | |
| 144 virtual ~TestDragController() {} | |
|
sky
2014/10/23 17:25:31
no virtual, just override.
jonross
2014/10/23 22:18:00
Done.
| |
| 145 | |
| 146 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.
| |
| 147 const gfx::Point& press_pt, | |
| 148 ui::OSExchangeData* data) override {} | |
| 149 | |
| 150 virtual int GetDragOperationsForView(View* sender, | |
| 151 const gfx::Point& p) override { | |
| 152 return ui::DragDropTypes::DRAG_MOVE; | |
| 153 } | |
| 154 | |
| 155 virtual bool CanStartDragForView(View* sender, | |
| 156 const gfx::Point& press_pt, | |
| 157 const gfx::Point& p) override { | |
| 158 return true; | |
| 159 } | |
| 160 | |
| 161 private: | |
| 162 DISALLOW_COPY_AND_ASSIGN(TestDragController); | |
| 163 }; | |
| 164 | |
| 165 #if defined(USE_AURA) | |
| 166 // Basic implementation of a DragDropClient, tracking the state of the drag | |
| 167 // operation. While dragging addition mouse events are consumed, preventing the | |
| 168 // target view from receiving them. | |
| 169 class TestDragDropClient : public aura::client::DragDropClient, | |
| 170 public ui::EventHandler { | |
| 171 public: | |
| 172 TestDragDropClient(); | |
| 173 virtual ~TestDragDropClient(); | |
| 174 | |
| 175 // aura::client::DragDropClient: | |
| 176 virtual int StartDragAndDrop(const ui::OSExchangeData& data, | |
| 177 aura::Window* root_window, | |
| 178 aura::Window* source_window, | |
| 179 const gfx::Point& root_location, | |
| 180 int operation, | |
| 181 ui::DragDropTypes::DragEventSource source) override; | |
| 182 virtual void DragUpdate(aura::Window* target, | |
| 183 const ui::LocatedEvent& event) override; | |
| 184 virtual void Drop(aura::Window* target, | |
| 185 const ui::LocatedEvent& event) override; | |
| 186 virtual void DragCancel() override; | |
| 187 virtual bool IsDragDropInProgress() override; | |
| 188 | |
| 189 // ui::EventHandler: | |
| 190 virtual void OnMouseEvent(ui::MouseEvent* event) override; | |
| 191 | |
| 192 private: | |
| 193 // True while receiving ui::LocatedEvents for drag operations. | |
| 194 bool drag_in_progress_; | |
| 195 | |
| 196 // Target window where drag operations are occuring. | |
| 197 aura::Window* target_; | |
| 198 | |
| 199 DISALLOW_COPY_AND_ASSIGN(TestDragDropClient); | |
| 200 }; | |
| 201 | |
| 202 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.
| |
| 203 target_(NULL) { | |
|
sky
2014/10/23 17:25:31
nullptr
jonross
2014/10/23 22:18:01
Done.
| |
| 204 } | |
| 205 | |
| 206 TestDragDropClient::~TestDragDropClient() { | |
| 207 } | |
| 208 | |
| 209 int TestDragDropClient::StartDragAndDrop(const ui::OSExchangeData& data, | |
| 210 aura::Window* root_window, | |
| 211 aura::Window* source_window, | |
| 212 const gfx::Point& root_location, | |
| 213 int operation, | |
| 214 ui::DragDropTypes::DragEventSource source) { | |
| 215 if (IsDragDropInProgress()) | |
| 216 return 0; | |
|
sky
2014/10/23 17:25:31
DRAG_NONE
jonross
2014/10/23 22:18:01
Done.
| |
| 217 drag_in_progress_ = true; | |
| 218 target_ = root_window; | |
| 219 return operation; | |
| 220 } | |
| 221 | |
| 222 void TestDragDropClient::DragUpdate(aura::Window* target, | |
| 223 const ui::LocatedEvent& event) { | |
| 224 } | |
| 225 | |
| 226 void TestDragDropClient::Drop(aura::Window* target, | |
| 227 const ui::LocatedEvent& event) { | |
| 228 drag_in_progress_ = false; | |
| 229 } | |
| 230 | |
| 231 void TestDragDropClient::DragCancel() { | |
| 232 drag_in_progress_ = false; | |
| 233 } | |
| 234 | |
| 235 bool TestDragDropClient::IsDragDropInProgress() { | |
| 236 return drag_in_progress_; | |
| 237 } | |
| 238 | |
| 239 void TestDragDropClient::OnMouseEvent(ui::MouseEvent* event) { | |
| 240 if (!IsDragDropInProgress()) | |
| 241 return; | |
| 242 switch (event->type()) { | |
| 243 case ui::ET_MOUSE_DRAGGED: | |
| 244 DragUpdate(target_, *event); | |
| 245 event->StopPropagation(); | |
| 246 break; | |
| 247 case ui::ET_MOUSE_RELEASED: | |
| 248 Drop(target_, *event); | |
| 249 event->StopPropagation(); | |
| 250 break; | |
| 251 default: | |
| 252 break; | |
| 253 } | |
| 254 } | |
| 255 #endif // defined(USE_AURA) | |
| 256 | |
| 129 // Tests if the listener is notified correctly, when a mouse click happens on a | 257 // Tests if the listener is notified correctly, when a mouse click happens on a |
| 130 // MenuButton that has a regular ButtonListener. | 258 // MenuButton that has a regular ButtonListener. |
| 131 TEST_F(MenuButtonTest, ActivateNonDropDownOnMouseClick) { | 259 TEST_F(MenuButtonTest, ActivateNonDropDownOnMouseClick) { |
| 132 scoped_ptr<TestButtonListener> button_listener(new TestButtonListener); | 260 scoped_ptr<TestButtonListener> button_listener(new TestButtonListener); |
| 133 CreateMenuButtonWithButtonListener(button_listener.get()); | 261 CreateMenuButtonWithButtonListener(button_listener.get()); |
| 134 | 262 |
| 135 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | 263 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); |
| 136 | 264 |
| 137 generator.set_current_location(gfx::Point(10, 10)); | 265 generator.set_current_location(gfx::Point(10, 10)); |
| 138 generator.ClickLeftButton(); | 266 generator.ClickLeftButton(); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 | 351 |
| 224 // Reseting the final lock should return the button's state to normal... | 352 // Reseting the final lock should return the button's state to normal... |
| 225 pressed_lock2.reset(); | 353 pressed_lock2.reset(); |
| 226 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); | 354 EXPECT_EQ(Button::STATE_NORMAL, button()->state()); |
| 227 | 355 |
| 228 // ...And it should respond to mouse movement again. | 356 // ...And it should respond to mouse movement again. |
| 229 generator.MoveMouseTo(gfx::Point(10, 10)); | 357 generator.MoveMouseTo(gfx::Point(10, 10)); |
| 230 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); | 358 EXPECT_EQ(Button::STATE_HOVERED, button()->state()); |
| 231 } | 359 } |
| 232 | 360 |
| 361 // Test that the MenuButton does not become pressed if it can be dragged, until | |
| 362 // a release occurs. | |
| 363 TEST_F(MenuButtonTest, DraggableMenuButtonActivatesOnRelease) { | |
| 364 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.
| |
| 365 new TestMenuButtonListener); | |
| 366 CreateMenuButtonWithMenuButtonListener(menu_button_listener.get()); | |
| 367 TestDragController drag_controller; | |
| 368 button()->set_drag_controller(&drag_controller); | |
| 369 | |
| 370 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | |
| 371 | |
| 372 generator.set_current_location(gfx::Point(10, 10)); | |
| 373 generator.PressLeftButton(); | |
| 374 EXPECT_EQ(NULL, menu_button_listener->last_source()); | |
|
sky
2014/10/23 17:25:31
nullptr
jonross
2014/10/23 22:18:01
Done.
| |
| 375 | |
| 376 | |
|
sky
2014/10/23 17:25:31
only one newline is needed here.
jonross
2014/10/23 22:18:01
Done.
| |
| 377 generator.ReleaseLeftButton(); | |
| 378 EXPECT_EQ(button(), menu_button_listener->last_source()); | |
| 379 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
| |
| 380 } | |
| 381 | |
| 382 #if defined(USE_AURA) | |
| 383 // Tests that the MenuButton does not become pressed if it can be dragged, and a | |
| 384 // DragDropClient is processing the events. | |
| 385 TEST_F(MenuButtonTest, DraggableMenuButtonDoesNotActivateOnDrag) { | |
| 386 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.
| |
| 387 new TestMenuButtonListener); | |
| 388 CreateMenuButtonWithMenuButtonListener(menu_button_listener.get()); | |
| 389 TestDragController drag_controller; | |
| 390 button()->set_drag_controller(&drag_controller); | |
| 391 | |
| 392 TestDragDropClient drag_client; | |
| 393 SetDragDropClient(GetContext(), &drag_client); | |
| 394 button()->PrependPreTargetHandler(&drag_client); | |
| 395 | |
| 396 ui::test::EventGenerator generator(GetContext(), widget()->GetNativeWindow()); | |
| 397 generator.set_current_location(gfx::Point(10, 10)); | |
| 398 generator.DragMouseBy(10,0); | |
| 399 EXPECT_EQ(NULL, menu_button_listener->last_source()); | |
| 400 EXPECT_EQ(Button::STATE_NORMAL, menu_button_listener->last_source_state()); | |
| 401 } | |
| 402 #endif | |
| 403 | |
| 233 } // namespace views | 404 } // namespace views |
| OLD | NEW |