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
3 // found in the LICENSE file.
5 #import <Cocoa/Cocoa.h>
7 #include "ui/events/test/cocoa_test_event_utils.h"
9 namespace cocoa_test_event_utils {
11 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type,
12 NSUInteger modifiers) {
13 if (type == NSOtherMouseUp) {
14 // To synthesize middle clicks we need to create a CGEvent with the
15 // "center" button flags so that our resulting NSEvent will have the
16 // appropriate buttonNumber field. NSEvent provides no way to create a
17 // mouse event with a buttonNumber directly.
18 CGPoint location = { point.x, point.y };
19 CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp,
21 kCGMouseButtonCenter);
22 // Also specify the modifiers for the middle click case. This makes this
23 // test resilient to external modifiers being pressed.
24 CGEventSetFlags(cg_event, modifiers);
25 NSEvent* event = [NSEvent eventWithCGEvent:cg_event];
29 return [NSEvent mouseEventWithType:type
31 modifierFlags:modifiers
40 NSEvent* MouseEventWithType(NSEventType type, NSUInteger modifiers) {
41 return MouseEventAtPoint(NSZeroPoint, type, modifiers);
44 static NSEvent* MouseEventAtPointInWindow(NSPoint point,
47 NSUInteger clickCount) {
48 return [NSEvent mouseEventWithType:type
52 windowNumber:[window windowNumber]
59 NSEvent* RightMouseDownAtPointInWindow(NSPoint point, NSWindow* window) {
60 return MouseEventAtPointInWindow(point, NSRightMouseDown, window, 1);
63 NSEvent* RightMouseDownAtPoint(NSPoint point) {
64 return RightMouseDownAtPointInWindow(point, nil);
67 NSEvent* LeftMouseDownAtPointInWindow(NSPoint point, NSWindow* window) {
68 return MouseEventAtPointInWindow(point, NSLeftMouseDown, window, 1);
71 NSEvent* LeftMouseDownAtPoint(NSPoint point) {
72 return LeftMouseDownAtPointInWindow(point, nil);
75 std::pair<NSEvent*,NSEvent*> MouseClickInView(NSView* view,
76 NSUInteger clickCount) {
77 const NSRect bounds = [view convertRect:[view bounds] toView:nil];
78 const NSPoint mid_point = NSMakePoint(NSMidX(bounds), NSMidY(bounds));
79 NSEvent* down = MouseEventAtPointInWindow(mid_point, NSLeftMouseDown,
80 [view window], clickCount);
81 NSEvent* up = MouseEventAtPointInWindow(mid_point, NSLeftMouseUp,
82 [view window], clickCount);
83 return std::make_pair(down, up);
86 NSEvent* KeyEventWithCharacter(unichar c) {
87 return KeyEventWithKeyCode(0, c, NSKeyDown, 0);
90 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) {
91 return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers);
94 NSEvent* KeyEventWithKeyCode(unsigned short key_code,
96 NSEventType event_type,
97 NSUInteger modifiers) {
98 NSString* chars = [NSString stringWithCharacters:&c length:1];
99 return [NSEvent keyEventWithType:event_type
101 modifierFlags:modifiers
106 charactersIgnoringModifiers:chars
111 NSEvent* EnterExitEventWithType(NSEventType event_type) {
112 return [NSEvent enterExitEventWithType:event_type
123 NSEvent* OtherEventWithType(NSEventType event_type) {
124 return [NSEvent otherEventWithType:event_type
135 } // namespace cocoa_test_event_utils