1// Copyright 2013 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#ifndef CONTENT_TEST_SPEECH_MOCK_GOOGLE_STREAMING_SERVER_H_
6#define CONTENT_TEST_SPEECH_MOCK_GOOGLE_STREAMING_SERVER_H_
7
8#include "base/compiler_specific.h"
9#include "content/public/common/speech_recognition_result.h"
10#include "net/url_request/test_url_fetcher_factory.h"
11
12namespace content {
13
14struct SpeechRecognitionResult;
15
16// Provides a mock implementation of the Google remote streaming speech
17// recognition webservice, exploiting the TestURLFetcher to extract request
18// parameters and provide forged JSON responses back to the client.
19// It is intended for closing the server-side loop in speech tests that involve
20// the GoogleStreamingRemoteEngine client.
21class MockGoogleStreamingServer : public net::TestURLFetcherDelegateForTests {
22 public:
23  class Delegate {
24   public:
25    virtual void OnClientConnected() = 0;
26    virtual void OnClientAudioUpload() = 0;
27    virtual void OnClientAudioUploadComplete() = 0;
28    virtual void OnClientDisconnected() = 0;
29  };
30
31  explicit MockGoogleStreamingServer(Delegate* delegate);
32  virtual ~MockGoogleStreamingServer();
33
34  // net::TestURLFetcherDelegateForTests implementation.
35  virtual void OnRequestStart(int fetcher_id) OVERRIDE;
36  virtual void OnChunkUpload(int fetcher_id) OVERRIDE;
37  virtual void OnRequestEnd(int fetcher_id) OVERRIDE;
38
39  void SimulateResult(const content::SpeechRecognitionResult& result);
40  void SimulateServerFailure();
41  void SimulateMalformedResponse();
42
43  // Retrieves the language parmeter for the request (e.g., lang=en-US).
44  const std::string& GetRequestLanguage() const;
45
46  // Retrieves the grammar parmeter for the request (e.g., lm=http://url/g.xml).
47  const std::string& GetRequestGrammar() const;
48
49 private:
50  void SimulateServerResponse(bool success, const std::string& http_response);
51  net::TestURLFetcher* GetURLFetcher(bool downstream) const;
52
53  Delegate* delegate_;
54  int kDownstreamUrlFetcherId;
55  int kUpstreamUrlFetcherId;
56  net::TestURLFetcherFactory url_fetcher_factory_;
57
58  // Request arguments extracted by the HTTP query string.
59  std::string request_language;
60  std::string request_grammar;
61
62  DISALLOW_COPY_AND_ASSIGN(MockGoogleStreamingServer);
63};
64
65}  // namespace content
66
67#endif  // CONTENT_TEST_SPEECH_MOCK_GOOGLE_STREAMING_SERVER_H_
68