Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(761)

Side by Side Diff: content/browser/speech/mock_google_one_shot_server.cc

Issue 10703141: End-to-end browser tests for speech recognition. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "content/browser/speech/mock_google_one_shot_server.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop.h"
9 #include "base/string_util.h"
10 #include "base/values.h"
11 #include "base/string_number_conversions.h"
12 #include "base/utf_string_conversions.h"
13 #include "content/browser/speech/audio_buffer.h"
14 #include "content/browser/speech/google_one_shot_remote_engine.h"
15 #include "content/public/common/speech_recognition_error.h"
16 #include "net/base/escape.h"
17 #include "net/url_request/url_request_context_getter.h"
18 #include "net/url_request/url_request_status.h"
19
20 using content::SpeechRecognitionResult;
21 using content::SpeechRecognitionHypothesis;
22
23 namespace {
24 const int kURLFetcherID = 42;
25 }
26
27 namespace speech {
28
29 MockGoogleOneShotServer::MockGoogleOneShotServer(
30 MockGoogleSpeechRecognitionServerDelegate* delegate)
31 : delegate_(delegate) {
32 url_fetcher_factory_.SetDelegateForTests(this);
33 GoogleOneShotRemoteEngine::url_fetcher_id_for_tests = kURLFetcherID;
34 }
35
36 MockGoogleOneShotServer::~MockGoogleOneShotServer() {
37 }
38
39 void MockGoogleOneShotServer::OnRequestStart(int fetcher_id) {
40 if (fetcher_id != kURLFetcherID)
41 return;
42
43 // Extract request argument from the the request URI.
44 std::string query = GetURLFetcher()->GetOriginalURL().query();
45 std::vector<std::string> query_params;
46 Tokenize(query, "&", &query_params);
47 const net::UnescapeRule::Type kUnescapeAll =
48 net::UnescapeRule::NORMAL |
49 net::UnescapeRule::SPACES |
50 net::UnescapeRule::URL_SPECIAL_CHARS |
51 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE;
52 for (size_t i = 0; i < query_params.size(); ++i) {
53 const std::string query_param = query_params[i];
54 std::vector<std::string> param_parts;
55 Tokenize(query_param, "=", &param_parts);
56 if (param_parts.size() != 2)
57 continue;
58 std::string param_key = net::UnescapeURLComponent(param_parts[0],
59 kUnescapeAll);
60 std::string param_value = net::UnescapeURLComponent(param_parts[1],
61 kUnescapeAll);
62 if (param_key == "lang") {
63 request_language = param_value;
64 } else if (param_key == "lm") {
65 request_grammar = param_value;
66 }
67 }
68
69 DCHECK(delegate_);
70 delegate_->OnClientConnected();
71 }
72
73 void MockGoogleOneShotServer::OnChunkUpload(int fetcher_id) {
74 if (fetcher_id != kURLFetcherID)
75 return;
76 DCHECK(delegate_);
77 delegate_->OnClientAudioUpload();
78 if (GetURLFetcher()->did_receive_last_chunk())
79 delegate_->OnClientAudioUploadComplete();
80 }
81
82 void MockGoogleOneShotServer::OnRequestEnd(int fetcher_id) {
83 if (fetcher_id != kURLFetcherID)
84 return;
85 url_fetcher_factory_.RemoveFetcherFromMap(kURLFetcherID);
86 DCHECK(delegate_);
87 delegate_->OnClientDisconnected();
88 }
89
90 void MockGoogleOneShotServer::SimulateResult(
91 const content::SpeechRecognitionResult& result) {
92 std::string json = "{\"status\":0,\"hypotheses\":[";
93 for (size_t i = 0; i < result.hypotheses.size(); ++i) {
94 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i];
95 json += "{\"utterance\":\"";
96 json += UTF16ToUTF8(hypothesis.utterance);
97 json += "\",\"confidence\":";
98 json += base::DoubleToString(hypothesis.confidence);
99 json += "}";
100 if (i < result.hypotheses.size()-1)
101 json += ",";
102 }
103 json += "]}";
104 SimulateServerResponse(true, json);
105 }
106
107 void MockGoogleOneShotServer::SimulateServerFailure() {
108 SimulateServerResponse(false, "");
109 }
110
111 void MockGoogleOneShotServer::SimulateMalformedResponse() {
112 std::string json =
113 "{\"status\":0,\"hypotheses\":""[{\"unknownkey\":\"hello\"}]}";
114 SimulateServerResponse(true, json);
115 }
116
117 const std::string& MockGoogleOneShotServer::GetRequestLanguage() const {
118 return request_language;
119 }
120
121 const std::string& MockGoogleOneShotServer::GetRequestGrammar() const {
122 return request_grammar;
123 }
124
125 void MockGoogleOneShotServer::SimulateServerResponse(
126 bool success, const std::string& http_response) {
127 net::TestURLFetcher* fetcher = GetURLFetcher();
128 DCHECK(fetcher);
129
130 net::URLRequestStatus status;
131 status.set_status(success ? net::URLRequestStatus::SUCCESS :
132 net::URLRequestStatus::FAILED);
133 fetcher->set_status(status);
134 fetcher->set_response_code(success ? 200 : 500);
135 fetcher->SetResponseString(http_response);
136 fetcher->delegate()->OnURLFetchComplete(fetcher);
137 }
138
139 // Can return NULL if the SpeechRecognizer has not requested the connection yet.
140 net::TestURLFetcher* MockGoogleOneShotServer::GetURLFetcher() const {
141 return url_fetcher_factory_.GetFetcherByID(kURLFetcherID);
142 }
143
144 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698