ui_controls.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2013 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_BASE_TEST_UI_CONTROLS_H_
  5. #define UI_BASE_TEST_UI_CONTROLS_H_
  6. #include "base/callback_forward.h"
  7. #include "build/build_config.h"
  8. #include "build/chromeos_buildflags.h"
  9. #include "ui/events/keycodes/keyboard_codes.h"
  10. #include "ui/gfx/native_widget_types.h"
  11. namespace ui_controls {
  12. // A set of utility functions to generate native events in platform
  13. // independent way. Note that since the implementations depend on a window being
  14. // top level, these can only be called from test suites that are not sharded.
  15. // For aura tests, please look into |aura::test:EventGenerator| first. This
  16. // class provides a way to emulate events in synchronous way and it is often
  17. // easier to write tests with this class than using |ui_controls|.
  18. //
  19. // Many of the functions in this class include a variant that takes a Closure.
  20. // The version that takes a Closure waits until the generated event is
  21. // processed. Once the generated event is processed the Closure is Run (and
  22. // deleted). Note that this is a somewhat fragile process in that any event of
  23. // the correct type (key down, mouse click, etc.) will trigger the Closure to be
  24. // run. Hence a usage such as
  25. //
  26. // SendKeyPress(...);
  27. // SendKeyPressNotifyWhenDone(..., task);
  28. //
  29. // might trigger |task| early.
  30. //
  31. // Note: Windows does not currently do anything with the |window| argument for
  32. // these functions, so passing NULL is ok.
  33. // Per the above comment, these methods can only be called from non-sharded test
  34. // suites. This method ensures that they're not accidently called by sharded
  35. // tests.
  36. void EnableUIControls();
  37. #if BUILDFLAG(IS_APPLE)
  38. bool IsUIControlsEnabled();
  39. #endif
  40. // Send a key press with/without modifier keys.
  41. //
  42. // If you're writing a test chances are you want the variant in ui_test_utils.
  43. // See it for details.
  44. bool SendKeyPress(gfx::NativeWindow window,
  45. ui::KeyboardCode key,
  46. bool control,
  47. bool shift,
  48. bool alt,
  49. bool command);
  50. bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
  51. ui::KeyboardCode key,
  52. bool control,
  53. bool shift,
  54. bool alt,
  55. bool command,
  56. base::OnceClosure task);
  57. // Simulate a mouse move.
  58. bool SendMouseMove(int screen_x, int screen_y);
  59. // Returns false on Windows if the desired position is not over a window
  60. // belonging to the current process.
  61. bool SendMouseMoveNotifyWhenDone(int screen_x,
  62. int screen_y,
  63. base::OnceClosure task);
  64. enum MouseButton {
  65. LEFT = 0,
  66. MIDDLE,
  67. RIGHT,
  68. };
  69. // Used to indicate the state of the button when generating events.
  70. enum MouseButtonState {
  71. UP = 1,
  72. DOWN = 2
  73. };
  74. // The keys that may be held down while generating a mouse event.
  75. enum AcceleratorState {
  76. kNoAccelerator = 0,
  77. kShift = 1 << 0,
  78. kControl = 1 << 1,
  79. kAlt = 1 << 2,
  80. kCommand = 1 << 3,
  81. };
  82. enum TouchType { PRESS = 1 << 0, RELEASE = 1 << 1, MOVE = 1 << 2 };
  83. // Sends a mouse down and/or up message with optional one or multiple
  84. // accelerator keys. The click will be sent to wherever the cursor
  85. // currently is, so be sure to move the cursor before calling this
  86. // (and be sure the cursor has arrived!).
  87. // |accelerator_state| is a bitmask of AcceleratorState.
  88. bool SendMouseEvents(MouseButton type,
  89. int button_state,
  90. int accelerator_state = kNoAccelerator);
  91. bool SendMouseEventsNotifyWhenDone(MouseButton type,
  92. int button_state,
  93. base::OnceClosure task,
  94. int accelerator_state = kNoAccelerator);
  95. // Same as SendMouseEvents with UP | DOWN.
  96. bool SendMouseClick(MouseButton type);
  97. #if BUILDFLAG(IS_WIN)
  98. // Send WM_POINTER messages to generate touch events. There is no way to detect
  99. // when events are received by chrome, it's up to users of this API to detect
  100. // when events arrive. |action| is a bitmask of the TouchType constants that
  101. // indicate what events are generated, |num| is the number of the touch
  102. // pointers, |screen_x| and |screen_y| are the screen coordinates of a touch
  103. // pointer.
  104. bool SendTouchEvents(int action, int num, int screen_x, int screen_y);
  105. #elif BUILDFLAG(IS_CHROMEOS)
  106. // Sends a TouchEvent to the window system. |action| is a bitmask of the
  107. // TouchType constants that indicates what events are generated, |id| identifies
  108. // the touch point.
  109. // TODO(mukai): consolidate this interface with the Windows SendTouchEvents.
  110. bool SendTouchEvents(int action, int id, int x, int y);
  111. bool SendTouchEventsNotifyWhenDone(int action,
  112. int id,
  113. int x,
  114. int y,
  115. base::OnceClosure task);
  116. #endif
  117. #if defined(USE_AURA)
  118. class UIControlsAura;
  119. void InstallUIControlsAura(UIControlsAura* instance);
  120. #endif
  121. #if BUILDFLAG(IS_APPLE)
  122. // Returns true when tests need to use extra Tab and Shift-Tab key events
  123. // to traverse to the desired item; because the application is configured to
  124. // traverse more elements for accessibility reasons.
  125. bool IsFullKeyboardAccessEnabled();
  126. #endif
  127. } // namespace ui_controls
  128. #endif // UI_BASE_TEST_UI_CONTROLS_H_