| Index: chrome/test/speech/mock_google_one_shot_server.cc
|
| diff --git a/chrome/test/speech/mock_google_one_shot_server.cc b/chrome/test/speech/mock_google_one_shot_server.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..95d2172b9ac8a09c2d3820a8874bfbe3c7cdc95e
|
| --- /dev/null
|
| +++ b/chrome/test/speech/mock_google_one_shot_server.cc
|
| @@ -0,0 +1,143 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/test/speech/mock_google_one_shot_server.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/message_loop.h"
|
| +#include "base/string_util.h"
|
| +#include "base/values.h"
|
| +#include "base/string_number_conversions.h"
|
| +#include "base/utf_string_conversions.h"
|
| +#include "content/public/browser/speech_recognition_manager.h"
|
| +#include "content/public/common/speech_recognition_error.h"
|
| +#include "net/base/escape.h"
|
| +#include "net/url_request/url_request_context_getter.h"
|
| +#include "net/url_request/url_fetcher_delegate.h"
|
| +#include "net/url_request/url_request_status.h"
|
| +
|
| +using content::SpeechRecognitionResult;
|
| +using content::SpeechRecognitionHypothesis;
|
| +
|
| +namespace {
|
| +const int kURLFetcherID = 42;
|
| +}
|
| +
|
| +namespace speech {
|
| +
|
| +MockGoogleOneShotServer::MockGoogleOneShotServer(Delegate* delegate)
|
| + : delegate_(delegate) {
|
| + url_fetcher_factory_.SetDelegateForTests(this);
|
| + content::SpeechRecognitionManager::SetURLFetcherIDForTests(kURLFetcherID);
|
| +}
|
| +
|
| +MockGoogleOneShotServer::~MockGoogleOneShotServer() {
|
| +}
|
| +
|
| +void MockGoogleOneShotServer::OnRequestStart(int fetcher_id) {
|
| + if (fetcher_id != kURLFetcherID)
|
| + return;
|
| +
|
| + // Extract request argument from the the request URI.
|
| + std::string query = GetURLFetcher()->GetOriginalURL().query();
|
| + std::vector<std::string> query_params;
|
| + Tokenize(query, "&", &query_params);
|
| + const net::UnescapeRule::Type kUnescapeAll =
|
| + net::UnescapeRule::NORMAL |
|
| + net::UnescapeRule::SPACES |
|
| + net::UnescapeRule::URL_SPECIAL_CHARS |
|
| + net::UnescapeRule::REPLACE_PLUS_WITH_SPACE;
|
| + for (size_t i = 0; i < query_params.size(); ++i) {
|
| + const std::string query_param = query_params[i];
|
| + std::vector<std::string> param_parts;
|
| + Tokenize(query_param, "=", ¶m_parts);
|
| + if (param_parts.size() != 2)
|
| + continue;
|
| + std::string param_key = net::UnescapeURLComponent(param_parts[0],
|
| + kUnescapeAll);
|
| + std::string param_value = net::UnescapeURLComponent(param_parts[1],
|
| + kUnescapeAll);
|
| + if (param_key == "lang") {
|
| + request_language = param_value;
|
| + } else if (param_key == "lm") {
|
| + request_grammar = param_value;
|
| + }
|
| + }
|
| +
|
| + DCHECK(delegate_);
|
| + delegate_->OnClientConnected();
|
| +}
|
| +
|
| +void MockGoogleOneShotServer::OnChunkUpload(int fetcher_id) {
|
| + if (fetcher_id != kURLFetcherID)
|
| + return;
|
| + DCHECK(delegate_);
|
| + delegate_->OnClientAudioUpload();
|
| + if (GetURLFetcher()->did_receive_last_chunk())
|
| + delegate_->OnClientAudioUploadComplete();
|
| +}
|
| +
|
| +void MockGoogleOneShotServer::OnRequestEnd(int fetcher_id) {
|
| + if (fetcher_id != kURLFetcherID)
|
| + return;
|
| + url_fetcher_factory_.RemoveFetcherFromMap(kURLFetcherID);
|
| + DCHECK(delegate_);
|
| + delegate_->OnClientDisconnected();
|
| +}
|
| +
|
| +void MockGoogleOneShotServer::SimulateResult(
|
| + const content::SpeechRecognitionResult& result) {
|
| + std::string json = "{\"status\":0,\"hypotheses\":[";
|
| + for (size_t i = 0; i < result.hypotheses.size(); ++i) {
|
| + const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i];
|
| + json += "{\"utterance\":\"";
|
| + json += UTF16ToUTF8(hypothesis.utterance);
|
| + json += "\",\"confidence\":";
|
| + json += base::DoubleToString(hypothesis.confidence);
|
| + json += "}";
|
| + if (i < result.hypotheses.size()-1)
|
| + json += ",";
|
| + }
|
| + json += "]}";
|
| + SimulateServerResponse(true, json);
|
| +}
|
| +
|
| +void MockGoogleOneShotServer::SimulateServerFailure() {
|
| + SimulateServerResponse(false, "");
|
| +}
|
| +
|
| +void MockGoogleOneShotServer::SimulateMalformedResponse() {
|
| + std::string json =
|
| + "{\"status\":0,\"hypotheses\":""[{\"unknownkey\":\"hello\"}]}";
|
| + SimulateServerResponse(true, json);
|
| +}
|
| +
|
| +const std::string& MockGoogleOneShotServer::GetRequestLanguage() const {
|
| + return request_language;
|
| +}
|
| +
|
| +const std::string& MockGoogleOneShotServer::GetRequestGrammar() const {
|
| + return request_grammar;
|
| +}
|
| +
|
| +void MockGoogleOneShotServer::SimulateServerResponse(
|
| + bool success, const std::string& http_response) {
|
| + net::TestURLFetcher* fetcher = GetURLFetcher();
|
| + DCHECK(fetcher);
|
| +
|
| + net::URLRequestStatus status;
|
| + status.set_status(success ? net::URLRequestStatus::SUCCESS :
|
| + net::URLRequestStatus::FAILED);
|
| + fetcher->set_status(status);
|
| + fetcher->set_response_code(success ? 200 : 500);
|
| + fetcher->SetResponseString(http_response);
|
| + fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| +}
|
| +
|
| +// Can return NULL if the SpeechRecognizer has not requested the connection yet.
|
| +net::TestURLFetcher* MockGoogleOneShotServer::GetURLFetcher() const {
|
| + return url_fetcher_factory_.GetFetcherByID(kURLFetcherID);
|
| +}
|
| +
|
| +} // namespace speech
|
|
|