1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/test_runner/mock_web_speech_recognizer.h"
7 #include "base/logging.h"
8 #include "components/test_runner/web_test_delegate.h"
9 #include "third_party/WebKit/public/web/WebSpeechRecognitionResult.h"
10 #include "third_party/WebKit/public/web/WebSpeechRecognizerClient.h"
12 namespace test_runner
{
16 // Task class for calling a client function that does not take any parameters.
17 typedef void (blink::WebSpeechRecognizerClient::*ClientFunctionPointer
)(
18 const blink::WebSpeechRecognitionHandle
&);
19 class ClientCallTask
: public MockWebSpeechRecognizer::Task
{
21 ClientCallTask(MockWebSpeechRecognizer
* mock
, ClientFunctionPointer function
)
22 : MockWebSpeechRecognizer::Task(mock
), function_(function
) {}
24 ~ClientCallTask() override
{}
27 (recognizer_
->Client()->*function_
)(recognizer_
->Handle());
31 ClientFunctionPointer function_
;
33 DISALLOW_COPY_AND_ASSIGN(ClientCallTask
);
36 // Task for delivering a result event.
37 class ResultTask
: public MockWebSpeechRecognizer::Task
{
39 ResultTask(MockWebSpeechRecognizer
* mock
,
40 const blink::WebString transcript
,
42 : MockWebSpeechRecognizer::Task(mock
),
43 transcript_(transcript
),
44 confidence_(confidence
) {}
46 ~ResultTask() override
{}
49 blink::WebVector
<blink::WebString
> transcripts(static_cast<size_t>(1));
50 blink::WebVector
<float> confidences(static_cast<size_t>(1));
51 transcripts
[0] = transcript_
;
52 confidences
[0] = confidence_
;
53 blink::WebVector
<blink::WebSpeechRecognitionResult
> final_results(
54 static_cast<size_t>(1));
55 blink::WebVector
<blink::WebSpeechRecognitionResult
> interim_results
;
56 final_results
[0].assign(transcripts
, confidences
, true);
58 recognizer_
->Client()->didReceiveResults(
59 recognizer_
->Handle(), final_results
, interim_results
);
63 blink::WebString transcript_
;
66 DISALLOW_COPY_AND_ASSIGN(ResultTask
);
69 // Task for delivering a nomatch event.
70 class NoMatchTask
: public MockWebSpeechRecognizer::Task
{
72 NoMatchTask(MockWebSpeechRecognizer
* mock
)
73 : MockWebSpeechRecognizer::Task(mock
) {}
75 ~NoMatchTask() override
{}
78 recognizer_
->Client()->didReceiveNoMatch(
79 recognizer_
->Handle(), blink::WebSpeechRecognitionResult());
83 DISALLOW_COPY_AND_ASSIGN(NoMatchTask
);
86 // Task for delivering an error event.
87 class ErrorTask
: public MockWebSpeechRecognizer::Task
{
89 ErrorTask(MockWebSpeechRecognizer
* mock
,
90 blink::WebSpeechRecognizerClient::ErrorCode code
,
91 const blink::WebString
& message
)
92 : MockWebSpeechRecognizer::Task(mock
), code_(code
), message_(message
) {}
94 ~ErrorTask() override
{}
97 recognizer_
->Client()->didReceiveError(
98 recognizer_
->Handle(), message_
, code_
);
102 blink::WebSpeechRecognizerClient::ErrorCode code_
;
103 blink::WebString message_
;
105 DISALLOW_COPY_AND_ASSIGN(ErrorTask
);
110 MockWebSpeechRecognizer::MockWebSpeechRecognizer()
111 : was_aborted_(false), task_queue_running_(false), delegate_(0) {
114 MockWebSpeechRecognizer::~MockWebSpeechRecognizer() {
118 void MockWebSpeechRecognizer::SetDelegate(WebTestDelegate
* delegate
) {
119 delegate_
= delegate
;
122 void MockWebSpeechRecognizer::start(
123 const blink::WebSpeechRecognitionHandle
& handle
,
124 const blink::WebSpeechRecognitionParams
& params
,
125 blink::WebSpeechRecognizerClient
* client
) {
126 was_aborted_
= false;
130 task_queue_
.push_back(
131 new ClientCallTask(this, &blink::WebSpeechRecognizerClient::didStart
));
132 task_queue_
.push_back(new ClientCallTask(
133 this, &blink::WebSpeechRecognizerClient::didStartAudio
));
134 task_queue_
.push_back(new ClientCallTask(
135 this, &blink::WebSpeechRecognizerClient::didStartSound
));
137 if (!mock_transcripts_
.empty()) {
138 DCHECK_EQ(mock_transcripts_
.size(), mock_confidences_
.size());
140 for (size_t i
= 0; i
< mock_transcripts_
.size(); ++i
)
141 task_queue_
.push_back(
142 new ResultTask(this, mock_transcripts_
[i
], mock_confidences_
[i
]));
144 mock_transcripts_
.clear();
145 mock_confidences_
.clear();
147 task_queue_
.push_back(new NoMatchTask(this));
149 task_queue_
.push_back(
150 new ClientCallTask(this, &blink::WebSpeechRecognizerClient::didEndSound
));
151 task_queue_
.push_back(
152 new ClientCallTask(this, &blink::WebSpeechRecognizerClient::didEndAudio
));
153 task_queue_
.push_back(
154 new ClientCallTask(this, &blink::WebSpeechRecognizerClient::didEnd
));
159 void MockWebSpeechRecognizer::stop(
160 const blink::WebSpeechRecognitionHandle
& handle
,
161 blink::WebSpeechRecognizerClient
* client
) {
169 void MockWebSpeechRecognizer::abort(
170 const blink::WebSpeechRecognitionHandle
& handle
,
171 blink::WebSpeechRecognizerClient
* client
) {
177 task_queue_
.push_back(
178 new ClientCallTask(this, &blink::WebSpeechRecognizerClient::didEnd
));
182 void MockWebSpeechRecognizer::AddMockResult(const blink::WebString
& transcript
,
184 mock_transcripts_
.push_back(transcript
);
185 mock_confidences_
.push_back(confidence
);
188 void MockWebSpeechRecognizer::SetError(const blink::WebString
& error
,
189 const blink::WebString
& message
) {
190 blink::WebSpeechRecognizerClient::ErrorCode code
;
191 if (error
== "OtherError")
192 code
= blink::WebSpeechRecognizerClient::OtherError
;
193 else if (error
== "NoSpeechError")
194 code
= blink::WebSpeechRecognizerClient::NoSpeechError
;
195 else if (error
== "AbortedError")
196 code
= blink::WebSpeechRecognizerClient::AbortedError
;
197 else if (error
== "AudioCaptureError")
198 code
= blink::WebSpeechRecognizerClient::AudioCaptureError
;
199 else if (error
== "NetworkError")
200 code
= blink::WebSpeechRecognizerClient::NetworkError
;
201 else if (error
== "NotAllowedError")
202 code
= blink::WebSpeechRecognizerClient::NotAllowedError
;
203 else if (error
== "ServiceNotAllowedError")
204 code
= blink::WebSpeechRecognizerClient::ServiceNotAllowedError
;
205 else if (error
== "BadGrammarError")
206 code
= blink::WebSpeechRecognizerClient::BadGrammarError
;
207 else if (error
== "LanguageNotSupportedError")
208 code
= blink::WebSpeechRecognizerClient::LanguageNotSupportedError
;
213 task_queue_
.push_back(new ErrorTask(this, code
, message
));
214 task_queue_
.push_back(
215 new ClientCallTask(this, &blink::WebSpeechRecognizerClient::didEnd
));
219 void MockWebSpeechRecognizer::StartTaskQueue() {
220 if (task_queue_running_
)
222 delegate_
->PostTask(new StepTask(this));
223 task_queue_running_
= true;
226 void MockWebSpeechRecognizer::ClearTaskQueue() {
227 while (!task_queue_
.empty()) {
228 delete task_queue_
.front();
229 task_queue_
.pop_front();
231 task_queue_running_
= false;
234 void MockWebSpeechRecognizer::StepTask::RunIfValid() {
235 if (object_
->task_queue_
.empty()) {
236 object_
->task_queue_running_
= false;
240 Task
* task
= object_
->task_queue_
.front();
241 object_
->task_queue_
.pop_front();
245 if (object_
->task_queue_
.empty()) {
246 object_
->task_queue_running_
= false;
250 object_
->delegate_
->PostTask(new StepTask(object_
));
253 } // namespace test_runner