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..cb6b357f19df83cd1361b6453a8164d6292ffc3a 100644 |
--- a/chrome/test/base/interactive_test_utils_mac.mm |
+++ b/chrome/test/base/interactive_test_utils_mac.mm |
@@ -9,7 +9,145 @@ |
#include "base/message_loop/message_loop.h" |
#include "chrome/app/chrome_command_ids.h" |
+#include "chrome/browser/ui/views/tabs/window_finder.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" |
+#include "ui/views/cocoa/bridged_native_widget.h" |
+#include "ui/views/event_monitor.h" |
+#include "ui/views/widget/native_widget_mac.h" |
+ |
+namespace views { |
+namespace test { |
+class BridgedNativeWidgetTestApi { |
tapted
2016/03/10 11:51:19
ooh interesting - there's already a BridgedNativeW
themblsha
2016/03/10 17:18:58
I've added BridgedNativeWidget::IsRunMoveLoopActiv
tapted
2016/03/11 09:38:28
nice - I like IsRunMoveLoopActive
But did you see
|
+ public: |
+ explicit BridgedNativeWidgetTestApi(NSWindow* window) { |
+ bridge_ = NativeWidgetMac::GetBridgeForNativeWindow(window); |
+ DCHECK(bridge_); |
+ } |
+ |
+ bool WindowIsMoving() const { |
+ return bridge_->window_move_loop_.get(); |
+ } |
+ |
+ private: |
+ BridgedNativeWidget* bridge_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BridgedNativeWidgetTestApi); |
+}; |
+} // namespace test |
+} // namespace views |
+ |
+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); |
+ }); |
+} |
+ |
+class ScopedCGEventsEnabler { |
+ public: |
+ ScopedCGEventsEnabler() |
+ : enable_cgevents_(ui_controls::SendMouseEventsAsCGEvents()) { |
+ ui_controls::SetSendMouseEventsAsCGEvents(true); |
+ } |
+ |
+ ~ScopedCGEventsEnabler() { |
+ ui_controls::SetSendMouseEventsAsCGEvents(enable_cgevents_); |
+ } |
+ |
+ private: |
+ bool enable_cgevents_; |
+}; |
+ |
+void RunAtBackgroundQueue(void (^block)()) { |
+ 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)); |
+ |
+ block(); |
+ |
+ 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(); |
+} |
+ |
+void WaitForSystemWindowMoveToStop() { |
+ // NOTE: This is a potentially troublesome part, currently it only works |
+ // with MacViewsBrowser when the moved window entered RunMoveLoop. |
+ // On non-MacViewsBrowser builds or when the window was moved using the |
+ // caption we would be unable to detect that the window was moved using the |
+ // WindowServer and would not wait for the final NSWindowMovedEventType |
+ // notification. |
+ // |
+ // As a possible solution it would be possible to add an |
+ // additional argument to the function |
+ // |force_wait_for_system_window_move_to_finish|, and force waiting for |
+ // notification if this ever becomes a problem. |
+ NSWindow* window = |
+ GetLocalProcessWindowAtPoint(views::EventMonitor::GetLastMouseLocation(), |
+ std::set<gfx::NativeWindow>()); |
+ const bool window_is_moving_by_system = |
+ window && |
+ views::test::BridgedNativeWidgetTestApi(window).WindowIsMoving(); |
+ |
+ if (window_is_moving_by_system) { |
+ // Wait for a final NSWindowMovedEventType notification, otherwise |
+ // the window won't be in the final position. It arrives asynchronously |
+ // after the mouse move events. |
+ NSEvent* window_move_event = |
+ [NSEvent otherEventWithType:NSAppKitDefined |
+ location:NSZeroPoint |
+ modifierFlags:0 |
+ timestamp:0 |
+ windowNumber:0 |
+ context:0 |
+ subtype:NSWindowMovedEventType |
+ data1:0 |
+ data2:0]; |
+ scoped_refptr<content::MessageLoopRunner> no_window_move_runner = |
+ new content::MessageLoopRunner; |
+ ui_controls::NotifyWhenEventIsProcessed( |
+ window_move_event, no_window_move_runner->QuitClosure()); |
+ no_window_move_runner->Run(); |
+ } |
+} |
+ |
+} // namespace |
namespace ui_test_utils { |
@@ -44,4 +182,36 @@ 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); |
+ |
+ ScopedCGEventsEnabler cgevents_enabler; |
+ |
+ RunAtBackgroundQueue(^() { |
+ 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 dragging works |
+ // on the WindowServer level. |
+ for (unsigned int i = 1; i <= steps; ++i) { |
tapted
2016/03/10 11:51:19
nit: just int
themblsha
2016/03/10 17:18:58
Done.
|
+ const double progress = double(i) / steps; |
tapted
2016/03/10 11:51:19
we tend to use static_cast instead of this style -
themblsha
2016/03/10 17:18:58
Done.
|
+ 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()); |
tapted
2016/03/10 11:51:19
yeah - I think we can remove this for now? - `usle
themblsha
2016/03/10 17:18:58
Okay :-)
|
+ |
+ WaitForSystemWindowMoveToStop(); |
+ MouseUp(); |
+ }); |
+} |
+ |
} // namespace ui_test_utils |