Update broken references to image assets
[chromium-blink-merge.git] / content / test / mock_google_streaming_server.cc
blobad9937aae7654471a1dda37099a866a7e3c32f0e
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"
7 #include "base/bind.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/sys_byteorder.h"
13 #include "base/values.h"
14 #include "content/browser/speech/google_streaming_remote_engine.h"
15 #include "content/browser/speech/proto/google_streaming_api.pb.h"
16 #include "content/browser/speech/speech_recognition_manager_impl.h"
17 #include "net/base/escape.h"
18 #include "net/base/net_errors.h"
19 #include "net/url_request/url_fetcher_delegate.h"
20 #include "net/url_request/url_request_status.h"
22 using base::HostToNet32;
23 using base::checked_cast;
25 namespace content {
27 MockGoogleStreamingServer::MockGoogleStreamingServer(Delegate* delegate)
28 : delegate_(delegate),
29 kDownstreamUrlFetcherId(
30 GoogleStreamingRemoteEngine::kDownstreamUrlFetcherIdForTesting),
31 kUpstreamUrlFetcherId(
32 GoogleStreamingRemoteEngine::kUpstreamUrlFetcherIdForTesting) {
33 url_fetcher_factory_.SetDelegateForTests(this);
36 MockGoogleStreamingServer::~MockGoogleStreamingServer() {
39 void MockGoogleStreamingServer::OnRequestStart(int fetcher_id) {
40 if (fetcher_id != kDownstreamUrlFetcherId)
41 return;
43 // Extract request argument from the the request URI.
44 std::string query = GetURLFetcher(true)->GetOriginalURL().query();
45 const net::UnescapeRule::Type kUnescapeAll =
46 net::UnescapeRule::NORMAL |
47 net::UnescapeRule::SPACES |
48 net::UnescapeRule::URL_SPECIAL_CHARS |
49 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE;
50 for (const base::StringPiece& query_param :
51 base::SplitStringPiece(query, "&", base::KEEP_WHITESPACE,
52 base::SPLIT_WANT_NONEMPTY)) {
53 std::vector<std::string> param_parts = base::SplitString(
54 query_param, "=", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
55 if (param_parts.size() != 2)
56 continue;
57 std::string param_key = net::UnescapeURLComponent(param_parts[0],
58 kUnescapeAll);
59 std::string param_value = net::UnescapeURLComponent(param_parts[1],
60 kUnescapeAll);
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)
73 return;
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)
81 return;
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() {
116 std::string json =
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);
133 fetcher->set_status(
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