base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / events / test / cocoa_test_event_utils.mm
blob2694b84e1d552ef1dd5575a881a08dc11933c303
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,
20                                                   location,
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];
26     CFRelease(cg_event);
27     return event;
28   }
29   return [NSEvent mouseEventWithType:type
30                             location:point
31                        modifierFlags:modifiers
32                            timestamp:0
33                         windowNumber:0
34                              context:nil
35                          eventNumber:0
36                           clickCount:1
37                             pressure:1.0];
40 NSEvent* MouseEventWithType(NSEventType type, NSUInteger modifiers) {
41   return MouseEventAtPoint(NSZeroPoint, type, modifiers);
44 static NSEvent* MouseEventAtPointInWindow(NSPoint point,
45                                           NSEventType type,
46                                           NSWindow* window,
47                                           NSUInteger clickCount) {
48   return [NSEvent mouseEventWithType:type
49                             location:point
50                        modifierFlags:0
51                            timestamp:0
52                         windowNumber:[window windowNumber]
53                              context:nil
54                          eventNumber:0
55                           clickCount:clickCount
56                             pressure:1.0];
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,
95                              unichar c,
96                              NSEventType event_type,
97                              NSUInteger modifiers) {
98   NSString* chars = [NSString stringWithCharacters:&c length:1];
99   return [NSEvent keyEventWithType:event_type
100                           location:NSZeroPoint
101                      modifierFlags:modifiers
102                          timestamp:0
103                       windowNumber:0
104                            context:nil
105                         characters:chars
106        charactersIgnoringModifiers:chars
107                          isARepeat:NO
108                            keyCode:key_code];
111 NSEvent* EnterExitEventWithType(NSEventType event_type) {
112   return [NSEvent enterExitEventWithType:event_type
113                                 location:NSZeroPoint
114                            modifierFlags:0
115                                timestamp:0
116                             windowNumber:0
117                                  context:nil
118                              eventNumber:0
119                           trackingNumber:0
120                                 userData:NULL];
123 NSEvent* OtherEventWithType(NSEventType event_type) {
124   return [NSEvent otherEventWithType:event_type
125                             location:NSZeroPoint
126                        modifierFlags:0
127                            timestamp:0
128                         windowNumber:0
129                              context:nil
130                              subtype:0
131                                data1:0
132                                data2:0];
135 }  // namespace cocoa_test_event_utils