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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
content / browser / speech / soda_speech_recognition_engine_impl_unittest.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/speech/soda_speech_recognition_engine_impl.h"
#include <array>
#include <memory>
#include <vector>
#include "base/containers/queue.h"
#include "base/run_loop.h"
#include "content/browser/speech/fake_speech_recognition_manager_delegate.h"
#include "content/browser/speech/speech_recognition_engine.h"
#include "content/browser/speech/speech_recognizer_impl.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "media/mojo/mojom/audio_data.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kFirstSpeechResult[] = "the brown fox";
const char kSecondSpeechResult[] = "the brown fox jumped over the lazy dog";
} // namespace
using testing::_;
namespace content {
class SodaSpeechRecognitionEngineImplTest
: public testing::Test,
public SpeechRecognitionEngine::Delegate {
public:
SodaSpeechRecognitionEngineImplTest() = default;
// testing::Test methods.
void SetUp() override;
void TearDown() override;
// SpeechRecognitionRequestDelegate methods.
void OnSpeechRecognitionEngineResults(
const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& results)
override;
void OnSpeechRecognitionEngineEndOfUtterance() override;
void OnSpeechRecognitionEngineError(
const media::mojom::SpeechRecognitionError& error) override;
// context.
std::unique_ptr<SodaSpeechRecognitionEngineImpl> CreateSpeechRecognition(
SpeechRecognitionSessionConfig config,
SpeechRecognitionManagerDelegate* speech_recognition_mgr_delegate,
bool set_ready_cb);
void OnSpeechRecognitionReady() { recognition_ready_ = true; }
// operations.
void SendDummyAudioChunk();
void FillRecognitionExpectResults(
std::vector<media::mojom::WebSpeechRecognitionResultPtr>& results,
const char* transcription,
bool is_final);
void SendSpeechResult(const char* result, bool is_final);
void SendTranscriptionError();
void ExpectResultsReceived(
const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& results);
bool ResultsAreEqual(
const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& a,
const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& b);
protected:
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<content::TestBrowserContext> browser_context_;
std::unique_ptr<MockOnDeviceWebSpeechRecognitionService> mock_service_;
std::unique_ptr<FakeSpeechRecognitionManagerDelegate>
fake_speech_recognition_mgr_delegate_;
std::unique_ptr<SodaSpeechRecognitionEngineImpl> client_under_test_;
base::queue<std::vector<media::mojom::WebSpeechRecognitionResultPtr>>
results_;
media::mojom::SpeechRecognitionErrorCode error_;
int end_of_utterance_counter_ = 0;
bool recognition_ready_ = false;
base::WeakPtrFactory<SodaSpeechRecognitionEngineImplTest> weak_factory_{this};
};
void SodaSpeechRecognitionEngineImplTest::SetUp() {
error_ = media::mojom::SpeechRecognitionErrorCode::kNone;
end_of_utterance_counter_ = 0;
recognition_ready_ = false;
browser_context_ = std::make_unique<content::TestBrowserContext>();
mock_service_ = std::make_unique<MockOnDeviceWebSpeechRecognitionService>(
browser_context_.get());
fake_speech_recognition_mgr_delegate_ =
std::make_unique<FakeSpeechRecognitionManagerDelegate>(
mock_service_.get());
}
void SodaSpeechRecognitionEngineImplTest::TearDown() {}
void SodaSpeechRecognitionEngineImplTest::OnSpeechRecognitionEngineResults(
const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& results) {
results_.push(mojo::Clone(results));
}
void SodaSpeechRecognitionEngineImplTest::
OnSpeechRecognitionEngineEndOfUtterance() {
++end_of_utterance_counter_;
}
void SodaSpeechRecognitionEngineImplTest::OnSpeechRecognitionEngineError(
const media::mojom::SpeechRecognitionError& error) {
error_ = error.code;
}
std::unique_ptr<SodaSpeechRecognitionEngineImpl>
SodaSpeechRecognitionEngineImplTest::CreateSpeechRecognition(
SpeechRecognitionSessionConfig config,
SpeechRecognitionManagerDelegate* speech_recognition_mgr_delegate,
bool set_ready_cb) {
std::unique_ptr<SodaSpeechRecognitionEngineImpl> client_under_test =
std::make_unique<SodaSpeechRecognitionEngineImpl>(config);
client_under_test->set_delegate(this);
if (set_ready_cb) {
recognition_ready_ = false;
client_under_test->SetOnReadyCallback(base::BindOnce(
&SodaSpeechRecognitionEngineImplTest::OnSpeechRecognitionReady,
weak_factory_.GetWeakPtr()));
}
client_under_test->SetSpeechRecognitionManagerDelegateForTesting(
speech_recognition_mgr_delegate);
int chunk_duration_ms = client_under_test->GetDesiredAudioChunkDurationMs();
// Audio converter shall provide audio based on these parameters as output.
// Hard coded, WebSpeech specific parameters are utilized here.
int frames_per_buffer =
(SpeechRecognizerImpl::kAudioSampleRate * chunk_duration_ms) / 1000;
media::AudioParameters audio_parameters = media::AudioParameters(
media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::ChannelLayoutConfig::FromLayout<
SpeechRecognizerImpl::kChannelLayout>(),
SpeechRecognizerImpl::kAudioSampleRate, frames_per_buffer);
client_under_test->SetAudioParameters(audio_parameters);
return client_under_test;
}
void SodaSpeechRecognitionEngineImplTest::SendDummyAudioChunk() {
// Enough data so that the encoder will output something, as can't read 0
// bytes from a Mojo stream.
std::array<unsigned char, 2000 * 2> dummy_audio_buffer_data = {'\0'};
scoped_refptr<AudioChunk> dummy_audio_chunk(new AudioChunk(
&dummy_audio_buffer_data[0], sizeof(dummy_audio_buffer_data),
2 /* bytes per sample */));
DCHECK(client_under_test_.get());
base::RunLoop loop;
client_under_test_->TakeAudioChunk(*dummy_audio_chunk.get());
loop.RunUntilIdle();
}
void SodaSpeechRecognitionEngineImplTest::FillRecognitionExpectResults(
std::vector<media::mojom::WebSpeechRecognitionResultPtr>& results,
const char* transcription,
bool is_final) {
results.push_back(media::mojom::WebSpeechRecognitionResult::New());
media::mojom::WebSpeechRecognitionResultPtr& result = results.back();
result->is_provisional = !is_final;
media::mojom::SpeechRecognitionHypothesisPtr hypothesis =
media::mojom::SpeechRecognitionHypothesis::New();
hypothesis->confidence = 1.0;
hypothesis->utterance = base::UTF8ToUTF16(transcription);
result->hypotheses.push_back(std::move(hypothesis));
}
void SodaSpeechRecognitionEngineImplTest::SendSpeechResult(const char* result,
bool is_final) {
base::RunLoop loop;
mock_service_->SendSpeechRecognitionResult(
media::SpeechRecognitionResult(result, is_final));
loop.RunUntilIdle();
}
void SodaSpeechRecognitionEngineImplTest::SendTranscriptionError() {
base::RunLoop loop;
mock_service_->SendSpeechRecognitionError();
loop.RunUntilIdle();
}
void SodaSpeechRecognitionEngineImplTest::ExpectResultsReceived(
const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& results) {
ASSERT_GE(1U, results_.size());
ASSERT_TRUE(ResultsAreEqual(results, results_.front()));
results_.pop();
}
bool SodaSpeechRecognitionEngineImplTest::ResultsAreEqual(
const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& a,
const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& b) {
if (a.size() != b.size()) {
return false;
}
auto it_a = a.begin();
auto it_b = b.begin();
for (; it_a != a.end() && it_b != b.end(); ++it_a, ++it_b) {
if ((*it_a)->is_provisional != (*it_b)->is_provisional ||
(*it_a)->hypotheses.size() != (*it_b)->hypotheses.size()) {
return false;
}
for (size_t i = 0; i < (*it_a)->hypotheses.size(); ++i) {
const media::mojom::SpeechRecognitionHypothesisPtr& hyp_a =
(*it_a)->hypotheses[i];
const media::mojom::SpeechRecognitionHypothesisPtr& hyp_b =
(*it_b)->hypotheses[i];
if (hyp_a->utterance != hyp_b->utterance ||
hyp_a->confidence != hyp_b->confidence) {
return false;
}
}
}
return true;
}
TEST_F(SodaSpeechRecognitionEngineImplTest, SpeechRecognitionResults) {
SpeechRecognitionSessionConfig config;
client_under_test_ = CreateSpeechRecognition(
config, fake_speech_recognition_mgr_delegate_.get(), true);
ASSERT_TRUE(client_under_test_->Initialize());
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(recognition_ready_);
EXPECT_CALL(*mock_service_, SendAudioToSpeechRecognitionService(_)).Times(2);
client_under_test_->StartRecognition();
SendDummyAudioChunk();
std::vector<media::mojom::WebSpeechRecognitionResultPtr> first_results;
FillRecognitionExpectResults(first_results, kFirstSpeechResult, false);
SendSpeechResult(kFirstSpeechResult, /*is_final=*/false);
ExpectResultsReceived(first_results);
SendDummyAudioChunk();
std::vector<media::mojom::WebSpeechRecognitionResultPtr> second_results;
FillRecognitionExpectResults(second_results, kSecondSpeechResult, false);
SendSpeechResult(kSecondSpeechResult, /*is_final=*/false);
ExpectResultsReceived(second_results);
SendTranscriptionError();
ASSERT_EQ(media::mojom::SpeechRecognitionErrorCode::kNoSpeech, error_);
}
TEST_F(SodaSpeechRecognitionEngineImplTest, SpeechRecognitionAudioChunksEnded) {
SpeechRecognitionSessionConfig config;
client_under_test_ = CreateSpeechRecognition(
config, fake_speech_recognition_mgr_delegate_.get(), true);
ASSERT_TRUE(client_under_test_->Initialize());
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(recognition_ready_);
EXPECT_CALL(*mock_service_, SendAudioToSpeechRecognitionService(_)).Times(1);
client_under_test_->StartRecognition();
SendDummyAudioChunk();
std::vector<media::mojom::WebSpeechRecognitionResultPtr> first_results;
FillRecognitionExpectResults(first_results, kFirstSpeechResult, false);
SendSpeechResult(kFirstSpeechResult, /*is_final=*/false);
ExpectResultsReceived(first_results);
base::RunLoop loop;
client_under_test_->AudioChunksEnded();
client_under_test_->EndRecognition();
loop.RunUntilIdle();
ASSERT_EQ(media::mojom::SpeechRecognitionErrorCode::kAborted, error_);
}
TEST_F(SodaSpeechRecognitionEngineImplTest, SpeechRecognitionEndOfUtterance) {
SpeechRecognitionSessionConfig config;
config.continuous = false;
client_under_test_ = CreateSpeechRecognition(
config, fake_speech_recognition_mgr_delegate_.get(), true);
ASSERT_TRUE(client_under_test_->Initialize());
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(recognition_ready_);
EXPECT_CALL(*mock_service_, SendAudioToSpeechRecognitionService(_)).Times(1);
client_under_test_->StartRecognition();
SendDummyAudioChunk();
std::vector<media::mojom::WebSpeechRecognitionResultPtr> first_results;
FillRecognitionExpectResults(first_results, kFirstSpeechResult, false);
SendSpeechResult(kFirstSpeechResult, /*is_final=*/false);
ExpectResultsReceived(first_results);
std::vector<media::mojom::WebSpeechRecognitionResultPtr> second_results;
FillRecognitionExpectResults(second_results, kSecondSpeechResult, true);
SendSpeechResult(kSecondSpeechResult, /*is_final=*/true);
ExpectResultsReceived(second_results);
ASSERT_EQ(1, end_of_utterance_counter_);
}
TEST_F(SodaSpeechRecognitionEngineImplTest, SpeechRecognitionEnd) {
SpeechRecognitionSessionConfig config;
config.continuous = false;
client_under_test_ = CreateSpeechRecognition(
config, fake_speech_recognition_mgr_delegate_.get(), true);
ASSERT_TRUE(client_under_test_->Initialize());
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(recognition_ready_);
EXPECT_CALL(*mock_service_, SendAudioToSpeechRecognitionService(_)).Times(1);
client_under_test_->StartRecognition();
SendDummyAudioChunk();
std::vector<media::mojom::WebSpeechRecognitionResultPtr> first_results;
FillRecognitionExpectResults(first_results, kFirstSpeechResult, false);
SendSpeechResult(kFirstSpeechResult, /*is_final=*/false);
ExpectResultsReceived(first_results);
client_under_test_->EndRecognition();
SendDummyAudioChunk();
ASSERT_EQ(media::mojom::SpeechRecognitionErrorCode::kNotAllowed, error_);
}
TEST_F(SodaSpeechRecognitionEngineImplTest, SetOnReadyCallbackAfterBind) {
SpeechRecognitionSessionConfig config;
client_under_test_ = CreateSpeechRecognition(
config, fake_speech_recognition_mgr_delegate_.get(), false);
ASSERT_TRUE(client_under_test_->Initialize());
base::RunLoop().RunUntilIdle();
recognition_ready_ = false;
client_under_test_->SetOnReadyCallback(base::BindOnce(
&SodaSpeechRecognitionEngineImplTest::OnSpeechRecognitionReady,
weak_factory_.GetWeakPtr()));
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(recognition_ready_);
}
} // namespace content