Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: chrome/test/base/interactive_test_utils_mac.mm

Issue 1747803003: MacViews: Implement Tab Dragging (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed click simulation, reimplemented CocoaWindowMoveLoop without relying on the WindowServer. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/threading/simple_thread.h"
11 #include "base/threading/thread_task_runner_handle.h"
11 #include "chrome/app/chrome_command_ids.h" 12 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/ui/views/tabs/window_finder.h"
12 #import "ui/base/test/windowed_nsnotification_observer.h" 14 #import "ui/base/test/windowed_nsnotification_observer.h"
15 #include "ui/gfx/animation/tween.h"
16 #include "ui/gfx/mac/coordinate_conversion.h"
17 #include "ui/views/cocoa/bridged_native_widget.h"
18 #include "ui/views/event_monitor.h"
19 #include "ui/views/widget/native_widget_mac.h"
20
21 namespace {
22
23 bool WaitForEvent(bool (^block)(const base::Closure& quit_closure)) {
tapted 2016/05/23 07:29:28 I replaced everything except `ScopedCGEventsEnable
themblsha 2016/05/26 15:13:25 Wow, amazing. It even works when simulating a drag
24 base::RunLoop runner;
25 bool result = 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()) {
tapted 2016/05/23 07:29:27 |delay| argument isn't needed if MoveWithoutAck is
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(
tapted 2016/05/23 07:29:27 It looks like this is only ever needed to calculat
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 {
tapted 2016/05/23 07:29:28 comment this, e.g. // While in scope, causes even
themblsha 2016/05/26 15:13:25 Done.
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_;
tapted 2016/05/23 07:29:28 Is this member needed? (i.e. do we need the comple
themblsha 2016/05/26 15:13:25 No, I guess I was trying to write a generic versio
82 };
tapted 2016/05/23 07:29:28 nit: DISALLOW_COPY_AND_ASSIGN(..)
83
84 class BlockRunner : public base::DelegateSimpleThread::Delegate {
tapted 2016/05/23 07:29:27 needs a comment
85 public:
86 BlockRunner(void (^block)(), const base::Closure& quit_closure)
87 : block_(block),
88 quit_closure_(quit_closure),
89 task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
90 ~BlockRunner() override {}
tapted 2016/05/23 07:29:27 remove this? I don't think it's needed
91
92 void Run() override {
93 std::unique_ptr<base::MessageLoop> loop(
tapted 2016/05/23 07:29:28 Can this just be on the stack? base::MessageLoop
themblsha 2016/05/26 15:13:25 It can, thanks :)
94 new base::MessageLoop(base::MessageLoop::TYPE_DEFAULT));
95
96 block_();
97
98 task_runner_->PostTask(FROM_HERE, quit_closure_);
99 }
100
101 private:
102 void (^block_)();
103 base::Closure quit_closure_;
104 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
105 };
tapted 2016/05/23 07:29:28 nit: DISALLOW_COPY_AND_ASSIGN
106
107 void RunAtBackgroundQueue(void (^block)()) {
108 DCHECK_EQ(dispatch_get_current_queue(), dispatch_get_main_queue());
109 base::RunLoop runner;
110
111 BlockRunner thread_runner(block, runner.QuitClosure());
112 base::DelegateSimpleThread thread(&thread_runner,
113 "interactive_test_utils.BackgroundQueue");
114 thread.Start();
115
116 // We need to run the loop on the main thread, so the mouse events will be
117 // actually processed.
118 runner.Run();
119
120 thread.Join();
121 }
122
123 bool WindowIsMoving(NSWindow* window) {
tapted 2016/05/23 07:29:28 Perhaps WindowHasActiveDragger
124 views::BridgedNativeWidget* bridge =
125 views::NativeWidgetMac::GetBridgeForNativeWindow(window);
126 DCHECK(bridge);
127 return bridge->IsRunMoveLoopActive();
tapted 2016/05/23 07:29:28 It's not good to couple together this file with Br
128 }
129
130 // Returns true if window under the cursor is currently moving by WindowServer.
131 bool WindowIsMovingBySystem() {
tapted 2016/05/23 07:29:28 `BySystem` is not accurate any longer. Perhaps Win
132 NSWindow* window = WindowFinder().GetLocalProcessWindowAtPoint(
133 views::EventMonitor::GetLastMouseLocation(),
tapted 2016/05/23 07:29:28 Use gfx::ScreenPointFromNSPoint([NSEvent mouseLoca
134 std::set<gfx::NativeWindow>());
135 return window && WindowIsMoving(window);
136 }
137
138 // If current window under the cursor is currently moving by WindowServer, wait
139 // for the NSWindowMovedEventType notification.
140 //
141 // Returns whether the window under the cursor was moved by the system.
142 bool WaitForSystemWindowMoveToStop() {
143 // NOTE: This is a potentially troublesome part, currently it only works
144 // with MacViewsBrowser when the moved window entered RunMoveLoop.
145 // On non-MacViewsBrowser builds or when the window was moved using the
146 // caption we would be unable to detect that the window was moved using the
147 // WindowServer and would not wait for the final NSWindowMovedEventType
148 // notification.
149 //
150 // As a possible solution it would be possible to add an
151 // additional argument to the function
152 // |force_wait_for_system_window_move_to_finish|, and force waiting for
153 // notification if this ever becomes a problem.
154 NSWindow* window = WindowFinder().GetLocalProcessWindowAtPoint(
155 views::EventMonitor::GetLastMouseLocation(),
156 std::set<gfx::NativeWindow>());
157 const bool window_is_moving_by_system = window && WindowIsMoving(window);
158
159 if (WindowIsMovingBySystem()) {
160 // Wait for a final NSWindowMovedEventType notification, otherwise
161 // the window won't be in the final position. It arrives asynchronously
162 // after the mouse move events.
163 NSEvent* window_move_event =
164 [NSEvent otherEventWithType:NSAppKitDefined
165 location:NSZeroPoint
166 modifierFlags:0
167 timestamp:0
168 windowNumber:0
169 context:0
170 subtype:NSWindowMovedEventType
171 data1:0
172 data2:0];
173 base::RunLoop no_window_move_runner;
174 ui_controls::NotifyWhenEventIsProcessed(
175 window_move_event, no_window_move_runner.QuitClosure());
176 no_window_move_runner.Run();
177 }
178
179 return window_is_moving_by_system;
180 }
181
182 } // namespace
13 183
14 namespace ui_test_utils { 184 namespace ui_test_utils {
tapted 2016/05/23 07:29:28 move this to the top of the file before the anonym
themblsha 2016/05/26 15:13:25 Done. Also removed the unneeded functions you've a
15 185
16 void HideNativeWindow(gfx::NativeWindow window) { 186 void HideNativeWindow(gfx::NativeWindow window) {
17 [window orderOut:nil]; 187 [window orderOut:nil];
18 } 188 }
19 189
20 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { 190 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) {
21 // Make sure an unbundled program can get the input focus. 191 // Make sure an unbundled program can get the input focus.
22 ProcessSerialNumber psn = { 0, kCurrentProcess }; 192 ProcessSerialNumber psn = { 0, kCurrentProcess };
23 TransformProcessType(&psn,kProcessTransformToForegroundApplication); 193 TransformProcessType(&psn,kProcessTransformToForegroundApplication);
24 SetFrontProcess(&psn); 194 SetFrontProcess(&psn);
(...skipping 12 matching lines...) Expand all
37 // This is because normal AppKit menu updating does not get invoked when 207 // This is because normal AppKit menu updating does not get invoked when
38 // events are sent via ui_test_utils::SendKeyPressSync. 208 // events are sent via ui_test_utils::SendKeyPressSync.
39 BOOL notification_observed = [async_waiter wait]; 209 BOOL notification_observed = [async_waiter wait];
40 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush. 210 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush.
41 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; 211 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu];
42 [[file_menu delegate] menuNeedsUpdate:file_menu]; 212 [[file_menu delegate] menuNeedsUpdate:file_menu];
43 213
44 return !async_waiter || notification_observed; 214 return !async_waiter || notification_observed;
45 } 215 }
46 216
217 void DragAndDrop(const std::list<DragAndDropOperation>& operations) {
218 const bool should_be_moved_by_system =
219 DragAndDropMoveOperations(operations).size() > 2;
220
221 ScopedCGEventsEnabler cgevents_enabler;
222
223 RunAtBackgroundQueue(^() {
224 std::list<DragAndDropOperation> mutable_operations(operations);
225 bool window_was_moved_by_system = false;
226
227 while (!mutable_operations.empty()) {
tapted 2016/05/23 07:29:28 This can just iterate normally - I don't see any n
themblsha 2016/05/26 15:13:25 I think the code is simpler with a list, see the r
228 DragAndDropOperation op = mutable_operations.front();
229 mutable_operations.pop_front();
230 const bool last_operation = mutable_operations.empty();
231 const bool have_remaining_move_operations =
232 !DragAndDropMoveOperations(mutable_operations).empty();
233
234 switch (op.type()) {
235 case DragAndDropOperation::Type::Move:
236 case DragAndDropOperation::Type::MoveWithoutAck:
237 MouseMove(op.point(), op.delay());
238 // During the drag a new window could be both detached and reattached,
239 // and if we check for WindowIsMovingBySystem() only at the very end,
240 // it will return false, as the original window was stationary.
241 window_was_moved_by_system |= WindowIsMovingBySystem();
242
243 if (!have_remaining_move_operations) {
244 // WaitForSystemWindowMoveToStop() is necessary to make sure window
245 // frame is final after the drag-n-drop operation.
246 window_was_moved_by_system |= WaitForSystemWindowMoveToStop();
247 DCHECK_EQ(window_was_moved_by_system, should_be_moved_by_system);
248 }
249 break;
250 case DragAndDropOperation::Type::MouseDown:
251 MouseDown();
252 break;
253 case DragAndDropOperation::Type::MouseUp:
254 MouseUp();
255
256 if (last_operation) {
257 DCHECK(!WindowIsMovingBySystem());
258 }
259 break;
260 case DragAndDropOperation::Type::SetMousePositionOverride:
261 ui_controls::SetMousePositionOverride(true, op.point());
262 break;
263 case DragAndDropOperation::Type::UnsetMousePositionOverride:
264 ui_controls::SetMousePositionOverride(false, gfx::Point());
265 break;
266 case DragAndDropOperation::Type::DebugDelay:
267 usleep(op.delay().InMicroseconds());
268 break;
269 }
270 }
271 });
272 }
273
274 void DragAndDrop(const gfx::Point& from, const gfx::Point& to, int steps) {
275 DCHECK_GE(steps, 1);
276 std::list<DragAndDropOperation> operations;
277 operations.push_back(DragAndDropOperation::Move(from));
278 operations.push_back(DragAndDropOperation::MouseDown());
279
280 for (int i = 1; i <= steps; ++i) {
281 const double progress = static_cast<double>(i) / steps;
282 operations.push_back(DragAndDropOperation::Move(
283 gfx::Point(gfx::Tween::IntValueBetween(progress, from.x(), to.x()),
284 gfx::Tween::IntValueBetween(progress, from.y(), to.y()))));
285 }
286
287 operations.push_back(DragAndDropOperation::MouseUp());
288 DragAndDrop(operations);
289 }
290
291 // static
292 DragAndDropOperation DragAndDropOperation::Move(const gfx::Point& p) {
293 return DragAndDropOperation(Type::Move, p);
294 }
295
296 // static
297 DragAndDropOperation DragAndDropOperation::MoveWithoutAck(
298 const gfx::Point& p,
299 const base::TimeDelta& delay) {
300 return DragAndDropOperation(Type::MoveWithoutAck, p, delay);
301 }
302
303 // static
304 DragAndDropOperation DragAndDropOperation::MouseDown() {
305 return DragAndDropOperation(Type::MouseDown, gfx::Point());
306 }
307
308 // static
309 DragAndDropOperation DragAndDropOperation::MouseUp() {
310 return DragAndDropOperation(Type::MouseUp, gfx::Point());
311 }
312
313 // static
314 DragAndDropOperation DragAndDropOperation::SetMousePositionOverride(
315 const gfx::Point& p) {
316 return DragAndDropOperation(Type::SetMousePositionOverride, p);
317 }
318
319 // static
320 DragAndDropOperation DragAndDropOperation::UnsetMousePositionOverride() {
321 return DragAndDropOperation(Type::UnsetMousePositionOverride, gfx::Point());
322 }
323
324 // static
325 DragAndDropOperation DragAndDropOperation::DebugDelay() {
326 return DragAndDropOperation(Type::DebugDelay, gfx::Point(),
327 base::TimeDelta::FromSeconds(1));
328 }
329
47 } // namespace ui_test_utils 330 } // namespace ui_test_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698