| 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" |
| 9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 10 #include "ui/events/base_event_utils.h" | 11 #include "ui/events/base_event_utils.h" |
| 11 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | 12 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
| 12 | 13 |
| 13 namespace cocoa_test_event_utils { | 14 namespace cocoa_test_event_utils { |
| 14 | 15 |
| 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 |
| 15 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type, | 52 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type, |
| 16 NSUInteger modifiers) { | 53 NSUInteger modifiers) { |
| 17 if (type == NSOtherMouseUp) { | 54 if (type == NSOtherMouseUp) { |
| 18 // To synthesize middle clicks we need to create a CGEvent with the | 55 // To synthesize middle clicks we need to create a CGEvent with the |
| 19 // "center" button flags so that our resulting NSEvent will have the | 56 // "center" button flags so that our resulting NSEvent will have the |
| 20 // appropriate buttonNumber field. NSEvent provides no way to create a | 57 // appropriate buttonNumber field. NSEvent provides no way to create a |
| 21 // mouse event with a buttonNumber directly. | 58 // mouse event with a buttonNumber directly. |
| 22 CGPoint location = { point.x, point.y }; | 59 CGPoint location = { point.x, point.y }; |
| 23 CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp, | 60 CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp, |
| 24 location, | 61 location, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 NSUInteger clickCount) { | 128 NSUInteger clickCount) { |
| 92 const NSRect bounds = [view convertRect:[view bounds] toView:nil]; | 129 const NSRect bounds = [view convertRect:[view bounds] toView:nil]; |
| 93 const NSPoint mid_point = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); | 130 const NSPoint mid_point = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); |
| 94 NSEvent* down = MouseEventAtPointInWindow(mid_point, NSRightMouseDown, | 131 NSEvent* down = MouseEventAtPointInWindow(mid_point, NSRightMouseDown, |
| 95 [view window], clickCount); | 132 [view window], clickCount); |
| 96 NSEvent* up = MouseEventAtPointInWindow(mid_point, NSRightMouseUp, | 133 NSEvent* up = MouseEventAtPointInWindow(mid_point, NSRightMouseUp, |
| 97 [view window], clickCount); | 134 [view window], clickCount); |
| 98 return std::make_pair(down, up); | 135 return std::make_pair(down, up); |
| 99 } | 136 } |
| 100 | 137 |
| 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 |
| 101 NSEvent* KeyEventWithCharacter(unichar c) { | 152 NSEvent* KeyEventWithCharacter(unichar c) { |
| 102 return KeyEventWithKeyCode(0, c, NSKeyDown, 0); | 153 return KeyEventWithKeyCode(0, c, NSKeyDown, 0); |
| 103 } | 154 } |
| 104 | 155 |
| 105 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) { | 156 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) { |
| 106 return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers); | 157 return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers); |
| 107 } | 158 } |
| 108 | 159 |
| 109 NSEvent* KeyEventWithKeyCode(unsigned short key_code, | 160 NSEvent* KeyEventWithKeyCode(unsigned short key_code, |
| 110 unichar c, | 161 unichar c, |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 context:nil | 292 context:nil |
| 242 characters:characters | 293 characters:characters |
| 243 charactersIgnoringModifiers:charactersIgnoringModifiers | 294 charactersIgnoringModifiers:charactersIgnoringModifiers |
| 244 isARepeat:NO | 295 isARepeat:NO |
| 245 keyCode:(unsigned short)macKeycode]; | 296 keyCode:(unsigned short)macKeycode]; |
| 246 | 297 |
| 247 return event; | 298 return event; |
| 248 } | 299 } |
| 249 | 300 |
| 250 } // namespace cocoa_test_event_utils | 301 } // namespace cocoa_test_event_utils |
| OLD | NEW |