summaryrefslogtreecommitdiffstats
path: root/content/test/mock_google_streaming_server.h
blob: eea5d41d66900eb8144ffbc1d48ec35122c6976b (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
// 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.

#ifndef CONTENT_TEST_MOCK_GOOGLE_STREAMING_SERVER_H_
#define CONTENT_TEST_MOCK_GOOGLE_STREAMING_SERVER_H_

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "content/public/common/speech_recognition_result.h"
#include "net/url_request/test_url_fetcher_factory.h"

namespace content {

struct SpeechRecognitionResult;

// Provides a mock implementation of the Google remote streaming speech
// recognition webservice, exploiting the TestURLFetcher to extract request
// parameters and provide forged JSON responses back to the client.
// It is intended for closing the server-side loop in speech tests that involve
// the GoogleStreamingRemoteEngine client.
class MockGoogleStreamingServer : public net::TestURLFetcherDelegateForTests {
 public:
  class Delegate {
   public:
    virtual void OnClientConnected() = 0;
    virtual void OnClientAudioUpload() = 0;
    virtual void OnClientAudioUploadComplete() = 0;
    virtual void OnClientDisconnected() = 0;
  };

  explicit MockGoogleStreamingServer(Delegate* delegate);
  virtual ~MockGoogleStreamingServer();

  // net::TestURLFetcherDelegateForTests implementation.
  void OnRequestStart(int fetcher_id) override;
  void OnChunkUpload(int fetcher_id) override;
  void OnRequestEnd(int fetcher_id) override;

  void SimulateResult(const content::SpeechRecognitionResult& result);
  void SimulateServerFailure();
  void SimulateMalformedResponse();

  // Retrieves the language parmeter for the request (e.g., lang=en-US).
  const std::string& GetRequestLanguage() const;

  // Retrieves the grammar parmeter for the request (e.g., lm=http://url/g.xml).
  const std::string& GetRequestGrammar() const;

 private:
  void SimulateServerResponse(bool success, const std::string& http_response);
  net::TestURLFetcher* GetURLFetcher(bool downstream) const;

  Delegate* delegate_;
  int kDownstreamUrlFetcherId;
  int kUpstreamUrlFetcherId;
  net::TestURLFetcherFactory url_fetcher_factory_;

  // Request arguments extracted by the HTTP query string.
  std::string request_language;
  std::string request_grammar;

  DISALLOW_COPY_AND_ASSIGN(MockGoogleStreamingServer);
};

}  // namespace content

#endif  // CONTENT_TEST_MOCK_GOOGLE_STREAMING_SERVER_H_