summaryrefslogtreecommitdiffstats
path: root/content/test/mock_google_streaming_server.cc
blob: 3484ca3fba003108933d84a373f1b1966ea0cad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2013 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 "content/test/mock_google_streaming_server.h"

#include <stddef.h>
#include <stdint.h>

#include "base/bind.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/sys_byteorder.h"
#include "base/values.h"
#include "content/browser/speech/google_streaming_remote_engine.h"
#include "content/browser/speech/proto/google_streaming_api.pb.h"
#include "content/browser/speech/speech_recognition_manager_impl.h"
#include "net/base/escape.h"
#include "net/base/net_errors.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_status.h"

using base::HostToNet32;
using base::checked_cast;

namespace content {

MockGoogleStreamingServer::MockGoogleStreamingServer(Delegate* delegate)
    : delegate_(delegate),
      kDownstreamUrlFetcherId(
          GoogleStreamingRemoteEngine::kDownstreamUrlFetcherIdForTesting),
      kUpstreamUrlFetcherId(
          GoogleStreamingRemoteEngine::kUpstreamUrlFetcherIdForTesting) {
  url_fetcher_factory_.SetDelegateForTests(this);
}

MockGoogleStreamingServer::~MockGoogleStreamingServer() {
}

void MockGoogleStreamingServer::OnRequestStart(int fetcher_id) {
  if (fetcher_id != kDownstreamUrlFetcherId)
    return;

  // Extract request argument from the the request URI.
  std::string query = GetURLFetcher(true)->GetOriginalURL().query();
  const net::UnescapeRule::Type kUnescapeAll =
      net::UnescapeRule::NORMAL |
      net::UnescapeRule::SPACES |
      net::UnescapeRule::URL_SPECIAL_CHARS |
      net::UnescapeRule::REPLACE_PLUS_WITH_SPACE;
  for (const base::StringPiece& query_param :
       base::SplitStringPiece(query, "&", base::KEEP_WHITESPACE,
                              base::SPLIT_WANT_NONEMPTY)) {
    std::vector<std::string> param_parts = base::SplitString(
        query_param, "=", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
    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;
    }
  }

  delegate_->OnClientConnected();
}

void MockGoogleStreamingServer::OnChunkUpload(int fetcher_id) {
  if (fetcher_id != kUpstreamUrlFetcherId)
      return;
  delegate_->OnClientAudioUpload();
  if (GetURLFetcher(false)->did_receive_last_chunk())
    delegate_->OnClientAudioUploadComplete();
}

void MockGoogleStreamingServer::OnRequestEnd(int fetcher_id) {
  if (fetcher_id != kDownstreamUrlFetcherId)
    return;
  url_fetcher_factory_.RemoveFetcherFromMap(kDownstreamUrlFetcherId);
  delegate_->OnClientDisconnected();
}

void MockGoogleStreamingServer::SimulateResult(
    const SpeechRecognitionResult& result) {
  proto::SpeechRecognitionEvent proto_event;
  proto_event.set_status(proto::SpeechRecognitionEvent::STATUS_SUCCESS);
  proto::SpeechRecognitionResult* proto_result = proto_event.add_result();
  proto_result->set_final(!result.is_provisional);
  for (size_t i = 0; i < result.hypotheses.size(); ++i) {
    proto::SpeechRecognitionAlternative* proto_alternative =
        proto_result->add_alternative();
    const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i];
    proto_alternative->set_confidence(hypothesis.confidence);
    proto_alternative->set_transcript(base::UTF16ToUTF8(hypothesis.utterance));
  }

  std::string msg_string;
  proto_event.SerializeToString(&msg_string);

  // Prepend 4 byte prefix length indication to the protobuf message as
  // envisaged by the google streaming recognition webservice protocol.
  uint32_t prefix = HostToNet32(checked_cast<uint32_t>(msg_string.size()));
  msg_string.insert(0, reinterpret_cast<char*>(&prefix), sizeof(prefix));

  SimulateServerResponse(true, msg_string);
}

void MockGoogleStreamingServer::SimulateServerFailure() {
  SimulateServerResponse(false, "");
}

void MockGoogleStreamingServer::SimulateMalformedResponse() {
  std::string json =
      "{\"status\":0,\"hypotheses\":""[{\"unknownkey\":\"hello\"}]}";
  SimulateServerResponse(true, json);
}

const std::string& MockGoogleStreamingServer::GetRequestLanguage() const {
  return request_language;
}

const std::string& MockGoogleStreamingServer::GetRequestGrammar() const {
  return request_grammar;
}

void MockGoogleStreamingServer::SimulateServerResponse(
    bool success, const std::string& http_response) {
  net::TestURLFetcher* fetcher = GetURLFetcher(true);

  fetcher->set_status(
      net::URLRequestStatus::FromError(success ? net::OK : net::ERR_FAILED));
  fetcher->set_response_code(success ? 200 : 500);
  fetcher->SetResponseString(http_response);
  fetcher->delegate()->OnURLFetchDownloadProgress(fetcher, 0, 0);
}

// Can return NULL if the SpeechRecognizer has not requested the connection yet.
net::TestURLFetcher* MockGoogleStreamingServer::GetURLFetcher(
    bool downstream) const {
  return url_fetcher_factory_.GetFetcherByID(
      downstream ? kDownstreamUrlFetcherId : kUpstreamUrlFetcherId);
}

}  // namespace content