| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/test/widget_event_generator.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #import "base/test/mock_chrome_application_mac.h" |
| 10 #import "ui/events/test/cocoa_test_event_utils.h" |
| 11 #include "ui/gfx/vector2d_conversions.h" |
| 12 #include "ui/views/widget/widget.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Singleton to provide state for swizzled Objective C methods. |
| 17 views::test::WidgetEventGeneratorMac* g_active_generator = NULL; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 @interface NSEventDonor : NSObject |
| 22 @end |
| 23 |
| 24 @implementation NSEventDonor |
| 25 |
| 26 // Donate +[NSEvent pressedMouseButtons] by retrieving the flags from the |
| 27 // active generator. |
| 28 + (NSUInteger)pressedMouseButtons { |
| 29 int flags = g_active_generator->flags(); |
| 30 NSUInteger bitmask = 0; |
| 31 if (flags & ui::EF_LEFT_MOUSE_BUTTON) |
| 32 bitmask |= 1; |
| 33 if (flags & ui::EF_RIGHT_MOUSE_BUTTON) |
| 34 bitmask |= 1 << 1; |
| 35 if (flags & ui::EF_MIDDLE_MOUSE_BUTTON) |
| 36 bitmask |= 1 << 2; |
| 37 return bitmask; |
| 38 } |
| 39 |
| 40 @end |
| 41 |
| 42 namespace views { |
| 43 namespace test { |
| 44 |
| 45 class WidgetEventGeneratorMac::Impl { |
| 46 public: |
| 47 Impl(); |
| 48 ~Impl() {} |
| 49 |
| 50 private: |
| 51 ScopedClassSwizzler swizzle_pressed_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(Impl); |
| 54 }; |
| 55 |
| 56 WidgetEventGeneratorMac::Impl::Impl() |
| 57 : swizzle_pressed_([NSEvent class], |
| 58 [NSEventDonor class], |
| 59 @selector(pressedMouseButtons)) { |
| 60 } |
| 61 |
| 62 namespace { |
| 63 |
| 64 NSPoint ConvertRootPointToTarget(NSWindow* target, |
| 65 const gfx::Point& point_in_root) { |
| 66 // Normally this would do [NSWindow convertScreenToBase:]. However, Cocoa can |
| 67 // reposition the window on screen and make things flaky. Initially, just |
| 68 // assume that the contentRect of |target| is at the top-left corner of the |
| 69 // screen. |
| 70 NSRect content_rect = [target contentRectForFrameRect:[target frame]]; |
| 71 return NSMakePoint(point_in_root.x(), |
| 72 NSHeight(content_rect) - point_in_root.y()); |
| 73 } |
| 74 |
| 75 // Inverse of ui::EventFlagsFromModifiers(). |
| 76 NSUInteger EventFlagsToModifiers(int flags) { |
| 77 NSUInteger modifiers = 0; |
| 78 modifiers |= (flags & ui::EF_CAPS_LOCK_DOWN) ? NSAlphaShiftKeyMask : 0; |
| 79 modifiers |= (flags & ui::EF_SHIFT_DOWN) ? NSShiftKeyMask : 0; |
| 80 modifiers |= (flags & ui::EF_CONTROL_DOWN) ? NSControlKeyMask : 0; |
| 81 modifiers |= (flags & ui::EF_ALT_DOWN) ? NSAlternateKeyMask : 0; |
| 82 modifiers |= (flags & ui::EF_COMMAND_DOWN) ? NSCommandKeyMask : 0; |
| 83 // ui::EF_*_MOUSE_BUTTON not handled here. |
| 84 // NSFunctionKeyMask, NSNumericPadKeyMask and NSHelpKeyMask not mapped. |
| 85 return modifiers; |
| 86 } |
| 87 |
| 88 // Picks the corresponding mouse event type for the buttons set in |flags|. |
| 89 NSEventType PickMouseEventType(int flags, |
| 90 NSEventType left, |
| 91 NSEventType right, |
| 92 NSEventType other) { |
| 93 if (flags & ui::EF_LEFT_MOUSE_BUTTON) |
| 94 return left; |
| 95 if (flags & ui::EF_RIGHT_MOUSE_BUTTON) |
| 96 return right; |
| 97 return other; |
| 98 } |
| 99 |
| 100 // Inverse of ui::EventTypeFromNative(). If non-null |modifiers| will be set |
| 101 // using the inverse of ui::EventFlagsFromNSEventWithModifiers(). |
| 102 NSEventType EventTypeToNative(ui::EventType ui_event_type, |
| 103 int flags, |
| 104 NSUInteger* modifiers) { |
| 105 if (modifiers) |
| 106 *modifiers = EventFlagsToModifiers(flags); |
| 107 switch (ui_event_type) { |
| 108 case ui::ET_UNKNOWN: |
| 109 return 0; |
| 110 case ui::ET_KEY_PRESSED: |
| 111 return NSKeyDown; |
| 112 case ui::ET_KEY_RELEASED: |
| 113 return NSKeyUp; |
| 114 case ui::ET_MOUSE_PRESSED: |
| 115 return PickMouseEventType(flags, |
| 116 NSLeftMouseDown, |
| 117 NSRightMouseDown, |
| 118 NSOtherMouseDown); |
| 119 case ui::ET_MOUSE_RELEASED: |
| 120 return PickMouseEventType(flags, |
| 121 NSLeftMouseUp, |
| 122 NSRightMouseUp, |
| 123 NSOtherMouseUp); |
| 124 case ui::ET_MOUSE_DRAGGED: |
| 125 return PickMouseEventType(flags, |
| 126 NSLeftMouseDragged, |
| 127 NSRightMouseDragged, |
| 128 NSOtherMouseDragged); |
| 129 case ui::ET_MOUSE_MOVED: |
| 130 return NSMouseMoved; |
| 131 case ui::ET_MOUSEWHEEL: |
| 132 return NSScrollWheel; |
| 133 case ui::ET_MOUSE_ENTERED: |
| 134 return NSMouseEntered; |
| 135 case ui::ET_MOUSE_EXITED: |
| 136 return NSMouseExited; |
| 137 case ui::ET_SCROLL_FLING_START: |
| 138 return NSEventTypeSwipe; |
| 139 default: |
| 140 NOTREACHED(); |
| 141 return 0; |
| 142 } |
| 143 } |
| 144 |
| 145 // Emulate the dispatching that would be performed by -[NSWindow sendEvent:]. |
| 146 // sendEvent is a black box which (among other things) will try to peek at the |
| 147 // event queue and can block indefinitely. |
| 148 void EmulateSendEvent(NSWindow* window, NSEvent* event) { |
| 149 NSResponder* responder = [window firstResponder]; |
| 150 switch ([event type]) { |
| 151 case NSKeyDown: |
| 152 [responder keyDown:event]; |
| 153 return; |
| 154 case NSKeyUp: |
| 155 [responder keyUp:event]; |
| 156 return; |
| 157 } |
| 158 |
| 159 // For mouse events, NSWindow will use -[NSView hitTest:] for the initial |
| 160 // mouseDown, and then keep track of the NSView returned. The toolkit-views |
| 161 // RootView does this too. So, for tests, assume tracking will be done there, |
| 162 // and the NSWindow's contentView is wrapping a views::internal::RootView. |
| 163 responder = [window contentView]; |
| 164 switch ([event type]) { |
| 165 case NSLeftMouseDown: |
| 166 [responder mouseDown:event]; |
| 167 break; |
| 168 case NSRightMouseDown: |
| 169 [responder rightMouseDown:event]; |
| 170 break; |
| 171 case NSOtherMouseDown: |
| 172 [responder otherMouseDown:event]; |
| 173 break; |
| 174 case NSLeftMouseUp: |
| 175 [responder mouseUp:event]; |
| 176 break; |
| 177 case NSRightMouseUp: |
| 178 [responder rightMouseUp:event]; |
| 179 break; |
| 180 case NSOtherMouseUp: |
| 181 [responder otherMouseUp:event]; |
| 182 break; |
| 183 case NSLeftMouseDragged: |
| 184 [responder mouseDragged:event]; |
| 185 break; |
| 186 case NSRightMouseDragged: |
| 187 [responder rightMouseDragged:event]; |
| 188 break; |
| 189 case NSOtherMouseDragged: |
| 190 [responder otherMouseDragged:event]; |
| 191 break; |
| 192 case NSMouseMoved: |
| 193 // Assumes [NSWindow acceptsMouseMovedEvents] would return YES, and that |
| 194 // NSTrackingAreas have been appropriately installed on |responder|. |
| 195 [responder mouseMoved:event]; |
| 196 break; |
| 197 case NSScrollWheel: |
| 198 [responder scrollWheel:event]; |
| 199 break; |
| 200 case NSMouseEntered: |
| 201 case NSMouseExited: |
| 202 // With the assumptions in NSMouseMoved, it doesn't make sense for the |
| 203 // generator to handle entered/exited separately. It's the responsibility |
| 204 // of views::internal::RootView to convert the moved events into entered |
| 205 // and exited events for the individual views. |
| 206 NOTREACHED(); |
| 207 break; |
| 208 case NSEventTypeSwipe: |
| 209 // NSEventTypeSwipe events can't be generated using public interfaces on |
| 210 // NSEvent, so this will need to be handled at a higher level. |
| 211 NOTREACHED(); |
| 212 break; |
| 213 default: |
| 214 NOTREACHED(); |
| 215 } |
| 216 } |
| 217 |
| 218 void DispatchMouseEventInWindow(NSWindow* window, |
| 219 ui::EventType event_type, |
| 220 const gfx::Point& point_in_root, |
| 221 int flags) { |
| 222 NSUInteger click_count = 0; |
| 223 if (event_type == ui::ET_MOUSE_PRESSED || |
| 224 event_type == ui::ET_MOUSE_RELEASED) { |
| 225 if (flags & ui::EF_IS_TRIPLE_CLICK) |
| 226 click_count = 3; |
| 227 else if (flags & ui::EF_IS_DOUBLE_CLICK) |
| 228 click_count = 2; |
| 229 else |
| 230 click_count = 1; |
| 231 } |
| 232 NSPoint point = ConvertRootPointToTarget(window, point_in_root); |
| 233 NSUInteger modifiers = 0; |
| 234 NSEventType type = EventTypeToNative(event_type, flags, &modifiers); |
| 235 NSEvent* event = [NSEvent mouseEventWithType:type |
| 236 location:point |
| 237 modifierFlags:modifiers |
| 238 timestamp:0 |
| 239 windowNumber:[window windowNumber] |
| 240 context:nil |
| 241 eventNumber:0 |
| 242 clickCount:click_count |
| 243 pressure:1.0]; |
| 244 |
| 245 // Typically events go through NSApplication. For tests, dispatch the event |
| 246 // directly to make things more predicatble. |
| 247 EmulateSendEvent(window, event); |
| 248 } |
| 249 |
| 250 } // namespace |
| 251 |
| 252 WidgetEventGenerator::WidgetEventGenerator(Widget* widget) |
| 253 : WidgetEventGeneratorMac(widget->GetNativeWindow()) { |
| 254 } |
| 255 |
| 256 WidgetEventGenerator::WidgetEventGenerator(gfx::NativeView context) |
| 257 : WidgetEventGeneratorMac([context window]) { |
| 258 } |
| 259 |
| 260 WidgetEventGenerator::WidgetEventGenerator( |
| 261 gfx::NativeView context, |
| 262 Widget* window_for_initial_location) |
| 263 : WidgetEventGeneratorMac([context window]) { |
| 264 set_current_location( |
| 265 window_for_initial_location->GetRestoredBounds().CenterPoint()); |
| 266 } |
| 267 |
| 268 WidgetEventGenerator::~WidgetEventGenerator() { |
| 269 } |
| 270 |
| 271 WidgetEventGeneratorMac::WidgetEventGeneratorMac(gfx::NativeWindow ns_window) |
| 272 : EventGeneratorBase(gfx::Point()), |
| 273 impl_(new Impl), |
| 274 ns_window_(ns_window) { |
| 275 DCHECK(!g_active_generator); |
| 276 g_active_generator = this; |
| 277 } |
| 278 |
| 279 WidgetEventGeneratorMac::~WidgetEventGeneratorMac() { |
| 280 DCHECK_EQ(this, g_active_generator); |
| 281 g_active_generator = NULL; |
| 282 } |
| 283 |
| 284 void WidgetEventGeneratorMac::GestureTapAt(const gfx::Point& point) { |
| 285 NOTIMPLEMENTED(); |
| 286 } |
| 287 |
| 288 void WidgetEventGeneratorMac::GestureScrollSequence( |
| 289 const gfx::Point& start, |
| 290 const gfx::Point& end, |
| 291 const base::TimeDelta& duration, |
| 292 int steps) { |
| 293 NOTIMPLEMENTED(); |
| 294 } |
| 295 |
| 296 void WidgetEventGeneratorMac::GestureMultiFingerScroll( |
| 297 int count, |
| 298 const gfx::Point start[], |
| 299 int event_separation_time_ms, |
| 300 int steps, |
| 301 int move_x, |
| 302 int move_y) { |
| 303 NOTIMPLEMENTED(); |
| 304 } |
| 305 |
| 306 gfx::Point WidgetEventGeneratorMac::GetLocationInCurrentRoot() const { |
| 307 // WidgetEventGeneratorMac currently assumes a single window positioned at the |
| 308 // screen origin to avoid flakiness when AppKit repositions the window (e.g. |
| 309 // depending where the Dock is positioned on screen). |
| 310 // TODO(tapted): Support multiple windows if tests need them. |
| 311 return current_location(); |
| 312 } |
| 313 |
| 314 void WidgetEventGeneratorMac::DoMoveMouseTo(const gfx::Point& point_in_screen, |
| 315 int count) { |
| 316 DCHECK_GT(count, 0); |
| 317 const ui::EventType event_type = (flags() & ui::EF_LEFT_MOUSE_BUTTON) ? |
| 318 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED; |
| 319 |
| 320 gfx::Vector2dF diff(point_in_screen - current_location()); |
| 321 for (float i = 1; i <= count; i++) { |
| 322 gfx::Vector2dF step(diff); |
| 323 step.Scale(i / count); |
| 324 gfx::Point move_point = current_location() + gfx::ToRoundedVector2d(step); |
| 325 // TOOD(tapted): Handle moving between windows. |
| 326 DispatchMouseEventInWindow(ns_window_, event_type, move_point, flags()); |
| 327 } |
| 328 set_current_location(point_in_screen); |
| 329 } |
| 330 |
| 331 void WidgetEventGeneratorMac::DispatchMouseEvent( |
| 332 ui::EventType type, |
| 333 const gfx::Point& location_in_root, |
| 334 int flags, |
| 335 int changed_button_flags) { |
| 336 DispatchMouseEventInWindow(ns_window_, |
| 337 type, |
| 338 location_in_root, |
| 339 changed_button_flags); |
| 340 } |
| 341 |
| 342 } // namespace views |
| 343 } // namespace test |
| OLD | NEW |