| OLD | NEW |
| 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 speech { | 16 namespace content { |
| 17 | 17 |
| 18 class GoogleOneShotRemoteEngineTest | 18 class GoogleOneShotRemoteEngineTest : public SpeechRecognitionEngineDelegate, |
| 19 : public SpeechRecognitionEngineDelegate, | 19 public testing::Test { |
| 20 public testing::Test { | |
| 21 public: | 20 public: |
| 22 GoogleOneShotRemoteEngineTest() | 21 GoogleOneShotRemoteEngineTest() |
| 23 : error_(content::SPEECH_RECOGNITION_ERROR_NONE) {} | 22 : error_(SPEECH_RECOGNITION_ERROR_NONE) {} |
| 24 | 23 |
| 25 // Creates a speech recognition request and invokes its URL fetcher delegate | 24 // Creates a speech recognition request and invokes its URL fetcher delegate |
| 26 // with the given test data. | 25 // with the given test data. |
| 27 void CreateAndTestRequest(bool success, const std::string& http_response); | 26 void CreateAndTestRequest(bool success, const std::string& http_response); |
| 28 | 27 |
| 29 // SpeechRecognitionRequestDelegate methods. | 28 // SpeechRecognitionRequestDelegate methods. |
| 30 virtual void OnSpeechRecognitionEngineResult( | 29 virtual void OnSpeechRecognitionEngineResult( |
| 31 const content::SpeechRecognitionResult& result) OVERRIDE { | 30 const SpeechRecognitionResult& result) OVERRIDE { |
| 32 result_ = result; | 31 result_ = result; |
| 33 } | 32 } |
| 34 | 33 |
| 35 virtual void OnSpeechRecognitionEngineError( | 34 virtual void OnSpeechRecognitionEngineError( |
| 36 const content::SpeechRecognitionError& error) OVERRIDE { | 35 const SpeechRecognitionError& error) OVERRIDE { |
| 37 error_ = error.code; | 36 error_ = error.code; |
| 38 } | 37 } |
| 39 | 38 |
| 40 protected: | 39 protected: |
| 41 MessageLoop message_loop_; | 40 MessageLoop message_loop_; |
| 42 net::TestURLFetcherFactory url_fetcher_factory_; | 41 net::TestURLFetcherFactory url_fetcher_factory_; |
| 43 content::SpeechRecognitionErrorCode error_; | 42 SpeechRecognitionErrorCode error_; |
| 44 content::SpeechRecognitionResult result_; | 43 SpeechRecognitionResult result_; |
| 45 }; | 44 }; |
| 46 | 45 |
| 47 void GoogleOneShotRemoteEngineTest::CreateAndTestRequest( | 46 void GoogleOneShotRemoteEngineTest::CreateAndTestRequest( |
| 48 bool success, const std::string& http_response) { | 47 bool success, const std::string& http_response) { |
| 49 GoogleOneShotRemoteEngine client(NULL); | 48 GoogleOneShotRemoteEngine client(NULL); |
| 50 unsigned char dummy_audio_buffer_data[2] = {'\0', '\0'}; | 49 unsigned char dummy_audio_buffer_data[2] = {'\0', '\0'}; |
| 51 scoped_refptr<AudioChunk> dummy_audio_chunk( | 50 scoped_refptr<AudioChunk> dummy_audio_chunk( |
| 52 new AudioChunk(&dummy_audio_buffer_data[0], | 51 new AudioChunk(&dummy_audio_buffer_data[0], |
| 53 sizeof(dummy_audio_buffer_data), | 52 sizeof(dummy_audio_buffer_data), |
| 54 2 /* bytes per sample */)); | 53 2 /* bytes per sample */)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 69 | 68 |
| 70 fetcher->delegate()->OnURLFetchComplete(fetcher); | 69 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 71 // Parsed response will be available in result_. | 70 // Parsed response will be available in result_. |
| 72 } | 71 } |
| 73 | 72 |
| 74 TEST_F(GoogleOneShotRemoteEngineTest, BasicTest) { | 73 TEST_F(GoogleOneShotRemoteEngineTest, BasicTest) { |
| 75 // Normal success case with one result. | 74 // Normal success case with one result. |
| 76 CreateAndTestRequest(true, | 75 CreateAndTestRequest(true, |
| 77 "{\"status\":0,\"hypotheses\":" | 76 "{\"status\":0,\"hypotheses\":" |
| 78 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); | 77 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); |
| 79 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NONE); | 78 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE); |
| 80 EXPECT_EQ(1U, result_.hypotheses.size()); | 79 EXPECT_EQ(1U, result_.hypotheses.size()); |
| 81 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance); | 80 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance); |
| 82 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); | 81 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); |
| 83 | 82 |
| 84 // Normal success case with multiple results. | 83 // Normal success case with multiple results. |
| 85 CreateAndTestRequest(true, | 84 CreateAndTestRequest(true, |
| 86 "{\"status\":0,\"hypotheses\":[" | 85 "{\"status\":0,\"hypotheses\":[" |
| 87 "{\"utterance\":\"hello\",\"confidence\":0.9}," | 86 "{\"utterance\":\"hello\",\"confidence\":0.9}," |
| 88 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); | 87 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); |
| 89 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NONE); | 88 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE); |
| 90 EXPECT_EQ(2u, result_.hypotheses.size()); | 89 EXPECT_EQ(2u, result_.hypotheses.size()); |
| 91 EXPECT_EQ(ASCIIToUTF16("hello"), result_.hypotheses[0].utterance); | 90 EXPECT_EQ(ASCIIToUTF16("hello"), result_.hypotheses[0].utterance); |
| 92 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); | 91 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); |
| 93 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[1].utterance); | 92 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[1].utterance); |
| 94 EXPECT_EQ(0.5, result_.hypotheses[1].confidence); | 93 EXPECT_EQ(0.5, result_.hypotheses[1].confidence); |
| 95 | 94 |
| 96 // Zero results. | 95 // Zero results. |
| 97 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}"); | 96 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}"); |
| 98 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NONE); | 97 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE); |
| 99 EXPECT_EQ(0U, result_.hypotheses.size()); | 98 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 100 | 99 |
| 101 // Http failure case. | 100 // Http failure case. |
| 102 CreateAndTestRequest(false, ""); | 101 CreateAndTestRequest(false, ""); |
| 103 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NETWORK); | 102 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); |
| 104 EXPECT_EQ(0U, result_.hypotheses.size()); | 103 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 105 | 104 |
| 106 // Invalid status case. | 105 // Invalid status case. |
| 107 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}"); | 106 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}"); |
| 108 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NETWORK); | 107 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); |
| 109 EXPECT_EQ(0U, result_.hypotheses.size()); | 108 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 110 | 109 |
| 111 // Server-side error case. | 110 // Server-side error case. |
| 112 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}"); | 111 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}"); |
| 113 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NETWORK); | 112 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); |
| 114 EXPECT_EQ(0U, result_.hypotheses.size()); | 113 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 115 | 114 |
| 116 // Malformed JSON case. | 115 // Malformed JSON case. |
| 117 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":" | 116 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":" |
| 118 "[{\"unknownkey\":\"hello\"}]}"); | 117 "[{\"unknownkey\":\"hello\"}]}"); |
| 119 EXPECT_EQ(error_, content::SPEECH_RECOGNITION_ERROR_NETWORK); | 118 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); |
| 120 EXPECT_EQ(0U, result_.hypotheses.size()); | 119 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 121 } | 120 } |
| 122 | 121 |
| 123 } // namespace speech | 122 } // namespace content |
| OLD | NEW |