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.
5 #import <Cocoa/Cocoa.h>
7 #include "ui/base/test/cocoa_test_event_utils.h"
9 ScopedClassSwizzler::ScopedClassSwizzler(Class target, Class source,
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_);
16 ScopedClassSwizzler::~ScopedClassSwizzler() {
17 method_exchangeImplementations(old_selector_impl_, new_selector_impl_);
20 namespace cocoa_test_event_utils {
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,
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];
40 return [NSEvent mouseEventWithType:type
42 modifierFlags:modifiers
51 NSEvent* MouseEventWithType(NSEventType type, NSUInteger modifiers) {
52 return MouseEventAtPoint(NSZeroPoint, type, modifiers);
55 static NSEvent* MouseEventAtPointInWindow(NSPoint point,
58 NSUInteger clickCount) {
59 return [NSEvent mouseEventWithType:type
63 windowNumber:[window windowNumber]
70 NSEvent* LeftMouseDownAtPointInWindow(NSPoint point, NSWindow* window) {
71 return MouseEventAtPointInWindow(point, NSLeftMouseDown, window, 1);
74 NSEvent* LeftMouseDownAtPoint(NSPoint point) {
75 return LeftMouseDownAtPointInWindow(point, nil);
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);
89 NSEvent* KeyEventWithCharacter(unichar c) {
90 return KeyEventWithKeyCode(0, c, NSKeyDown, 0);
93 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) {
94 return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers);
97 NSEvent* KeyEventWithKeyCode(unsigned short key_code,
99 NSEventType event_type,
100 NSUInteger modifiers) {
101 NSString* chars = [NSString stringWithCharacters:&c length:1];
102 return [NSEvent keyEventWithType:event_type
104 modifierFlags:modifiers
109 charactersIgnoringModifiers:chars
114 NSEvent* EnterExitEventWithType(NSEventType event_type) {
115 return [NSEvent enterExitEventWithType:event_type
126 NSEvent* OtherEventWithType(NSEventType event_type) {
127 return [NSEvent otherEventWithType:event_type
138 } // namespace cocoa_test_event_utils