Chromium Code Reviews| Index: chrome/test/base/interactive_test_utils_mac.mm |
| diff --git a/chrome/test/base/interactive_test_utils_mac.mm b/chrome/test/base/interactive_test_utils_mac.mm |
| index 0d00f3a6614e3aacd4c9141ed971ae02590ea1d1..871b5e90be72d8c61608f4442542e100c5d7bbd5 100644 |
| --- a/chrome/test/base/interactive_test_utils_mac.mm |
| +++ b/chrome/test/base/interactive_test_utils_mac.mm |
| @@ -9,7 +9,43 @@ |
| #include "base/message_loop/message_loop.h" |
| #include "chrome/app/chrome_command_ids.h" |
| +#include "content/public/test/test_utils.h" |
| #import "ui/base/test/windowed_nsnotification_observer.h" |
| +#include "ui/gfx/animation/tween.h" |
| +#include "ui/gfx/mac/coordinate_conversion.h" |
| + |
| +namespace { |
| + |
| +bool WaitForEvent( |
| + bool (^dispatch_event_block)(const base::Closure& quit_closure)) { |
| + scoped_refptr<content::MessageLoopRunner> runner = |
| + new content::MessageLoopRunner; |
| + bool result = dispatch_event_block(runner->QuitClosure()); |
| + runner->Run(); |
| + return result; |
| +} |
| + |
| +bool MouseMove(const gfx::Point& p) { |
| + return WaitForEvent(^(const base::Closure& quit_closure) { |
| + return ui_controls::SendMouseMoveNotifyWhenDone(p.x(), p.y(), quit_closure); |
| + }); |
| +} |
| + |
| +bool MouseDown() { |
| + return WaitForEvent(^(const base::Closure& quit_closure) { |
| + return ui_controls::SendMouseEventsNotifyWhenDone( |
| + ui_controls::LEFT, ui_controls::DOWN, quit_closure); |
| + }); |
| +} |
| + |
| +bool MouseUp() { |
| + return WaitForEvent(^(const base::Closure& quit_closure) { |
| + return ui_controls::SendMouseEventsNotifyWhenDone( |
| + ui_controls::LEFT, ui_controls::UP, quit_closure); |
| + }); |
| +} |
| + |
| +} // namespace |
| namespace ui_test_utils { |
| @@ -44,4 +80,56 @@ bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { |
| return !async_waiter || notification_observed; |
| } |
| +void DragAndDrop(const gfx::Point& from, |
| + const gfx::Point& to, |
| + base::TimeDelta delay, |
| + unsigned int steps) { |
| + DCHECK_GE(steps, 1u); |
| + const bool enable_cgevents = ui_controls::SendMouseEventsAsCGEvents(); |
| + ui_controls::SetSendMouseEventsAsCGEvents(true); |
| + |
| + auto queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); |
| + scoped_refptr<content::MessageLoopRunner> runner = |
| + new content::MessageLoopRunner; |
| + |
| + dispatch_async(queue, ^{ |
| + scoped_ptr<base::MessageLoop> loop( |
| + new base::MessageLoop(base::MessageLoop::TYPE_DEFAULT)); |
| + |
| + MouseMove(from); |
| + MouseDown(); |
| + |
| + // If steps is greater than 1, then we need to use CGEvents, otherwise |
| + // the CocoaWindowMoveLoop won't actually work, as the window dragged works |
| + // on the WindowServer level. |
| + for (unsigned int i = 1; i <= steps; ++i) { |
| + const double progress = double(i) / steps; |
| + const gfx::Point p( |
| + gfx::Tween::IntValueBetween(progress, from.x(), to.x()), |
| + gfx::Tween::IntValueBetween(progress, from.y(), to.y())); |
| + MouseMove(p); |
| + } |
| + |
| + // specify a delay if there's a non-zero drag'n'drop delay, like |
| + // kTearDuration in tab_strip_drag_controller.mm |
| + usleep(delay.InMicroseconds()); |
| + |
| + MouseUp(); |
| + |
| + // FIXME(mblsha): Need to find a way to wait until CocoaWindowMoveLoop is |
| + // finished. |
| + usleep(100000.0); |
|
themblsha
2016/03/03 17:56:51
Now the code works very reliably with the exceptio
tapted
2016/03/04 06:33:08
There's a BridgedNativeWidgetTestApi which has acc
themblsha
2016/03/09 17:40:22
Done.
|
| + |
| + dispatch_async(dispatch_get_main_queue(), ^{ |
| + runner->Quit(); |
| + }); |
| + }); |
| + |
| + // we need to run the loop on the main thread, so the mouse events will be |
| + // actually processed |
| + runner->Run(); |
| + |
| + ui_controls::SetSendMouseEventsAsCGEvents(enable_cgevents); |
| +} |
| + |
| } // namespace ui_test_utils |