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/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "content/public/browser/speech_recognition_event_listener.h"
13 #include "content/public/browser/speech_recognition_manager_delegate.h"
14 #include "content/public/common/speech_recognition_result.h"
15 #include "content/public/test/test_utils.h"
16 #include "testing/gtest/include/gtest/gtest.h"
19 const char kTestResult
[] = "Pictures of the moon";
21 void RunCallback(const base::Closure recognition_started_closure
) {
22 recognition_started_closure
.Run();
28 FakeSpeechRecognitionManager::FakeSpeechRecognitionManager()
31 fake_result_(kTestResult
),
32 did_cancel_all_(false),
33 should_send_fake_response_(true),
37 void FakeSpeechRecognitionManager::SetDelegate(
38 SpeechRecognitionManagerDelegate
* delegate
) {
42 FakeSpeechRecognitionManager::~FakeSpeechRecognitionManager() {
43 // Expect the owner of |delegate_| to cleanup our reference before we shut
44 // down, just to be safe as we do not own |delegate_|.
48 void FakeSpeechRecognitionManager::WaitForRecognitionStarted() {
49 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
50 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner
;
51 recognition_started_closure_
= runner
->QuitClosure();
53 recognition_started_closure_
.Reset();
56 void FakeSpeechRecognitionManager::SetFakeResult(const std::string
& value
) {
60 int FakeSpeechRecognitionManager::CreateSession(
61 const SpeechRecognitionSessionConfig
& config
) {
62 VLOG(1) << "FAKE CreateSession invoked.";
63 EXPECT_EQ(0, session_id_
);
64 EXPECT_EQ(NULL
, listener_
);
65 listener_
= config
.event_listener
.get();
66 if (config
.grammars
.size() > 0)
67 grammar_
= config
.grammars
[0].url
;
68 session_ctx_
= config
.initial_context
;
69 session_config_
= config
;
74 void FakeSpeechRecognitionManager::StartSession(int session_id
) {
75 VLOG(1) << "FAKE StartSession invoked.";
76 EXPECT_EQ(session_id
, session_id_
);
77 EXPECT_TRUE(listener_
!= NULL
);
80 delegate_
->GetEventListener()->OnRecognitionStart(session_id_
);
82 if (should_send_fake_response_
) {
83 // Give the fake result in a short while.
84 base::ThreadTaskRunnerHandle::Get()->PostTask(
87 &FakeSpeechRecognitionManager::SetFakeRecognitionResult
,
88 // This class does not need to be refcounted (typically done by
89 // PostTask) since it will outlive the test and gets released only
90 // when the test shuts down. Disabling refcounting here saves a bit
91 // of unnecessary code and the factory method can return a plain
92 // pointer below as required by the real code.
93 base::Unretained(this)));
95 if (!recognition_started_closure_
.is_null()) {
96 BrowserThread::PostTask(
99 base::Bind(&RunCallback
, recognition_started_closure_
));
103 void FakeSpeechRecognitionManager::AbortSession(int session_id
) {
104 VLOG(1) << "FAKE AbortSession invoked.";
105 EXPECT_EQ(session_id_
, session_id
);
110 void FakeSpeechRecognitionManager::StopAudioCaptureForSession(int session_id
) {
111 VLOG(1) << "StopRecording invoked.";
112 EXPECT_EQ(session_id_
, session_id
);
113 // Nothing to do here since we aren't really recording.
116 void FakeSpeechRecognitionManager::AbortAllSessionsForRenderProcess(
117 int render_process_id
) {
118 VLOG(1) << "CancelAllRequestsWithDelegate invoked.";
119 EXPECT_TRUE(should_send_fake_response_
||
120 session_ctx_
.render_process_id
== render_process_id
);
121 did_cancel_all_
= true;
124 void FakeSpeechRecognitionManager::AbortAllSessionsForRenderView(
125 int render_process_id
, int render_view_id
) {
126 DCHECK(delegate_
); // We only expect this to be called via |delegate_|.
129 bool FakeSpeechRecognitionManager::HasAudioInputDevices() { return true; }
131 base::string16
FakeSpeechRecognitionManager::GetAudioInputDeviceModel() {
132 return base::string16();
135 int FakeSpeechRecognitionManager::GetSession(int render_process_id
,
137 int request_id
) const {
138 return session_ctx_
.render_process_id
== render_process_id
&&
139 session_ctx_
.render_view_id
== render_view_id
&&
140 session_ctx_
.request_id
== request_id
;
143 const SpeechRecognitionSessionConfig
&
144 FakeSpeechRecognitionManager::GetSessionConfig(int session_id
) const {
145 EXPECT_EQ(session_id
, session_id_
);
146 return session_config_
;
149 SpeechRecognitionSessionContext
FakeSpeechRecognitionManager::GetSessionContext(
150 int session_id
) const {
151 EXPECT_EQ(session_id
, session_id_
);
155 void FakeSpeechRecognitionManager::SetFakeRecognitionResult() {
156 if (!session_id_
) // Do a check in case we were cancelled..
159 VLOG(1) << "Setting fake recognition result.";
160 listener_
->OnAudioEnd(session_id_
);
161 SpeechRecognitionResult result
;
162 result
.hypotheses
.push_back(SpeechRecognitionHypothesis(
163 base::ASCIIToUTF16(kTestResult
), 1.0));
164 SpeechRecognitionResults results
;
165 results
.push_back(result
);
166 listener_
->OnRecognitionResults(session_id_
, results
);
167 listener_
->OnRecognitionEnd(session_id_
);
170 VLOG(1) << "Finished setting fake recognition result.";
173 } // namespace content