| 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() : error_(false) { } |
| 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(bool error, |
| 25 const SpeechInputResultArray& result) { | 25 const SpeechInputResult& result) { |
| 26 error_ = error; | 26 error_ = error; |
| 27 result_ = result; | 27 result_ = result; |
| 28 } | 28 } |
| 29 | 29 |
| 30 protected: | 30 protected: |
| 31 MessageLoop message_loop_; | 31 MessageLoop message_loop_; |
| 32 TestURLFetcherFactory url_fetcher_factory_; | 32 TestURLFetcherFactory url_fetcher_factory_; |
| 33 bool error_; | 33 bool error_; |
| 34 SpeechInputResultArray result_; | 34 SpeechInputResult result_; |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 void SpeechRecognitionRequestTest::CreateAndTestRequest( | 37 void SpeechRecognitionRequestTest::CreateAndTestRequest( |
| 38 bool success, const std::string& http_response) { | 38 bool success, const std::string& http_response) { |
| 39 SpeechRecognitionRequest request(NULL, this); | 39 SpeechRecognitionRequest request(NULL, this); |
| 40 request.Start(std::string(), std::string(), false, std::string(), | 40 request.Start(std::string(), std::string(), false, std::string(), |
| 41 std::string(), std::string()); | 41 std::string(), std::string()); |
| 42 request.UploadAudioChunk(std::string(" "), true); | 42 request.UploadAudioChunk(std::string(" "), true); |
| 43 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 43 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 44 ASSERT_TRUE(fetcher); | 44 ASSERT_TRUE(fetcher); |
| 45 | 45 |
| 46 fetcher->set_url(fetcher->original_url()); | 46 fetcher->set_url(fetcher->original_url()); |
| 47 net::URLRequestStatus status; | 47 net::URLRequestStatus status; |
| 48 status.set_status(success ? net::URLRequestStatus::SUCCESS : | 48 status.set_status(success ? net::URLRequestStatus::SUCCESS : |
| 49 net::URLRequestStatus::FAILED); | 49 net::URLRequestStatus::FAILED); |
| 50 fetcher->set_status(status); | 50 fetcher->set_status(status); |
| 51 fetcher->set_response_code(success ? 200 : 500); | 51 fetcher->set_response_code(success ? 200 : 500); |
| 52 fetcher->SetResponseString(http_response); | 52 fetcher->SetResponseString(http_response); |
| 53 | 53 |
| 54 fetcher->delegate()->OnURLFetchComplete(fetcher); | 54 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 55 // Parsed response will be available in result_. | 55 // Parsed response will be available in result_. |
| 56 } | 56 } |
| 57 | 57 |
| 58 TEST_F(SpeechRecognitionRequestTest, BasicTest) { | 58 TEST_F(SpeechRecognitionRequestTest, BasicTest) { |
| 59 // Normal success case with one result. | 59 // Normal success case with one result. |
| 60 CreateAndTestRequest(true, | 60 CreateAndTestRequest(true, |
| 61 "{\"hypotheses\":[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); | 61 "{\"status\":0,\"hypotheses\":" |
| 62 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); |
| 62 EXPECT_FALSE(error_); | 63 EXPECT_FALSE(error_); |
| 63 EXPECT_EQ(1U, result_.size()); | 64 EXPECT_EQ(1U, result_.hypotheses.size()); |
| 64 EXPECT_EQ(ASCIIToUTF16("123456"), result_[0].utterance); | 65 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance); |
| 65 EXPECT_EQ(0.9, result_[0].confidence); | 66 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); |
| 66 | 67 |
| 67 // Normal success case with multiple results. | 68 // Normal success case with multiple results. |
| 68 CreateAndTestRequest(true, | 69 CreateAndTestRequest(true, |
| 69 "{\"hypotheses\":[{\"utterance\":\"hello\",\"confidence\":0.9}," | 70 "{\"status\":0,\"hypotheses\":[" |
| 71 "{\"utterance\":\"hello\",\"confidence\":0.9}," |
| 70 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); | 72 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); |
| 71 EXPECT_FALSE(error_); | 73 EXPECT_FALSE(error_); |
| 72 EXPECT_EQ(2u, result_.size()); | 74 EXPECT_EQ(2u, result_.hypotheses.size()); |
| 73 EXPECT_EQ(ASCIIToUTF16("hello"), result_[0].utterance); | 75 EXPECT_EQ(ASCIIToUTF16("hello"), result_.hypotheses[0].utterance); |
| 74 EXPECT_EQ(0.9, result_[0].confidence); | 76 EXPECT_EQ(0.9, result_.hypotheses[0].confidence); |
| 75 EXPECT_EQ(ASCIIToUTF16("123456"), result_[1].utterance); | 77 EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[1].utterance); |
| 76 EXPECT_EQ(0.5, result_[1].confidence); | 78 EXPECT_EQ(0.5, result_.hypotheses[1].confidence); |
| 77 | 79 |
| 78 // Zero results. | 80 // Zero results. |
| 79 CreateAndTestRequest(true, "{\"hypotheses\":[]}"); | 81 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}"); |
| 80 EXPECT_FALSE(error_); | 82 EXPECT_FALSE(error_); |
| 81 EXPECT_EQ(0U, result_.size()); | 83 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 82 | 84 |
| 83 // Http failure case. | 85 // Http failure case. |
| 84 CreateAndTestRequest(false, ""); | 86 CreateAndTestRequest(false, ""); |
| 85 EXPECT_TRUE(error_); | 87 EXPECT_TRUE(error_); |
| 86 EXPECT_EQ(0U, result_.size()); | 88 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 89 |
| 90 // Invalid status case. |
| 91 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}"); |
| 92 EXPECT_TRUE(error_); |
| 93 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 94 |
| 95 // Server-side error case. |
| 96 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}"); |
| 97 EXPECT_TRUE(error_); |
| 98 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 87 | 99 |
| 88 // Malformed JSON case. | 100 // Malformed JSON case. |
| 89 CreateAndTestRequest(true, "{\"hypotheses\":[{\"unknownkey\":\"hello\"}]}"); | 101 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":" |
| 102 "[{\"unknownkey\":\"hello\"}]}"); |
| 90 EXPECT_TRUE(error_); | 103 EXPECT_TRUE(error_); |
| 91 EXPECT_EQ(0U, result_.size()); | 104 EXPECT_EQ(0U, result_.hypotheses.size()); |
| 92 } | 105 } |
| 93 | 106 |
| 94 } // namespace speech_input | 107 } // namespace speech_input |
| OLD | NEW |