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

Side by Side Diff: content/browser/speech/google_one_shot_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: Fixed compilation issues on windows. Created 8 years, 8 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_one_shot_remote_engine.h"
9 #include "content/public/common/speech_recognition_error.h"
9 #include "content/public/common/speech_recognition_result.h" 10 #include "content/public/common/speech_recognition_result.h"
10 #include "content/test/test_url_fetcher_factory.h" 11 #include "content/test/test_url_fetcher_factory.h"
11 #include "net/url_request/url_request_context_getter.h" 12 #include "net/url_request/url_request_context_getter.h"
12 #include "net/url_request/url_request_status.h" 13 #include "net/url_request/url_request_status.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 namespace speech { 16 namespace speech {
16 17
17 class SpeechRecognitionRequestTest : public SpeechRecognitionRequestDelegate, 18 class GoogleOneShotRemoteEngineTest
18 public testing::Test { 19 : public SpeechRecognitionEngineDelegate,
20 public testing::Test {
19 public: 21 public:
20 SpeechRecognitionRequestTest() { } 22 GoogleOneShotRemoteEngineTest()
23 : error_(content::SPEECH_RECOGNITION_ERROR_NONE) {}
21 24
22 // Creates a speech recognition request and invokes it's URL fetcher delegate 25 // Creates a speech recognition request and invokes it's URL fetcher delegate
23 // with the given test data. 26 // with the given test data.
24 void CreateAndTestRequest(bool success, const std::string& http_response); 27 void CreateAndTestRequest(bool success, const std::string& http_response);
25 28
26 // SpeechRecognitionRequestDelegate methods. 29 // SpeechRecognitionRequestDelegate methods.
27 virtual void SetRecognitionResult( 30 virtual void OnSpeechRecognitionEngineResult(
28 const content::SpeechRecognitionResult& result) OVERRIDE { 31 const content::SpeechRecognitionResult& result) OVERRIDE {
29 result_ = result; 32 result_ = result;
30 } 33 }
31 34
35 virtual void OnSpeechRecognitionEngineError(
36 const content::SpeechRecognitionError& error) OVERRIDE {
37 error_ = error.code;
38 }
39
32 protected: 40 protected:
33 MessageLoop message_loop_; 41 MessageLoop message_loop_;
34 TestURLFetcherFactory url_fetcher_factory_; 42 TestURLFetcherFactory url_fetcher_factory_;
43 content::SpeechRecognitionErrorCode error_;
35 content::SpeechRecognitionResult result_; 44 content::SpeechRecognitionResult result_;
36 }; 45 };
37 46
38 void SpeechRecognitionRequestTest::CreateAndTestRequest( 47 void GoogleOneShotRemoteEngineTest::CreateAndTestRequest(
39 bool success, const std::string& http_response) { 48 bool success, const std::string& http_response) {
40 SpeechRecognitionRequest request(NULL, this); 49 GoogleOneShotRemoteEngine 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'}; 50 unsigned char dummy_audio_buffer_data[2] = {'\0', '\0'};
44 AudioChunk dummy_audio_chunk(&dummy_audio_buffer_data[0], 51 AudioChunk dummy_audio_chunk(&dummy_audio_buffer_data[0],
45 sizeof(dummy_audio_buffer_data), 52 sizeof(dummy_audio_buffer_data),
46 2 /* bytes per sample */); 53 2 /* bytes per sample */);
47 request.UploadAudioChunk(dummy_audio_chunk, true); 54 client.set_delegate(this);
55 client.StartRecognition();
56 client.TakeAudioChunk(dummy_audio_chunk);
57 client.AudioChunksEnded();
48 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 58 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
49 ASSERT_TRUE(fetcher); 59 ASSERT_TRUE(fetcher);
50 60
51 fetcher->set_url(fetcher->GetOriginalURL()); 61 fetcher->set_url(fetcher->GetOriginalURL());
52 net::URLRequestStatus status; 62 net::URLRequestStatus status;
53 status.set_status(success ? net::URLRequestStatus::SUCCESS : 63 status.set_status(success ? net::URLRequestStatus::SUCCESS :
54 net::URLRequestStatus::FAILED); 64 net::URLRequestStatus::FAILED);
55 fetcher->set_status(status); 65 fetcher->set_status(status);
56 fetcher->set_response_code(success ? 200 : 500); 66 fetcher->set_response_code(success ? 200 : 500);
57 fetcher->SetResponseString(http_response); 67 fetcher->SetResponseString(http_response);
58 68
59 fetcher->delegate()->OnURLFetchComplete(fetcher); 69 fetcher->delegate()->OnURLFetchComplete(fetcher);
60 // Parsed response will be available in result_. 70 // Parsed response will be available in result_.
61 } 71 }
62 72
63 TEST_F(SpeechRecognitionRequestTest, BasicTest) { 73 TEST_F(GoogleOneShotRemoteEngineTest, BasicTest) {
64 // Normal success case with one result. 74 // Normal success case with one result.
65 CreateAndTestRequest(true, 75 CreateAndTestRequest(true,
66 "{\"status\":0,\"hypotheses\":" 76 "{\"status\":0,\"hypotheses\":"
67 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); 77 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}");
68 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NONE); 78 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NONE);
69 EXPECT_EQ(1U, result_.hypotheses.size()); 79 EXPECT_EQ(1U, result_.hypotheses.size());
70 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance); 80 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance);
71 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); 81 EXPECT_EQ(0.9, result_.hypotheses[0].confidence);
72 82
73 // Normal success case with multiple results. 83 // Normal success case with multiple results.
74 CreateAndTestRequest(true, 84 CreateAndTestRequest(true,
75 "{\"status\":0,\"hypotheses\":[" 85 "{\"status\":0,\"hypotheses\":["
76 "{\"utterance\":\"hello\",\"confidence\":0.9}," 86 "{\"utterance\":\"hello\",\"confidence\":0.9},"
77 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); 87 "{\"utterance\":\"123456\",\"confidence\":0.5}]}");
78 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NONE); 88 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NONE);
79 EXPECT_EQ(2u, result_.hypotheses.size()); 89 EXPECT_EQ(2u, result_.hypotheses.size());
80 EXPECT_EQ(ASCIIToUTF16("hello"), result_.hypotheses[0].utterance); 90 EXPECT_EQ(ASCIIToUTF16("hello"), result_.hypotheses[0].utterance);
81 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); 91 EXPECT_EQ(0.9, result_.hypotheses[0].confidence);
82 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[1].utterance); 92 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[1].utterance);
83 EXPECT_EQ(0.5, result_.hypotheses[1].confidence); 93 EXPECT_EQ(0.5, result_.hypotheses[1].confidence);
84 94
85 // Zero results. 95 // Zero results.
86 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}"); 96 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}");
87 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NONE); 97 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NONE);
88 EXPECT_EQ(0U, result_.hypotheses.size()); 98 EXPECT_EQ(0U, result_.hypotheses.size());
89 99
90 // Http failure case. 100 // Http failure case.
91 CreateAndTestRequest(false, ""); 101 CreateAndTestRequest(false, "");
92 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NETWORK); 102 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NETWORK);
93 EXPECT_EQ(0U, result_.hypotheses.size()); 103 EXPECT_EQ(0U, result_.hypotheses.size());
94 104
95 // Invalid status case. 105 // Invalid status case.
96 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}"); 106 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}");
97 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NETWORK); 107 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NETWORK);
98 EXPECT_EQ(0U, result_.hypotheses.size()); 108 EXPECT_EQ(0U, result_.hypotheses.size());
99 109
100 // Server-side error case. 110 // Server-side error case.
101 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}"); 111 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}");
102 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NETWORK); 112 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NETWORK);
103 EXPECT_EQ(0U, result_.hypotheses.size()); 113 EXPECT_EQ(0U, result_.hypotheses.size());
104 114
105 // Malformed JSON case. 115 // Malformed JSON case.
106 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":" 116 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":"
107 "[{\"unknownkey\":\"hello\"}]}"); 117 "[{\"unknownkey\":\"hello\"}]}");
108 EXPECT_EQ(result_.error, content::SPEECH_RECOGNITION_ERROR_NETWORK); 118 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NETWORK);
109 EXPECT_EQ(0U, result_.hypotheses.size()); 119 EXPECT_EQ(0U, result_.hypotheses.size());
110 } 120 }
111 121
112 } // namespace speech 122 } // namespace speech
OLDNEW
« no previous file with comments | « content/browser/speech/google_one_shot_remote_engine.cc ('k') | content/browser/speech/speech_recognition_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698