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

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

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

Powered by Google App Engine
This is Rietveld 408576698