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

Side by Side Diff: chrome/test/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: Moved test to /chrome/test 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 "chrome/test/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/public/browser/speech_recognition_manager.h"
14 #include "content/public/common/speech_recognition_error.h"
15 #include "net/base/escape.h"
16 #include "net/url_request/url_request_context_getter.h"
17 #include "net/url_request/url_fetcher_delegate.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(Delegate* delegate)
30 : delegate_(delegate) {
31 url_fetcher_factory_.SetDelegateForTests(this);
32 content::SpeechRecognitionManager::SetURLFetcherIDForTests(kURLFetcherID);
33 }
34
35 MockGoogleOneShotServer::~MockGoogleOneShotServer() {
36 }
37
38 void MockGoogleOneShotServer::OnRequestStart(int fetcher_id) {
39 if (fetcher_id != kURLFetcherID)
40 return;
41
42 // Extract request argument from the the request URI.
43 std::string query = GetURLFetcher()->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, "=", &param_parts);
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;
65 }
66 }
67
68 DCHECK(delegate_);
69 delegate_->OnClientConnected();
70 }
71
72 void MockGoogleOneShotServer::OnChunkUpload(int fetcher_id) {
73 if (fetcher_id != kURLFetcherID)
74 return;
75 DCHECK(delegate_);
76 delegate_->OnClientAudioUpload();
77 if (GetURLFetcher()->did_receive_last_chunk())
78 delegate_->OnClientAudioUploadComplete();
79 }
80
81 void MockGoogleOneShotServer::OnRequestEnd(int fetcher_id) {
82 if (fetcher_id != kURLFetcherID)
83 return;
84 url_fetcher_factory_.RemoveFetcherFromMap(kURLFetcherID);
85 DCHECK(delegate_);
86 delegate_->OnClientDisconnected();
87 }
88
89 void MockGoogleOneShotServer::SimulateResult(
90 const content::SpeechRecognitionResult& result) {
91 std::string json = "{\"status\":0,\"hypotheses\":[";
92 for (size_t i = 0; i < result.hypotheses.size(); ++i) {
93 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i];
94 json += "{\"utterance\":\"";
95 json += UTF16ToUTF8(hypothesis.utterance);
96 json += "\",\"confidence\":";
97 json += base::DoubleToString(hypothesis.confidence);
98 json += "}";
99 if (i < result.hypotheses.size()-1)
100 json += ",";
101 }
102 json += "]}";
103 SimulateServerResponse(true, json);
104 }
105
106 void MockGoogleOneShotServer::SimulateServerFailure() {
107 SimulateServerResponse(false, "");
108 }
109
110 void MockGoogleOneShotServer::SimulateMalformedResponse() {
111 std::string json =
112 "{\"status\":0,\"hypotheses\":""[{\"unknownkey\":\"hello\"}]}";
113 SimulateServerResponse(true, json);
114 }
115
116 const std::string& MockGoogleOneShotServer::GetRequestLanguage() const {
117 return request_language;
118 }
119
120 const std::string& MockGoogleOneShotServer::GetRequestGrammar() const {
121 return request_grammar;
122 }
123
124 void MockGoogleOneShotServer::SimulateServerResponse(
125 bool success, const std::string& http_response) {
126 net::TestURLFetcher* fetcher = GetURLFetcher();
127 DCHECK(fetcher);
128
129 net::URLRequestStatus status;
130 status.set_status(success ? net::URLRequestStatus::SUCCESS :
131 net::URLRequestStatus::FAILED);
132 fetcher->set_status(status);
133 fetcher->set_response_code(success ? 200 : 500);
134 fetcher->SetResponseString(http_response);
135 fetcher->delegate()->OnURLFetchComplete(fetcher);
136 }
137
138 // Can return NULL if the SpeechRecognizer has not requested the connection yet.
139 net::TestURLFetcher* MockGoogleOneShotServer::GetURLFetcher() const {
140 return url_fetcher_factory_.GetFetcherByID(kURLFetcherID);
141 }
142
143 } // namespace speech
OLDNEW
« no previous file with comments | « chrome/test/speech/mock_google_one_shot_server.h ('k') | chrome/test/speech/speech_recognition_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698