| 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 <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 EventsMacTest() {} | 30 EventsMacTest() {} |
| 31 | 31 |
| 32 gfx::Point Flip(gfx::Point window_location) { | 32 gfx::Point Flip(gfx::Point window_location) { |
| 33 NSRect window_frame = [test_window() frame]; | 33 NSRect window_frame = [test_window() frame]; |
| 34 CGFloat content_height = | 34 CGFloat content_height = |
| 35 NSHeight([test_window() contentRectForFrameRect:window_frame]); | 35 NSHeight([test_window() contentRectForFrameRect:window_frame]); |
| 36 window_location.set_y(content_height - window_location.y()); | 36 window_location.set_y(content_height - window_location.y()); |
| 37 return window_location; | 37 return window_location; |
| 38 } | 38 } |
| 39 | 39 |
| 40 // TODO(tapted): Move this to cocoa_test_event_utils. It's not a drop-in | |
| 41 // replacement because -[NSApp sendEvent:] may route events generated this way | |
| 42 // differently. | |
| 43 NSEvent* TestMouseEvent(CGEventType type, | 40 NSEvent* TestMouseEvent(CGEventType type, |
| 44 const gfx::Point& window_location, | 41 const gfx::Point& window_location, |
| 45 CGEventFlags event_flags) { | 42 CGEventFlags event_flags) { |
| 46 // CGEventCreateMouseEvent() ignores the CGMouseButton parameter unless | 43 // CGEventCreateMouseEvent() ignores the CGMouseButton parameter unless |
| 47 // |type| is one of kCGEventOtherMouse{Up,Down,Dragged}. It can be an | 44 // |type| is one of kCGEventOtherMouse{Up,Down,Dragged}. It can be an |
| 48 // integer up to 31. However, constants are only supplied up to 2. For now, | 45 // integer up to 31. However, constants are only supplied up to 2. For now, |
| 49 // just assume "other" means the third/center mouse button, and rely on | 46 // just assume "other" means the third/center mouse button, and rely on |
| 50 // Quartz ignoring it when the type is not "other". | 47 // Quartz ignoring it when the type is not "other". |
| 51 CGMouseButton other_button = kCGMouseButtonCenter; | 48 CGMouseButton other_button = kCGMouseButtonCenter; |
| 52 CGPoint screen_point = cocoa_test_event_utils::ScreenPointFromWindow( | 49 base::ScopedCFTypeRef<CGEventRef> mouse(CGEventCreateMouseEvent( |
| 53 Flip(window_location).ToCGPoint(), test_window()); | 50 nullptr, type, TestWindowPointToScreen(window_location), other_button)); |
| 54 base::ScopedCFTypeRef<CGEventRef> mouse( | |
| 55 CGEventCreateMouseEvent(nullptr, type, screen_point, other_button)); | |
| 56 CGEventSetFlags(mouse, event_flags); | 51 CGEventSetFlags(mouse, event_flags); |
| 57 return cocoa_test_event_utils::AttachWindowToCGEvent(mouse, test_window()); | 52 return EventWithTestWindow(mouse); |
| 58 } | 53 } |
| 59 | 54 |
| 60 // Creates a scroll event from a "real" mouse wheel (i.e. not a trackpad). | |
| 61 NSEvent* TestScrollEvent(const gfx::Point& window_location, | 55 NSEvent* TestScrollEvent(const gfx::Point& window_location, |
| 62 int32_t delta_x, | 56 int32_t delta_x, |
| 63 int32_t delta_y) { | 57 int32_t delta_y) { |
| 64 return cocoa_test_event_utils::TestScrollEvent( | 58 base::ScopedCFTypeRef<CGEventRef> scroll(CGEventCreateScrollWheelEvent( |
| 65 Flip(window_location).ToCGPoint(), test_window(), delta_x, delta_y); | 59 nullptr, kCGScrollEventUnitLine, 2, delta_y, delta_x)); |
| 60 CGEventSetLocation(scroll, TestWindowPointToScreen(window_location)); |
| 61 return EventWithTestWindow(scroll); |
| 66 } | 62 } |
| 67 | 63 |
| 68 private: | 64 private: |
| 65 CGPoint TestWindowPointToScreen(const gfx::Point& window_location) { |
| 66 // CGEvents are always in global display coordinates. These are like screen |
| 67 // coordinates, but flipped. But first the point needs to be converted out |
| 68 // of window coordinates (which also requires flipping). |
| 69 NSPoint window_point = |
| 70 NSPointFromCGPoint(Flip(window_location).ToCGPoint()); |
| 71 NSRect window_rect = NSMakeRect(window_point.x, window_point.y, 0, 0); |
| 72 NSPoint screen_point = |
| 73 [test_window() convertRectToScreen:window_rect].origin; |
| 74 CGFloat primary_screen_height = |
| 75 NSHeight([[[NSScreen screens] firstObject] frame]); |
| 76 screen_point.y = primary_screen_height - screen_point.y; |
| 77 return NSPointToCGPoint(screen_point); |
| 78 } |
| 79 |
| 80 NSEvent* EventWithTestWindow(CGEventRef event) { |
| 81 // These CGEventFields were made public in the 10.7 SDK, but don't help to |
| 82 // populate the -[NSEvent window] pointer when creating an event with |
| 83 // +[NSEvent eventWithCGEvent:]. Set that separately, using reflection. |
| 84 CGEventSetIntegerValueField(event, kCGMouseEventWindowUnderMousePointer, |
| 85 [test_window() windowNumber]); |
| 86 CGEventSetIntegerValueField( |
| 87 event, kCGMouseEventWindowUnderMousePointerThatCanHandleThisEvent, |
| 88 [test_window() windowNumber]); |
| 89 NSEvent* ns_event = [NSEvent eventWithCGEvent:event]; |
| 90 EXPECT_EQ(nil, [ns_event window]); // Verify assumptions. |
| 91 [ns_event setValue:test_window() forKey:@"_window"]; |
| 92 EXPECT_EQ(test_window(), [ns_event window]); |
| 93 return ns_event; |
| 94 } |
| 95 |
| 69 DISALLOW_COPY_AND_ASSIGN(EventsMacTest); | 96 DISALLOW_COPY_AND_ASSIGN(EventsMacTest); |
| 70 }; | 97 }; |
| 71 | 98 |
| 72 } // namespace | 99 } // namespace |
| 73 | 100 |
| 74 TEST_F(EventsMacTest, EventFlagsFromNative) { | 101 TEST_F(EventsMacTest, EventFlagsFromNative) { |
| 75 // Left click. | 102 // Left click. |
| 76 NSEvent* left = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0); | 103 NSEvent* left = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0); |
| 77 EXPECT_EQ(EF_LEFT_MOUSE_BUTTON, EventFlagsFromNative(left)); | 104 EXPECT_EQ(EF_LEFT_MOUSE_BUTTON, EventFlagsFromNative(left)); |
| 78 | 105 |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 event = cocoa_test_event_utils::MouseEventWithType(NSMouseMoved, 0); | 282 event = cocoa_test_event_utils::MouseEventWithType(NSMouseMoved, 0); |
| 256 EXPECT_EQ(ui::ET_MOUSE_MOVED, ui::EventTypeFromNative(event)); | 283 EXPECT_EQ(ui::ET_MOUSE_MOVED, ui::EventTypeFromNative(event)); |
| 257 | 284 |
| 258 event = cocoa_test_event_utils::EnterEvent(); | 285 event = cocoa_test_event_utils::EnterEvent(); |
| 259 EXPECT_EQ(ui::ET_MOUSE_ENTERED, ui::EventTypeFromNative(event)); | 286 EXPECT_EQ(ui::ET_MOUSE_ENTERED, ui::EventTypeFromNative(event)); |
| 260 event = cocoa_test_event_utils::ExitEvent(); | 287 event = cocoa_test_event_utils::ExitEvent(); |
| 261 EXPECT_EQ(ui::ET_MOUSE_EXITED, ui::EventTypeFromNative(event)); | 288 EXPECT_EQ(ui::ET_MOUSE_EXITED, ui::EventTypeFromNative(event)); |
| 262 } | 289 } |
| 263 | 290 |
| 264 } // namespace ui | 291 } // namespace ui |
| OLD | NEW |