OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/test/base/interactive_test_utils.h" | 5 #include "chrome/test/base/interactive_test_utils.h" |
6 | 6 |
7 #include <Carbon/Carbon.h> | 7 #include <Carbon/Carbon.h> |
8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
| 12 #include "chrome/browser/ui/views/tabs/window_finder.h" |
12 #import "ui/base/test/windowed_nsnotification_observer.h" | 13 #import "ui/base/test/windowed_nsnotification_observer.h" |
| 14 #include "ui/gfx/animation/tween.h" |
| 15 #include "ui/gfx/mac/coordinate_conversion.h" |
| 16 #include "ui/views/cocoa/bridged_native_widget.h" |
| 17 #include "ui/views/event_monitor.h" |
| 18 #include "ui/views/widget/native_widget_mac.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 bool WaitForEvent( |
| 23 bool (^dispatch_event_block)(const base::Closure& quit_closure)) { |
| 24 base::RunLoop runner; |
| 25 bool result = dispatch_event_block(runner.QuitClosure()); |
| 26 runner.Run(); |
| 27 return result; |
| 28 } |
| 29 |
| 30 bool MouseMove(const gfx::Point& p, |
| 31 const base::TimeDelta& delay = base::TimeDelta()) { |
| 32 if (!delay.is_zero()) { |
| 33 bool result = |
| 34 ui_controls::SendMouseMoveNotifyWhenDone(p.x(), p.y(), base::Closure()); |
| 35 usleep(delay.InMicroseconds()); |
| 36 return result; |
| 37 } |
| 38 |
| 39 return WaitForEvent(^(const base::Closure& quit_closure) { |
| 40 return ui_controls::SendMouseMoveNotifyWhenDone(p.x(), p.y(), quit_closure); |
| 41 }); |
| 42 } |
| 43 |
| 44 bool MouseDown() { |
| 45 return WaitForEvent(^(const base::Closure& quit_closure) { |
| 46 return ui_controls::SendMouseEventsNotifyWhenDone( |
| 47 ui_controls::LEFT, ui_controls::DOWN, quit_closure); |
| 48 }); |
| 49 } |
| 50 |
| 51 bool MouseUp() { |
| 52 return WaitForEvent(^(const base::Closure& quit_closure) { |
| 53 return ui_controls::SendMouseEventsNotifyWhenDone( |
| 54 ui_controls::LEFT, ui_controls::UP, quit_closure); |
| 55 }); |
| 56 } |
| 57 |
| 58 std::vector<ui_test_utils::DragAndDropOperation> DragAndDropMoveOperations( |
| 59 const std::list<ui_test_utils::DragAndDropOperation>& operations) { |
| 60 std::vector<ui_test_utils::DragAndDropOperation> move_operations; |
| 61 std::copy_if(operations.begin(), operations.end(), |
| 62 std::back_inserter(move_operations), |
| 63 [](const ui_test_utils::DragAndDropOperation& op) { |
| 64 return op.type() == ui_test_utils::DragAndDropOperation::Type::Move; |
| 65 }); |
| 66 return move_operations; |
| 67 } |
| 68 |
| 69 class ScopedCGEventsEnabler { |
| 70 public: |
| 71 ScopedCGEventsEnabler() |
| 72 : enable_cgevents_(ui_controls::SendMouseEventsAsCGEvents()) { |
| 73 ui_controls::SetSendMouseEventsAsCGEvents(true); |
| 74 } |
| 75 |
| 76 ~ScopedCGEventsEnabler() { |
| 77 ui_controls::SetSendMouseEventsAsCGEvents(enable_cgevents_); |
| 78 } |
| 79 |
| 80 private: |
| 81 bool enable_cgevents_; |
| 82 }; |
| 83 |
| 84 void RunAtBackgroundQueue(void (^block)()) { |
| 85 DCHECK_EQ(dispatch_get_current_queue(), dispatch_get_main_queue()); |
| 86 |
| 87 auto queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); |
| 88 base::RunLoop runner; |
| 89 base::RunLoop* runner_ptr = &runner; |
| 90 |
| 91 dispatch_async(queue, ^{ |
| 92 scoped_ptr<base::MessageLoop> loop( |
| 93 new base::MessageLoop(base::MessageLoop::TYPE_DEFAULT)); |
| 94 |
| 95 block(); |
| 96 |
| 97 dispatch_async(dispatch_get_main_queue(), ^{ |
| 98 runner_ptr->Quit(); |
| 99 }); |
| 100 }); |
| 101 |
| 102 // We need to run the loop on the main thread, so the mouse events will be |
| 103 // actually processed. |
| 104 runner.Run(); |
| 105 } |
| 106 |
| 107 bool WindowIsMoving(NSWindow* window) { |
| 108 views::BridgedNativeWidget* bridge = |
| 109 views::NativeWidgetMac::GetBridgeForNativeWindow(window); |
| 110 DCHECK(bridge); |
| 111 return bridge->IsRunMoveLoopActive(); |
| 112 } |
| 113 |
| 114 // Returns true if window under the cursor is currently moving by WindowServer. |
| 115 bool WindowIsMovingBySystem() { |
| 116 NSWindow* window = WindowFinder().GetLocalProcessWindowAtPoint( |
| 117 views::EventMonitor::GetLastMouseLocation(), |
| 118 std::set<gfx::NativeWindow>()); |
| 119 return window && WindowIsMoving(window); |
| 120 } |
| 121 |
| 122 // If current window under the cursor is currently moving by WindowServer, wait |
| 123 // for the NSWindowMovedEventType notification. |
| 124 // |
| 125 // Returns whether the window under the cursor was moved by the system. |
| 126 bool WaitForSystemWindowMoveToStop() { |
| 127 // NOTE: This is a potentially troublesome part, currently it only works |
| 128 // with MacViewsBrowser when the moved window entered RunMoveLoop. |
| 129 // On non-MacViewsBrowser builds or when the window was moved using the |
| 130 // caption we would be unable to detect that the window was moved using the |
| 131 // WindowServer and would not wait for the final NSWindowMovedEventType |
| 132 // notification. |
| 133 // |
| 134 // As a possible solution it would be possible to add an |
| 135 // additional argument to the function |
| 136 // |force_wait_for_system_window_move_to_finish|, and force waiting for |
| 137 // notification if this ever becomes a problem. |
| 138 NSWindow* window = WindowFinder().GetLocalProcessWindowAtPoint( |
| 139 views::EventMonitor::GetLastMouseLocation(), |
| 140 std::set<gfx::NativeWindow>()); |
| 141 const bool window_is_moving_by_system = window && WindowIsMoving(window); |
| 142 |
| 143 if (WindowIsMovingBySystem()) { |
| 144 // Wait for a final NSWindowMovedEventType notification, otherwise |
| 145 // the window won't be in the final position. It arrives asynchronously |
| 146 // after the mouse move events. |
| 147 NSEvent* window_move_event = |
| 148 [NSEvent otherEventWithType:NSAppKitDefined |
| 149 location:NSZeroPoint |
| 150 modifierFlags:0 |
| 151 timestamp:0 |
| 152 windowNumber:0 |
| 153 context:0 |
| 154 subtype:NSWindowMovedEventType |
| 155 data1:0 |
| 156 data2:0]; |
| 157 base::RunLoop no_window_move_runner; |
| 158 ui_controls::NotifyWhenEventIsProcessed( |
| 159 window_move_event, no_window_move_runner.QuitClosure()); |
| 160 no_window_move_runner.Run(); |
| 161 } |
| 162 |
| 163 return window_is_moving_by_system; |
| 164 } |
| 165 |
| 166 } // namespace |
13 | 167 |
14 namespace ui_test_utils { | 168 namespace ui_test_utils { |
15 | 169 |
16 void HideNativeWindow(gfx::NativeWindow window) { | 170 void HideNativeWindow(gfx::NativeWindow window) { |
17 [window orderOut:nil]; | 171 [window orderOut:nil]; |
18 } | 172 } |
19 | 173 |
20 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { | 174 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { |
21 // Make sure an unbundled program can get the input focus. | 175 // Make sure an unbundled program can get the input focus. |
22 ProcessSerialNumber psn = { 0, kCurrentProcess }; | 176 ProcessSerialNumber psn = { 0, kCurrentProcess }; |
(...skipping 14 matching lines...) Expand all Loading... |
37 // This is because normal AppKit menu updating does not get invoked when | 191 // This is because normal AppKit menu updating does not get invoked when |
38 // events are sent via ui_test_utils::SendKeyPressSync. | 192 // events are sent via ui_test_utils::SendKeyPressSync. |
39 BOOL notification_observed = [async_waiter wait]; | 193 BOOL notification_observed = [async_waiter wait]; |
40 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush. | 194 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush. |
41 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; | 195 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; |
42 [[file_menu delegate] menuNeedsUpdate:file_menu]; | 196 [[file_menu delegate] menuNeedsUpdate:file_menu]; |
43 | 197 |
44 return !async_waiter || notification_observed; | 198 return !async_waiter || notification_observed; |
45 } | 199 } |
46 | 200 |
| 201 void DragAndDrop(const std::list<DragAndDropOperation>& operations) { |
| 202 const bool should_be_moved_by_system = |
| 203 DragAndDropMoveOperations(operations).size() > 2; |
| 204 |
| 205 ScopedCGEventsEnabler cgevents_enabler; |
| 206 |
| 207 RunAtBackgroundQueue(^() { |
| 208 std::list<DragAndDropOperation> mutable_operations(operations); |
| 209 bool window_was_moved_by_system = false; |
| 210 |
| 211 while (!mutable_operations.empty()) { |
| 212 DragAndDropOperation op = mutable_operations.front(); |
| 213 mutable_operations.pop_front(); |
| 214 const bool last_operation = mutable_operations.empty(); |
| 215 const bool have_remaining_move_operations = |
| 216 !DragAndDropMoveOperations(mutable_operations).empty(); |
| 217 |
| 218 switch(op.type()) { |
| 219 case DragAndDropOperation::Type::Move: |
| 220 case DragAndDropOperation::Type::MoveWithoutAck: |
| 221 MouseMove(op.point(), op.delay()); |
| 222 // During the drag a new window could be both detached and reattached, |
| 223 // and if we check for WindowIsMovingBySystem() only at the very end, |
| 224 // it will return false, as the original window was stationary. |
| 225 window_was_moved_by_system |= WindowIsMovingBySystem(); |
| 226 |
| 227 if (!have_remaining_move_operations) { |
| 228 // WaitForSystemWindowMoveToStop() is necessary to make sure window |
| 229 // frame is final after the drag-n-drop operation. |
| 230 window_was_moved_by_system |= WaitForSystemWindowMoveToStop(); |
| 231 DCHECK_EQ(window_was_moved_by_system, should_be_moved_by_system); |
| 232 } |
| 233 break; |
| 234 case DragAndDropOperation::Type::MouseDown: |
| 235 MouseDown(); |
| 236 break; |
| 237 case DragAndDropOperation::Type::MouseUp: |
| 238 MouseUp(); |
| 239 |
| 240 if (last_operation) { |
| 241 DCHECK(!WindowIsMovingBySystem()); |
| 242 } |
| 243 break; |
| 244 case DragAndDropOperation::Type::SetMousePositionOverride: |
| 245 ui_controls::SetMousePositionOverride(true, op.point()); |
| 246 break; |
| 247 case DragAndDropOperation::Type::UnsetMousePositionOverride: |
| 248 ui_controls::SetMousePositionOverride(false, gfx::Point()); |
| 249 break; |
| 250 case DragAndDropOperation::Type::DebugDelay: |
| 251 usleep(op.delay().InMicroseconds()); |
| 252 break; |
| 253 } |
| 254 } |
| 255 }); |
| 256 } |
| 257 |
| 258 void DragAndDrop(const gfx::Point& from, |
| 259 const gfx::Point& to, |
| 260 int steps) { |
| 261 DCHECK_GE(steps, 1); |
| 262 std::list<DragAndDropOperation> operations; |
| 263 operations.emplace_back(DragAndDropOperation::Move(from)); |
| 264 operations.emplace_back(DragAndDropOperation::MouseDown()); |
| 265 |
| 266 for (int i = 1; i <= steps; ++i) { |
| 267 const double progress = static_cast<double>(i) / steps; |
| 268 operations.emplace_back(DragAndDropOperation::Move( |
| 269 gfx::Point(gfx::Tween::IntValueBetween(progress, from.x(), to.x()), |
| 270 gfx::Tween::IntValueBetween(progress, from.y(), to.y())))); |
| 271 } |
| 272 |
| 273 operations.emplace_back(DragAndDropOperation::MouseUp()); |
| 274 DragAndDrop(operations); |
| 275 } |
| 276 |
| 277 // static |
| 278 DragAndDropOperation DragAndDropOperation::Move(const gfx::Point& p) { |
| 279 return DragAndDropOperation(Type::Move, p); |
| 280 } |
| 281 |
| 282 // static |
| 283 DragAndDropOperation DragAndDropOperation::MoveWithoutAck( |
| 284 const gfx::Point& p, |
| 285 const base::TimeDelta& delay) { |
| 286 return DragAndDropOperation(Type::MoveWithoutAck, p, delay); |
| 287 } |
| 288 |
| 289 // static |
| 290 DragAndDropOperation DragAndDropOperation::MouseDown() { |
| 291 return DragAndDropOperation(Type::MouseDown, gfx::Point()); |
| 292 } |
| 293 |
| 294 // static |
| 295 DragAndDropOperation DragAndDropOperation::MouseUp() { |
| 296 return DragAndDropOperation(Type::MouseUp, gfx::Point()); |
| 297 } |
| 298 |
| 299 // static |
| 300 DragAndDropOperation DragAndDropOperation::SetMousePositionOverride( |
| 301 const gfx::Point& p) { |
| 302 return DragAndDropOperation(Type::SetMousePositionOverride, p); |
| 303 } |
| 304 |
| 305 // static |
| 306 DragAndDropOperation DragAndDropOperation::UnsetMousePositionOverride() { |
| 307 return DragAndDropOperation(Type::UnsetMousePositionOverride, gfx::Point()); |
| 308 } |
| 309 |
| 310 // static |
| 311 DragAndDropOperation DragAndDropOperation::DebugDelay() { |
| 312 return DragAndDropOperation(Type::DebugDelay, gfx::Point(), |
| 313 base::TimeDelta::FromSeconds(1)); |
| 314 } |
| 315 |
47 } // namespace ui_test_utils | 316 } // namespace ui_test_utils |
OLD | NEW |