File: ui_controls_aura.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (192 lines) | stat: -rw-r--r-- 6,685 bytes parent folder | download | duplicates (4)
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
// 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.

#include "ui/base/test/ui_controls_aura.h"

#include "base/check.h"
#include "base/functional/callback.h"
#include "build/build_config.h"
#include "ui/gfx/native_widget_types.h"

namespace ui_controls {
namespace {
UIControlsAura* g_instance = nullptr;
bool g_ui_controls_enabled = false;

void CheckUIControlsEnabled() {
  CHECK(g_ui_controls_enabled)
      << "In order to use ui_controls methods, you must be in a test "
         "executable that enables UI Controls. Currently, this is "
         "interactive_ui_tests and some fuzzing tests.\n"
         "This limitation prevents attempting to send input that might require "
         "the test process to be active and focused in an environment where "
         "the process is not guaranteed to be running exclusively, which can "
         "lead to flaky tests.";
}

}  // namespace

#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_WIN)
void EnableUIControls() {
  g_ui_controls_enabled = true;
}
#endif  // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_WIN)

void ResetUIControlsIfEnabled() {}

// An interface to provide Aura implementation of UI control.

// static
bool SendKeyPress(gfx::NativeWindow window,
                  ui::KeyboardCode key,
                  bool control,
                  bool shift,
                  bool alt,
                  bool command) {
  CheckUIControlsEnabled();
  return g_instance->SendKeyEvents(
      window, key, kKeyPress | kKeyRelease,
      GenerateAcceleratorState(control, shift, alt, command));
}

// static
bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
                                ui::KeyboardCode key,
                                bool control,
                                bool shift,
                                bool alt,
                                bool command,
                                base::OnceClosure task,
                                KeyEventType wait_for) {
  CheckUIControlsEnabled();
  CHECK(wait_for == ui_controls::KeyEventType::kKeyPress ||
        wait_for == ui_controls::KeyEventType::kKeyRelease);
  return g_instance->SendKeyEventsNotifyWhenDone(
      window, key, kKeyPress | kKeyRelease, std::move(task),
      GenerateAcceleratorState(control, shift, alt, command), wait_for);
}

// static
bool SendKeyEvents(gfx::NativeWindow window,
                   ui::KeyboardCode key,
                   int key_event_types,
                   int accelerator_state) {
  CheckUIControlsEnabled();

  // Make sure `key_event_types` abd `accelerator_state` is valid.
  // `key_event_types` must include at least one key event type.
  DCHECK(key_event_types > 0 && key_event_types <= (kKeyPress | kKeyRelease));
  DCHECK(accelerator_state >= 0 &&
         accelerator_state <= (kShift | kControl | kAlt | kCommand));

  return g_instance->SendKeyEvents(window, key, key_event_types,
                                   accelerator_state);
}

// static
bool SendKeyEventsNotifyWhenDone(gfx::NativeWindow window,
                                 ui::KeyboardCode key,
                                 int key_event_types,
                                 base::OnceClosure task,
                                 int accelerator_state) {
  CheckUIControlsEnabled();

  // Make sure `key_event_types` abd `accelerator_state` is valid.
  // `key_event_types` must include at least one key event type.
  DCHECK(key_event_types > 0 && key_event_types <= (kKeyPress | kKeyRelease));
  DCHECK(accelerator_state >= 0 &&
         accelerator_state <= (kShift | kControl | kAlt | kCommand));

  return g_instance->SendKeyEventsNotifyWhenDone(
      window, key, key_event_types, std::move(task), accelerator_state,
      KeyEventType::kKeyPress);
}

// static
bool SendMouseMove(int x, int y, gfx::NativeWindow) {
  // TODO(crbug.com/40249511): Maybe use the window hint on other platforms.
  CheckUIControlsEnabled();
  return g_instance->SendMouseMove(x, y);
}

// static
bool SendMouseMoveNotifyWhenDone(int x,
                                 int y,
                                 base::OnceClosure task,
                                 gfx::NativeWindow) {
  // TODO(crbug.com/40249511): Maybe use the window hint on other platforms.
  CheckUIControlsEnabled();
  return g_instance->SendMouseMoveNotifyWhenDone(x, y, std::move(task));
}

// static
bool SendMouseEvents(MouseButton type,
                     int button_state,
                     int accelerator_state,
                     gfx::NativeWindow) {
  // TODO(crbug.com/40249511): Maybe use the window hint on other platforms.
  CheckUIControlsEnabled();
  return g_instance->SendMouseEvents(type, button_state, accelerator_state);
}

// static
bool SendMouseEventsNotifyWhenDone(MouseButton type,
                                   int button_state,
                                   base::OnceClosure task,
                                   int accelerator_state,
                                   gfx::NativeWindow) {
  // TODO(crbug.com/40249511): Maybe use the window hint on other platforms.
  CheckUIControlsEnabled();
  return g_instance->SendMouseEventsNotifyWhenDone(
      type, button_state, std::move(task), accelerator_state);
}

// static
bool SendMouseClick(MouseButton type, gfx::NativeWindow) {
  // TODO(crbug.com/40249511): Do any Aura platforms need to use the hint?
  CheckUIControlsEnabled();
  return g_instance->SendMouseClick(type);
}

#if BUILDFLAG(IS_WIN)
// static
bool SendTouchEvents(int action, int num, int x, int y) {
  CheckUIControlsEnabled();
  return g_instance->SendTouchEvents(action, num, x, y);
}
#elif BUILDFLAG(IS_CHROMEOS)
// static
bool SendTouchEvents(int action, int id, int x, int y) {
  CheckUIControlsEnabled();
  return g_instance->SendTouchEvents(action, id, x, y);
}

// static
bool SendTouchEventsNotifyWhenDone(int action,
                                   int id,
                                   int x,
                                   int y,
                                   base::OnceClosure task) {
  CheckUIControlsEnabled();
  return g_instance->SendTouchEventsNotifyWhenDone(action, id, x, y,
                                                   std::move(task));
}
#endif

UIControlsAura::UIControlsAura() {
}

UIControlsAura::~UIControlsAura() {
}

#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)
// static. declared in ui_controls.h
void InstallUIControlsAura(UIControlsAura* instance) {
  g_ui_controls_enabled = true;
  delete g_instance;
  g_instance = instance;
}
#endif  // BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)

}  // namespace ui_controls