File: fake_accessibility_service.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (269 lines) | stat: -rw-r--r-- 10,149 bytes parent folder | download | duplicates (6)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_ACCESSIBILITY_SERVICE_FAKE_ACCESSIBILITY_SERVICE_H_
#define CHROME_BROWSER_ASH_ACCESSIBILITY_SERVICE_FAKE_ACCESSIBILITY_SERVICE_H_

#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/unguessable_token.h"
#include "chrome/browser/accessibility/service/accessibility_service_router.h"
#include "components/keyed_service/core/keyed_service.h"
#include "mojo/public/cpp/bindings/associated_receiver_set.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "services/accessibility/public/mojom/accessibility_service.mojom.h"
#include "services/accessibility/public/mojom/autoclick.mojom.h"
#include "services/accessibility/public/mojom/automation.mojom.h"
#include "services/accessibility/public/mojom/automation_client.mojom.h"
#include "services/accessibility/public/mojom/file_loader.mojom.h"
#include "services/accessibility/public/mojom/speech_recognition.mojom.h"
#include "services/accessibility/public/mojom/tts.mojom.h"
#include "services/accessibility/public/mojom/user_input.mojom.h"
#include "services/accessibility/public/mojom/user_interface.mojom.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_event.h"
#include "ui/accessibility/ax_tree_update.h"

