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

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

Issue 2005773002: MacViewsBrowser: TabDragging / simplifications Base URL: https://chromium.googlesource.com/chromium/src.git@20160523-MacViewsBrowser-TabDragging2
Patch Set: 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
« no previous file with comments | « chrome/test/base/interactive_test_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/threading/simple_thread.h"
11 #include "base/threading/thread_task_runner_handle.h"
12 #include "chrome/app/chrome_command_ids.h" 10 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/ui/views/tabs/window_finder.h" 11 #include "chrome/browser/ui/views/tabs/window_finder.h"
14 #import "ui/base/test/windowed_nsnotification_observer.h" 12 #import "ui/base/test/windowed_nsnotification_observer.h"
15 #include "ui/gfx/animation/tween.h" 13 #include "ui/gfx/animation/tween.h"
16 #include "ui/gfx/mac/coordinate_conversion.h" 14
17 #include "ui/views/cocoa/bridged_native_widget.h" 15 namespace ui_test_utils {
18 #include "ui/views/event_monitor.h"
19 #include "ui/views/widget/native_widget_mac.h"
20 16
21 namespace { 17 namespace {
22 18
23 bool WaitForEvent(bool (^block)(const base::Closure& quit_closure)) { 19 // comment
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()) {
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 { 20 class ScopedCGEventsEnabler {
70 public: 21 public:
71 ScopedCGEventsEnabler() 22 ScopedCGEventsEnabler()
72 : enable_cgevents_(ui_controls::SendMouseEventsAsCGEvents()) { 23 : enable_cgevents_(ui_controls::SendMouseEventsAsCGEvents()) {
73 ui_controls::SetSendMouseEventsAsCGEvents(true); 24 ui_controls::SetSendMouseEventsAsCGEvents(true);
74 } 25 }
75 26
76 ~ScopedCGEventsEnabler() { 27 ~ScopedCGEventsEnabler() {
77 ui_controls::SetSendMouseEventsAsCGEvents(enable_cgevents_); 28 ui_controls::SetSendMouseEventsAsCGEvents(enable_cgevents_);
78 } 29 }
79 30
80 private: 31 private:
81 bool enable_cgevents_; 32 bool enable_cgevents_;
82 }; 33 };
83 34
84 class BlockRunner : public base::DelegateSimpleThread::Delegate { 35 // comment
36 class OperationRunner {
85 public: 37 public:
86 BlockRunner(void (^block)(), const base::Closure& quit_closure) 38 static void Run(const std::list<DragAndDropOperation>& operations) {
87 : block_(block), 39 OperationRunner runner(operations);
88 quit_closure_(quit_closure), 40 base::RunLoop run_loop;
89 task_runner_(base::ThreadTaskRunnerHandle::Get()) {} 41 runner.quit_closure_ = run_loop.QuitClosure();
90 ~BlockRunner() override {} 42 base::MessageLoop::current()->PostTask(
91 43 FROM_HERE,
92 void Run() override { 44 base::Bind(&OperationRunner::Next, base::Unretained(&runner)));
93 std::unique_ptr<base::MessageLoop> loop( 45 run_loop.Run();
94 new base::MessageLoop(base::MessageLoop::TYPE_DEFAULT));
95
96 block_();
97
98 task_runner_->PostTask(FROM_HERE, quit_closure_);
99 } 46 }
100 47
101 private: 48 private:
102 void (^block_)(); 49 explicit OperationRunner(const std::list<DragAndDropOperation>& operations)
50 : operations_(operations.begin(), operations.end()) {}
51
52 void Next() {
53 if (progress_ == operations_.size()) {
54 base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
55 return;
56 }
57
58 const DragAndDropOperation& op = operations_[progress_++];
59 auto next = base::Bind(&OperationRunner::Next, base::Unretained(this));
60 switch (op.type()) {
61 case DragAndDropOperation::Type::Move:
62 ui_controls::SendMouseMoveNotifyWhenDone(op.point().x(), op.point().y(),
63 next);
64 break;
65
66 case DragAndDropOperation::Type::MouseDown:
67 ui_controls::SendMouseEventsNotifyWhenDone(ui_controls::LEFT,
68 ui_controls::DOWN, next);
69 break;
70
71 case DragAndDropOperation::Type::MouseUp:
72 ui_controls::SendMouseEventsNotifyWhenDone(ui_controls::LEFT,
73 ui_controls::UP, next);
74 break;
75
76 case DragAndDropOperation::Type::SetMousePositionOverride:
77 ui_controls::SetMousePositionOverride(true, op.point());
78 base::MessageLoop::current()->PostTask(FROM_HERE, next);
79 break;
80
81 case DragAndDropOperation::Type::UnsetMousePositionOverride:
82 ui_controls::SetMousePositionOverride(false, gfx::Point());
83 base::MessageLoop::current()->PostTask(FROM_HERE, next);
84 break;
85
86 case DragAndDropOperation::Type::DebugDelay:
87 base::MessageLoop::current()->PostDelayedTask(
88 FROM_HERE, next, base::TimeDelta::FromSeconds(1));
89 break;
90 }
91 }
92
93 ~OperationRunner() { DCHECK_EQ(operations_.size(), progress_); }
94
103 base::Closure quit_closure_; 95 base::Closure quit_closure_;
104 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 96 size_t progress_ = 0;
97 std::vector<DragAndDropOperation> operations_;
98
99 DISALLOW_COPY_AND_ASSIGN(OperationRunner);
105 }; 100 };
106 101
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) {
124 views::BridgedNativeWidget* bridge =
125 views::NativeWidgetMac::GetBridgeForNativeWindow(window);
126 DCHECK(bridge);
127 return bridge->IsRunMoveLoopActive();
128 }
129
130 // Returns true if window under the cursor is currently moving by WindowServer.
131 bool WindowIsMovingBySystem() {
132 NSWindow* window = WindowFinder().GetLocalProcessWindowAtPoint(
133 views::EventMonitor::GetLastMouseLocation(),
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 102 } // namespace
183 103
184 namespace ui_test_utils {
185
186 void HideNativeWindow(gfx::NativeWindow window) { 104 void HideNativeWindow(gfx::NativeWindow window) {
187 [window orderOut:nil]; 105 [window orderOut:nil];
188 } 106 }
189 107
190 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { 108 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) {
191 // Make sure an unbundled program can get the input focus. 109 // Make sure an unbundled program can get the input focus.
192 ProcessSerialNumber psn = { 0, kCurrentProcess }; 110 ProcessSerialNumber psn = { 0, kCurrentProcess };
193 TransformProcessType(&psn,kProcessTransformToForegroundApplication); 111 TransformProcessType(&psn,kProcessTransformToForegroundApplication);
194 SetFrontProcess(&psn); 112 SetFrontProcess(&psn);
195 113
(...skipping 12 matching lines...) Expand all
208 // events are sent via ui_test_utils::SendKeyPressSync. 126 // events are sent via ui_test_utils::SendKeyPressSync.
209 BOOL notification_observed = [async_waiter wait]; 127 BOOL notification_observed = [async_waiter wait];
210 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush. 128 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush.
211 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; 129 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu];
212 [[file_menu delegate] menuNeedsUpdate:file_menu]; 130 [[file_menu delegate] menuNeedsUpdate:file_menu];
213 131
214 return !async_waiter || notification_observed; 132 return !async_waiter || notification_observed;
215 } 133 }
216 134
217 void DragAndDrop(const std::list<DragAndDropOperation>& operations) { 135 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; 136 ScopedCGEventsEnabler cgevents_enabler;
222 137 OperationRunner::Run(operations);
223 RunAtBackgroundQueue(^() {
224 std::list<DragAndDropOperation> mutable_operations(operations);
225 bool window_was_moved_by_system = false;
226
227 while (!mutable_operations.empty()) {
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 } 138 }
273 139
274 void DragAndDrop(const gfx::Point& from, const gfx::Point& to, int steps) { 140 void DragAndDrop(const gfx::Point& from, const gfx::Point& to, int steps) {
275 DCHECK_GE(steps, 1); 141 DCHECK_GE(steps, 1);
276 std::list<DragAndDropOperation> operations; 142 std::list<DragAndDropOperation> operations;
277 operations.push_back(DragAndDropOperation::Move(from)); 143 operations.push_back(DragAndDropOperation::Move(from));
278 operations.push_back(DragAndDropOperation::MouseDown()); 144 operations.push_back(DragAndDropOperation::MouseDown());
279 145
280 for (int i = 1; i <= steps; ++i) { 146 for (int i = 1; i <= steps; ++i) {
281 const double progress = static_cast<double>(i) / steps; 147 const double progress = static_cast<double>(i) / steps;
282 operations.push_back(DragAndDropOperation::Move( 148 operations.push_back(DragAndDropOperation::Move(
283 gfx::Point(gfx::Tween::IntValueBetween(progress, from.x(), to.x()), 149 gfx::Point(gfx::Tween::IntValueBetween(progress, from.x(), to.x()),
284 gfx::Tween::IntValueBetween(progress, from.y(), to.y())))); 150 gfx::Tween::IntValueBetween(progress, from.y(), to.y()))));
285 } 151 }
286 152
287 operations.push_back(DragAndDropOperation::MouseUp()); 153 operations.push_back(DragAndDropOperation::MouseUp());
288 DragAndDrop(operations); 154 DragAndDrop(operations);
289 } 155 }
290 156
291 // static 157 // static
292 DragAndDropOperation DragAndDropOperation::Move(const gfx::Point& p) { 158 DragAndDropOperation DragAndDropOperation::Move(const gfx::Point& p) {
293 return DragAndDropOperation(Type::Move, p); 159 return DragAndDropOperation(Type::Move, p);
294 } 160 }
295 161
296 // static 162 // 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() { 163 DragAndDropOperation DragAndDropOperation::MouseDown() {
305 return DragAndDropOperation(Type::MouseDown, gfx::Point()); 164 return DragAndDropOperation(Type::MouseDown, gfx::Point());
306 } 165 }
307 166
308 // static 167 // static
309 DragAndDropOperation DragAndDropOperation::MouseUp() { 168 DragAndDropOperation DragAndDropOperation::MouseUp() {
310 return DragAndDropOperation(Type::MouseUp, gfx::Point()); 169 return DragAndDropOperation(Type::MouseUp, gfx::Point());
311 } 170 }
312 171
313 // static 172 // static
314 DragAndDropOperation DragAndDropOperation::SetMousePositionOverride( 173 DragAndDropOperation DragAndDropOperation::SetMousePositionOverride(
315 const gfx::Point& p) { 174 const gfx::Point& p) {
316 return DragAndDropOperation(Type::SetMousePositionOverride, p); 175 return DragAndDropOperation(Type::SetMousePositionOverride, p);
317 } 176 }
318 177
319 // static 178 // static
320 DragAndDropOperation DragAndDropOperation::UnsetMousePositionOverride() { 179 DragAndDropOperation DragAndDropOperation::UnsetMousePositionOverride() {
321 return DragAndDropOperation(Type::UnsetMousePositionOverride, gfx::Point()); 180 return DragAndDropOperation(Type::UnsetMousePositionOverride, gfx::Point());
322 } 181 }
323 182
324 // static 183 // static
325 DragAndDropOperation DragAndDropOperation::DebugDelay() { 184 DragAndDropOperation DragAndDropOperation::DebugDelay() {
326 return DragAndDropOperation(Type::DebugDelay, gfx::Point(), 185 return DragAndDropOperation(Type::DebugDelay, gfx::Point());
327 base::TimeDelta::FromSeconds(1));
328 } 186 }
329 187
330 } // namespace ui_test_utils 188 } // namespace ui_test_utils
OLDNEW
« no previous file with comments | « chrome/test/base/interactive_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698