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

content / public / test / fake_speech_recognition_manager.h [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_PUBLIC_TEST_FAKE_SPEECH_RECOGNITION_MANAGER_H_
#define CONTENT_PUBLIC_TEST_FAKE_SPEECH_RECOGNITION_MANAGER_H_

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "content/public/browser/speech_recognition_audio_forwarder_config.h"
#include "content/public/browser/speech_recognition_event_listener.h"
#include "content/public/browser/speech_recognition_manager.h"
#include "content/public/browser/speech_recognition_session_config.h"
#include "content/public/browser/speech_recognition_session_context.h"

namespace content {

class SpeechRecognitionManagerDelegate;

// Fake SpeechRecognitionManager class that can be used for tests.
// By default the recognition manager will respond with "Pictures of the moon"
// as recognized result from speech. This result can be overridden by calling
// SetFakeResult().
class FakeSpeechRecognitionManager : public SpeechRecognitionManager,
                                     public SpeechRecognitionEventListener {
 public:
  FakeSpeechRecognitionManager();

  FakeSpeechRecognitionManager(const FakeSpeechRecognitionManager&) = delete;
  FakeSpeechRecognitionManager& operator=(const FakeSpeechRecognitionManager&) =
      delete;

  ~FakeSpeechRecognitionManager() override;

  const std::string& grammar() const {
    return grammar_;
  }

  bool did_cancel_all() const { return did_cancel_all_; }

  void set_should_send_fake_response(bool send) {
    should_send_fake_response_ = send;
  }

  bool should_send_fake_response() const { return should_send_fake_response_; }

  bool is_recognizing() const { return is_recognizing_; }

  void WaitForRecognitionStarted();
  void WaitForRecognitionEnded();

  // |end_recognition| means recognition will be ended and session state cleared
  // after sending the fake response.
  void SendFakeResponse(bool end_recognition,
                        base::OnceClosure on_fake_response_sent);

  void SetFakeResult(const std::string& result, bool is_final);

  void SendFakeError(base::OnceClosure on_fake_error_sent);

  // SpeechRecognitionManager methods.
  int CreateSession(const SpeechRecognitionSessionConfig& config) override;
  int CreateSession(
      const SpeechRecognitionSessionConfig& config,
      mojo::PendingReceiver<media::mojom::SpeechRecognitionSession>
          session_receiver,
      mojo::PendingRemote<media::mojom::SpeechRecognitionSessionClient>
          client_remote,
      std::optional<SpeechRecognitionAudioForwarderConfig>
          audio_forwarder_config) override;
  void StartSession(int session_id) override;
  void AbortSession(int session_id) override;
  void StopAudioCaptureForSession(int session_id) override;
  void AbortAllSessionsForRenderFrame(int render_process_id,
                                      int render_frame_id) override;
  const SpeechRecognitionSessionConfig& GetSessionConfig(
      int session_id) override;
  SpeechRecognitionSessionContext GetSessionContext(int session_id) override;
  bool UseOnDeviceSpeechRecognition(
      const SpeechRecognitionSessionConfig& config) override;

  // SpeechRecognitionEventListener implementation.
  void OnRecognitionStart(int session_id) override {}
  void OnAudioStart(int session_id) override {}
  void OnSoundStart(int session_id) override {}
  void OnSoundEnd(int session_id) override {}
  void OnAudioEnd(int session_id) override {}
  void OnRecognitionEnd(int session_id) override {}
  void OnRecognitionResults(
      int session_id,
      const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& result)
      override {}
  void OnRecognitionError(
      int session_id,
      const media::mojom::SpeechRecognitionError& error) override {}
  void OnAudioLevelsChange(int session_id,
                           float volume,
                           float noise_volume) override {}

  void SetDelegate(SpeechRecognitionManagerDelegate* delegate);

 private:
  void OnRecognitionStarted();
  void OnRecognitionEnded();
  void OnFakeResponseSent();
  void SetFakeRecognitionResult(bool end_recognition);
  void SendFakeSpeechRecognitionError();
  void OnFakeErrorSent();

  int session_id_;
  raw_ptr<SpeechRecognitionEventListener, AcrossTasksDanglingUntriaged>
      listener_;
  SpeechRecognitionSessionConfig session_config_;
  SpeechRecognitionSessionContext session_ctx_;
  std::string fake_result_;
  bool is_final_ = true;
  std::string grammar_;
  bool did_cancel_all_ = false;
  bool should_send_fake_response_ = true;
  bool has_sent_result_ = false;
  bool is_recognizing_ = false;
  base::OnceClosure recognition_started_closure_;
  base::OnceClosure recognition_ended_closure_;
  base::OnceClosure on_fake_response_sent_closure_;
  base::OnceClosure on_fake_error_sent_closure_;
  raw_ptr<SpeechRecognitionManagerDelegate> delegate_ = nullptr;  // Not owned.
};

}  // namespace content

#endif  // CONTENT_PUBLIC_TEST_FAKE_SPEECH_RECOGNITION_MANAGER_H_