| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/speech/speech_recognition_request.h" | 6 #include "chrome/browser/speech/speech_recognition_request.h" |
| 7 #include "chrome/common/net/url_request_context_getter.h" | 7 #include "chrome/common/net/url_request_context_getter.h" |
| 8 #include "chrome/common/net/test_url_fetcher_factory.h" | 8 #include "chrome/common/net/test_url_fetcher_factory.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, const string16& result) { | 24 virtual void SetRecognitionResult(bool error, |
| 25 const SpeechInputResultArray& result) { |
| 25 error_ = error; | 26 error_ = error; |
| 26 result_ = result; | 27 result_ = result; |
| 27 } | 28 } |
| 28 | 29 |
| 29 // testing::Test methods. | 30 // testing::Test methods. |
| 30 virtual void SetUp() { | 31 virtual void SetUp() { |
| 31 URLFetcher::set_factory(&url_fetcher_factory_); | 32 URLFetcher::set_factory(&url_fetcher_factory_); |
| 32 } | 33 } |
| 33 | 34 |
| 34 virtual void TearDown() { | 35 virtual void TearDown() { |
| 35 URLFetcher::set_factory(NULL); | 36 URLFetcher::set_factory(NULL); |
| 36 } | 37 } |
| 37 | 38 |
| 38 protected: | 39 protected: |
| 39 TestURLFetcherFactory url_fetcher_factory_; | 40 TestURLFetcherFactory url_fetcher_factory_; |
| 40 bool error_; | 41 bool error_; |
| 41 string16 result_; | 42 SpeechInputResultArray result_; |
| 42 }; | 43 }; |
| 43 | 44 |
| 44 void SpeechRecognitionRequestTest::CreateAndTestRequest( | 45 void SpeechRecognitionRequestTest::CreateAndTestRequest( |
| 45 bool success, const std::string& http_response) { | 46 bool success, const std::string& http_response) { |
| 46 SpeechRecognitionRequest request(NULL, GURL(""), this); | 47 SpeechRecognitionRequest request(NULL, GURL(""), this); |
| 47 request.Send(std::string(), std::string()); | 48 request.Send(std::string(), std::string()); |
| 48 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 49 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 49 ASSERT_TRUE(fetcher); | 50 ASSERT_TRUE(fetcher); |
| 50 URLRequestStatus status; | 51 URLRequestStatus status; |
| 51 status.set_status(success ? URLRequestStatus::SUCCESS : | 52 status.set_status(success ? URLRequestStatus::SUCCESS : |
| 52 URLRequestStatus::FAILED); | 53 URLRequestStatus::FAILED); |
| 53 fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(), | 54 fetcher->delegate()->OnURLFetchComplete(fetcher, fetcher->original_url(), |
| 54 status, success ? 200 : 500, | 55 status, success ? 200 : 500, |
| 55 ResponseCookies(), | 56 ResponseCookies(), |
| 56 http_response); | 57 http_response); |
| 57 // Parsed response will be available in result_. | 58 // Parsed response will be available in result_. |
| 58 } | 59 } |
| 59 | 60 |
| 60 TEST_F(SpeechRecognitionRequestTest, BasicTest) { | 61 TEST_F(SpeechRecognitionRequestTest, BasicTest) { |
| 61 // Normal success case with one result. | 62 // Normal success case with one result. |
| 62 CreateAndTestRequest(true, | 63 CreateAndTestRequest(true, |
| 63 "{\"hypotheses\":[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); | 64 "{\"hypotheses\":[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); |
| 64 EXPECT_FALSE(error_); | 65 EXPECT_FALSE(error_); |
| 65 EXPECT_EQ(ASCIIToUTF16("123456"), result_); | 66 EXPECT_EQ(1U, result_.size()); |
| 67 EXPECT_EQ(ASCIIToUTF16("123456"), result_[0].utterance); |
| 68 EXPECT_EQ(0.9, result_[0].confidence); |
| 66 | 69 |
| 67 // Normal success case with multiple results. | 70 // Normal success case with multiple results. |
| 68 CreateAndTestRequest(true, | 71 CreateAndTestRequest(true, |
| 69 "{\"hypotheses\":[{\"utterance\":\"hello\",\"confidence\":0.9}," | 72 "{\"hypotheses\":[{\"utterance\":\"hello\",\"confidence\":0.9}," |
| 70 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); | 73 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); |
| 71 EXPECT_FALSE(error_); | 74 EXPECT_FALSE(error_); |
| 72 EXPECT_EQ(ASCIIToUTF16("hello"), result_); | 75 EXPECT_EQ(2u, result_.size()); |
| 76 EXPECT_EQ(ASCIIToUTF16("hello"), result_[0].utterance); |
| 77 EXPECT_EQ(0.9, result_[0].confidence); |
| 78 EXPECT_EQ(ASCIIToUTF16("123456"), result_[1].utterance); |
| 79 EXPECT_EQ(0.5, result_[1].confidence); |
| 73 | 80 |
| 74 // Http failure case. | 81 // Http failure case. |
| 75 CreateAndTestRequest(false, ""); | 82 CreateAndTestRequest(false, ""); |
| 76 EXPECT_TRUE(error_); | 83 EXPECT_TRUE(error_); |
| 77 EXPECT_EQ(ASCIIToUTF16(""), result_); | 84 EXPECT_EQ(0U, result_.size()); |
| 78 | 85 |
| 79 // Malformed JSON case. | 86 // Malformed JSON case. |
| 80 CreateAndTestRequest(true, "{\"hypotheses\":[{\"unknownkey\":\"hello\"}]}"); | 87 CreateAndTestRequest(true, "{\"hypotheses\":[{\"unknownkey\":\"hello\"}]}"); |
| 81 EXPECT_TRUE(error_); | 88 EXPECT_TRUE(error_); |
| 82 EXPECT_EQ(ASCIIToUTF16(""), result_); | 89 EXPECT_EQ(0U, result_.size()); |
| 83 } | 90 } |
| 84 | 91 |
| 85 } // namespace speech_input | 92 } // namespace speech_input |
| OLD | NEW |