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

Side by Side Diff: ui/base/test/ui_controls_mac.mm

Issue 1747803003: MacViews: Implement Tab Dragging (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix review issues. Created 4 years, 6 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "ui/base/test/ui_controls.h" 5 #include "ui/base/test/ui_controls.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #import "base/mac/foundation_util.h"
13 #import "base/mac/scoped_objc_class_swizzler.h"
12 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
13 #include "ui/base/cocoa/cocoa_base_utils.h" 15 #include "ui/base/cocoa/cocoa_base_utils.h"
14 #include "ui/events/keycodes/keyboard_code_conversion_mac.h" 16 #include "ui/events/keycodes/keyboard_code_conversion_mac.h"
15 #import "ui/events/test/cocoa_test_event_utils.h" 17 #import "ui/events/test/cocoa_test_event_utils.h"
18 #include "ui/gfx/geometry/point.h"
19 #import "ui/gfx/mac/coordinate_conversion.h"
16 20
17 // Implementation details: We use [NSApplication sendEvent:] instead 21 // Implementation details: We use [NSApplication sendEvent:] instead
18 // of [NSApplication postEvent:atStart:] so that the event gets sent 22 // of [NSApplication postEvent:atStart:] so that the event gets sent
19 // immediately. This lets us run the post-event task right 23 // immediately. This lets us run the post-event task right
20 // immediately as well. Unfortunately I cannot subclass NSEvent (it's 24 // immediately as well. Unfortunately I cannot subclass NSEvent (it's
21 // probably a class cluster) to allow other easy answers. For 25 // probably a class cluster) to allow other easy answers. For
22 // example, if I could subclass NSEvent, I could run the Task in it's 26 // example, if I could subclass NSEvent, I could run the Task in it's
23 // dealloc routine (which necessarily happens after the event is 27 // dealloc routine (which necessarily happens after the event is
24 // dispatched). Unlike Linux, Mac does not have message loop 28 // dispatched). Unlike Linux, Mac does not have message loop
25 // observer/notification. Unlike windows, I cannot post non-events 29 // observer/notification. Unlike windows, I cannot post non-events
(...skipping 19 matching lines...) Expand all
45 49
46 using cocoa_test_event_utils::SynthesizeKeyEvent; 50 using cocoa_test_event_utils::SynthesizeKeyEvent;
47 using cocoa_test_event_utils::TimeIntervalSinceSystemStartup; 51 using cocoa_test_event_utils::TimeIntervalSinceSystemStartup;
48 52
49 namespace { 53 namespace {
50 54
51 // Stores the current mouse location on the screen. So that we can use it 55 // Stores the current mouse location on the screen. So that we can use it
52 // when firing keyboard and mouse click events. 56 // when firing keyboard and mouse click events.
53 NSPoint g_mouse_location = { 0, 0 }; 57 NSPoint g_mouse_location = { 0, 0 };
54 58
59 // Stores the current pressed mouse buttons. Indexed by
60 // ui_controls::MouseButton.
61 bool g_mouse_button_down[3] = {false, false, false};
62
55 bool g_ui_controls_enabled = false; 63 bool g_ui_controls_enabled = false;
56 64
57 // Creates the proper sequence of autoreleased key events for a key down + up. 65 // Creates the proper sequence of autoreleased key events for a key down + up.
58 void SynthesizeKeyEventsSequence(NSWindow* window, 66 void SynthesizeKeyEventsSequence(NSWindow* window,
59 ui::KeyboardCode keycode, 67 ui::KeyboardCode keycode,
60 bool control, 68 bool control,
61 bool shift, 69 bool shift,
62 bool alt, 70 bool alt,
63 bool command, 71 bool command,
64 std::vector<NSEvent*>* events) { 72 std::vector<NSEvent*>* events) {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 return window; 174 return window;
167 } 175 }
168 176
169 // Note that -[NSApplication orderedWindows] won't include NSPanels. If a test 177 // Note that -[NSApplication orderedWindows] won't include NSPanels. If a test
170 // uses those, it will need to handle that itself. 178 // uses those, it will need to handle that itself.
171 return nil; 179 return nil;
172 } 180 }
173 181
174 } // namespace 182 } // namespace
175 183
184 // Donates testing implementations of NSEvent methods.
185 @interface FakeNSEventTestingDonor : NSObject
186 @end
187
188 @implementation FakeNSEventTestingDonor
189 + (NSPoint)mouseLocation {
190 return g_mouse_location;
191 }
192
193 + (NSUInteger)pressedMouseButtons {
194 NSUInteger result = 0;
195 const int buttons[3] = {
196 ui_controls::LEFT, ui_controls::RIGHT, ui_controls::MIDDLE};
197 for (size_t i = 0; i < arraysize(buttons); ++i) {
198 if (g_mouse_button_down[buttons[i]])
199 result |= (1 << i);
200 }
201 return result;
202 }
203 @end
204
205 // Donates testing implementations of NSWindow methods.
206 @interface FakeNSWindowTestingDonor : NSObject
207 @end
208
209 @implementation FakeNSWindowTestingDonor
210 - (NSPoint)mouseLocationOutsideOfEventStream {
211 NSWindow* window = base::mac::ObjCCastStrict<NSWindow>(self);
212 return ui::ConvertPointFromWindowToScreen(window, g_mouse_location);
213 }
214 @end
215
216 namespace {
217
218 // Swizzles several Cocoa functions that are used to directly get mouse state,
219 // so that they will return the current simulated mouse position and pressed
220 // mouse buttons.
221 class NSEventSwizzler {
222 public:
223 static void Install() {
224 static NSEventSwizzler* swizzler = nullptr;
225 if (!swizzler) {
226 swizzler = new NSEventSwizzler();
227 }
228 }
229
230 protected:
231 NSEventSwizzler()
232 : mouse_location_swizzler_(new base::mac::ScopedObjCClassSwizzler(
233 [NSEvent class],
234 [FakeNSEventTestingDonor class],
235 @selector(mouseLocation))),
236 pressed_mouse_buttons_swizzler_(new base::mac::ScopedObjCClassSwizzler(
237 [NSEvent class],
238 [FakeNSEventTestingDonor class],
239 @selector(pressedMouseButtons))),
240 mouse_location_outside_of_event_stream_swizzler_(
241 new base::mac::ScopedObjCClassSwizzler(
242 [NSWindow class],
243 [FakeNSWindowTestingDonor class],
244 @selector(mouseLocationOutsideOfEventStream))) {}
245
246 private:
247 std::unique_ptr<base::mac::ScopedObjCClassSwizzler> mouse_location_swizzler_;
248 std::unique_ptr<base::mac::ScopedObjCClassSwizzler>
249 pressed_mouse_buttons_swizzler_;
250 std::unique_ptr<base::mac::ScopedObjCClassSwizzler>
251 mouse_location_outside_of_event_stream_swizzler_;
252
253 DISALLOW_COPY_AND_ASSIGN(NSEventSwizzler);
254 };
255
256 } // namespace
257
176 namespace ui_controls { 258 namespace ui_controls {
177 259
178 void EnableUIControls() { 260 void EnableUIControls() {
179 g_ui_controls_enabled = true; 261 g_ui_controls_enabled = true;
180 } 262 }
181 263
182 bool IsUIControlsEnabled() { 264 bool IsUIControlsEnabled() {
183 return g_ui_controls_enabled; 265 return g_ui_controls_enabled;
184 } 266 }
185 267
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 CHECK(g_ui_controls_enabled); 314 CHECK(g_ui_controls_enabled);
233 return SendMouseMoveNotifyWhenDone(x, y, base::Closure()); 315 return SendMouseMoveNotifyWhenDone(x, y, base::Closure());
234 } 316 }
235 317
236 // Input position is in screen coordinates. However, NSMouseMoved 318 // Input position is in screen coordinates. However, NSMouseMoved
237 // events require them window-relative, so we adjust. We *DO* flip 319 // events require them window-relative, so we adjust. We *DO* flip
238 // the coordinate space, so input events can be the same for all 320 // the coordinate space, so input events can be the same for all
239 // platforms. E.g. (0,0) is upper-left. 321 // platforms. E.g. (0,0) is upper-left.
240 bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& task) { 322 bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& task) {
241 CHECK(g_ui_controls_enabled); 323 CHECK(g_ui_controls_enabled);
242 CGFloat screenHeight = 324 g_mouse_location = gfx::ScreenPointToNSPoint(gfx::Point(x, y)); // flip!
243 [[[NSScreen screens] firstObject] frame].size.height; 325 NSEventSwizzler::Install();
244 g_mouse_location = NSMakePoint(x, screenHeight - y); // flip!
245 326
246 NSWindow* window = WindowAtCurrentMouseLocation(); 327 NSWindow* window = WindowAtCurrentMouseLocation();
247 328
248 NSPoint pointInWindow = g_mouse_location; 329 NSPoint pointInWindow = g_mouse_location;
249 if (window) 330 if (window)
250 pointInWindow = ui::ConvertPointFromScreenToWindow(window, pointInWindow); 331 pointInWindow = ui::ConvertPointFromScreenToWindow(window, pointInWindow);
251 NSTimeInterval timestamp = TimeIntervalSinceSystemStartup(); 332 NSTimeInterval timestamp = TimeIntervalSinceSystemStartup();
252 333
334 NSEventType event_type = NSMouseMoved;
335 if (g_mouse_button_down[LEFT]) {
336 event_type = NSLeftMouseDragged;
337 } else if (g_mouse_button_down[RIGHT]) {
338 event_type = NSRightMouseDragged;
339 } else if (g_mouse_button_down[MIDDLE]) {
340 event_type = NSOtherMouseDragged;
341 }
342
253 NSEvent* event = 343 NSEvent* event =
254 [NSEvent mouseEventWithType:NSMouseMoved 344 [NSEvent mouseEventWithType:event_type
255 location:pointInWindow 345 location:pointInWindow
256 modifierFlags:0 346 modifierFlags:0
257 timestamp:timestamp 347 timestamp:timestamp
258 windowNumber:[window windowNumber] 348 windowNumber:[window windowNumber]
259 context:nil 349 context:nil
260 eventNumber:0 350 eventNumber:0
261 clickCount:0 351 clickCount:event_type == NSMouseMoved ? 0 : 1
262 pressure:0.0]; 352 pressure:event_type == NSMouseMoved ? 0.0 : 1.0];
263 [[NSApplication sharedApplication] postEvent:event atStart:NO]; 353 [[NSApplication sharedApplication] postEvent:event atStart:NO];
264 354
265 if (!task.is_null()) { 355 if (!task.is_null()) {
266 base::MessageLoop::current()->PostTask( 356 base::MessageLoop::current()->PostTask(
267 FROM_HERE, base::Bind(&EventQueueWatcher, task)); 357 FROM_HERE, base::Bind(&EventQueueWatcher, task));
268 } 358 }
269 359
270 return true; 360 return true;
271 } 361 }
272 362
273 bool SendMouseEvents(MouseButton type, int state) { 363 bool SendMouseEvents(MouseButton type, int state) {
274 CHECK(g_ui_controls_enabled); 364 CHECK(g_ui_controls_enabled);
275 return SendMouseEventsNotifyWhenDone(type, state, base::Closure()); 365 return SendMouseEventsNotifyWhenDone(type, state, base::Closure());
276 } 366 }
277 367
278 bool SendMouseEventsNotifyWhenDone(MouseButton type, int state, 368 bool SendMouseEventsNotifyWhenDone(MouseButton type, int state,
279 const base::Closure& task) { 369 const base::Closure& task) {
280 CHECK(g_ui_controls_enabled); 370 CHECK(g_ui_controls_enabled);
281 // On windows it appears state can be (UP|DOWN). It is unclear if 371 // On windows it appears state can be (UP|DOWN). It is unclear if
282 // that'll happen here but prepare for it just in case. 372 // that'll happen here but prepare for it just in case.
283 if (state == (UP|DOWN)) { 373 if (state == (UP|DOWN)) {
284 return (SendMouseEventsNotifyWhenDone(type, DOWN, base::Closure()) && 374 return (SendMouseEventsNotifyWhenDone(type, DOWN, base::Closure()) &&
285 SendMouseEventsNotifyWhenDone(type, UP, task)); 375 SendMouseEventsNotifyWhenDone(type, UP, task));
286 } 376 }
287 NSEventType etype = NSLeftMouseDown; 377 NSEventType event_type = NSLeftMouseDown;
288 if (type == LEFT) { 378 if (type == LEFT) {
289 if (state == UP) { 379 if (state == UP) {
290 etype = NSLeftMouseUp; 380 event_type = NSLeftMouseUp;
291 } else { 381 } else {
292 etype = NSLeftMouseDown; 382 event_type = NSLeftMouseDown;
293 } 383 }
294 } else if (type == MIDDLE) { 384 } else if (type == MIDDLE) {
295 if (state == UP) { 385 if (state == UP) {
296 etype = NSOtherMouseUp; 386 event_type = NSOtherMouseUp;
297 } else { 387 } else {
298 etype = NSOtherMouseDown; 388 event_type = NSOtherMouseDown;
299 } 389 }
300 } else if (type == RIGHT) { 390 } else if (type == RIGHT) {
301 if (state == UP) { 391 if (state == UP) {
302 etype = NSRightMouseUp; 392 event_type = NSRightMouseUp;
303 } else { 393 } else {
304 etype = NSRightMouseDown; 394 event_type = NSRightMouseDown;
305 } 395 }
306 } else { 396 } else {
397 NOTREACHED();
307 return false; 398 return false;
308 } 399 }
400 g_mouse_button_down[type] = state == DOWN;
401
309 NSWindow* window = WindowAtCurrentMouseLocation(); 402 NSWindow* window = WindowAtCurrentMouseLocation();
310 NSPoint pointInWindow = g_mouse_location; 403 NSPoint pointInWindow = g_mouse_location;
311 if (window) 404 if (window)
312 pointInWindow = ui::ConvertPointFromScreenToWindow(window, pointInWindow); 405 pointInWindow = ui::ConvertPointFromScreenToWindow(window, pointInWindow);
313 406
407 NSEventSwizzler::Install();
408
314 NSEvent* event = 409 NSEvent* event =
315 [NSEvent mouseEventWithType:etype 410 [NSEvent mouseEventWithType:event_type
316 location:pointInWindow 411 location:pointInWindow
317 modifierFlags:0 412 modifierFlags:0
318 timestamp:TimeIntervalSinceSystemStartup() 413 timestamp:TimeIntervalSinceSystemStartup()
319 windowNumber:[window windowNumber] 414 windowNumber:[window windowNumber]
320 context:nil 415 context:nil
321 eventNumber:0 416 eventNumber:0
322 clickCount:1 417 clickCount:1
323 pressure:(state == DOWN ? 1.0 : 0.0 )]; 418 pressure:(state == DOWN ? 1.0 : 0.0 )];
tapted 2016/06/06 07:12:36 yeah, let's fix this too. I think even `git cl for
themblsha 2016/06/06 17:20:27 Done.
324 [[NSApplication sharedApplication] postEvent:event atStart:NO]; 419 [[NSApplication sharedApplication] postEvent:event atStart:NO];
325 420
326 if (!task.is_null()) { 421 if (!task.is_null()) {
327 base::MessageLoop::current()->PostTask( 422 base::MessageLoop::current()->PostTask(
328 FROM_HERE, base::Bind(&EventQueueWatcher, task)); 423 FROM_HERE, base::Bind(&EventQueueWatcher, task));
329 } 424 }
330 425
331 return true; 426 return true;
332 } 427 }
333 428
334 bool SendMouseClick(MouseButton type) { 429 bool SendMouseClick(MouseButton type) {
335 CHECK(g_ui_controls_enabled); 430 CHECK(g_ui_controls_enabled);
336 return SendMouseEventsNotifyWhenDone(type, UP|DOWN, base::Closure()); 431 return SendMouseEventsNotifyWhenDone(type, UP|DOWN, base::Closure());
337 } 432 }
338 433
339 void RunClosureAfterAllPendingUIEvents(const base::Closure& closure) { 434 void RunClosureAfterAllPendingUIEvents(const base::Closure& closure) {
340 base::MessageLoop::current()->PostTask( 435 base::MessageLoop::current()->PostTask(
341 FROM_HERE, base::Bind(&EventQueueWatcher, closure)); 436 FROM_HERE, base::Bind(&EventQueueWatcher, closure));
342 } 437 }
343 438
344 bool IsFullKeyboardAccessEnabled() { 439 bool IsFullKeyboardAccessEnabled() {
345 return [NSApp isFullKeyboardAccessEnabled]; 440 return [NSApp isFullKeyboardAccessEnabled];
346 } 441 }
347 442
348 } // namespace ui_controls 443 } // namespace ui_controls
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698