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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/events/test/cocoa_test_event_utils.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/events/test/cocoa_test_event_utils.mm
diff --git a/ui/events/test/cocoa_test_event_utils.mm b/ui/events/test/cocoa_test_event_utils.mm
index 9bd42df4ef83bdd4800800ca2f25ab94715dfb79..fe0d9d897177e1e7403fc5fad7d5bc8646d0f724 100644
--- a/ui/events/test/cocoa_test_event_utils.mm
+++ b/ui/events/test/cocoa_test_event_utils.mm
@@ -12,6 +12,38 @@
#import "ui/events/keycodes/keyboard_code_conversion_mac.h"
namespace cocoa_test_event_utils {
+namespace {
+CGEventType NSTypeToCGType(NSEventType mouse_type) {
+ switch (mouse_type) {
+ case NSLeftMouseDown:
+ return kCGEventLeftMouseDown;
+ case NSLeftMouseUp:
+ return kCGEventLeftMouseUp;
+ case NSRightMouseDown:
+ return kCGEventRightMouseDown;
+ case NSRightMouseUp:
+ return kCGEventRightMouseUp;
+ case NSMouseMoved:
+ return kCGEventMouseMoved;
+ case NSLeftMouseDragged:
+ return kCGEventLeftMouseDragged;
+ case NSRightMouseDragged:
+ return kCGEventRightMouseDragged;
+ case NSOtherMouseDown:
+ return kCGEventOtherMouseDown;
+ case NSOtherMouseUp:
+ return kCGEventOtherMouseUp;
+ case NSOtherMouseDragged:
+ return kCGEventOtherMouseDragged;
+ case NSMouseEntered:
+ case NSMouseExited:
+ NOTREACHED() << "AppKit generates these from MouseMove events.";
+ default:
+ NOTREACHED();
+ }
+ return kCGEventNull;
+}
+}
CGPoint ScreenPointFromWindow(NSPoint window_point, NSWindow* window) {
NSRect window_rect = NSMakeRect(window_point.x, window_point.y, 0, 0);
@@ -66,50 +98,38 @@ NSEvent* AttachWindowToCGEvent(CGEventRef event, NSWindow* window) {
NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type,
NSUInteger modifiers) {
- if (type == NSOtherMouseUp) {
- // To synthesize middle clicks we need to create a CGEvent with the
- // "center" button flags so that our resulting NSEvent will have the
- // appropriate buttonNumber field. NSEvent provides no way to create a
- // mouse event with a buttonNumber directly.
- CGPoint location = { point.x, point.y };
- CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp,
- location,
- kCGMouseButtonCenter);
- // Also specify the modifiers for the middle click case. This makes this
- // test resilient to external modifiers being pressed.
- CGEventSetFlags(cg_event, static_cast<CGEventFlags>(modifiers));
- NSEvent* event = [NSEvent eventWithCGEvent:cg_event];
- CFRelease(cg_event);
- return event;
- }
- return [NSEvent mouseEventWithType:type
- location:point
- modifierFlags:modifiers
- timestamp:TimeIntervalSinceSystemStartup()
- windowNumber:0
- context:nil
- eventNumber:0
- clickCount:1
- pressure:1.0];
+ return TestMouseEvent(point, type, nil, 1, modifiers);
}
NSEvent* MouseEventWithType(NSEventType type, NSUInteger modifiers) {
- return MouseEventAtPoint(NSZeroPoint, type, modifiers);
+ return TestMouseEvent(NSZeroPoint, type, nil, modifiers, 1);
}
NSEvent* MouseEventAtPointInWindow(NSPoint point,
NSEventType type,
NSWindow* window,
NSUInteger clickCount) {
- return [NSEvent mouseEventWithType:type
- location:point
- modifierFlags:0
- timestamp:TimeIntervalSinceSystemStartup()
- windowNumber:[window windowNumber]
- context:nil
- eventNumber:0
- clickCount:clickCount
- pressure:1.0];
+ return TestMouseEvent(point, type, window, 0, clickCount);
+}
+
+NSEvent* TestMouseEvent(NSPoint window_point,
+ NSEventType type,
+ NSWindow* window,
+ NSUInteger modifiers,
+ NSUInteger clickCount) {
+ DCHECK_EQ(1u, clickCount);
+ CGEventType cg_type = NSTypeToCGType(type);
+ // CGEventCreateMouseEvent() ignores the CGMouseButton parameter unless
+ // |type| is one of kCGEventOtherMouse{Up,Down,Dragged}. It can be an
+ // integer up to 31. However, constants are only supplied up to 2. For now,
+ // just assume "other" means the third/center mouse button, and rely on
+ // Quartz ignoring it when the type is not "other".
+ CGMouseButton other_button = kCGMouseButtonCenter;
+ CGPoint screen_point = ScreenPointFromWindow(window_point, window);
+ base::ScopedCFTypeRef<CGEventRef> mouse(
+ CGEventCreateMouseEvent(nullptr, cg_type, screen_point, other_button));
+ CGEventSetFlags(mouse, modifiers);
+ return AttachWindowToCGEvent(mouse, window);
}
NSEvent* RightMouseDownAtPointInWindow(NSPoint point, NSWindow* window) {
« 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