| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
| 6 #include "content/browser/speech/speech_recognition_request.h" | 6 #include "content/browser/speech/speech_recognition_request.h" |
| 7 #include "content/test/test_url_fetcher_factory.h" | 7 #include "content/test/test_url_fetcher_factory.h" |
| 8 #include "net/url_request/url_request_context_getter.h" | 8 #include "net/url_request/url_request_context_getter.h" |
| 9 #include "net/url_request/url_request_status.h" | 9 #include "net/url_request/url_request_status.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace speech_input { | 12 namespace speech_input { |
| 13 | 13 |
| 14 class SpeechRecognitionRequestTest : public SpeechRecognitionRequestDelegate, | 14 class SpeechRecognitionRequestTest : public SpeechRecognitionRequestDelegate, |
| 15 public testing::Test { | 15 public testing::Test { |
| 16 public: | 16 public: |
| 17 SpeechRecognitionRequestTest() : error_(false) { } | 17 SpeechRecognitionRequestTest() { } |
| 18 | 18 |
| 19 // Creates a speech recognition request and invokes it's URL fetcher delegate | 19 // Creates a speech recognition request and invokes it's URL fetcher delegate |
| 20 // with the given test data. | 20 // with the given test data. |
| 21 void CreateAndTestRequest(bool success, const std::string& http_response); | 21 void CreateAndTestRequest(bool success, const std::string& http_response); |
| 22 | 22 |
| 23 // SpeechRecognitionRequestDelegate methods. | 23 // SpeechRecognitionRequestDelegate methods. |
| 24 virtual void SetRecognitionResult(bool error, | 24 virtual void SetRecognitionResult(const SpeechInputResult& result) OVERRIDE { |
| 25 const SpeechInputResultArray& result) { | |
| 26 error_ = error; | |
| 27 result_ = result; | 25 result_ = result; |
| 28 } | 26 } |
| 29 | 27 |
| 30 protected: | 28 protected: |
| 31 MessageLoop message_loop_; | 29 MessageLoop message_loop_; |
| 32 TestURLFetcherFactory url_fetcher_factory_; | 30 TestURLFetcherFactory url_fetcher_factory_; |
| 33 bool error_; | 31 SpeechInputResult result_; |
| 34 SpeechInputResultArray result_; | |
| 35 }; | 32 }; |
| 36 | 33 |
| 37 void SpeechRecognitionRequestTest::CreateAndTestRequest( | 34 void SpeechRecognitionRequestTest::CreateAndTestRequest( |
| 38 bool success, const std::string& http_response) { | 35 bool success, const std::string& http_response) { |
| 39 SpeechRecognitionRequest request(NULL, this); | 36 SpeechRecognitionRequest request(NULL, this); |
| 40 request.Start(std::string(), std::string(), false, std::string(), | 37 request.Start(std::string(), std::string(), false, std::string(), |
| 41 std::string(), std::string()); | 38 std::string(), std::string()); |
| 42 request.UploadAudioChunk(std::string(" "), true); | 39 request.UploadAudioChunk(std::string(" "), true); |
| 43 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 40 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 44 ASSERT_TRUE(fetcher); | 41 ASSERT_TRUE(fetcher); |
| 45 | 42 |
| 46 fetcher->set_url(fetcher->original_url()); | 43 fetcher->set_url(fetcher->original_url()); |
| 47 net::URLRequestStatus status; | 44 net::URLRequestStatus status; |
| 48 status.set_status(success ? net::URLRequestStatus::SUCCESS : | 45 status.set_status(success ? net::URLRequestStatus::SUCCESS : |
| 49 net::URLRequestStatus::FAILED); | 46 net::URLRequestStatus::FAILED); |
| 50 fetcher->set_status(status); | 47 fetcher->set_status(status); |
| 51 fetcher->set_response_code(success ? 200 : 500); | 48 fetcher->set_response_code(success ? 200 : 500); |
| 52 fetcher->SetResponseString(http_response); | 49 fetcher->SetResponseString(http_response); |
| 53 | 50 |
| 54 fetcher->delegate()->OnURLFetchComplete(fetcher); | 51 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 55 // Parsed response will be available in result_. | 52 // Parsed response will be available in result_. |
| 56 } | 53 } |
| 57 | 54 |
| 58 TEST_F(SpeechRecognitionRequestTest, BasicTest) { | 55 TEST_F(SpeechRecognitionRequestTest, BasicTest) { |
| 59 // Normal success case with one result. | 56 // Normal success case with one result. |
| 60 CreateAndTestRequest(true, | 57 CreateAndTestRequest(true, |
| 61 "{\"hypotheses\":[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); | 58 "{\"status\":0,\"hypotheses\":" |
| 62 EXPECT_FALSE(error_); | 59 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); |
| 63 EXPECT_EQ(1U, result_.size()); | 60 EXPECT_EQ(result_.error, kErrorNone); |
| 64 EXPECT_EQ(ASCIIToUTF16("123456"), result_[0].utterance); | 61 EXPECT_EQ(1U, result_.hypotheses.size()); |
| 65 EXPECT_EQ(0.9, result_[0].confidence); | 62 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance); |
| 63 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); |
| 66 | 64 |
| 67 // Normal success case with multiple results. | 65 // Normal success case with multiple results. |
| 68 CreateAndTestRequest(true, | 66 CreateAndTestRequest(true, |
| 69 "{\"hypotheses\":[{\"utterance\":\"hello\",\"confidence\":0.9}," | 67 "{\"status\":0,\"hypotheses\":[" |
| 68 "{\"utterance\":\"hello\",\"confidence\":0.9}," |
| 70 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); | 69 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); |
| 71 EXPECT_FALSE(error_); | 70 EXPECT_EQ(result_.error, kErrorNone); |
| 72 EXPECT_EQ(2u, result_.size()); | 71 EXPECT_EQ(2u, result_.hypotheses.size()); |
| 73 EXPECT_EQ(ASCIIToUTF16("hello"), result_[0].utterance); | 72 EXPECT_EQ(ASCIIToUTF16("hello"), result_.hypotheses[0].utterance); |
| 74 EXPECT_EQ(0.9, result_[0].confidence); | 73 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); |
| 75 EXPECT_EQ(ASCIIToUTF16("123456"), result_[1].utterance); | 74 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[1].utterance); |
| 76 EXPECT_EQ(0.5, result_[1].confidence); | 75 EXPECT_EQ(0.5, result_.hypotheses[1].confidence); |
| 77 | 76 |
| 78 // Zero results. | 77 // Zero results. |
| 79 CreateAndTestRequest(true, "{\"hypotheses\":[]}"); | 78 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}"); |
| 80 EXPECT_FALSE(error_); | 79 EXPECT_EQ(result_.error, kErrorNone); |
| 81 EXPECT_EQ(0U, result_.size()); | 80 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 82 | 81 |
| 83 // Http failure case. | 82 // Http failure case. |
| 84 CreateAndTestRequest(false, ""); | 83 CreateAndTestRequest(false, ""); |
| 85 EXPECT_TRUE(error_); | 84 EXPECT_EQ(result_.error, kErrorNetwork); |
| 86 EXPECT_EQ(0U, result_.size()); | 85 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 86 |
| 87 // Invalid status case. |
| 88 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}"); |
| 89 EXPECT_EQ(result_.error, kErrorNetwork); |
| 90 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 91 |
| 92 // Server-side error case. |
| 93 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}"); |
| 94 EXPECT_EQ(result_.error, kErrorNetwork); |
| 95 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 87 | 96 |
| 88 // Malformed JSON case. | 97 // Malformed JSON case. |
| 89 CreateAndTestRequest(true, "{\"hypotheses\":[{\"unknownkey\":\"hello\"}]}"); | 98 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":" |
| 90 EXPECT_TRUE(error_); | 99 "[{\"unknownkey\":\"hello\"}]}"); |
| 91 EXPECT_EQ(0U, result_.size()); | 100 EXPECT_EQ(result_.error, kErrorNetwork); |
| 101 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 92 } | 102 } |
| 93 | 103 |
| 94 } // namespace speech_input | 104 } // namespace speech_input |
| OLD | NEW |