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

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

Issue 11316330: Revert 170701 since the WebKit change has now been rolled in again. (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 OnSpeechRecognitionEngineResult( 29 virtual void OnSpeechRecognitionEngineResults(
30 const SpeechRecognitionResult& result) OVERRIDE { 30 const SpeechRecognitionResults& results) OVERRIDE {
31 result_ = result; 31 results_ = results;
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
39 protected: 45 protected:
40 MessageLoop message_loop_; 46 MessageLoop message_loop_;
41 net::TestURLFetcherFactory url_fetcher_factory_; 47 net::TestURLFetcherFactory url_fetcher_factory_;
42 SpeechRecognitionErrorCode error_; 48 SpeechRecognitionErrorCode error_;
43 SpeechRecognitionResult result_; 49 SpeechRecognitionResults results_;
44 }; 50 };
45 51
46 void GoogleOneShotRemoteEngineTest::CreateAndTestRequest( 52 void GoogleOneShotRemoteEngineTest::CreateAndTestRequest(
47 bool success, const std::string& http_response) { 53 bool success, const std::string& http_response) {
48 GoogleOneShotRemoteEngine client(NULL); 54 GoogleOneShotRemoteEngine client(NULL);
49 unsigned char dummy_audio_buffer_data[2] = {'\0', '\0'}; 55 unsigned char dummy_audio_buffer_data[2] = {'\0', '\0'};
50 scoped_refptr<AudioChunk> dummy_audio_chunk( 56 scoped_refptr<AudioChunk> dummy_audio_chunk(
51 new AudioChunk(&dummy_audio_buffer_data[0], 57 new AudioChunk(&dummy_audio_buffer_data[0],
52 sizeof(dummy_audio_buffer_data), 58 sizeof(dummy_audio_buffer_data),
53 2 /* bytes per sample */)); 59 2 /* bytes per sample */));
54 client.set_delegate(this); 60 client.set_delegate(this);
55 client.StartRecognition(); 61 client.StartRecognition();
56 client.TakeAudioChunk(*dummy_audio_chunk); 62 client.TakeAudioChunk(*dummy_audio_chunk);
57 client.AudioChunksEnded(); 63 client.AudioChunksEnded();
58 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 64 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
59 ASSERT_TRUE(fetcher); 65 ASSERT_TRUE(fetcher);
60 66
61 fetcher->set_url(fetcher->GetOriginalURL()); 67 fetcher->set_url(fetcher->GetOriginalURL());
62 net::URLRequestStatus status; 68 net::URLRequestStatus status;
63 status.set_status(success ? net::URLRequestStatus::SUCCESS : 69 status.set_status(success ? net::URLRequestStatus::SUCCESS :
64 net::URLRequestStatus::FAILED); 70 net::URLRequestStatus::FAILED);
65 fetcher->set_status(status); 71 fetcher->set_status(status);
66 fetcher->set_response_code(success ? 200 : 500); 72 fetcher->set_response_code(success ? 200 : 500);
67 fetcher->SetResponseString(http_response); 73 fetcher->SetResponseString(http_response);
68 74
69 fetcher->delegate()->OnURLFetchComplete(fetcher); 75 fetcher->delegate()->OnURLFetchComplete(fetcher);
70 // Parsed response will be available in result_. 76 // Parsed response will be available in result().
71 } 77 }
72 78
73 TEST_F(GoogleOneShotRemoteEngineTest, BasicTest) { 79 TEST_F(GoogleOneShotRemoteEngineTest, BasicTest) {
74 // Normal success case with one result. 80 // Normal success case with one result.
75 CreateAndTestRequest(true, 81 CreateAndTestRequest(true,
76 "{\"status\":0,\"hypotheses\":" 82 "{\"status\":0,\"hypotheses\":"
77 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); 83 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}");
78 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE); 84 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE);
79 EXPECT_EQ(1U, result_.hypotheses.size()); 85 EXPECT_EQ(1U, result().hypotheses.size());
80 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance); 86 EXPECT_EQ(ASCIIToUTF16("123456"), result().hypotheses[0].utterance);
81 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); 87 EXPECT_EQ(0.9, result().hypotheses[0].confidence);
82 88
83 // Normal success case with multiple results. 89 // Normal success case with multiple results.
84 CreateAndTestRequest(true, 90 CreateAndTestRequest(true,
85 "{\"status\":0,\"hypotheses\":[" 91 "{\"status\":0,\"hypotheses\":["
86 "{\"utterance\":\"hello\",\"confidence\":0.9}," 92 "{\"utterance\":\"hello\",\"confidence\":0.9},"
87 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); 93 "{\"utterance\":\"123456\",\"confidence\":0.5}]}");
88 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE); 94 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE);
89 EXPECT_EQ(2u, result_.hypotheses.size()); 95 EXPECT_EQ(2u, result().hypotheses.size());
90 EXPECT_EQ(ASCIIToUTF16("hello"), result_.hypotheses[0].utterance); 96 EXPECT_EQ(ASCIIToUTF16("hello"), result().hypotheses[0].utterance);
91 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); 97 EXPECT_EQ(0.9, result().hypotheses[0].confidence);
92 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[1].utterance); 98 EXPECT_EQ(ASCIIToUTF16("123456"), result().hypotheses[1].utterance);
93 EXPECT_EQ(0.5, result_.hypotheses[1].confidence); 99 EXPECT_EQ(0.5, result().hypotheses[1].confidence);
94 100
95 // Zero results. 101 // Zero results.
96 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}"); 102 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}");
97 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE); 103 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE);
98 EXPECT_EQ(0U, result_.hypotheses.size()); 104 EXPECT_EQ(0U, result().hypotheses.size());
99 105
100 // Http failure case. 106 // Http failure case.
101 CreateAndTestRequest(false, ""); 107 CreateAndTestRequest(false, "");
102 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); 108 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK);
103 EXPECT_EQ(0U, result_.hypotheses.size()); 109 EXPECT_EQ(0U, result().hypotheses.size());
104 110
105 // Invalid status case. 111 // Invalid status case.
106 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}"); 112 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}");
107 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); 113 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK);
108 EXPECT_EQ(0U, result_.hypotheses.size()); 114 EXPECT_EQ(0U, result().hypotheses.size());
109 115
110 // Server-side error case. 116 // Server-side error case.
111 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}"); 117 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}");
112 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); 118 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK);
113 EXPECT_EQ(0U, result_.hypotheses.size()); 119 EXPECT_EQ(0U, result().hypotheses.size());
114 120
115 // Malformed JSON case. 121 // Malformed JSON case.
116 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":" 122 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":"
117 "[{\"unknownkey\":\"hello\"}]}"); 123 "[{\"unknownkey\":\"hello\"}]}");
118 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); 124 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK);
119 EXPECT_EQ(0U, result_.hypotheses.size()); 125 EXPECT_EQ(0U, result().hypotheses.size());
120 } 126 }
121 127
122 } // namespace content 128 } // 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