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 151 152 153 154 155 156
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/speech/speech_recognition_test_helper.h"
#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/speech/cros_speech_recognition_service_factory.h"
#include "chrome/browser/speech/fake_speech_recognition_service.h"
#include "chrome/browser/speech/fake_speech_recognizer.h"
#include "chrome/browser/speech/speech_recognition_constants.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/soda/soda_installer.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/fake_speech_recognition_manager.h"
#include "media/mojo/mojom/speech_recognition.mojom.h"
SpeechRecognitionTestHelper::SpeechRecognitionTestHelper(
speech::SpeechRecognitionType type,
media::mojom::RecognizerClientType client_type)
: feature_under_test_(client_type), type_(type) {}
SpeechRecognitionTestHelper::~SpeechRecognitionTestHelper() = default;
void SpeechRecognitionTestHelper::SetUp(Profile* profile) {
if (type_ == speech::SpeechRecognitionType::kNetwork)
SetUpNetworkRecognition();
else
SetUpOnDeviceRecognition(profile);
}
void SpeechRecognitionTestHelper::SetUpNetworkRecognition() {
fake_speech_recognition_manager_ =
std::make_unique<content::FakeSpeechRecognitionManager>();
fake_speech_recognition_manager_->set_should_send_fake_response(false);
content::SpeechRecognitionManager::SetManagerForTesting(
fake_speech_recognition_manager_.get());
}
void SpeechRecognitionTestHelper::SetUpOnDeviceRecognition(Profile* profile) {
// Fake that SODA is installed so SpeechRecognitionPrivate uses
// OnDeviceSpeechRecognizer.
speech::SodaInstaller::GetInstance()->NotifySodaInstalledForTesting(
speech::LanguageCode::kEnUs);
speech::SodaInstaller::GetInstance()->NotifySodaInstalledForTesting();
CrosSpeechRecognitionServiceFactory::GetInstanceForTest()
->SetTestingFactoryAndUse(
profile,
base::BindRepeating(&SpeechRecognitionTestHelper::
CreateTestOnDeviceSpeechRecognitionService,
base::Unretained(this)));
}
void SpeechRecognitionTestHelper::OnRecognizerBound(
speech::FakeSpeechRecognizer* bound_recognizer) {
if (bound_recognizer->recognition_options()->recognizer_client_type ==
feature_under_test_) {
fake_recognizer_ = bound_recognizer->GetWeakPtr();
}
}
std::unique_ptr<KeyedService>
SpeechRecognitionTestHelper::CreateTestOnDeviceSpeechRecognitionService(
content::BrowserContext* context) {
std::unique_ptr<speech::FakeSpeechRecognitionService> fake_service =
std::make_unique<speech::FakeSpeechRecognitionService>();
fake_service_ = fake_service.get();
fake_service_->AddObserver(this);
return std::move(fake_service);
}
void SpeechRecognitionTestHelper::WaitForRecognitionStarted() {
// In the case of OnDevice recognition tests the fake_recognizer_ will not
// exist until events have propagated to the point that a recognition client
// impl is instantiated. So here we will wait for events to propagate in case
// the fake_recognizer_ needs to be instantiated.
base::RunLoop().RunUntilIdle();
// Only wait for recognition to start if it hasn't been started yet.
if (type_ == speech::SpeechRecognitionType::kNetwork &&
!fake_speech_recognition_manager_->is_recognizing()) {
fake_speech_recognition_manager_->WaitForRecognitionStarted();
} else if (type_ == speech::SpeechRecognitionType::kOnDevice &&
fake_recognizer_ && !fake_recognizer_->is_capturing_audio()) {
fake_recognizer_->WaitForRecognitionStarted();
}
// Wait for any subsequent events to propagate.
base::RunLoop().RunUntilIdle();
}
void SpeechRecognitionTestHelper::WaitForRecognitionStopped() {
// Only wait for recognition to stop if it hasn't been stopped yet.
if (type_ == speech::SpeechRecognitionType::kNetwork &&
fake_speech_recognition_manager_->is_recognizing()) {
fake_speech_recognition_manager_->WaitForRecognitionEnded();
}
base::RunLoop().RunUntilIdle();
}
void SpeechRecognitionTestHelper::SendInterimResultAndWait(
const std::string& transcript) {
SendFakeSpeechResultAndWait(transcript, /*is_final=*/false);
}
void SpeechRecognitionTestHelper::SendFinalResultAndWait(
const std::string& transcript) {
SendFakeSpeechResultAndWait(transcript, /*is_final=*/true);
}
void SpeechRecognitionTestHelper::SendFakeSpeechResultAndWait(
const std::string& transcript,
bool is_final) {
base::RunLoop loop;
if (type_ == speech::SpeechRecognitionType::kNetwork) {
fake_speech_recognition_manager_->SetFakeResult(transcript, is_final);
fake_speech_recognition_manager_->SendFakeResponse(
false /* end recognition */, loop.QuitClosure());
loop.Run();
} else {
CHECK(fake_recognizer_->is_capturing_audio());
fake_recognizer_->SendSpeechRecognitionResult(
media::SpeechRecognitionResult(transcript, is_final));
loop.RunUntilIdle();
}
}
void SpeechRecognitionTestHelper::SendErrorAndWait() {
base::RunLoop loop;
if (type_ == speech::SpeechRecognitionType::kNetwork) {
fake_speech_recognition_manager_->SendFakeError(loop.QuitClosure());
loop.Run();
} else {
CHECK(fake_recognizer_);
fake_recognizer_->SendSpeechRecognitionError();
loop.RunUntilIdle();
}
}
std::vector<base::test::FeatureRef>
SpeechRecognitionTestHelper::GetEnabledFeatures() {
std::vector<base::test::FeatureRef> features;
if (type_ == speech::SpeechRecognitionType::kOnDevice)
features.push_back(ash::features::kOnDeviceSpeechRecognition);
return features;
}
std::vector<base::test::FeatureRef>
SpeechRecognitionTestHelper::GetDisabledFeatures() {
std::vector<base::test::FeatureRef> features;
if (type_ == speech::SpeechRecognitionType::kNetwork)
features.push_back(ash::features::kOnDeviceSpeechRecognition);
return features;
}
|