namespace ash {

// A fake Chrome OS Accessibility service to use for Chrome testing.
// This class acts as an AccessibilityServiceRouter in the browser process
// and then implements service mojom to act as a mock service.
class FakeAccessibilityService
    : public ax::AccessibilityServiceRouter,
      public ax::mojom::Automation,
      public ax::mojom::AssistiveTechnologyController,
      public ax::mojom::Autoclick {
 public:
  FakeAccessibilityService();
  FakeAccessibilityService(const FakeAccessibilityService&) = delete;
  FakeAccessibilityService& operator=(const FakeAccessibilityService&) = delete;
  ~FakeAccessibilityService() override;

  // AccessibilityServiceRouter:
  void BindAccessibilityServiceClient(
      mojo::PendingRemote<ax::mojom::AccessibilityServiceClient>
          accessibility_service_client) override;
  void BindAssistiveTechnologyController(
      mojo::PendingReceiver<ax::mojom::AssistiveTechnologyController>
          at_controller_receiver,
      const std::vector<ax::mojom::AssistiveTechnologyType>& enabled_features)
      override;
  void ConnectDevToolsAgent(
      mojo::PendingAssociatedReceiver<blink::mojom::DevToolsAgent> agent,
      ax::mojom::AssistiveTechnologyType type) override;

  // ax::mojom::Automation:
  void DispatchTreeDestroyedEvent(const ui::AXTreeID& tree_id) override;
  void DispatchActionResult(const ui::AXActionData& data, bool result) override;
  void DispatchAccessibilityEvents(
      const ui::AXTreeID& tree_id,
      const std::vector<ui::AXTreeUpdate>& updates,
      const gfx::Point& mouse_location,
      const std::vector<ui::AXEvent>& events) override;
  void DispatchAccessibilityLocationChange(
      const ui::AXTreeID& tree_id,
      int node_id,
      const ui::AXRelativeBounds& bounds) override;
  void DispatchAccessibilityScrollChange(const ui::AXTreeID& tree_id,
                                         int node_id,
                                         int scroll_x,
                                         int scroll_y) override;
  void DispatchGetTextLocationResult(
      const ui::AXActionData& data,
      const std::optional<gfx::Rect>& rect) override;

  // ax::mojom::AssistiveTechnologyController:
  void EnableAssistiveTechnology(
      const std::vector<ax::mojom::AssistiveTechnologyType>& enabled_features)
      override;

  // In the service, V8 JS implements autoclick.
  // ax::mojom::Autoclick:
  void RequestScrollableBoundsForPoint(const gfx::Point& point) override;

  //
  // Methods for testing.
  //

  // Whether the service client remote is bound.
  bool IsBound() const;

  // Waits for EnableAssistiveTechnology to be called |count| times.
  void WaitForATChangeCount(int count);

  // Gets the currently enabled assistive technology types.
  const std::set<ax::mojom::AssistiveTechnologyType>& GetEnabledATs() const {
    return enabled_ATs_;
  }

  int GetDevtoolsConnectionCount(ax::mojom::AssistiveTechnologyType type) const;

  // Allows tests to bind APIs multiple times, mimicking multiple
  // V8 instances in the service.
  void BindAnotherAutomation();
  void BindAnotherAutomationClient();
  void BindAnotherAutoclickClient();
  void BindAnotherSpeechRecognition();
  void BindAnotherTts();
  void BindAnotherUserInput();
  void BindAnotherUserInterface();

  //
  // Methods to pretend an AutomationClient request came from the service.
  //

  // Calls ax::mojom::AutomationClient::Enable or ::Disable.
  void AutomationClientEnable(bool enabled);

  // Waits for Automation events to come in.
  void WaitForAutomationEvents();

  //
  // Methods to pretend a SpeechRecognition request came from the service.
  //

  void RequestSpeechRecognitionStart(
      ax::mojom::StartOptionsPtr options,
      base::OnceCallback<void(ax::mojom::SpeechRecognitionStartInfoPtr)>
          callback);
  void RequestSpeechRecognitionStop(
      ax::mojom::StopOptionsPtr options,
      base::OnceCallback<void(const std::optional<std::string>&)> callback);

  //
  // Methods to pretend a TTS request came from the service.
  //

  // Sends a request for speech using the default options, but with on_event
  // set to true.
  void RequestSpeak(
      const std::string& utterance,
      base::OnceCallback<void(ax::mojom::TtsSpeakResultPtr)> callback);

  // Sends a request for speech using the given options.
  void RequestSpeak(
      const std::string& utterance,
      ax::mojom::TtsOptionsPtr options,
      base::OnceCallback<void(ax::mojom::TtsSpeakResultPtr)> callback);

  // Sends a request to stop speech.
  void RequestStop();

  // Sends a request to pause speech.
  void RequestPause();

  // Sends a request to resume speech.
  void RequestResume();

  // Asks if speech is in progress.
  void IsTtsSpeaking(base::OnceCallback<void(bool)> callback);

  // Sends a request from the service for the TTS voices list.
  void RequestTtsVoices(ax::mojom::Tts::GetVoicesCallback callback);

  //
  // Methods to pretend a UserInput request came from the service.
  //

  // Sends a request to send a synthetic key event.
  void RequestSendSyntheticKeyEventForShortcutOrNavigation(
      ax::mojom::SyntheticKeyEventPtr key_event);

  // Sends a request to send a synthetic mouse event.
  void RequestSendSyntheticMouseEvent(
      ax::mojom::SyntheticMouseEventPtr mouse_event);

  //
  // Methods to pretend a UserInterface request came from the service.
  //

  void RequestDarkenScreen(bool darken);

  void RequestOpenSettingsSubpage(const std::string& subpage);

  void RequestShowConfirmationDialog(
      const std::string& title,
      const std::string& description,
      const std::optional<std::string>& cancel_name,
      ax::mojom::UserInterface::ShowConfirmationDialogCallback callback);

  void RequestSetFocusRings(
      std::vector<ax::mojom::FocusRingInfoPtr> focus_rings,
      ax::mojom::AssistiveTechnologyType at_type);

  void RequestSetHighlights(const std::vector<gfx::Rect>& rects, SkColor color);

  void RequestSetVirtualKeyboardVisible(bool is_visible);

  // Getters for automation events.
  std::vector<ui::AXTreeID> tree_destroyed_events() const {
    return tree_destroyed_events_;
  }
  std::vector<std::tuple<ui::AXActionData, bool>> action_results() const {
    return action_results_;
  }
  std::vector<ui::AXTreeID> accessibility_events() const {
    return accessibility_events_;
  }
  std::vector<ui::AXTreeID> location_changes() const {
    return location_changes_;
  }

  // Loads a file indicated by |relative_path|, which must be a relative path.
  // The base path used to load paths from is the base accessibility resources
  // directory in ChromeOS. |callback| runs when the operation is completed.
  void RequestLoadFile(
      base::FilePath relative_path,
      ax::mojom::AccessibilityFileLoader::LoadCallback callback);

  void set_autoclick_scrollable_bounds(const gfx::Rect& bounds) {
    autoclick_scrollable_bounds_ = bounds;
  }

 private:
  // Emulates V8 getting the autoclick receiver in the service process.
  void OnAutoclickBoundCallback(
      mojo::PendingReceiver<ax::mojom::Autoclick> autoclick_receiver);

  base::OnceClosure change_ATs_closure_;
  std::set<ax::mojom::AssistiveTechnologyType> enabled_ATs_;
  base::OnceClosure automation_events_closure_;

  std::vector<ui::AXTreeID> tree_destroyed_events_;
  std::vector<std::tuple<ui::AXActionData, bool>> action_results_;
  std::vector<ui::AXTreeID> accessibility_events_;
  std::vector<ui::AXTreeID> location_changes_;
  std::vector<ui::AXTreeID> scroll_changes_;

  gfx::Rect autoclick_scrollable_bounds_;

  std::map<ax::mojom::AssistiveTechnologyType, int> connect_devtools_counts;

  // Number of times ATs changed state.
  int at_change_count_ = 0;
  int expected_count_ = 0;

  mojo::AssociatedReceiverSet<ax::mojom::Automation> automation_receivers_;
  mojo::RemoteSet<ax::mojom::AutomationClient> automation_client_remotes_;

  mojo::RemoteSet<ax::mojom::SpeechRecognition> sr_remotes_;
  mojo::RemoteSet<ax::mojom::AutoclickClient> autoclick_client_remotes_;
  mojo::ReceiverSet<ax::mojom::Autoclick> autoclick_receivers_;
  mojo::RemoteSet<ax::mojom::Tts> tts_remotes_;
  mojo::RemoteSet<ax::mojom::UserInput> ui_remotes_;
  mojo::RemoteSet<ax::mojom::UserInterface> ux_remotes_;

  mojo::ReceiverSet<ax::mojom::AssistiveTechnologyController>
      at_controller_receivers_;
  mojo::Remote<ax::mojom::AccessibilityServiceClient>
      accessibility_service_client_remote_;
  mojo::Remote<ax::mojom::AccessibilityFileLoader> file_loader_remote_;
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_ACCESSIBILITY_SERVICE_FAKE_ACCESSIBILITY_SERVICE_H_