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.
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"
19 class GoogleOneShotRemoteEngineTest
: public SpeechRecognitionEngineDelegate
,
20 public testing::Test
{
22 GoogleOneShotRemoteEngineTest()
23 : error_(SPEECH_RECOGNITION_ERROR_NONE
) {}
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
);
29 // SpeechRecognitionRequestDelegate methods.
30 void OnSpeechRecognitionEngineResults(
31 const SpeechRecognitionResults
& results
) override
{
35 void OnSpeechRecognitionEngineError(
36 const SpeechRecognitionError
& error
) override
{
40 // Accessor for the only result item.
41 const SpeechRecognitionResult
& result() const {
42 DCHECK_EQ(results_
.size(), 1U);
47 base::MessageLoop message_loop_
;
48 net::TestURLFetcherFactory url_fetcher_factory_
;
49 SpeechRecognitionErrorCode error_
;
50 SpeechRecognitionResults results_
;
53 void GoogleOneShotRemoteEngineTest::CreateAndTestRequest(
54 bool success
, const std::string
& http_response
) {
55 GoogleOneShotRemoteEngine
client(NULL
);
56 unsigned char dummy_audio_buffer_data
[2] = {'\0', '\0'};
57 scoped_refptr
<AudioChunk
> dummy_audio_chunk(
58 new AudioChunk(&dummy_audio_buffer_data
[0],
59 sizeof(dummy_audio_buffer_data
),
60 2 /* bytes per sample */));
61 client
.set_delegate(this);
62 client
.StartRecognition();
63 client
.TakeAudioChunk(*dummy_audio_chunk
.get());
64 client
.AudioChunksEnded();
65 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
68 fetcher
->set_url(fetcher
->GetOriginalURL());
70 net::URLRequestStatus::FromError(success
? net::OK
: net::ERR_FAILED
));
71 fetcher
->set_response_code(success
? 200 : 500);
72 fetcher
->SetResponseString(http_response
);
74 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
75 // Parsed response will be available in result().
78 TEST_F(GoogleOneShotRemoteEngineTest
, BasicTest
) {
79 // Normal success case with one result.
80 CreateAndTestRequest(true,
81 "{\"status\":0,\"hypotheses\":"
82 "[{\"utterance\":\"123456\",\"confidence\":0.9}]}");
83 EXPECT_EQ(error_
, SPEECH_RECOGNITION_ERROR_NONE
);
84 EXPECT_EQ(1U, result().hypotheses
.size());
85 EXPECT_EQ(base::ASCIIToUTF16("123456"), result().hypotheses
[0].utterance
);
86 EXPECT_EQ(0.9, result().hypotheses
[0].confidence
);
88 // Normal success case with multiple results.
89 CreateAndTestRequest(true,
90 "{\"status\":0,\"hypotheses\":["
91 "{\"utterance\":\"hello\",\"confidence\":0.9},"
92 "{\"utterance\":\"123456\",\"confidence\":0.5}]}");
93 EXPECT_EQ(error_
, SPEECH_RECOGNITION_ERROR_NONE
);
94 EXPECT_EQ(2u, result().hypotheses
.size());
95 EXPECT_EQ(base::ASCIIToUTF16("hello"), result().hypotheses
[0].utterance
);
96 EXPECT_EQ(0.9, result().hypotheses
[0].confidence
);
97 EXPECT_EQ(base::ASCIIToUTF16("123456"), result().hypotheses
[1].utterance
);
98 EXPECT_EQ(0.5, result().hypotheses
[1].confidence
);
101 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}");
102 EXPECT_EQ(error_
, SPEECH_RECOGNITION_ERROR_NONE
);
103 EXPECT_EQ(0U, result().hypotheses
.size());
105 // Http failure case.
106 CreateAndTestRequest(false, std::string());
107 EXPECT_EQ(error_
, SPEECH_RECOGNITION_ERROR_NETWORK
);
108 EXPECT_EQ(0U, result().hypotheses
.size());
110 // Invalid status case.
111 CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}");
112 EXPECT_EQ(error_
, SPEECH_RECOGNITION_ERROR_NETWORK
);
113 EXPECT_EQ(0U, result().hypotheses
.size());
115 // Server-side error case.
116 CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}");
117 EXPECT_EQ(error_
, SPEECH_RECOGNITION_ERROR_NETWORK
);
118 EXPECT_EQ(0U, result().hypotheses
.size());
120 // Malformed JSON case.
121 CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":"
122 "[{\"unknownkey\":\"hello\"}]}");
123 EXPECT_EQ(error_
, SPEECH_RECOGNITION_ERROR_NETWORK
);
124 EXPECT_EQ(0U, result().hypotheses
.size());
127 } // namespace content