1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_BASE_TEST_UI_CONTROLS_H_
#define UI_BASE_TEST_UI_CONTROLS_H_
#include <cstdint>
#include "base/functional/callback_forward.h"
#include "build/build_config.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/native_widget_types.h"
namespace ui_controls {
enum KeyEventType { kKeyPress = 1 << 0, kKeyRelease = 1 << 1 };
// A set of utility functions to generate native events in platform
// independent way. Note that since the implementations depend on a window being
// top level, these can only be called from test suites that are not sharded.
// For aura tests, please look into |aura::test:EventGenerator| first. This
// class provides a way to emulate events in synchronous way and it is often
// easier to write tests with this class than using |ui_controls|.
//
// Many of the functions in this class include a variant that takes a Closure.
// The version that takes a Closure waits until the generated event is
// processed. Once the generated event is processed the Closure is Run (and
// deleted). Note that this is a somewhat fragile process in that any event of
// the correct type (key down, mouse click, etc.) will trigger the Closure to be
// run. Hence a usage such as
//
// SendKeyPress(...);
// SendKeyPressNotifyWhenDone(..., task);
//
// might trigger |task| early.
//
// Note: Windows does not currently do anything with the |window| argument for
// these functions, so passing NULL is ok.
// Per the above comment, these methods can only be called from non-sharded test
// suites. This method ensures that they're not accidently called by sharded
// tests.
void EnableUIControls();
// Reset the state in ui controls logic that are updated by the test to the
// initial state.
void ResetUIControlsIfEnabled();
#if BUILDFLAG(IS_APPLE)
bool IsUIControlsEnabled();
#endif
// Generates keyboard accelerator state in bitmap from each key boolean.
int GenerateAcceleratorState(bool control, bool shift, bool alt, bool command);
// Send a key press with/without modifier keys. This will trigger a key release
// event after the key press.
//
// If you're writing a test chances are you want the variant in ui_test_utils.
// See it for details.
bool SendKeyPress(gfx::NativeWindow window,
ui::KeyboardCode key,
bool control,
bool shift,
bool alt,
bool command);
bool SendKeyPressNotifyWhenDone(
gfx::NativeWindow window,
ui::KeyboardCode key,
bool control,
bool shift,
bool alt,
bool command,
base::OnceClosure task,
KeyEventType wait_for = KeyEventType::kKeyRelease);
// The keys that may be held down while generating a keyboard/mouse event.
enum AcceleratorState {
kNoAccelerator = 0,
kShift = 1 << 0,
kControl = 1 << 1,
kAlt = 1 << 2,
kCommand = 1 << 3,
};
// Not supported on win.
// TODO(crbug.com/40256300): Support this on win.
#if !BUILDFLAG(IS_WIN)
// Sends a key press and/or release message with/without modifier keys.
// `key_event_types` is a bitmask of KeyEventType constants that indicates what
// events are generated.
bool SendKeyEvents(gfx::NativeWindow window,
ui::KeyboardCode key,
int key_event_types,
int accelerator_state = kNoAccelerator);
bool SendKeyEventsNotifyWhenDone(gfx::NativeWindow window,
ui::KeyboardCode key,
int key_event_types,
base::OnceClosure task,
int accelerator_state = kNoAccelerator);
#endif // !BUILDFLAG(IS_WIN)
// A default value for a window hint specifies that no window hint is given and
// an appropriate target window should be deduced from the target or current
// mouse position.
// Simulate a mouse move.
//
// The `window_hint` - if specified - helps the method correctly target the
// appropriate window on platforms where mouse events must be explicitly
// targeted.
//
// NOTE: On Mac, hover events are not delivered reliably to windows. To combat
// this, if you specify a window hint for a move with no buttons down, the hover
// events will be sent directly to the window. This may, unfortunately, bypass
// other observers, so if you are expecting an event observer to pick up the
// move rather than a window, do not specify a hint.
//
// Returns false on Windows if the desired position is not over a window
// belonging to the current process.
bool SendMouseMove(int screen_x,
int screen_y,
gfx::NativeWindow window_hint = gfx::NativeWindow());
bool SendMouseMoveNotifyWhenDone(
int screen_x,
int screen_y,
base::OnceClosure task,
gfx::NativeWindow window_hint = gfx::NativeWindow());
enum MouseButton {
LEFT = 0,
MIDDLE,
RIGHT,
};
// Used to indicate the state of the button when generating events.
enum MouseButtonState {
UP = 1,
DOWN = 2
};
enum TouchType {
kTouchPress = 1 << 0,
kTouchRelease = 1 << 1,
kTouchMove = 1 << 2,
};
// Sends a mouse down and/or up message with optional one or multiple
// accelerator keys. The click will be sent to wherever the cursor
// currently is, so be sure to move the cursor before calling this
// (and be sure the cursor has arrived!).
// |accelerator_state| is a bitmask of AcceleratorState.
//
// The `window_hint` - if specified - helps the method correctly target the
// appropriate window on platforms where mouse events must be explicitly
// targeted.
bool SendMouseEvents(MouseButton type,
int button_state,
int accelerator_state = kNoAccelerator,
gfx::NativeWindow window_hint = gfx::NativeWindow());
bool SendMouseEventsNotifyWhenDone(
MouseButton type,
int button_state,
base::OnceClosure task,
int accelerator_state = kNoAccelerator,
gfx::NativeWindow window_hint = gfx::NativeWindow());
// Same as SendMouseEvents with UP | DOWN.
bool SendMouseClick(MouseButton type,
gfx::NativeWindow window_hint = gfx::NativeWindow());
#if BUILDFLAG(IS_WIN)
// Send WM_POINTER messages to generate touch events. There is no way to detect
// when events are received by chrome, it's up to users of this API to detect
// when events arrive. |action| is a bitmask of the TouchType constants that
// indicate what events are generated, |num| is the number of the touch
// pointers, |screen_x| and |screen_y| are the screen coordinates of a touch
// pointer.
bool SendTouchEvents(int action, int num, int screen_x, int screen_y);
#elif BUILDFLAG(IS_CHROMEOS)
// Sends a TouchEvent to the window system. |action| is a bitmask of the
// TouchType constants that indicates what events are generated, |id| identifies
// the touch point.
// TODO(mukai): consolidate this interface with the Windows SendTouchEvents.
bool SendTouchEvents(int action, int id, int x, int y);
bool SendTouchEventsNotifyWhenDone(int action,
int id,
int x,
int y,
base::OnceClosure task);
#endif
#if BUILDFLAG(IS_LINUX)
// Forces the platform implementation to use screen coordinates, even if they're
// not really available, the next time that ui_controls::SendMouseMove() or
// ui_controls::SendMouseMoveNotifyWhenDone() is called, or some other method
// using these methods internally, e.g. ui_test_utils::SendMouseMoveSync(). All
// following calls will behave normally (unless this method is called again).
void ForceUseScreenCoordinatesOnce();
#endif
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)
class UIControlsAura;
void InstallUIControlsAura(UIControlsAura* instance);
#endif
#if BUILDFLAG(IS_APPLE)
// Returns true when tests need to use extra Tab and Shift-Tab key events
// to traverse to the desired item; because the application is configured to
// traverse more elements for accessibility reasons.
bool IsFullKeyboardAccessEnabled();
#endif
} // namespace ui_controls
#endif // UI_BASE_TEST_UI_CONTROLS_H_
|