• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "content/public/test/fake_speech_recognition_manager.h"
6 
7 #include "base/bind.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"
14 
15 namespace {
16 const char kTestResult[] = "Pictures of the moon";
17 
RunCallback(const base::Closure recognition_started_closure)18 void RunCallback(const base::Closure recognition_started_closure) {
19   recognition_started_closure.Run();
20 }
21 }  // namespace
22 
23 namespace content {
24 
FakeSpeechRecognitionManager()25 FakeSpeechRecognitionManager::FakeSpeechRecognitionManager()
26     : session_id_(0),
27       listener_(NULL),
28       fake_result_(kTestResult),
29       did_cancel_all_(false),
30       should_send_fake_response_(true) {
31 }
32 
~FakeSpeechRecognitionManager()33 FakeSpeechRecognitionManager::~FakeSpeechRecognitionManager() {
34 }
35 
WaitForRecognitionStarted()36 void FakeSpeechRecognitionManager::WaitForRecognitionStarted() {
37   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
38   scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
39   recognition_started_closure_ = runner->QuitClosure();
40   runner->Run();
41   recognition_started_closure_.Reset();
42 }
43 
SetFakeResult(const std::string & value)44 void FakeSpeechRecognitionManager::SetFakeResult(const std::string& value) {
45   fake_result_ = value;
46 }
47 
CreateSession(const SpeechRecognitionSessionConfig & config)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;
58   session_id_ = 1;
59   return session_id_;
60 }
61 
StartSession(int session_id)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);
66 
67   if (should_send_fake_response_) {
68     // Give the fake result in a short while.
69     base::MessageLoop::current()->PostTask(
70         FROM_HERE,
71         base::Bind(
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)));
79   }
80   if (!recognition_started_closure_.is_null()) {
81     BrowserThread::PostTask(
82         BrowserThread::UI,
83         FROM_HERE,
84         base::Bind(&RunCallback, recognition_started_closure_));
85   }
86 }
87 
AbortSession(int session_id)88 void FakeSpeechRecognitionManager::AbortSession(int session_id) {
89   VLOG(1) << "FAKE AbortSession invoked.";
90   EXPECT_EQ(session_id_, session_id);
91   session_id_ = 0;
92   listener_ = NULL;
93 }
94 
StopAudioCaptureForSession(int 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.
99 }
100 
AbortAllSessionsForRenderProcess(int render_process_id)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;
107 }
108 
AbortAllSessionsForRenderView(int render_process_id,int render_view_id)109 void FakeSpeechRecognitionManager::AbortAllSessionsForRenderView(
110     int render_process_id, int render_view_id) {
111   NOTREACHED();
112 }
113 
HasAudioInputDevices()114 bool FakeSpeechRecognitionManager::HasAudioInputDevices() { return true; }
115 
GetAudioInputDeviceModel()116 base::string16 FakeSpeechRecognitionManager::GetAudioInputDeviceModel() {
117   return base::string16();
118 }
119 
GetSession(int render_process_id,int render_view_id,int request_id) const120 int FakeSpeechRecognitionManager::GetSession(int render_process_id,
121                                              int render_view_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;
126 }
127 
128 const SpeechRecognitionSessionConfig&
GetSessionConfig(int session_id) const129     FakeSpeechRecognitionManager::GetSessionConfig(int session_id) const {
130   EXPECT_EQ(session_id, session_id_);
131   return session_config_;
132 }
133 
GetSessionContext(int session_id) const134 SpeechRecognitionSessionContext FakeSpeechRecognitionManager::GetSessionContext(
135     int session_id) const {
136   EXPECT_EQ(session_id, session_id_);
137   return session_ctx_;
138 }
139 
SetFakeRecognitionResult()140 void FakeSpeechRecognitionManager::SetFakeRecognitionResult() {
141   if (!session_id_)  // Do a check in case we were cancelled..
142     return;
143 
144   VLOG(1) << "Setting fake recognition result.";
145   listener_->OnAudioEnd(session_id_);
146   SpeechRecognitionResult result;
147   result.hypotheses.push_back(SpeechRecognitionHypothesis(
148       ASCIIToUTF16(kTestResult), 1.0));
149   SpeechRecognitionResults results;
150   results.push_back(result);
151   listener_->OnRecognitionResults(session_id_, results);
152   listener_->OnRecognitionEnd(session_id_);
153   session_id_ = 0;
154   listener_ = NULL;
155   VLOG(1) << "Finished setting fake recognition result.";
156 }
157 
158 }  // namespace content
159