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..0edc0e050da9c58ab1252a8a6b01ace0eea21dd9 100644 |
--- a/chrome/test/base/interactive_test_utils_mac.mm |
+++ b/chrome/test/base/interactive_test_utils_mac.mm |
@@ -9,7 +9,42 @@ |
#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" |
+ |
+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 +79,45 @@ bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { |
return !async_waiter || notification_observed; |
} |
+void DragAndDrop(const gfx::Point& from, |
+ const gfx::Point& to, |
+ base::TimeDelta delay) { |
+ 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(); |
+ |
+ // FIXME(mblsha): If we do the drag without an intermediate step, it won't |
+ // actually work |
+ const int steps = 2; |
+ for (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(); |
+ |
+ 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(); |
+} |
+ |
} // namespace ui_test_utils |