| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ui/events/test/cocoa_test_event_utils.h" | 5 #import "ui/events/test/cocoa_test_event_utils.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/mac/scoped_cftyperef.h" | |
| 10 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 11 #include "ui/events/base_event_utils.h" | 10 #include "ui/events/base_event_utils.h" |
| 12 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | 11 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
| 13 | 12 |
| 14 namespace cocoa_test_event_utils { | 13 namespace cocoa_test_event_utils { |
| 15 | 14 |
| 16 CGPoint ScreenPointFromWindow(NSPoint window_point, NSWindow* window) { | |
| 17 NSRect window_rect = NSMakeRect(window_point.x, window_point.y, 0, 0); | |
| 18 NSPoint screen_point = window | |
| 19 ? [window convertRectToScreen:window_rect].origin | |
| 20 : window_rect.origin; | |
| 21 CGFloat primary_screen_height = | |
| 22 NSHeight([[[NSScreen screens] firstObject] frame]); | |
| 23 screen_point.y = primary_screen_height - screen_point.y; | |
| 24 return NSPointToCGPoint(screen_point); | |
| 25 } | |
| 26 | |
| 27 NSEvent* AttachWindowToCGEvent(CGEventRef event, NSWindow* window) { | |
| 28 // These CGEventFields were made public in the 10.7 SDK, but don't help to | |
| 29 // populate the -[NSEvent window] pointer when creating an event with | |
| 30 // +[NSEvent eventWithCGEvent:]. Set that separately, using reflection. | |
| 31 CGEventSetIntegerValueField(event, kCGMouseEventWindowUnderMousePointer, | |
| 32 [window windowNumber]); | |
| 33 CGEventSetIntegerValueField( | |
| 34 event, kCGMouseEventWindowUnderMousePointerThatCanHandleThisEvent, | |
| 35 [window windowNumber]); | |
| 36 | |
| 37 // CGEventTimestamp is nanoseconds since system startup as a 64-bit integer. | |
| 38 // Use EventTimeForNow() so that it can be mocked for tests. | |
| 39 CGEventTimestamp timestamp = | |
| 40 (ui::EventTimeForNow() - base::TimeTicks()).InMicroseconds() * | |
| 41 base::Time::kNanosecondsPerMicrosecond; | |
| 42 CGEventSetTimestamp(event, timestamp); | |
| 43 | |
| 44 NSEvent* ns_event = [NSEvent eventWithCGEvent:event]; | |
| 45 DCHECK_EQ(nil, [ns_event window]); // Verify assumptions. | |
| 46 [ns_event setValue:window forKey:@"_window"]; | |
| 47 DCHECK_EQ(window, [ns_event window]); | |
| 48 | |
| 49 return ns_event; | |
| 50 } | |
| 51 | |
| 52 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type, | 15 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type, |
| 53 NSUInteger modifiers) { | 16 NSUInteger modifiers) { |
| 54 if (type == NSOtherMouseUp) { | 17 if (type == NSOtherMouseUp) { |
| 55 // To synthesize middle clicks we need to create a CGEvent with the | 18 // To synthesize middle clicks we need to create a CGEvent with the |
| 56 // "center" button flags so that our resulting NSEvent will have the | 19 // "center" button flags so that our resulting NSEvent will have the |
| 57 // appropriate buttonNumber field. NSEvent provides no way to create a | 20 // appropriate buttonNumber field. NSEvent provides no way to create a |
| 58 // mouse event with a buttonNumber directly. | 21 // mouse event with a buttonNumber directly. |
| 59 CGPoint location = { point.x, point.y }; | 22 CGPoint location = { point.x, point.y }; |
| 60 CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp, | 23 CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp, |
| 61 location, | 24 location, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 NSUInteger clickCount) { | 91 NSUInteger clickCount) { |
| 129 const NSRect bounds = [view convertRect:[view bounds] toView:nil]; | 92 const NSRect bounds = [view convertRect:[view bounds] toView:nil]; |
| 130 const NSPoint mid_point = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); | 93 const NSPoint mid_point = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); |
| 131 NSEvent* down = MouseEventAtPointInWindow(mid_point, NSRightMouseDown, | 94 NSEvent* down = MouseEventAtPointInWindow(mid_point, NSRightMouseDown, |
| 132 [view window], clickCount); | 95 [view window], clickCount); |
| 133 NSEvent* up = MouseEventAtPointInWindow(mid_point, NSRightMouseUp, | 96 NSEvent* up = MouseEventAtPointInWindow(mid_point, NSRightMouseUp, |
| 134 [view window], clickCount); | 97 [view window], clickCount); |
| 135 return std::make_pair(down, up); | 98 return std::make_pair(down, up); |
| 136 } | 99 } |
| 137 | 100 |
| 138 NSEvent* TestScrollEvent(NSPoint location, | |
| 139 NSWindow* window, | |
| 140 CGFloat delta_x, | |
| 141 CGFloat delta_y) { | |
| 142 const uint32_t wheel_count = 2; | |
| 143 int32_t wheel1 = static_cast<int>(delta_y); | |
| 144 int32_t wheel2 = static_cast<int>(delta_x); | |
| 145 CGScrollEventUnit units = kCGScrollEventUnitLine; | |
| 146 base::ScopedCFTypeRef<CGEventRef> scroll(CGEventCreateScrollWheelEvent( | |
| 147 nullptr, units, wheel_count, wheel1, wheel2)); | |
| 148 CGEventSetLocation(scroll, ScreenPointFromWindow(location, window)); | |
| 149 return AttachWindowToCGEvent(scroll, window); | |
| 150 } | |
| 151 | |
| 152 NSEvent* KeyEventWithCharacter(unichar c) { | 101 NSEvent* KeyEventWithCharacter(unichar c) { |
| 153 return KeyEventWithKeyCode(0, c, NSKeyDown, 0); | 102 return KeyEventWithKeyCode(0, c, NSKeyDown, 0); |
| 154 } | 103 } |
| 155 | 104 |
| 156 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) { | 105 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) { |
| 157 return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers); | 106 return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers); |
| 158 } | 107 } |
| 159 | 108 |
| 160 NSEvent* KeyEventWithKeyCode(unsigned short key_code, | 109 NSEvent* KeyEventWithKeyCode(unsigned short key_code, |
| 161 unichar c, | 110 unichar c, |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 context:nil | 241 context:nil |
| 293 characters:characters | 242 characters:characters |
| 294 charactersIgnoringModifiers:charactersIgnoringModifiers | 243 charactersIgnoringModifiers:charactersIgnoringModifiers |
| 295 isARepeat:NO | 244 isARepeat:NO |
| 296 keyCode:(unsigned short)macKeycode]; | 245 keyCode:(unsigned short)macKeycode]; |
| 297 | 246 |
| 298 return event; | 247 return event; |
| 299 } | 248 } |
| 300 | 249 |
| 301 } // namespace cocoa_test_event_utils | 250 } // namespace cocoa_test_event_utils |
| OLD | NEW |