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

Side by Side Diff: content/browser/speech/google_ssfe_remote_engine_unittest.cc

Issue 9663066: Refactoring of chrome speech recognition architecture (CL1.3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased from master. Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/message_loop.h" 5 #include "base/message_loop.h"
6 #include "base/utf_string_conversions.h" 6 #include "base/utf_string_conversions.h"
7 #include "content/browser/speech/audio_buffer.h" 7 #include "content/browser/speech/audio_buffer.h"
8 #include "content/browser/speech/speech_recognition_request.h" 8 #include "content/browser/speech/google_ssfe_remote_engine.h"
9 #include "content/public/common/speech_recognition_result.h" 9 #include "content/public/common/speech_recognition_result.h"
10 #include "content/test/test_url_fetcher_factory.h" 10 #include "content/test/test_url_fetcher_factory.h"
11 #include "net/url_request/url_request_context_getter.h" 11 #include "net/url_request/url_request_context_getter.h"
12 #include "net/url_request/url_request_status.h" 12 #include "net/url_request/url_request_status.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace speech { 15 namespace speech {
16 16
17 class SpeechRecognitionRequestTest : public SpeechRecognitionRequestDelegate, 17 class GoogleSSFERemoteEngineTest
18 public testing::Test { 18 : public SpeechRecognitionEngineDelegate,
19 public testing::Test {
19 public: 20 public:
20 SpeechRecognitionRequestTest() { } 21 GoogleSSFERemoteEngineTest() { }
hans 2012/03/16 11:12:56 not really your code, but while you're here, there
Primiano Tucci (use gerrit) 2012/03/16 15:03:42 Done.
21 22
22 // Creates a speech recognition request and invokes it's URL fetcher delegate 23 // Creates a speech recognition request and invokes it's URL fetcher delegate
23 // with the given test data. 24 // with the given test data.
24 void CreateAndTestRequest(bool success, const std::string& http_response); 25 void CreateAndTestRequest(bool success, const std::string& http_response);
25 26
26 // SpeechRecognitionRequestDelegate methods. 27 // SpeechRecognitionRequestDelegate methods.
27 virtual void SetRecognitionResult( 28 virtual void OnSpeechEngineResult(
28 const content::SpeechRecognitionResult& result) OVERRIDE { 29 const content::SpeechRecognitionResult& result) OVERRIDE {
29 result_ = result; 30 result_ = result;
30 } 31 }
31 32
33 virtual void OnSpeechEngineError(
34 const content::SpeechRecognitionError& error) OVERRIDE {
35 result_.error = error.code;
36 }
37
32 protected: 38 protected:
33 MessageLoop message_loop_; 39 MessageLoop message_loop_;
34 TestURLFetcherFactory url_fetcher_factory_; 40 TestURLFetcherFactory url_fetcher_factory_;
35 content::SpeechRecognitionResult result_; 41 content::SpeechRecognitionResult result_;
36 }; 42 };
37 43
38 void SpeechRecognitionRequestTest::CreateAndTestRequest( 44 void GoogleSSFERemoteEngineTest::CreateAndTestRequest(
39 bool success, const std::string& http_response) { 45 bool success, const std::string& http_response) {
40 SpeechRecognitionRequest request(NULL, this); 46 GoogleSSFERemoteEngine client(NULL);
41 request.Start(std::string(), std::string(), false, std::string(),
42 std::string(), std::string());
43 unsigned char dummy_audio_buffer_data[2] = {'\0', '\0'}; 47 unsigned char dummy_audio_buffer_data[2] = {'\0', '\0'};
44 AudioChunk dummy_audio_chunk(&dummy_audio_buffer_data[0], 48 AudioChunk dummy_audio_chunk(&dummy_audio_buffer_data[0],
45 sizeof(dummy_audio_buffer_data), 49 sizeof(dummy_audio_buffer_data),
46 2 /* bytes per sample */); 50 2 /* bytes per sample */);
47 request.UploadAudioChunk(dummy_audio_chunk, true); 51 client.set_delegate(this);
52 client.SpeechRecognitionBegins();
53 client.PushSpeechAudio(dummy_audio_chunk);
54 client.SpeechAudioStreamComplete();
48 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 55 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
49 ASSERT_TRUE(fetcher); 56 ASSERT_TRUE(fetcher);
50 57
51 fetcher->set_url(fetcher->GetOriginalURL()); 58 fetcher->set_url(fetcher->GetOriginalURL());
52 net::URLRequestStatus status; 59 net::URLRequestStatus status;
53 status.set_status(success ? net::URLRequestStatus::SUCCESS : 60 status.set_status(success ? net::URLRequestStatus::SUCCESS :
54 net::URLRequestStatus::FAILED); 61 net::URLRequestStatus::FAILED);
55 fetcher->set_status(status); 62 fetcher->set_status(status);
56 fetcher->set_response_code(success ? 200 : 500); 63 fetcher->set_response_code(success ? 200 : 500);
57 fetcher->SetResponseString(http_response); 64 fetcher->SetResponseString(http_response);
58 65
59 fetcher->delegate()->OnURLFetchComplete(fetcher); 66 fetcher->delegate()->OnURLFetchComplete(fetcher);
60 // Parsed response will be available in result_. 67 // Parsed response will be available in result_.
61 } 68 }
62 69
63 TEST_F(SpeechRecognitionRequestTest, BasicTest) { 70 TEST_F(GoogleSSFERemoteEngineTest, BasicTest) {
64 // Normal success case with one result. 71 // Normal success case with one result.
65 CreateAndTestRequest(true, 72 CreateAndTestRequest(true,
66 "{\"status\":0,\"hypotheses\":" 73 "{\"status\":0,\"hypotheses\":"
67 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); 74 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}");
68 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NONE); 75 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NONE);
69 EXPECT_EQ(1U, result_.hypotheses.size()); 76 EXPECT_EQ(1U, result_.hypotheses.size());
70 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance); 77 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance);
71 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); 78 EXPECT_EQ(0.9, result_.hypotheses[0].confidence);
72 79
73 // Normal success case with multiple results. 80 // Normal success case with multiple results.
(...skipping 29 matching lines...) Expand all
103 EXPECT_EQ(0U, result_.hypotheses.size()); 110 EXPECT_EQ(0U, result_.hypotheses.size());
104 111
105 // Malformed JSON case. 112 // Malformed JSON case.
106 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":" 113 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":"
107 "[{\"unknownkey\":\"hello\"}]}"); 114 "[{\"unknownkey\":\"hello\"}]}");
108 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NETWORK); 115 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NETWORK);
109 EXPECT_EQ(0U, result_.hypotheses.size()); 116 EXPECT_EQ(0U, result_.hypotheses.size());
110 } 117 }
111 118
112 } // namespace speech 119 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698