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/mac/scoped_cftyperef.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 NSUInteger clickCount) { | 128 NSUInteger clickCount) { |
129 const NSRect bounds = [view convertRect:[view bounds] toView:nil]; | 129 const NSRect bounds = [view convertRect:[view bounds] toView:nil]; |
130 const NSPoint mid_point = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); | 130 const NSPoint mid_point = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); |
131 NSEvent* down = MouseEventAtPointInWindow(mid_point, NSRightMouseDown, | 131 NSEvent* down = MouseEventAtPointInWindow(mid_point, NSRightMouseDown, |
132 [view window], clickCount); | 132 [view window], clickCount); |
133 NSEvent* up = MouseEventAtPointInWindow(mid_point, NSRightMouseUp, | 133 NSEvent* up = MouseEventAtPointInWindow(mid_point, NSRightMouseUp, |
134 [view window], clickCount); | 134 [view window], clickCount); |
135 return std::make_pair(down, up); | 135 return std::make_pair(down, up); |
136 } | 136 } |
137 | 137 |
138 NSEvent* TestScrollEvent(NSPoint location, | 138 NSEvent* TestScrollEvent(NSPoint window_point, |
139 NSWindow* window, | 139 NSWindow* window, |
140 CGFloat delta_x, | 140 CGFloat delta_x, |
141 CGFloat delta_y) { | 141 CGFloat delta_y, |
| 142 bool has_precise_deltas, |
| 143 NSEventPhase event_phase, |
| 144 NSEventPhase momentum_phase) { |
142 const uint32_t wheel_count = 2; | 145 const uint32_t wheel_count = 2; |
143 int32_t wheel1 = static_cast<int>(delta_y); | 146 int32_t wheel1 = static_cast<int>(delta_y); |
144 int32_t wheel2 = static_cast<int>(delta_x); | 147 int32_t wheel2 = static_cast<int>(delta_x); |
145 CGScrollEventUnit units = kCGScrollEventUnitLine; | 148 CGScrollEventUnit units = |
| 149 has_precise_deltas ? kCGScrollEventUnitPixel : kCGScrollEventUnitLine; |
146 base::ScopedCFTypeRef<CGEventRef> scroll(CGEventCreateScrollWheelEvent( | 150 base::ScopedCFTypeRef<CGEventRef> scroll(CGEventCreateScrollWheelEvent( |
147 nullptr, units, wheel_count, wheel1, wheel2)); | 151 nullptr, units, wheel_count, wheel1, wheel2)); |
148 CGEventSetLocation(scroll, ScreenPointFromWindow(location, window)); | 152 CGEventSetLocation(scroll, ScreenPointFromWindow(window_point, window)); |
149 return AttachWindowToCGEvent(scroll, window); | 153 |
| 154 // Always set event flags, otherwise +[NSEvent eventWithCGEvent:] populates |
| 155 // flags from current keyboard state which can make tests flaky. |
| 156 CGEventSetFlags(scroll, 0); |
| 157 |
| 158 if (has_precise_deltas) { |
| 159 // kCGScrollWheelEventIsContinuous is -[NSEvent hasPreciseScrollingDeltas]. |
| 160 // CGEventTypes.h says it should be non-zero for pixel-based scrolling. |
| 161 // Verify that CGEventCreateScrollWheelEvent() set it. |
| 162 DCHECK_EQ(1, CGEventGetIntegerValueField(scroll, |
| 163 kCGScrollWheelEventIsContinuous)); |
| 164 } |
| 165 |
| 166 // Don't set phase information when neither. |
| 167 if (event_phase != NSEventPhaseNone || momentum_phase != NSEventPhaseNone) { |
| 168 // AppKit conflates CGScrollPhase (bitmask flags) and CGMomentumScrollPhase |
| 169 // (an enum) into NSEventPhase, where it is used for both -[NSEvent phase] |
| 170 // and -[NSEvent momentumPhase]. Do a reverse mapping here. |
| 171 int cg_event_phase = 0; |
| 172 if (event_phase & NSEventPhaseBegan) |
| 173 cg_event_phase |= kCGScrollPhaseBegan; |
| 174 if (event_phase & NSEventPhaseChanged) |
| 175 cg_event_phase |= kCGScrollPhaseChanged; |
| 176 if (event_phase & NSEventPhaseEnded) |
| 177 cg_event_phase |= kCGScrollPhaseEnded; |
| 178 if (event_phase & NSEventPhaseCancelled) |
| 179 cg_event_phase |= kCGScrollPhaseCancelled; |
| 180 if (event_phase & NSEventPhaseMayBegin) |
| 181 cg_event_phase |= kCGScrollPhaseMayBegin; |
| 182 |
| 183 CGMomentumScrollPhase cg_momentum_phase = kCGMomentumScrollPhaseNone; |
| 184 switch (momentum_phase) { |
| 185 case NSEventPhaseNone: |
| 186 break; |
| 187 case NSEventPhaseBegan: |
| 188 cg_momentum_phase = kCGMomentumScrollPhaseBegin; |
| 189 break; |
| 190 case NSEventPhaseChanged: |
| 191 cg_momentum_phase = kCGMomentumScrollPhaseContinue; |
| 192 break; |
| 193 case NSEventPhaseEnded: |
| 194 cg_momentum_phase = kCGMomentumScrollPhaseEnd; |
| 195 break; |
| 196 default: |
| 197 // Those are the only 4 options for CGMomentumScrollPhase. If something |
| 198 // else was provided it should probably never appear on an NSEvent. |
| 199 NOTREACHED(); |
| 200 } |
| 201 CGEventSetIntegerValueField(scroll, kCGScrollWheelEventScrollPhase, |
| 202 cg_event_phase); |
| 203 CGEventSetIntegerValueField(scroll, kCGScrollWheelEventMomentumPhase, |
| 204 cg_momentum_phase); |
| 205 } |
| 206 NSEvent* event = AttachWindowToCGEvent(scroll, window); |
| 207 DCHECK_EQ(has_precise_deltas, [event hasPreciseScrollingDeltas]); |
| 208 DCHECK_EQ(event_phase, [event phase]); |
| 209 DCHECK_EQ(momentum_phase, [event momentumPhase]); |
| 210 return event; |
150 } | 211 } |
151 | 212 |
152 NSEvent* KeyEventWithCharacter(unichar c) { | 213 NSEvent* KeyEventWithCharacter(unichar c) { |
153 return KeyEventWithKeyCode(0, c, NSKeyDown, 0); | 214 return KeyEventWithKeyCode(0, c, NSKeyDown, 0); |
154 } | 215 } |
155 | 216 |
156 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) { | 217 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) { |
157 return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers); | 218 return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers); |
158 } | 219 } |
159 | 220 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 context:nil | 353 context:nil |
293 characters:characters | 354 characters:characters |
294 charactersIgnoringModifiers:charactersIgnoringModifiers | 355 charactersIgnoringModifiers:charactersIgnoringModifiers |
295 isARepeat:NO | 356 isARepeat:NO |
296 keyCode:(unsigned short)macKeycode]; | 357 keyCode:(unsigned short)macKeycode]; |
297 | 358 |
298 return event; | 359 return event; |
299 } | 360 } |
300 | 361 |
301 } // namespace cocoa_test_event_utils | 362 } // namespace cocoa_test_event_utils |
OLD | NEW |