Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / ui / base / test / cocoa_test_event_utils.mm
1 // Copyright (c) 2011 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.
4
5 #import <Cocoa/Cocoa.h>
6
7 #include "ui/base/test/cocoa_test_event_utils.h"
8
9 ScopedClassSwizzler::ScopedClassSwizzler(Class target, Class source,
10                                          SEL selector) {
11   old_selector_impl_ = class_getInstanceMethod(target, selector);
12   new_selector_impl_ = class_getInstanceMethod(source, selector);
13   method_exchangeImplementations(old_selector_impl_, new_selector_impl_);
14 }
15
16 ScopedClassSwizzler::~ScopedClassSwizzler() {
17   method_exchangeImplementations(old_selector_impl_, new_selector_impl_);
18 }
19
20 namespace cocoa_test_event_utils {
21
22 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type,
23                            NSUInteger modifiers) {
24   if (type == NSOtherMouseUp) {
25     // To synthesize middle clicks we need to create a CGEvent with the
26     // "center" button flags so that our resulting NSEvent will have the
27     // appropriate buttonNumber field. NSEvent provides no way to create a
28     // mouse event with a buttonNumber directly.
29     CGPoint location = { point.x, point.y };
30     CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp,
31                                                   location,
32                                                   kCGMouseButtonCenter);
33     // Also specify the modifiers for the middle click case. This makes this
34     // test resilient to external modifiers being pressed.
35     CGEventSetFlags(cg_event, modifiers);
36     NSEvent* event = [NSEvent eventWithCGEvent:cg_event];
37     CFRelease(cg_event);
38     return event;
39   }
40   return [NSEvent mouseEventWithType:type
41                             location:point
42                        modifierFlags:modifiers
43                            timestamp:0
44                         windowNumber:0
45                              context:nil
46                          eventNumber:0
47                           clickCount:1
48                             pressure:1.0];
49 }
50
51 NSEvent* MouseEventWithType(NSEventType type, NSUInteger modifiers) {
52   return MouseEventAtPoint(NSZeroPoint, type, modifiers);
53 }
54
55 static NSEvent* MouseEventAtPointInWindow(NSPoint point,
56                                           NSEventType type,
57                                           NSWindow* window,
58                                           NSUInteger clickCount) {
59   return [NSEvent mouseEventWithType:type
60                             location:point
61                        modifierFlags:0
62                            timestamp:0
63                         windowNumber:[window windowNumber]
64                              context:nil
65                          eventNumber:0
66                           clickCount:clickCount
67                             pressure:1.0];
68 }
69
70 NSEvent* LeftMouseDownAtPointInWindow(NSPoint point, NSWindow* window) {
71   return MouseEventAtPointInWindow(point, NSLeftMouseDown, window, 1);
72 }
73
74 NSEvent* LeftMouseDownAtPoint(NSPoint point) {
75   return LeftMouseDownAtPointInWindow(point, nil);
76 }
77
78 std::pair<NSEvent*,NSEvent*> MouseClickInView(NSView* view,
79                                               NSUInteger clickCount) {
80   const NSRect bounds = [view convertRect:[view bounds] toView:nil];
81   const NSPoint mid_point = NSMakePoint(NSMidX(bounds), NSMidY(bounds));
82   NSEvent* down = MouseEventAtPointInWindow(mid_point, NSLeftMouseDown,
83                                             [view window], clickCount);
84   NSEvent* up = MouseEventAtPointInWindow(mid_point, NSLeftMouseUp,
85                                           [view window], clickCount);
86   return std::make_pair(down, up);
87 }
88
89 NSEvent* KeyEventWithCharacter(unichar c) {
90   return KeyEventWithKeyCode(0, c, NSKeyDown, 0);
91 }
92
93 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) {
94   return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers);
95 }
96
97 NSEvent* KeyEventWithKeyCode(unsigned short key_code,
98                              unichar c,
99                              NSEventType event_type,
100                              NSUInteger modifiers) {
101   NSString* chars = [NSString stringWithCharacters:&c length:1];
102   return [NSEvent keyEventWithType:event_type
103                           location:NSZeroPoint
104                      modifierFlags:modifiers
105                          timestamp:0
106                       windowNumber:0
107                            context:nil
108                         characters:chars
109        charactersIgnoringModifiers:chars
110                          isARepeat:NO
111                            keyCode:key_code];
112 }
113
114 NSEvent* EnterExitEventWithType(NSEventType event_type) {
115   return [NSEvent enterExitEventWithType:event_type
116                                 location:NSZeroPoint
117                            modifierFlags:0
118                                timestamp:0
119                             windowNumber:0
120                                  context:nil
121                              eventNumber:0
122                           trackingNumber:0
123                                 userData:NULL];
124 }
125
126 NSEvent* OtherEventWithType(NSEventType event_type) {
127   return [NSEvent otherEventWithType:event_type
128                             location:NSZeroPoint
129                        modifierFlags:0
130                            timestamp:0
131                         windowNumber:0
132                              context:nil
133                              subtype:0
134                                data1:0
135                                data2:0];
136 }
137
138 }  // namespace cocoa_test_event_utils