Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: ui/events/test/cocoa_test_event_utils.mm

Issue 2486643002: Mac: Offset test scroll events when attaching them to an NSWindow. (Closed)
Patch Set: not needed Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/mac/scoped_cftyperef.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "ui/events/base_event_utils.h" 11 #include "ui/events/base_event_utils.h"
12 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" 12 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
13 13
14 namespace cocoa_test_event_utils { 14 namespace cocoa_test_event_utils {
15 15
16 CGPoint ScreenPointFromWindow(NSPoint window_point, NSWindow* window) { 16 CGPoint ScreenPointFromWindow(NSPoint window_point, NSWindow* window) {
17 NSRect window_rect = NSMakeRect(window_point.x, window_point.y, 0, 0); 17 NSRect window_rect = NSMakeRect(window_point.x, window_point.y, 0, 0);
18 NSPoint screen_point = window 18 NSPoint screen_point = window
19 ? [window convertRectToScreen:window_rect].origin 19 ? [window convertRectToScreen:window_rect].origin
20 : window_rect.origin; 20 : window_rect.origin;
21 CGFloat primary_screen_height = 21 CGFloat primary_screen_height =
22 NSHeight([[[NSScreen screens] firstObject] frame]); 22 NSHeight([[[NSScreen screens] firstObject] frame]);
23 screen_point.y = primary_screen_height - screen_point.y; 23 screen_point.y = primary_screen_height - screen_point.y;
24 return NSPointToCGPoint(screen_point); 24 return NSPointToCGPoint(screen_point);
25 } 25 }
26 26
27 NSEvent* AttachWindowToCGEvent(CGEventRef event, NSWindow* window) { 27 NSEvent* AttachWindowToCGEvent(CGEventRef event, NSWindow* window) {
28 // -[NSEvent locationInWindow] changes from screen coordinates to window
29 // coordinates when a window is attached to the mouse event. -[NSEvent
30 // eventWithCGEvent:] handles the Quartz -> AppKit coordinate flipping, but
31 // not the offset. Unfortunately -eventWithCGEvent: uses the *screen* height
32 // to flip, not the window height (it doesn't know about the window yet). So
33 // to get the correct -[NSEvent locationInWindow], anticipate the bogus screen
34 // flip that eventWithCGEvent: will do. This is yuck, but NSEvent does not
35 // provide a way to generate test scrolling events any other way. Fortunately,
36 // once you do all the algebra, all we need to do here is offset by the window
37 // origin, but in different directions for x/y.
Avi (use Gerrit) 2016/11/08 16:20:47 o_O Is there a bug filed? This is surprising. (Th
tapted 2016/11/08 23:05:52 No bug.. It's annoying, but I think it's reasonabl
tapted 2016/11/08 23:54:20 Filed http://crbug.com/663553 that collects my cur
38 CGPoint location = CGEventGetLocation(event);
39 location.y += NSMinY([window frame]);
40 location.x -= NSMinX([window frame]);
41 CGEventSetLocation(event, location);
42
28 // These CGEventFields were made public in the 10.7 SDK, but don't help to 43 // 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 44 // populate the -[NSEvent window] pointer when creating an event with
30 // +[NSEvent eventWithCGEvent:]. Set that separately, using reflection. 45 // +[NSEvent eventWithCGEvent:]. Set that separately, using reflection.
31 CGEventSetIntegerValueField(event, kCGMouseEventWindowUnderMousePointer, 46 CGEventSetIntegerValueField(event, kCGMouseEventWindowUnderMousePointer,
32 [window windowNumber]); 47 [window windowNumber]);
33 CGEventSetIntegerValueField( 48 CGEventSetIntegerValueField(
34 event, kCGMouseEventWindowUnderMousePointerThatCanHandleThisEvent, 49 event, kCGMouseEventWindowUnderMousePointerThatCanHandleThisEvent,
35 [window windowNumber]); 50 [window windowNumber]);
36 51
37 // CGEventTimestamp is nanoseconds since system startup as a 64-bit integer. 52 // CGEventTimestamp is nanoseconds since system startup as a 64-bit integer.
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 } 215 }
201 CGEventSetIntegerValueField(scroll, kCGScrollWheelEventScrollPhase, 216 CGEventSetIntegerValueField(scroll, kCGScrollWheelEventScrollPhase,
202 cg_event_phase); 217 cg_event_phase);
203 CGEventSetIntegerValueField(scroll, kCGScrollWheelEventMomentumPhase, 218 CGEventSetIntegerValueField(scroll, kCGScrollWheelEventMomentumPhase,
204 cg_momentum_phase); 219 cg_momentum_phase);
205 } 220 }
206 NSEvent* event = AttachWindowToCGEvent(scroll, window); 221 NSEvent* event = AttachWindowToCGEvent(scroll, window);
207 DCHECK_EQ(has_precise_deltas, [event hasPreciseScrollingDeltas]); 222 DCHECK_EQ(has_precise_deltas, [event hasPreciseScrollingDeltas]);
208 DCHECK_EQ(event_phase, [event phase]); 223 DCHECK_EQ(event_phase, [event phase]);
209 DCHECK_EQ(momentum_phase, [event momentumPhase]); 224 DCHECK_EQ(momentum_phase, [event momentumPhase]);
225 DCHECK_EQ(window_point.x, [event locationInWindow].x);
226 DCHECK_EQ(window_point.y, [event locationInWindow].y);
210 return event; 227 return event;
211 } 228 }
212 229
213 NSEvent* KeyEventWithCharacter(unichar c) { 230 NSEvent* KeyEventWithCharacter(unichar c) {
214 return KeyEventWithKeyCode(0, c, NSKeyDown, 0); 231 return KeyEventWithKeyCode(0, c, NSKeyDown, 0);
215 } 232 }
216 233
217 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) { 234 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) {
218 return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers); 235 return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers);
219 } 236 }
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 context:nil 384 context:nil
368 characters:characters 385 characters:characters
369 charactersIgnoringModifiers:charactersIgnoringModifiers 386 charactersIgnoringModifiers:charactersIgnoringModifiers
370 isARepeat:NO 387 isARepeat:NO
371 keyCode:(unsigned short)macKeycode]; 388 keyCode:(unsigned short)macKeycode];
372 389
373 return event; 390 return event;
374 } 391 }
375 392
376 } // namespace cocoa_test_event_utils 393 } // namespace cocoa_test_event_utils
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698