1 // Copyright 2013 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 "content/test/mock_google_streaming_server.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/sys_byteorder.h"
12 #include "base/values.h"
13 #include "content/browser/speech/google_streaming_remote_engine.h"
14 #include "content/browser/speech/proto/google_streaming_api.pb.h"
15 #include "content/browser/speech/speech_recognition_manager_impl.h"
16 #include "net/base/escape.h"
17 #include "net/base/net_errors.h"
18 #include "net/url_request/url_fetcher_delegate.h"
19 #include "net/url_request/url_request_status.h"
21 using base::HostToNet32
;
22 using base::checked_cast
;
26 MockGoogleStreamingServer::MockGoogleStreamingServer(Delegate
* delegate
)
27 : delegate_(delegate
),
28 kDownstreamUrlFetcherId(
29 GoogleStreamingRemoteEngine::kDownstreamUrlFetcherIdForTesting
),
30 kUpstreamUrlFetcherId(
31 GoogleStreamingRemoteEngine::kUpstreamUrlFetcherIdForTesting
) {
32 url_fetcher_factory_
.SetDelegateForTests(this);
35 MockGoogleStreamingServer::~MockGoogleStreamingServer() {
38 void MockGoogleStreamingServer::OnRequestStart(int fetcher_id
) {
39 if (fetcher_id
!= kDownstreamUrlFetcherId
)
42 // Extract request argument from the the request URI.
43 std::string query
= GetURLFetcher(true)->GetOriginalURL().query();
44 std::vector
<std::string
> query_params
;
45 Tokenize(query
, "&", &query_params
);
46 const net::UnescapeRule::Type kUnescapeAll
=
47 net::UnescapeRule::NORMAL
|
48 net::UnescapeRule::SPACES
|
49 net::UnescapeRule::URL_SPECIAL_CHARS
|
50 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE
;
51 for (size_t i
= 0; i
< query_params
.size(); ++i
) {
52 const std::string query_param
= query_params
[i
];
53 std::vector
<std::string
> param_parts
;
54 Tokenize(query_param
, "=", ¶m_parts
);
55 if (param_parts
.size() != 2)
57 std::string param_key
= net::UnescapeURLComponent(param_parts
[0],
59 std::string param_value
= net::UnescapeURLComponent(param_parts
[1],
61 if (param_key
== "lang") {
62 request_language
= param_value
;
63 } else if (param_key
== "lm") {
64 request_grammar
= param_value
;
68 delegate_
->OnClientConnected();
71 void MockGoogleStreamingServer::OnChunkUpload(int fetcher_id
) {
72 if (fetcher_id
!= kUpstreamUrlFetcherId
)
74 delegate_
->OnClientAudioUpload();
75 if (GetURLFetcher(false)->did_receive_last_chunk())
76 delegate_
->OnClientAudioUploadComplete();
79 void MockGoogleStreamingServer::OnRequestEnd(int fetcher_id
) {
80 if (fetcher_id
!= kDownstreamUrlFetcherId
)
82 url_fetcher_factory_
.RemoveFetcherFromMap(kDownstreamUrlFetcherId
);
83 delegate_
->OnClientDisconnected();
86 void MockGoogleStreamingServer::SimulateResult(
87 const SpeechRecognitionResult
& result
) {
88 proto::SpeechRecognitionEvent proto_event
;
89 proto_event
.set_status(proto::SpeechRecognitionEvent::STATUS_SUCCESS
);
90 proto::SpeechRecognitionResult
* proto_result
= proto_event
.add_result();
91 proto_result
->set_final(!result
.is_provisional
);
92 for (size_t i
= 0; i
< result
.hypotheses
.size(); ++i
) {
93 proto::SpeechRecognitionAlternative
* proto_alternative
=
94 proto_result
->add_alternative();
95 const SpeechRecognitionHypothesis
& hypothesis
= result
.hypotheses
[i
];
96 proto_alternative
->set_confidence(hypothesis
.confidence
);
97 proto_alternative
->set_transcript(base::UTF16ToUTF8(hypothesis
.utterance
));
100 std::string msg_string
;
101 proto_event
.SerializeToString(&msg_string
);
103 // Prepend 4 byte prefix length indication to the protobuf message as
104 // envisaged by the google streaming recognition webservice protocol.
105 uint32 prefix
= HostToNet32(checked_cast
<uint32
>(msg_string
.size()));
106 msg_string
.insert(0, reinterpret_cast<char*>(&prefix
), sizeof(prefix
));
108 SimulateServerResponse(true, msg_string
);
111 void MockGoogleStreamingServer::SimulateServerFailure() {
112 SimulateServerResponse(false, "");
115 void MockGoogleStreamingServer::SimulateMalformedResponse() {
117 "{\"status\":0,\"hypotheses\":""[{\"unknownkey\":\"hello\"}]}";
118 SimulateServerResponse(true, json
);
121 const std::string
& MockGoogleStreamingServer::GetRequestLanguage() const {
122 return request_language
;
125 const std::string
& MockGoogleStreamingServer::GetRequestGrammar() const {
126 return request_grammar
;
129 void MockGoogleStreamingServer::SimulateServerResponse(
130 bool success
, const std::string
& http_response
) {
131 net::TestURLFetcher
* fetcher
= GetURLFetcher(true);
134 net::URLRequestStatus::FromError(success
? net::OK
: net::ERR_FAILED
));
135 fetcher
->set_response_code(success
? 200 : 500);
136 fetcher
->SetResponseString(http_response
);
137 fetcher
->delegate()->OnURLFetchDownloadProgress(fetcher
, 0, 0);
140 // Can return NULL if the SpeechRecognizer has not requested the connection yet.
141 net::TestURLFetcher
* MockGoogleStreamingServer::GetURLFetcher(
142 bool downstream
) const {
143 return url_fetcher_factory_
.GetFetcherByID(
144 downstream
? kDownstreamUrlFetcherId
: kUpstreamUrlFetcherId
);
147 } // namespace content