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