OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/test/mock_google_streaming_server.h" | 5 #include "content/test/mock_google_streaming_server.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/safe_numerics.h" | 8 #include "base/numerics/safe_conversions.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "base/sys_byteorder.h" | 11 #include "base/sys_byteorder.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "content/browser/speech/google_streaming_remote_engine.h" | 13 #include "content/browser/speech/google_streaming_remote_engine.h" |
14 #include "content/browser/speech/proto/google_streaming_api.pb.h" | 14 #include "content/browser/speech/proto/google_streaming_api.pb.h" |
15 #include "content/browser/speech/speech_recognition_manager_impl.h" | 15 #include "content/browser/speech/speech_recognition_manager_impl.h" |
16 #include "net/base/escape.h" | 16 #include "net/base/escape.h" |
17 #include "net/url_request/url_fetcher_delegate.h" | 17 #include "net/url_request/url_fetcher_delegate.h" |
18 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
19 | 19 |
20 using base::HostToNet32; | 20 using base::HostToNet32; |
21 using base::checked_numeric_cast; | 21 using base::checked_cast; |
22 | 22 |
23 namespace content { | 23 namespace content { |
24 | 24 |
25 MockGoogleStreamingServer::MockGoogleStreamingServer(Delegate* delegate) | 25 MockGoogleStreamingServer::MockGoogleStreamingServer(Delegate* delegate) |
26 : delegate_(delegate), | 26 : delegate_(delegate), |
27 kDownstreamUrlFetcherId( | 27 kDownstreamUrlFetcherId( |
28 GoogleStreamingRemoteEngine::kDownstreamUrlFetcherIdForTesting), | 28 GoogleStreamingRemoteEngine::kDownstreamUrlFetcherIdForTesting), |
29 kUpstreamUrlFetcherId( | 29 kUpstreamUrlFetcherId( |
30 GoogleStreamingRemoteEngine::kUpstreamUrlFetcherIdForTesting) { | 30 GoogleStreamingRemoteEngine::kUpstreamUrlFetcherIdForTesting) { |
31 url_fetcher_factory_.SetDelegateForTests(this); | 31 url_fetcher_factory_.SetDelegateForTests(this); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i]; | 94 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i]; |
95 proto_alternative->set_confidence(hypothesis.confidence); | 95 proto_alternative->set_confidence(hypothesis.confidence); |
96 proto_alternative->set_transcript(base::UTF16ToUTF8(hypothesis.utterance)); | 96 proto_alternative->set_transcript(base::UTF16ToUTF8(hypothesis.utterance)); |
97 } | 97 } |
98 | 98 |
99 std::string msg_string; | 99 std::string msg_string; |
100 proto_event.SerializeToString(&msg_string); | 100 proto_event.SerializeToString(&msg_string); |
101 | 101 |
102 // Prepend 4 byte prefix length indication to the protobuf message as | 102 // Prepend 4 byte prefix length indication to the protobuf message as |
103 // envisaged by the google streaming recognition webservice protocol. | 103 // envisaged by the google streaming recognition webservice protocol. |
104 uint32 prefix = HostToNet32(checked_numeric_cast<uint32>(msg_string.size())); | 104 uint32 prefix = HostToNet32(checked_cast<uint32>(msg_string.size())); |
105 msg_string.insert(0, reinterpret_cast<char*>(&prefix), sizeof(prefix)); | 105 msg_string.insert(0, reinterpret_cast<char*>(&prefix), sizeof(prefix)); |
106 | 106 |
107 SimulateServerResponse(true, msg_string); | 107 SimulateServerResponse(true, msg_string); |
108 } | 108 } |
109 | 109 |
110 void MockGoogleStreamingServer::SimulateServerFailure() { | 110 void MockGoogleStreamingServer::SimulateServerFailure() { |
111 SimulateServerResponse(false, ""); | 111 SimulateServerResponse(false, ""); |
112 } | 112 } |
113 | 113 |
114 void MockGoogleStreamingServer::SimulateMalformedResponse() { | 114 void MockGoogleStreamingServer::SimulateMalformedResponse() { |
(...skipping 24 matching lines...) Expand all Loading... |
139 } | 139 } |
140 | 140 |
141 // Can return NULL if the SpeechRecognizer has not requested the connection yet. | 141 // Can return NULL if the SpeechRecognizer has not requested the connection yet. |
142 net::TestURLFetcher* MockGoogleStreamingServer::GetURLFetcher( | 142 net::TestURLFetcher* MockGoogleStreamingServer::GetURLFetcher( |
143 bool downstream) const { | 143 bool downstream) const { |
144 return url_fetcher_factory_.GetFetcherByID( | 144 return url_fetcher_factory_.GetFetcherByID( |
145 downstream ? kDownstreamUrlFetcherId : kUpstreamUrlFetcherId); | 145 downstream ? kDownstreamUrlFetcherId : kUpstreamUrlFetcherId); |
146 } | 146 } |
147 | 147 |
148 } // namespace content | 148 } // namespace content |
OLD | NEW |