Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: chrome/browser/speech/speech_recognizer_unittest.cc

Issue 3124009: Adds SpeechRecognizer which provides a simple interface to record and recognize speech. (Closed)
Patch Set: Address comments. Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/speech/speech_recognizer.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 "chrome/browser/chrome_thread.h"
6 #include "chrome/browser/speech/speech_recognizer.h"
7 #include "chrome/common/net/test_url_fetcher_factory.h"
8 #include "media/audio/test_audio_input_controller_factory.h"
9 #include "net/url_request/url_request_status.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 using media::AudioInputController;
13 using media::TestAudioInputController;
14 using media::TestAudioInputControllerFactory;
15
16 namespace speech_input {
17
18 class SpeechRecognizerTest : public SpeechRecognizerDelegate,
19 public testing::Test {
20 public:
21 SpeechRecognizerTest()
22 : io_thread_(ChromeThread::IO, &message_loop_),
23 ALLOW_THIS_IN_INITIALIZER_LIST(
24 recognizer_(new SpeechRecognizer(this, 1))) {
25 }
26
27 void StartTest() {
28 EXPECT_TRUE(recognizer_->StartRecording());
29 }
30
31 // SpeechRecognizer::Delegate methods.
32 virtual void SetRecognitionResult(int render_view_id, bool error,
33 const string16& result) {
34 result_received_ = true;
35 }
36
37 virtual void DidCompleteRecording(int render_view_id) {
38 recording_complete_ = true;
39 }
40
41 virtual void DidCompleteRecognition(int render_view_id) {
42 recognition_complete_ = true;
43 }
44
45 // testing::Test methods.
46 virtual void SetUp() {
47 result_received_ = false;
48 recording_complete_ = false;
49 recognition_complete_ = false;
50 URLFetcher::set_factory(&url_fetcher_factory_);
51 AudioInputController::set_factory(&audio_input_controller_factory_);
52 }
53
54 virtual void TearDown() {
55 URLFetcher::set_factory(NULL);
56 AudioInputController::set_factory(NULL);
57 }
58
59 protected:
60 MessageLoopForIO message_loop_;
61 ChromeThread io_thread_;
62 scoped_refptr<SpeechRecognizer> recognizer_;
63 bool recording_complete_;
64 bool recognition_complete_;
65 bool result_received_;
66 TestURLFetcherFactory url_fetcher_factory_;
67 TestAudioInputControllerFactory audio_input_controller_factory_;
68 };
69
70 TEST_F(SpeechRecognizerTest, StopNoData) {
71 // Check for callbacks when stopping record before any audio gets recorded.
72 EXPECT_TRUE(recognizer_->StartRecording());
73 recognizer_->CancelRecognition();
74 EXPECT_FALSE(recording_complete_);
75 EXPECT_FALSE(recognition_complete_);
76 EXPECT_FALSE(result_received_);
77 }
78
79 TEST_F(SpeechRecognizerTest, CancelNoData) {
80 // Check for callbacks when canceling recognition before any audio gets
81 // recorded.
82 EXPECT_TRUE(recognizer_->StartRecording());
83 recognizer_->StopRecording();
84 EXPECT_TRUE(recording_complete_);
85 EXPECT_TRUE(recognition_complete_);
86 EXPECT_FALSE(result_received_);
87 }
88
89 TEST_F(SpeechRecognizerTest, StopWithData) {
90 uint8 data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
91
92 // Start recording, give some data and then stop. This should wait for the
93 // network callback to arrive before completion.
94 EXPECT_TRUE(recognizer_->StartRecording());
95 TestAudioInputController* controller =
96 audio_input_controller_factory_.controller();
97 ASSERT_TRUE(controller);
98 controller = audio_input_controller_factory_.controller();
99 ASSERT_TRUE(controller);
100 controller->event_handler()->OnData(controller, data, sizeof(data));
101 MessageLoop::current()->RunAllPending();
102 recognizer_->StopRecording();
103 EXPECT_TRUE(recording_complete_);
104 EXPECT_FALSE(recognition_complete_);
105 EXPECT_FALSE(result_received_);
106
107 // Issue the network callback to complete the process.
108 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
109 ASSERT_TRUE(fetcher);
110 URLRequestStatus status;
111 status.set_status(URLRequestStatus::SUCCESS);
112 fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(),
113 status, 200, ResponseCookies(), "");
114 EXPECT_TRUE(recognition_complete_);
115 EXPECT_TRUE(result_received_);
116 }
117
118 TEST_F(SpeechRecognizerTest, CancelWithData) {
119 uint8 data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
120
121 // Start recording, give some data and then cancel. This should not create
122 // a network request and finish immediately.
123 EXPECT_TRUE(recognizer_->StartRecording());
124 TestAudioInputController* controller =
125 audio_input_controller_factory_.controller();
126 ASSERT_TRUE(controller);
127 controller->event_handler()->OnData(controller, data, sizeof(data));
128 MessageLoop::current()->RunAllPending();
129 recognizer_->CancelRecognition();
130 EXPECT_EQ(NULL, url_fetcher_factory_.GetFetcherByID(0));
131 EXPECT_FALSE(recording_complete_);
132 EXPECT_FALSE(recognition_complete_);
133 EXPECT_FALSE(result_received_);
134 }
135
136 TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) {
137 // Check if things tear down properly if AudioInputController threw an error.
138 EXPECT_TRUE(recognizer_->StartRecording());
139 TestAudioInputController* controller =
140 audio_input_controller_factory_.controller();
141 ASSERT_TRUE(controller);
142 controller->event_handler()->OnError(controller, 0);
143 MessageLoop::current()->RunAllPending();
144 EXPECT_TRUE(recording_complete_);
145 EXPECT_TRUE(recognition_complete_);
146 EXPECT_FALSE(result_received_);
147 }
148
149 TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) {
150 uint8 data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
151
152 // Check if things tear down properly if AudioInputController threw an error
153 // after giving some audio data.
154 EXPECT_TRUE(recognizer_->StartRecording());
155 TestAudioInputController* controller =
156 audio_input_controller_factory_.controller();
157 ASSERT_TRUE(controller);
158 controller->event_handler()->OnData(controller, data, sizeof(data));
159 controller->event_handler()->OnError(controller, 0);
160 MessageLoop::current()->RunAllPending();
161 EXPECT_EQ(NULL, url_fetcher_factory_.GetFetcherByID(0));
162 EXPECT_TRUE(recording_complete_);
163 EXPECT_TRUE(recognition_complete_);
164 EXPECT_FALSE(result_received_);
165 }
166
167 } // namespace speech_input
OLDNEW
« no previous file with comments | « chrome/browser/speech/speech_recognizer.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698