cocoa_test_event_utils.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef UI_EVENTS_TEST_COCOA_TEST_EVENT_UTILS_H_
  5. #define UI_EVENTS_TEST_COCOA_TEST_EVENT_UTILS_H_
  6. #import <Cocoa/Cocoa.h>
  7. #import <objc/objc-class.h>
  8. #include <utility>
  9. #include "ui/events/keycodes/dom/dom_key.h"
  10. #include "ui/events/keycodes/keyboard_codes.h"
  11. namespace cocoa_test_event_utils {
  12. // Converts |window_point| in |window| coordinates (origin at bottom left of
  13. // window frame) to a point in global display coordinates for a CGEvent (origin
  14. // at top left of primary screen).
  15. CGPoint ScreenPointFromWindow(NSPoint window_point, NSWindow* window);
  16. // Converts |event| to an NSEvent with a timestamp from ui::EventTimeForNow(),
  17. // and attaches |window| to it.
  18. NSEvent* AttachWindowToCGEvent(CGEventRef event, NSWindow* window);
  19. // Create synthetic mouse events for testing. Currently these are very
  20. // basic, flesh out as needed. Points are all in window coordinates;
  21. // where the window is not specified, coordinate system is undefined
  22. // (but will be repeated when the event is queried).
  23. NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type,
  24. NSUInteger modifiers);
  25. NSEvent* MouseEventWithType(NSEventType type, NSUInteger modifiers);
  26. NSEvent* MouseEventAtPointInWindow(NSPoint point,
  27. NSEventType type,
  28. NSWindow* window,
  29. NSUInteger clickCount);
  30. NSEvent* RightMouseDownAtPoint(NSPoint point);
  31. NSEvent* RightMouseDownAtPointInWindow(NSPoint point, NSWindow* window);
  32. NSEvent* LeftMouseDownAtPoint(NSPoint point);
  33. NSEvent* LeftMouseDownAtPointInWindow(NSPoint point, NSWindow* window);
  34. // Return a mouse down and an up event with the given |clickCount| at
  35. // |view|'s midpoint.
  36. std::pair<NSEvent*, NSEvent*> MouseClickInView(NSView* view,
  37. NSUInteger clickCount);
  38. // Return a right mouse down and an up event with the given |clickCount| at
  39. // |view|'s midpoint.
  40. std::pair<NSEvent*, NSEvent*> RightMouseClickInView(NSView* view,
  41. NSUInteger clickCount);
  42. // Creates a test scroll event. |has_precise_deltas| determines the value of
  43. // -[NSEvent hasPreciseScrollingDeltas] - usually NO for a mouse wheel and YES
  44. // for a trackpad. If |window| is nil, |location| is assumed to be AppKit screen
  45. // coordinates (origin in bottom left of primary screen).
  46. NSEvent* TestScrollEvent(NSPoint location,
  47. NSWindow* window,
  48. CGFloat delta_x,
  49. CGFloat delta_y,
  50. bool has_precise_deltas,
  51. NSEventPhase event_phase,
  52. NSEventPhase momentum_phase);
  53. // Returns a key-down event with isARepeat:YES.
  54. NSEvent* KeyDownEventWithRepeat();
  55. // Returns a key event with the given character.
  56. NSEvent* KeyEventWithCharacter(unichar c);
  57. // Returns a key event with the given type and modifier flags.
  58. NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers);
  59. // Returns a key event with the given key code, type, and modifier flags.
  60. NSEvent* KeyEventWithKeyCode(unsigned short key_code,
  61. unichar c,
  62. NSEventType event_type,
  63. NSUInteger modifiers);
  64. // Returns a key event for pressing or releasing a modifier key (aka
  65. // NSEventTypeFlagsChanged). For example |key_code| == kVK_Shift with
  66. // (|modifiers| & NSEventModifierFlagShift) != 0 means Shift is pressed and
  67. // |key_code| == kVK_Shift with (|modifiers| & NSEventModifierFlagShift) == 0
  68. // means Shift is released.
  69. NSEvent* KeyEventWithModifierOnly(unsigned short key_code,
  70. NSUInteger modifiers);
  71. // Returns a mouse enter event.
  72. NSEvent* EnterEvent(NSPoint point = NSZeroPoint, NSWindow* window = nil);
  73. // Returns a mouse exit event.
  74. NSEvent* ExitEvent(NSPoint point = NSZeroPoint, NSWindow* window = nil);
  75. // Return an "other" event with the given type.
  76. NSEvent* OtherEventWithType(NSEventType event_type);
  77. // Time interval since system startup. Tests shouldn't rely on this.
  78. NSTimeInterval TimeIntervalSinceSystemStartup();
  79. // Creates a key event in a particular window.
  80. NSEvent* SynthesizeKeyEvent(NSWindow* window,
  81. bool keyDown,
  82. ui::KeyboardCode keycode,
  83. NSUInteger flags,
  84. ui::DomKey dom_key = ui::DomKey::NONE);
  85. } // namespace cocoa_test_event_utils
  86. #endif // UI_EVENTS_TEST_COCOA_TEST_EVENT_UTILS_H_