1 // Copyright (c) 2013 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 "content/public/test/fake_speech_recognition_manager.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/public/browser/speech_recognition_event_listener.h"
11 #include "content/public/common/speech_recognition_result.h"
12 #include "content/public/test/test_utils.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 const char kTestResult
[] = "Pictures of the moon";
18 void RunCallback(const base::Closure recognition_started_closure
) {
19 recognition_started_closure
.Run();
25 FakeSpeechRecognitionManager::FakeSpeechRecognitionManager()
28 fake_result_(kTestResult
),
29 did_cancel_all_(false),
30 should_send_fake_response_(true) {
33 FakeSpeechRecognitionManager::~FakeSpeechRecognitionManager() {
36 void FakeSpeechRecognitionManager::WaitForRecognitionStarted() {
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
38 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner
;
39 recognition_started_closure_
= runner
->QuitClosure();
41 recognition_started_closure_
.Reset();
44 void FakeSpeechRecognitionManager::SetFakeResult(const std::string
& value
) {
48 int FakeSpeechRecognitionManager::CreateSession(
49 const SpeechRecognitionSessionConfig
& config
) {
50 VLOG(1) << "FAKE CreateSession invoked.";
51 EXPECT_EQ(0, session_id_
);
52 EXPECT_EQ(NULL
, listener_
);
53 listener_
= config
.event_listener
.get();
54 if (config
.grammars
.size() > 0)
55 grammar_
= config
.grammars
[0].url
;
56 session_ctx_
= config
.initial_context
;
57 session_config_
= config
;
62 void FakeSpeechRecognitionManager::StartSession(int session_id
) {
63 VLOG(1) << "FAKE StartSession invoked.";
64 EXPECT_EQ(session_id
, session_id_
);
65 EXPECT_TRUE(listener_
!= NULL
);
67 if (should_send_fake_response_
) {
68 // Give the fake result in a short while.
69 base::MessageLoop::current()->PostTask(
72 &FakeSpeechRecognitionManager::SetFakeRecognitionResult
,
73 // This class does not need to be refcounted (typically done by
74 // PostTask) since it will outlive the test and gets released only
75 // when the test shuts down. Disabling refcounting here saves a bit
76 // of unnecessary code and the factory method can return a plain
77 // pointer below as required by the real code.
78 base::Unretained(this)));
80 if (!recognition_started_closure_
.is_null()) {
81 BrowserThread::PostTask(
84 base::Bind(&RunCallback
, recognition_started_closure_
));
88 void FakeSpeechRecognitionManager::AbortSession(int session_id
) {
89 VLOG(1) << "FAKE AbortSession invoked.";
90 EXPECT_EQ(session_id_
, session_id
);
95 void FakeSpeechRecognitionManager::StopAudioCaptureForSession(int session_id
) {
96 VLOG(1) << "StopRecording invoked.";
97 EXPECT_EQ(session_id_
, session_id
);
98 // Nothing to do here since we aren't really recording.
101 void FakeSpeechRecognitionManager::AbortAllSessionsForRenderProcess(
102 int render_process_id
) {
103 VLOG(1) << "CancelAllRequestsWithDelegate invoked.";
104 EXPECT_TRUE(should_send_fake_response_
||
105 session_ctx_
.render_process_id
== render_process_id
);
106 did_cancel_all_
= true;
109 void FakeSpeechRecognitionManager::AbortAllSessionsForRenderView(
110 int render_process_id
, int render_view_id
) {
114 bool FakeSpeechRecognitionManager::HasAudioInputDevices() { return true; }
116 base::string16
FakeSpeechRecognitionManager::GetAudioInputDeviceModel() {
117 return base::string16();
120 int FakeSpeechRecognitionManager::GetSession(int render_process_id
,
122 int request_id
) const {
123 return session_ctx_
.render_process_id
== render_process_id
&&
124 session_ctx_
.render_view_id
== render_view_id
&&
125 session_ctx_
.request_id
== request_id
;
128 const SpeechRecognitionSessionConfig
&
129 FakeSpeechRecognitionManager::GetSessionConfig(int session_id
) const {
130 EXPECT_EQ(session_id
, session_id_
);
131 return session_config_
;
134 SpeechRecognitionSessionContext
FakeSpeechRecognitionManager::GetSessionContext(
135 int session_id
) const {
136 EXPECT_EQ(session_id
, session_id_
);
140 void FakeSpeechRecognitionManager::SetFakeRecognitionResult() {
141 if (!session_id_
) // Do a check in case we were cancelled..
144 VLOG(1) << "Setting fake recognition result.";
145 listener_
->OnAudioEnd(session_id_
);
146 SpeechRecognitionResult result
;
147 result
.hypotheses
.push_back(SpeechRecognitionHypothesis(
148 base::ASCIIToUTF16(kTestResult
), 1.0));
149 SpeechRecognitionResults results
;
150 results
.push_back(result
);
151 listener_
->OnRecognitionResults(session_id_
, results
);
152 listener_
->OnRecognitionEnd(session_id_
);
155 VLOG(1) << "Finished setting fake recognition result.";
158 } // namespace content