File: speech_recognition_private_manager_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (247 lines) | stat: -rw-r--r-- 9,872 bytes parent folder | download | duplicates (5)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/extensions/speech/speech_recognition_private_manager.h"

#include "base/memory/raw_ptr.h"
#include "chrome/browser/ash/extensions/speech/speech_recognition_private_base_test.h"
#include "chrome/browser/ash/extensions/speech/speech_recognition_private_manager_factory.h"
#include "chrome/browser/ash/extensions/speech/speech_recognition_private_recognizer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/speech/speech_recognition_constants.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"

namespace {
const char kEnglishLocale[] = "en-US";
const char kExtensionId[] = "egfdjlfmgnehecnclamagfafdccgfndp";
}  // namespace

namespace extensions {

class SpeechRecognitionPrivateManagerTest
    : public SpeechRecognitionPrivateBaseTest {
 protected:
  SpeechRecognitionPrivateManagerTest() = default;
  ~SpeechRecognitionPrivateManagerTest() override = default;
  SpeechRecognitionPrivateManagerTest(
      const SpeechRecognitionPrivateManagerTest&) = delete;
  SpeechRecognitionPrivateManagerTest& operator=(
      const SpeechRecognitionPrivateManagerTest&) = delete;

  void SetUpOnMainThread() override {
    SpeechRecognitionPrivateBaseTest::SetUpOnMainThread();
    manager_ =
        SpeechRecognitionPrivateManagerFactory::GetForBrowserContext(profile());
  }

  void TearDownOnMainThread() override {
    manager_->recognition_data_.clear();
    SpeechRecognitionPrivateBaseTest::TearDownOnMainThread();
  }

  std::string CreateKey(const std::string& extension_id,
                        std::optional<int> client_id) {
    return manager_->CreateKey(extension_id, client_id);
  }

  void HandleStart(
      const std::string& key,
      std::optional<std::string> locale,
      std::optional<bool> interim_results,
      base::OnceCallback<void(speech::SpeechRecognitionType,
                              std::optional<std::string>)> on_start_callback) {
    manager_->HandleStart(key, locale, interim_results,
                          std::move(on_start_callback));
  }

  void HandleStopAndWait(
      const std::string& key,
      base::OnceCallback<void(std::optional<std::string>)> callback) {
    manager_->HandleStop(key, std::move(callback));
    WaitForRecognitionStopped();
  }

  SpeechRecognitionPrivateRecognizer* GetSpeechRecognizer(
      const std::string& key) {
    return manager_->GetSpeechRecognizer(key);
  }

  void DispatchOnStopEvent(const std::string& key) {
    manager_->HandleSpeechRecognitionStopped(key);
  }

  void DispatchOnResultEvent(const std::string& key,
                             const std::u16string& transcript,
                             bool is_final) {
    manager_->HandleSpeechRecognitionResult(key, transcript, is_final);
  }

  void DispatchOnErrorEvent(const std::string& key,
                            const std::string& message) {
    manager_->HandleSpeechRecognitionError(key, message);
  }

 private:
  raw_ptr<SpeechRecognitionPrivateManager, DanglingUntriaged> manager_;
};

INSTANTIATE_TEST_SUITE_P(
    Network,
    SpeechRecognitionPrivateManagerTest,
    ::testing::Values(speech::SpeechRecognitionType::kNetwork));

INSTANTIATE_TEST_SUITE_P(
    OnDevice,
    SpeechRecognitionPrivateManagerTest,
    ::testing::Values(speech::SpeechRecognitionType::kOnDevice));

IN_PROC_BROWSER_TEST_P(SpeechRecognitionPrivateManagerTest, CreateKey) {
  ASSERT_EQ("Testing", CreateKey("Testing", std::optional<int>()));
  ASSERT_EQ("Testing.0", CreateKey("Testing", std::optional<int>(0)));
  ASSERT_EQ("Testing.1", CreateKey("Testing", std::optional<int>(1)));
}

IN_PROC_BROWSER_TEST_P(SpeechRecognitionPrivateManagerTest,
                       GetSpeechRecognizer) {
  SpeechRecognitionPrivateRecognizer* first_recognizer = nullptr;
  SpeechRecognitionPrivateRecognizer* second_recognizer = nullptr;
  first_recognizer = GetSpeechRecognizer("Testing");
  second_recognizer = GetSpeechRecognizer("Testing");
  ASSERT_NE(nullptr, first_recognizer);
  ASSERT_NE(nullptr, second_recognizer);
  ASSERT_EQ(first_recognizer, second_recognizer);
  second_recognizer = GetSpeechRecognizer("Testing.0");
  ASSERT_NE(nullptr, second_recognizer);
  ASSERT_NE(first_recognizer, second_recognizer);
}

IN_PROC_BROWSER_TEST_P(SpeechRecognitionPrivateManagerTest, HandleStart) {
  const std::string key = "Testing";
  std::optional<std::string> locale;
  std::optional<bool> interim_results(true);

  HandleStart(key, locale, interim_results, base::DoNothing());
  WaitForRecognitionStarted();
  SpeechRecognitionPrivateRecognizer* first_recognizer =
      GetSpeechRecognizer(key);
  ASSERT_NE(nullptr, first_recognizer);
  ASSERT_EQ(kEnglishLocale, first_recognizer->locale());
  ASSERT_TRUE(first_recognizer->interim_results());
  ASSERT_EQ(SPEECH_RECOGNIZER_RECOGNIZING, first_recognizer->current_state());

  // Try to change some properties and start again. Calling HandleStart() when
  // speech recognition is active should cause an error. The error message is
  // verified in SpeechRecognitionPrivateRecognizerTest. For this test, just
  // verify that properties are not updated and that speech recognition is
  // canceled.
  interim_results = false;
  HandleStart(key, locale, interim_results, base::DoNothing());
  SpeechRecognitionPrivateRecognizer* second_recognizer =
      GetSpeechRecognizer(key);
  ASSERT_NE(nullptr, second_recognizer);
  ASSERT_EQ(first_recognizer, second_recognizer);
  ASSERT_EQ(kEnglishLocale, second_recognizer->locale());
  ASSERT_TRUE(second_recognizer->interim_results());
  ASSERT_EQ(SPEECH_RECOGNIZER_OFF, second_recognizer->current_state());
}

IN_PROC_BROWSER_TEST_P(SpeechRecognitionPrivateManagerTest,
                       HandleStartAndStop) {
  const std::string key = "Testing";
  std::optional<std::string> locale;
  std::optional<bool> interim_results(true);

  HandleStart(key, locale, interim_results, base::DoNothing());
  WaitForRecognitionStarted();
  SpeechRecognitionPrivateRecognizer* recognizer = GetSpeechRecognizer(key);
  ASSERT_NE(nullptr, recognizer);
  ASSERT_EQ(SPEECH_RECOGNIZER_RECOGNIZING, recognizer->current_state());

  HandleStopAndWait(key, base::DoNothing());
  recognizer = GetSpeechRecognizer(key);
  ASSERT_NE(nullptr, recognizer);
  ASSERT_EQ(SPEECH_RECOGNIZER_OFF, recognizer->current_state());
}

// Tests that events can be dispatched from the SpeechRecognitionPrivateManager
// and received and processed in an extension.
IN_PROC_BROWSER_TEST_P(SpeechRecognitionPrivateManagerTest,
                       DispatchOnStopEvent) {
  ASSERT_TRUE(RunSpeechRecognitionPrivateTest("onstop_event")) << message_;

  const char* kExtensionIdAndIncorrectClientId =
      "egfdjlfmgnehecnclamagfafdccgfndp.0";
  const char* kCorrectExtensionIdAndClientId =
      "egfdjlfmgnehecnclamagfafdccgfndp.4";
  const char* kSkippingEvent = "Skipping event";
  const char* kProcessingEvent = "Processing event";

  // Send onStop events and ensure that we only process the event whose client
  // ID matches the extension's client ID.
  const struct {
    const char* key;
    const char* expected;
  } kTestCases[] = {{kExtensionId, kSkippingEvent},
                    {kExtensionIdAndIncorrectClientId, kSkippingEvent},
                    {kCorrectExtensionIdAndClientId, kProcessingEvent}};

  for (const auto& test : kTestCases) {
    ExtensionTestMessageListener listener(test.expected);
    DispatchOnStopEvent(test.key);
    ASSERT_TRUE(listener.WaitUntilSatisfied());
  }
}

// Tests that events can be dispatched from the SpeechRecognitionPrivateManager
// and received and processed in an extension.
IN_PROC_BROWSER_TEST_P(SpeechRecognitionPrivateManagerTest,
                       DispatchOnResultEvent) {
  ASSERT_TRUE(RunSpeechRecognitionPrivateTest("onresult_event")) << message_;

  const char* kFirstClient = "egfdjlfmgnehecnclamagfafdccgfndp.1";
  const char* kSecondClient = "egfdjlfmgnehecnclamagfafdccgfndp.2";
  const char* kSuccess = "Received result";
  const char* kFirstClientSkip = "Skipping event in first listener";
  const char* kSecondClientSkip = "Skipping event in second listener";
  const std::u16string kTranscript = u"This is a test";

  const struct {
    const char* key;
    const std::u16string transcript;
    const bool is_final;
    const char* expected_success_message;
    const char* expected_skip_message;
  } kTestCases[] = {
      {kFirstClient, kTranscript, false, kSuccess, kSecondClientSkip},
      {kSecondClient, kTranscript, true, kSuccess, kFirstClientSkip}};

  for (const auto& test : kTestCases) {
    // For each onResult event, verify that it was successfully handled in one
    // listener and dropped in the other (there are only two listeners).
    ExtensionTestMessageListener success_listener(
        test.expected_success_message);
    ExtensionTestMessageListener skip_listener(test.expected_skip_message);
    DispatchOnResultEvent(test.key, test.transcript, test.is_final);
    ASSERT_TRUE(success_listener.WaitUntilSatisfied());
    ASSERT_TRUE(skip_listener.WaitUntilSatisfied());
  }
}

// Tests that events can be dispatched from the SpeechRecognitionPrivateManager
// and received and processed in an extension.
IN_PROC_BROWSER_TEST_P(SpeechRecognitionPrivateManagerTest,
                       DispatchOnErrorEvent) {
  ResultCatcher result_catcher;
  ExtensionTestMessageListener listener("Proceed");

  const Extension* extension = LoadExtensionAsComponent("onerror_event");
  ASSERT_TRUE(extension);
  ASSERT_TRUE(listener.WaitUntilSatisfied());

  DispatchOnErrorEvent(kExtensionId, "A fatal error");
  ASSERT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
}

}  // namespace extensions