| 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/message_loop.h" | |
| 6 #include "base/strings/utf_string_conversions.h" | |
| 7 #include "content/browser/speech/audio_buffer.h" | |
| 8 #include "content/browser/speech/google_one_shot_remote_engine.h" | |
| 9 #include "content/public/common/speech_recognition_error.h" | |
| 10 #include "content/public/common/speech_recognition_result.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 #include "net/url_request/test_url_fetcher_factory.h" | |
| 13 #include "net/url_request/url_request_context_getter.h" | |
| 14 #include "net/url_request/url_request_status.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 class GoogleOneShotRemoteEngineTest : public SpeechRecognitionEngineDelegate, | |
| 20 public testing::Test { | |
| 21 public: | |
| 22 GoogleOneShotRemoteEngineTest() | |
| 23 : error_(SPEECH_RECOGNITION_ERROR_NONE) {} | |
| 24 | |
| 25 // Creates a speech recognition request and invokes its URL fetcher delegate | |
| 26 // with the given test data. | |
| 27 void CreateAndTestRequest(bool success, const std::string& http_response); | |
| 28 | |
| 29 // SpeechRecognitionRequestDelegate methods. | |
| 30 void OnSpeechRecognitionEngineResults( | |
| 31 const SpeechRecognitionResults& results) override { | |
| 32 results_ = results; | |
| 33 } | |
| 34 | |
| 35 void OnSpeechRecognitionEngineEndOfUtterance() override { | |
| 36 } | |
| 37 | |
| 38 void OnSpeechRecognitionEngineError( | |
| 39 const SpeechRecognitionError& error) override { | |
| 40 error_ = error.code; | |
| 41 } | |
| 42 | |
| 43 // Accessor for the only result item. | |
| 44 const SpeechRecognitionResult& result() const { | |
| 45 DCHECK_EQ(results_.size(), 1U); | |
| 46 return results_[0]; | |
| 47 } | |
| 48 | |
| 49 protected: | |
| 50 base::MessageLoop message_loop_; | |
| 51 net::TestURLFetcherFactory url_fetcher_factory_; | |
| 52 SpeechRecognitionErrorCode error_; | |
| 53 SpeechRecognitionResults results_; | |
| 54 }; | |
| 55 | |
| 56 void GoogleOneShotRemoteEngineTest::CreateAndTestRequest( | |
| 57 bool success, const std::string& http_response) { | |
| 58 GoogleOneShotRemoteEngine client(NULL); | |
| 59 unsigned char dummy_audio_buffer_data[2] = {'\0', '\0'}; | |
| 60 scoped_refptr<AudioChunk> dummy_audio_chunk( | |
| 61 new AudioChunk(&dummy_audio_buffer_data[0], | |
| 62 sizeof(dummy_audio_buffer_data), | |
| 63 2 /* bytes per sample */)); | |
| 64 client.set_delegate(this); | |
| 65 client.StartRecognition(); | |
| 66 client.TakeAudioChunk(*dummy_audio_chunk.get()); | |
| 67 client.AudioChunksEnded(); | |
| 68 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | |
| 69 ASSERT_TRUE(fetcher); | |
| 70 | |
| 71 fetcher->set_url(fetcher->GetOriginalURL()); | |
| 72 fetcher->set_status( | |
| 73 net::URLRequestStatus::FromError(success ? net::OK : net::ERR_FAILED)); | |
| 74 fetcher->set_response_code(success ? 200 : 500); | |
| 75 fetcher->SetResponseString(http_response); | |
| 76 | |
| 77 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 78 // Parsed response will be available in result(). | |
| 79 } | |
| 80 | |
| 81 TEST_F(GoogleOneShotRemoteEngineTest, BasicTest) { | |
| 82 // Normal success case with one result. | |
| 83 CreateAndTestRequest(true, | |
| 84 "{\"status\":0,\"hypotheses\":" | |
| 85 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}"); | |
| 86 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE); | |
| 87 EXPECT_EQ(1U, result().hypotheses.size()); | |
| 88 EXPECT_EQ(base::ASCIIToUTF16("123456"), result().hypotheses[0].utterance); | |
| 89 EXPECT_EQ(0.9, result().hypotheses[0].confidence); | |
| 90 | |
| 91 // Normal success case with multiple results. | |
| 92 CreateAndTestRequest(true, | |
| 93 "{\"status\":0,\"hypotheses\":[" | |
| 94 "{\"utterance\":\"hello\",\"confidence\":0.9}," | |
| 95 "{\"utterance\":\"123456\",\"confidence\":0.5}]}"); | |
| 96 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE); | |
| 97 EXPECT_EQ(2u, result().hypotheses.size()); | |
| 98 EXPECT_EQ(base::ASCIIToUTF16("hello"), result().hypotheses[0].utterance); | |
| 99 EXPECT_EQ(0.9, result().hypotheses[0].confidence); | |
| 100 EXPECT_EQ(base::ASCIIToUTF16("123456"), result().hypotheses[1].utterance); | |
| 101 EXPECT_EQ(0.5, result().hypotheses[1].confidence); | |
| 102 | |
| 103 // Zero results. | |
| 104 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}"); | |
| 105 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NONE); | |
| 106 EXPECT_EQ(0U, result().hypotheses.size()); | |
| 107 | |
| 108 // Http failure case. | |
| 109 CreateAndTestRequest(false, std::string()); | |
| 110 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); | |
| 111 EXPECT_EQ(0U, result().hypotheses.size()); | |
| 112 | |
| 113 // Invalid status case. | |
| 114 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}"); | |
| 115 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); | |
| 116 EXPECT_EQ(0U, result().hypotheses.size()); | |
| 117 | |
| 118 // Server-side error case. | |
| 119 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}"); | |
| 120 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); | |
| 121 EXPECT_EQ(0U, result().hypotheses.size()); | |
| 122 | |
| 123 // Malformed JSON case. | |
| 124 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":" | |
| 125 "[{\"unknownkey\":\"hello\"}]}"); | |
| 126 EXPECT_EQ(error_, SPEECH_RECOGNITION_ERROR_NETWORK); | |
| 127 EXPECT_EQ(0U, result().hypotheses.size()); | |
| 128 } | |
| 129 | |
| 130 } // namespace content | |
| OLD | NEW |