Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
|
tapted
2016/06/28 05:07:41
CL description nit: start a new paragraph for "Thi
karandeepb
2016/06/28 12:07:49
Done.
| |
| 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 <mach/mach_time.h> | |
| 7 #include <stdint.h> | 6 #include <stdint.h> |
| 8 | 7 |
| 8 #include "base/time/time.h" | |
| 9 #include "ui/events/base_event_utils.h" | |
| 9 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | 10 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
| 10 #include "ui/events/test/cocoa_test_event_utils.h" | 11 #include "ui/events/test/cocoa_test_event_utils.h" |
|
tapted
2016/06/28 05:07:41
nit (while you're here), this should be before <Co
karandeepb
2016/06/28 12:07:49
Done.
| |
| 11 | 12 |
| 12 namespace cocoa_test_event_utils { | 13 namespace cocoa_test_event_utils { |
| 13 | 14 |
| 14 namespace { | |
| 15 | |
| 16 // From | |
| 17 // http://stackoverflow.com/questions/1597383/cgeventtimestamp-to-nsdate | |
| 18 // Which credits Apple sample code for this routine. | |
| 19 uint64_t UpTimeInNanoseconds(void) { | |
| 20 uint64_t time; | |
| 21 uint64_t timeNano; | |
| 22 static mach_timebase_info_data_t sTimebaseInfo; | |
| 23 | |
| 24 time = mach_absolute_time(); | |
| 25 | |
| 26 // Convert to nanoseconds. | |
| 27 | |
| 28 // If this is the first time we've run, get the timebase. | |
| 29 // We can use denom == 0 to indicate that sTimebaseInfo is | |
| 30 // uninitialised because it makes no sense to have a zero | |
| 31 // denominator is a fraction. | |
| 32 if (sTimebaseInfo.denom == 0) { | |
| 33 (void) mach_timebase_info(&sTimebaseInfo); | |
| 34 } | |
| 35 | |
| 36 // This could overflow; for testing needs we probably don't care. | |
| 37 timeNano = time * sTimebaseInfo.numer / sTimebaseInfo.denom; | |
| 38 return timeNano; | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type, | 15 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type, |
| 44 NSUInteger modifiers) { | 16 NSUInteger modifiers) { |
| 45 if (type == NSOtherMouseUp) { | 17 if (type == NSOtherMouseUp) { |
| 46 // To synthesize middle clicks we need to create a CGEvent with the | 18 // To synthesize middle clicks we need to create a CGEvent with the |
| 47 // "center" button flags so that our resulting NSEvent will have the | 19 // "center" button flags so that our resulting NSEvent will have the |
| 48 // appropriate buttonNumber field. NSEvent provides no way to create a | 20 // appropriate buttonNumber field. NSEvent provides no way to create a |
| 49 // mouse event with a buttonNumber directly. | 21 // mouse event with a buttonNumber directly. |
| 50 CGPoint location = { point.x, point.y }; | 22 CGPoint location = { point.x, point.y }; |
| 51 CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp, | 23 CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp, |
| 52 location, | 24 location, |
| 53 kCGMouseButtonCenter); | 25 kCGMouseButtonCenter); |
| 54 // Also specify the modifiers for the middle click case. This makes this | 26 // Also specify the modifiers for the middle click case. This makes this |
| 55 // test resilient to external modifiers being pressed. | 27 // test resilient to external modifiers being pressed. |
| 56 CGEventSetFlags(cg_event, static_cast<CGEventFlags>(modifiers)); | 28 CGEventSetFlags(cg_event, static_cast<CGEventFlags>(modifiers)); |
| 57 NSEvent* event = [NSEvent eventWithCGEvent:cg_event]; | 29 NSEvent* event = [NSEvent eventWithCGEvent:cg_event]; |
| 58 CFRelease(cg_event); | 30 CFRelease(cg_event); |
| 59 return event; | 31 return event; |
| 60 } | 32 } |
| 61 return [NSEvent mouseEventWithType:type | 33 return [NSEvent mouseEventWithType:type |
| 62 location:point | 34 location:point |
| 63 modifierFlags:modifiers | 35 modifierFlags:modifiers |
| 64 timestamp:0 | 36 timestamp:TimeIntervalSinceSystemStartup() |
| 65 windowNumber:0 | 37 windowNumber:0 |
| 66 context:nil | 38 context:nil |
| 67 eventNumber:0 | 39 eventNumber:0 |
| 68 clickCount:1 | 40 clickCount:1 |
| 69 pressure:1.0]; | 41 pressure:1.0]; |
| 70 } | 42 } |
| 71 | 43 |
| 72 NSEvent* MouseEventWithType(NSEventType type, NSUInteger modifiers) { | 44 NSEvent* MouseEventWithType(NSEventType type, NSUInteger modifiers) { |
| 73 return MouseEventAtPoint(NSZeroPoint, type, modifiers); | 45 return MouseEventAtPoint(NSZeroPoint, type, modifiers); |
| 74 } | 46 } |
| 75 | 47 |
| 76 NSEvent* MouseEventAtPointInWindow(NSPoint point, | 48 NSEvent* MouseEventAtPointInWindow(NSPoint point, |
| 77 NSEventType type, | 49 NSEventType type, |
| 78 NSWindow* window, | 50 NSWindow* window, |
| 79 NSUInteger clickCount) { | 51 NSUInteger clickCount) { |
| 80 return [NSEvent mouseEventWithType:type | 52 return [NSEvent mouseEventWithType:type |
| 81 location:point | 53 location:point |
| 82 modifierFlags:0 | 54 modifierFlags:0 |
| 83 timestamp:0 | 55 timestamp:TimeIntervalSinceSystemStartup() |
| 84 windowNumber:[window windowNumber] | 56 windowNumber:[window windowNumber] |
| 85 context:nil | 57 context:nil |
| 86 eventNumber:0 | 58 eventNumber:0 |
| 87 clickCount:clickCount | 59 clickCount:clickCount |
| 88 pressure:1.0]; | 60 pressure:1.0]; |
| 89 } | 61 } |
| 90 | 62 |
| 91 NSEvent* RightMouseDownAtPointInWindow(NSPoint point, NSWindow* window) { | 63 NSEvent* RightMouseDownAtPointInWindow(NSPoint point, NSWindow* window) { |
| 92 return MouseEventAtPointInWindow(point, NSRightMouseDown, window, 1); | 64 return MouseEventAtPointInWindow(point, NSRightMouseDown, window, 1); |
| 93 } | 65 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 } | 107 } |
| 136 | 108 |
| 137 NSEvent* KeyEventWithKeyCode(unsigned short key_code, | 109 NSEvent* KeyEventWithKeyCode(unsigned short key_code, |
| 138 unichar c, | 110 unichar c, |
| 139 NSEventType event_type, | 111 NSEventType event_type, |
| 140 NSUInteger modifiers) { | 112 NSUInteger modifiers) { |
| 141 NSString* chars = [NSString stringWithCharacters:&c length:1]; | 113 NSString* chars = [NSString stringWithCharacters:&c length:1]; |
| 142 return [NSEvent keyEventWithType:event_type | 114 return [NSEvent keyEventWithType:event_type |
| 143 location:NSZeroPoint | 115 location:NSZeroPoint |
| 144 modifierFlags:modifiers | 116 modifierFlags:modifiers |
| 145 timestamp:0 | 117 timestamp:TimeIntervalSinceSystemStartup() |
| 146 windowNumber:0 | 118 windowNumber:0 |
| 147 context:nil | 119 context:nil |
| 148 characters:chars | 120 characters:chars |
| 149 charactersIgnoringModifiers:chars | 121 charactersIgnoringModifiers:chars |
| 150 isARepeat:NO | 122 isARepeat:NO |
| 151 keyCode:key_code]; | 123 keyCode:key_code]; |
| 152 } | 124 } |
| 153 | 125 |
| 154 static NSEvent* EnterExitEventWithType(NSEventType event_type) { | 126 static NSEvent* EnterExitEventWithType(NSEventType event_type) { |
| 155 return [NSEvent enterExitEventWithType:event_type | 127 return [NSEvent enterExitEventWithType:event_type |
| 156 location:NSZeroPoint | 128 location:NSZeroPoint |
| 157 modifierFlags:0 | 129 modifierFlags:0 |
| 158 timestamp:0 | 130 timestamp:TimeIntervalSinceSystemStartup() |
| 159 windowNumber:0 | 131 windowNumber:0 |
| 160 context:nil | 132 context:nil |
| 161 eventNumber:0 | 133 eventNumber:0 |
| 162 trackingNumber:0 | 134 trackingNumber:0 |
| 163 userData:NULL]; | 135 userData:NULL]; |
| 164 } | 136 } |
| 165 | 137 |
| 166 NSEvent* EnterEvent() { | 138 NSEvent* EnterEvent() { |
| 167 return EnterExitEventWithType(NSMouseEntered); | 139 return EnterExitEventWithType(NSMouseEntered); |
| 168 } | 140 } |
| 169 | 141 |
| 170 NSEvent* ExitEvent() { | 142 NSEvent* ExitEvent() { |
| 171 return EnterExitEventWithType(NSMouseExited); | 143 return EnterExitEventWithType(NSMouseExited); |
| 172 } | 144 } |
| 173 | 145 |
| 174 NSEvent* OtherEventWithType(NSEventType event_type) { | 146 NSEvent* OtherEventWithType(NSEventType event_type) { |
| 175 return [NSEvent otherEventWithType:event_type | 147 return [NSEvent otherEventWithType:event_type |
| 176 location:NSZeroPoint | 148 location:NSZeroPoint |
| 177 modifierFlags:0 | 149 modifierFlags:0 |
| 178 timestamp:0 | 150 timestamp:TimeIntervalSinceSystemStartup() |
| 179 windowNumber:0 | 151 windowNumber:0 |
| 180 context:nil | 152 context:nil |
| 181 subtype:0 | 153 subtype:0 |
| 182 data1:0 | 154 data1:0 |
| 183 data2:0]; | 155 data2:0]; |
| 184 } | 156 } |
| 185 | 157 |
| 186 NSTimeInterval TimeIntervalSinceSystemStartup() { | 158 NSTimeInterval TimeIntervalSinceSystemStartup() { |
| 187 return UpTimeInNanoseconds() / 1000000000.0; | 159 base::TimeDelta time_elapsed = ui::EventTimeForNow() - base::TimeTicks(); |
| 160 return time_elapsed.InSeconds(); | |
|
tapted
2016/06/28 05:07:41
InSeconds -> InSecondsF
karandeepb
2016/06/28 12:07:49
Done.
| |
| 188 } | 161 } |
| 189 | 162 |
| 190 NSEvent* SynthesizeKeyEvent(NSWindow* window, | 163 NSEvent* SynthesizeKeyEvent(NSWindow* window, |
| 191 bool keyDown, | 164 bool keyDown, |
| 192 ui::KeyboardCode keycode, | 165 ui::KeyboardCode keycode, |
| 193 NSUInteger flags, | 166 NSUInteger flags, |
| 194 ui::DomKey dom_key) { | 167 ui::DomKey dom_key) { |
| 195 // If caps lock is set for an alpha keycode, treat it as if shift was pressed. | 168 // If caps lock is set for an alpha keycode, treat it as if shift was pressed. |
| 196 // Note on Mac (unlike other platforms) shift while caps is down does not go | 169 // Note on Mac (unlike other platforms) shift while caps is down does not go |
| 197 // back to lowercase. | 170 // back to lowercase. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 context:nil | 235 context:nil |
| 263 characters:characters | 236 characters:characters |
| 264 charactersIgnoringModifiers:charactersIgnoringModifiers | 237 charactersIgnoringModifiers:charactersIgnoringModifiers |
| 265 isARepeat:NO | 238 isARepeat:NO |
| 266 keyCode:(unsigned short)macKeycode]; | 239 keyCode:(unsigned short)macKeycode]; |
| 267 | 240 |
| 268 return event; | 241 return event; |
| 269 } | 242 } |
| 270 | 243 |
| 271 } // namespace cocoa_test_event_utils | 244 } // namespace cocoa_test_event_utils |
| OLD | NEW |