| 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 #include "ui/events/event_utils.h" | 5 #include "ui/events/event_utils.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/mac/scoped_cftyperef.h" | 9 #include "base/mac/scoped_cftyperef.h" |
| 10 #import "base/mac/scoped_objc_class_swizzler.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/events/event_constants.h" | 13 #include "ui/events/event_constants.h" |
| 13 #import "ui/events/test/cocoa_test_event_utils.h" | 14 #import "ui/events/test/cocoa_test_event_utils.h" |
| 14 #include "ui/gfx/point.h" | 15 #include "ui/gfx/point.h" |
| 15 #import "ui/gfx/test/ui_cocoa_test_helper.h" | 16 #import "ui/gfx/test/ui_cocoa_test_helper.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 NSWindow* g_test_window = nil; | 20 NSWindow* g_test_window = nil; |
| 20 | 21 |
| 21 } // namespace | 22 } // namespace |
| 22 | 23 |
| 23 // Mac APIs for creating test events are frustrating. Quartz APIs have, e.g., | 24 // Mac APIs for creating test events are frustrating. Quartz APIs have, e.g., |
| 24 // CGEventCreateMouseEvent() which can't set a window or modifier flags. | 25 // CGEventCreateMouseEvent() which can't set a window or modifier flags. |
| 25 // Cocoa APIs have +[NSEvent mouseEventWithType:..] which can't set | 26 // Cocoa APIs have +[NSEvent mouseEventWithType:..] which can't set |
| 26 // buttonNumber or scroll deltas. To work around this, these tests use some | 27 // buttonNumber or scroll deltas. To work around this, these tests use some |
| 27 // Objective C magic to donate member functions to NSEvent temporarily. | 28 // Objective C magic to donate member functions to NSEvent temporarily. |
| 28 @interface MiddleMouseButtonNumberDonor : NSObject | 29 @interface MiddleMouseButtonNumberDonor : NSObject |
| 29 @end | 30 @end |
| 30 | 31 |
| 31 @interface TestWindowDonor : NSObject | 32 @interface TestWindowDonor : NSObject |
| 32 @end | 33 @end |
| 33 | 34 |
| 34 @implementation MiddleMouseButtonNumberDonor | 35 @implementation MiddleMouseButtonNumberDonor |
| 35 - (NSUInteger)buttonNumber { return 2; } | 36 - (NSInteger)buttonNumber { return 2; } |
| 36 @end | 37 @end |
| 37 | 38 |
| 38 @implementation TestWindowDonor | 39 @implementation TestWindowDonor |
| 39 - (NSWindow*)window { return g_test_window; } | 40 - (NSWindow*)window { return g_test_window; } |
| 40 @end | 41 @end |
| 41 | 42 |
| 42 namespace ui { | 43 namespace ui { |
| 43 | 44 |
| 44 namespace { | 45 namespace { |
| 45 | 46 |
| 46 class EventsMacTest : public CocoaTest { | 47 class EventsMacTest : public CocoaTest { |
| 47 public: | 48 public: |
| 48 EventsMacTest() {} | 49 EventsMacTest() {} |
| 49 | 50 |
| 50 gfx::Point Flip(gfx::Point window_location) { | 51 gfx::Point Flip(gfx::Point window_location) { |
| 51 NSRect window_frame = [test_window() frame]; | 52 NSRect window_frame = [test_window() frame]; |
| 52 CGFloat content_height = | 53 CGFloat content_height = |
| 53 NSHeight([test_window() contentRectForFrameRect:window_frame]); | 54 NSHeight([test_window() contentRectForFrameRect:window_frame]); |
| 54 window_location.set_y(content_height - window_location.y()); | 55 window_location.set_y(content_height - window_location.y()); |
| 55 return window_location; | 56 return window_location; |
| 56 } | 57 } |
| 57 | 58 |
| 58 void SwizzleMiddleMouseButton() { | 59 void SwizzleMiddleMouseButton() { |
| 59 DCHECK(!swizzler_); | 60 DCHECK(!swizzler_); |
| 60 swizzler_.reset(new ScopedClassSwizzler( | 61 swizzler_.reset(new base::mac::ScopedObjCClassSwizzler( |
| 61 [NSEvent class], | 62 [NSEvent class], |
| 62 [MiddleMouseButtonNumberDonor class], | 63 [MiddleMouseButtonNumberDonor class], |
| 63 @selector(buttonNumber))); | 64 @selector(buttonNumber))); |
| 64 } | 65 } |
| 65 | 66 |
| 66 void SwizzleTestWindow() { | 67 void SwizzleTestWindow() { |
| 67 DCHECK(!g_test_window); | 68 DCHECK(!g_test_window); |
| 68 DCHECK(!swizzler_); | 69 DCHECK(!swizzler_); |
| 69 g_test_window = test_window(); | 70 g_test_window = test_window(); |
| 70 swizzler_.reset(new ScopedClassSwizzler( | 71 swizzler_.reset(new base::mac::ScopedObjCClassSwizzler( |
| 71 [NSEvent class], | 72 [NSEvent class], [TestWindowDonor class], @selector(window))); |
| 72 [TestWindowDonor class], | |
| 73 @selector(window))); | |
| 74 } | 73 } |
| 75 | 74 |
| 76 void ClearSwizzle() { | 75 void ClearSwizzle() { |
| 77 swizzler_.reset(); | 76 swizzler_.reset(); |
| 78 g_test_window = nil; | 77 g_test_window = nil; |
| 79 } | 78 } |
| 80 | 79 |
| 81 NSEvent* TestMouseEvent(NSEventType type, | 80 NSEvent* TestMouseEvent(NSEventType type, |
| 82 const gfx::Point &window_location, | 81 const gfx::Point &window_location, |
| 83 NSInteger modifier_flags) { | 82 NSInteger modifier_flags) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 110 NSPointFromCGPoint(Flip(window_location).ToCGPoint()); | 109 NSPointFromCGPoint(Flip(window_location).ToCGPoint()); |
| 111 NSPoint screen_point = [test_window() convertBaseToScreen:window_point]; | 110 NSPoint screen_point = [test_window() convertBaseToScreen:window_point]; |
| 112 CGFloat primary_screen_height = | 111 CGFloat primary_screen_height = |
| 113 NSHeight([[[NSScreen screens] objectAtIndex:0] frame]); | 112 NSHeight([[[NSScreen screens] objectAtIndex:0] frame]); |
| 114 screen_point.y = primary_screen_height - screen_point.y; | 113 screen_point.y = primary_screen_height - screen_point.y; |
| 115 CGEventSetLocation(scroll, NSPointToCGPoint(screen_point)); | 114 CGEventSetLocation(scroll, NSPointToCGPoint(screen_point)); |
| 116 return [NSEvent eventWithCGEvent:scroll]; | 115 return [NSEvent eventWithCGEvent:scroll]; |
| 117 } | 116 } |
| 118 | 117 |
| 119 private: | 118 private: |
| 120 scoped_ptr<ScopedClassSwizzler> swizzler_; | 119 scoped_ptr<base::mac::ScopedObjCClassSwizzler> swizzler_; |
| 121 | 120 |
| 122 DISALLOW_COPY_AND_ASSIGN(EventsMacTest); | 121 DISALLOW_COPY_AND_ASSIGN(EventsMacTest); |
| 123 }; | 122 }; |
| 124 | 123 |
| 125 } // namespace | 124 } // namespace |
| 126 | 125 |
| 127 TEST_F(EventsMacTest, EventFlagsFromNative) { | 126 TEST_F(EventsMacTest, EventFlagsFromNative) { |
| 128 // Left click. | 127 // Left click. |
| 129 NSEvent* left = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0); | 128 NSEvent* left = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0); |
| 130 EXPECT_EQ(EF_LEFT_MOUSE_BUTTON, EventFlagsFromNative(left)); | 129 EXPECT_EQ(EF_LEFT_MOUSE_BUTTON, EventFlagsFromNative(left)); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 event = cocoa_test_event_utils::MouseEventWithType(NSMouseMoved, 0); | 305 event = cocoa_test_event_utils::MouseEventWithType(NSMouseMoved, 0); |
| 307 EXPECT_EQ(ui::ET_MOUSE_MOVED, ui::EventTypeFromNative(event)); | 306 EXPECT_EQ(ui::ET_MOUSE_MOVED, ui::EventTypeFromNative(event)); |
| 308 | 307 |
| 309 event = cocoa_test_event_utils::EnterExitEventWithType(NSMouseEntered); | 308 event = cocoa_test_event_utils::EnterExitEventWithType(NSMouseEntered); |
| 310 EXPECT_EQ(ui::ET_MOUSE_ENTERED, ui::EventTypeFromNative(event)); | 309 EXPECT_EQ(ui::ET_MOUSE_ENTERED, ui::EventTypeFromNative(event)); |
| 311 event = cocoa_test_event_utils::EnterExitEventWithType(NSMouseExited); | 310 event = cocoa_test_event_utils::EnterExitEventWithType(NSMouseExited); |
| 312 EXPECT_EQ(ui::ET_MOUSE_EXITED, ui::EventTypeFromNative(event)); | 311 EXPECT_EQ(ui::ET_MOUSE_EXITED, ui::EventTypeFromNative(event)); |
| 313 } | 312 } |
| 314 | 313 |
| 315 } // namespace ui | 314 } // namespace ui |
| OLD | NEW |