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

Side by Side Diff: ui/events/test/cocoa_test_event_utils.mm

Issue 2484863004: Mac: Consolidate test mouse event generation into a single codepath.
Patch Set: Created 4 years, 1 month 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 | « ui/events/test/cocoa_test_event_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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #import "ui/events/test/cocoa_test_event_utils.h" 5 #import "ui/events/test/cocoa_test_event_utils.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/mac/scoped_cftyperef.h" 9 #include "base/mac/scoped_cftyperef.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "ui/events/base_event_utils.h" 11 #include "ui/events/base_event_utils.h"
12 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" 12 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
13 13
14 namespace cocoa_test_event_utils { 14 namespace cocoa_test_event_utils {
15 namespace {
16 CGEventType NSTypeToCGType(NSEventType mouse_type) {
17 switch (mouse_type) {
18 case NSLeftMouseDown:
19 return kCGEventLeftMouseDown;
20 case NSLeftMouseUp:
21 return kCGEventLeftMouseUp;
22 case NSRightMouseDown:
23 return kCGEventRightMouseDown;
24 case NSRightMouseUp:
25 return kCGEventRightMouseUp;
26 case NSMouseMoved:
27 return kCGEventMouseMoved;
28 case NSLeftMouseDragged:
29 return kCGEventLeftMouseDragged;
30 case NSRightMouseDragged:
31 return kCGEventRightMouseDragged;
32 case NSOtherMouseDown:
33 return kCGEventOtherMouseDown;
34 case NSOtherMouseUp:
35 return kCGEventOtherMouseUp;
36 case NSOtherMouseDragged:
37 return kCGEventOtherMouseDragged;
38 case NSMouseEntered:
39 case NSMouseExited:
40 NOTREACHED() << "AppKit generates these from MouseMove events.";
41 default:
42 NOTREACHED();
43 }
44 return kCGEventNull;
45 }
46 }
15 47
16 CGPoint ScreenPointFromWindow(NSPoint window_point, NSWindow* window) { 48 CGPoint ScreenPointFromWindow(NSPoint window_point, NSWindow* window) {
17 NSRect window_rect = NSMakeRect(window_point.x, window_point.y, 0, 0); 49 NSRect window_rect = NSMakeRect(window_point.x, window_point.y, 0, 0);
18 NSPoint screen_point = window 50 NSPoint screen_point = window
19 ? [window convertRectToScreen:window_rect].origin 51 ? [window convertRectToScreen:window_rect].origin
20 : window_rect.origin; 52 : window_rect.origin;
21 CGFloat primary_screen_height = 53 CGFloat primary_screen_height =
22 NSHeight([[[NSScreen screens] firstObject] frame]); 54 NSHeight([[[NSScreen screens] firstObject] frame]);
23 screen_point.y = primary_screen_height - screen_point.y; 55 screen_point.y = primary_screen_height - screen_point.y;
24 return NSPointToCGPoint(screen_point); 56 return NSPointToCGPoint(screen_point);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 NSEvent* ns_event = [NSEvent eventWithCGEvent:event]; 91 NSEvent* ns_event = [NSEvent eventWithCGEvent:event];
60 DCHECK_EQ(nil, [ns_event window]); // Verify assumptions. 92 DCHECK_EQ(nil, [ns_event window]); // Verify assumptions.
61 [ns_event setValue:window forKey:@"_window"]; 93 [ns_event setValue:window forKey:@"_window"];
62 DCHECK_EQ(window, [ns_event window]); 94 DCHECK_EQ(window, [ns_event window]);
63 95
64 return ns_event; 96 return ns_event;
65 } 97 }
66 98
67 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type, 99 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type,
68 NSUInteger modifiers) { 100 NSUInteger modifiers) {
69 if (type == NSOtherMouseUp) { 101 return TestMouseEvent(point, type, nil, 1, modifiers);
70 // To synthesize middle clicks we need to create a CGEvent with the
71 // "center" button flags so that our resulting NSEvent will have the
72 // appropriate buttonNumber field. NSEvent provides no way to create a
73 // mouse event with a buttonNumber directly.
74 CGPoint location = { point.x, point.y };
75 CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp,
76 location,
77 kCGMouseButtonCenter);
78 // Also specify the modifiers for the middle click case. This makes this
79 // test resilient to external modifiers being pressed.
80 CGEventSetFlags(cg_event, static_cast<CGEventFlags>(modifiers));
81 NSEvent* event = [NSEvent eventWithCGEvent:cg_event];
82 CFRelease(cg_event);
83 return event;
84 }
85 return [NSEvent mouseEventWithType:type
86 location:point
87 modifierFlags:modifiers
88 timestamp:TimeIntervalSinceSystemStartup()
89 windowNumber:0
90 context:nil
91 eventNumber:0
92 clickCount:1
93 pressure:1.0];
94 } 102 }
95 103
96 NSEvent* MouseEventWithType(NSEventType type, NSUInteger modifiers) { 104 NSEvent* MouseEventWithType(NSEventType type, NSUInteger modifiers) {
97 return MouseEventAtPoint(NSZeroPoint, type, modifiers); 105 return TestMouseEvent(NSZeroPoint, type, nil, modifiers, 1);
98 } 106 }
99 107
100 NSEvent* MouseEventAtPointInWindow(NSPoint point, 108 NSEvent* MouseEventAtPointInWindow(NSPoint point,
101 NSEventType type, 109 NSEventType type,
102 NSWindow* window, 110 NSWindow* window,
103 NSUInteger clickCount) { 111 NSUInteger clickCount) {
104 return [NSEvent mouseEventWithType:type 112 return TestMouseEvent(point, type, window, 0, clickCount);
105 location:point 113 }
106 modifierFlags:0 114
107 timestamp:TimeIntervalSinceSystemStartup() 115 NSEvent* TestMouseEvent(NSPoint window_point,
108 windowNumber:[window windowNumber] 116 NSEventType type,
109 context:nil 117 NSWindow* window,
110 eventNumber:0 118 NSUInteger modifiers,
111 clickCount:clickCount 119 NSUInteger clickCount) {
112 pressure:1.0]; 120 DCHECK_EQ(1u, clickCount);
121 CGEventType cg_type = NSTypeToCGType(type);
122 // CGEventCreateMouseEvent() ignores the CGMouseButton parameter unless
123 // |type| is one of kCGEventOtherMouse{Up,Down,Dragged}. It can be an
124 // integer up to 31. However, constants are only supplied up to 2. For now,
125 // just assume "other" means the third/center mouse button, and rely on
126 // Quartz ignoring it when the type is not "other".
127 CGMouseButton other_button = kCGMouseButtonCenter;
128 CGPoint screen_point = ScreenPointFromWindow(window_point, window);
129 base::ScopedCFTypeRef<CGEventRef> mouse(
130 CGEventCreateMouseEvent(nullptr, cg_type, screen_point, other_button));
131 CGEventSetFlags(mouse, modifiers);
132 return AttachWindowToCGEvent(mouse, window);
113 } 133 }
114 134
115 NSEvent* RightMouseDownAtPointInWindow(NSPoint point, NSWindow* window) { 135 NSEvent* RightMouseDownAtPointInWindow(NSPoint point, NSWindow* window) {
116 return MouseEventAtPointInWindow(point, NSRightMouseDown, window, 1); 136 return MouseEventAtPointInWindow(point, NSRightMouseDown, window, 1);
117 } 137 }
118 138
119 NSEvent* RightMouseDownAtPoint(NSPoint point) { 139 NSEvent* RightMouseDownAtPoint(NSPoint point) {
120 return RightMouseDownAtPointInWindow(point, nil); 140 return RightMouseDownAtPointInWindow(point, nil);
121 } 141 }
122 142
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 context:nil 404 context:nil
385 characters:characters 405 characters:characters
386 charactersIgnoringModifiers:charactersIgnoringModifiers 406 charactersIgnoringModifiers:charactersIgnoringModifiers
387 isARepeat:NO 407 isARepeat:NO
388 keyCode:(unsigned short)macKeycode]; 408 keyCode:(unsigned short)macKeycode];
389 409
390 return event; 410 return event;
391 } 411 }
392 412
393 } // namespace cocoa_test_event_utils 413 } // namespace cocoa_test_event_utils
OLDNEW
« no previous file with comments | « ui/events/test/cocoa_test_event_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698