IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / public / test / fake_speech_recognition_manager.cc
blob7f8e367a4326b73ca06e86c8830cc5ba0bbbe997
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"
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"
15 namespace {
16 const char kTestResult[] = "Pictures of the moon";
18 void RunCallback(const base::Closure recognition_started_closure) {
19 recognition_started_closure.Run();
21 } // namespace
23 namespace content {
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) {
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();
40 runner->Run();
41 recognition_started_closure_.Reset();
44 void FakeSpeechRecognitionManager::SetFakeResult(const std::string& value) {
45 fake_result_ = 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;
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_;
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(
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)));
80 if (!recognition_started_closure_.is_null()) {
81 BrowserThread::PostTask(
82 BrowserThread::UI,
83 FROM_HERE,
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);
91 session_id_ = 0;
92 listener_ = NULL;
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::AbortAllSessionsForListener(
102 SpeechRecognitionEventListener* listener) {
103 VLOG(1) << "CancelAllRequestsWithDelegate invoked.";
104 // listener_ is set to NULL if a fake result was received (see below), so
105 // check that listener_ matches the incoming parameter only when there is
106 // no fake result sent.
107 EXPECT_TRUE(should_send_fake_response_ || listener_ == listener);
108 did_cancel_all_ = true;
111 void FakeSpeechRecognitionManager::AbortAllSessionsForRenderView(
112 int render_process_id, int render_view_id) {
113 NOTREACHED();
116 bool FakeSpeechRecognitionManager::HasAudioInputDevices() { return true; }
118 base::string16 FakeSpeechRecognitionManager::GetAudioInputDeviceModel() {
119 return base::string16();
122 int FakeSpeechRecognitionManager::GetSession(int render_process_id,
123 int render_view_id,
124 int request_id) const {
125 return session_ctx_.render_process_id == render_process_id &&
126 session_ctx_.render_view_id == render_view_id &&
127 session_ctx_.request_id == request_id;
130 const SpeechRecognitionSessionConfig&
131 FakeSpeechRecognitionManager::GetSessionConfig(int session_id) const {
132 EXPECT_EQ(session_id, session_id_);
133 return session_config_;
136 SpeechRecognitionSessionContext FakeSpeechRecognitionManager::GetSessionContext(
137 int session_id) const {
138 EXPECT_EQ(session_id, session_id_);
139 return session_ctx_;
142 void FakeSpeechRecognitionManager::SetFakeRecognitionResult() {
143 if (!session_id_) // Do a check in case we were cancelled..
144 return;
146 VLOG(1) << "Setting fake recognition result.";
147 listener_->OnAudioEnd(session_id_);
148 SpeechRecognitionResult result;
149 result.hypotheses.push_back(SpeechRecognitionHypothesis(
150 base::ASCIIToUTF16(kTestResult), 1.0));
151 SpeechRecognitionResults results;
152 results.push_back(result);
153 listener_->OnRecognitionResults(session_id_, results);
154 listener_->OnRecognitionEnd(session_id_);
155 session_id_ = 0;
156 listener_ = NULL;
157 VLOG(1) << "Finished setting fake recognition result.";
160 } // namespace content