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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
// 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/ui/ash/projector/projector_client_impl.h"
#include <memory>
#include <optional>
#include "ash/constants/ash_features.h"
#include "ash/projector/projector_metrics.h"
#include "ash/public/cpp/locale_update_controller.h"
#include "ash/public/cpp/projector/projector_client.h"
#include "ash/public/cpp/projector/projector_controller.h"
#include "ash/public/cpp/test/mock_projector_controller.h"
#include "ash/webui/projector_app/public/cpp/projector_app_constants.h"
#include "ash/webui/projector_app/test/mock_app_client.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/branding_buildflags.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.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/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/application_locale_storage/application_locale_storage.h"
#include "components/prefs/pref_service.h"
#include "components/soda/mock_soda_installer.h"
#include "components/soda/soda_installer.h"
#include "components/soda/soda_installer_impl_chromeos.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace ash {
namespace {
const char kFirstSpeechResult[] = "the brown fox";
const char kSecondSpeechResult[] = "the brown fox jumped over the lazy dog";
const char kEnglishUS[] = "en-US";
constexpr char kS3FallbackReasonMetricName[] =
"Ash.Projector.OnDeviceToServerSpeechRecognitionFallbackReason";
inline void SetLocale(const std::string& locale) {
TestingBrowserProcess::GetGlobal()
->GetFeatures()
->application_locale_storage()
->Set(locale);
}
class MockLocaleUpdateController : public ash::LocaleUpdateController {
public:
MockLocaleUpdateController() = default;
MockLocaleUpdateController(const MockLocaleUpdateController&) = delete;
MockLocaleUpdateController& operator=(const MockLocaleUpdateController&) =
delete;
~MockLocaleUpdateController() override = default;
MOCK_METHOD(void, OnLocaleChanged, (), (override));
MOCK_METHOD(void,
ConfirmLocaleChange,
(const std::string&,
const std::string&,
const std::string&,
LocaleChangeConfirmationCallback),
(override));
MOCK_METHOD(void, AddObserver, (ash::LocaleChangeObserver*), (override));
MOCK_METHOD(void, RemoveObserver, (ash::LocaleChangeObserver*), (override));
};
struct ProjectorClientTestScenario {
const std::vector<base::test::FeatureRef> enabled_features;
const std::vector<base::test::FeatureRef> disabled_features;
ProjectorClientTestScenario(
const std::vector<base::test::FeatureRef>& enabled,
const std::vector<base::test::FeatureRef>& disabled)
: enabled_features(enabled), disabled_features(disabled) {}
};
} // namespace
class ProjectorClientImplUnitTest
: public testing::TestWithParam<ProjectorClientTestScenario>,
public speech::FakeSpeechRecognitionService::Observer {
public:
ProjectorClientImplUnitTest() = default;
ProjectorClientImplUnitTest(const ProjectorClientImplUnitTest&) = delete;
ProjectorClientImplUnitTest& operator=(const ProjectorClientImplUnitTest&) =
delete;
~ProjectorClientImplUnitTest() override = default;
Profile* profile() { return testing_profile_; }
MockProjectorController& projector_controller() {
return projector_controller_;
}
ProjectorClient* client() { return projector_client_.get(); }
void SetUp() override {
const auto& parameter = GetParam();
scoped_feature_list_.InitWithFeatures(parameter.enabled_features,
parameter.disabled_features);
ASSERT_TRUE(testing_profile_manager_.SetUp());
testing_profile_ = ProfileManager::GetPrimaryUserProfile();
ASSERT_TRUE(testing_profile_);
SetLocale(kEnglishUS);
soda_installer_ = std::make_unique<speech::MockSodaInstaller>();
ON_CALL(*soda_installer_, GetAvailableLanguages)
.WillByDefault(testing::Return(std::vector<std::string>({kEnglishUS})));
soda_installer_->NotifySodaInstalledForTesting();
soda_installer_->NotifySodaInstalledForTesting(speech::LanguageCode::kEnUs);
mock_app_client_ = std::make_unique<MockAppClient>();
mock_locale_controller_ = std::make_unique<MockLocaleUpdateController>();
projector_client_ = std::make_unique<ProjectorClientImpl>(
TestingBrowserProcess::GetGlobal()
->GetFeatures()
->application_locale_storage(),
&projector_controller_);
CrosSpeechRecognitionServiceFactory::GetInstanceForTest()
->SetTestingFactoryAndUse(
profile(), base::BindOnce(&ProjectorClientImplUnitTest::
CreateTestSpeechRecognitionService,
base::Unretained(this)));
}
void TearDown() override {
projector_client_.reset();
mock_locale_controller_.reset();
mock_app_client_.reset();
soda_installer_.reset();
testing::Test::TearDown();
}
std::unique_ptr<KeyedService> CreateTestSpeechRecognitionService(
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 fake_service;
}
void SendSpeechResult(const char* result, bool is_final) {
EXPECT_TRUE(fake_recognizer_->is_capturing_audio());
base::RunLoop loop;
fake_recognizer_->SendSpeechRecognitionResult(
media::SpeechRecognitionResult(result, is_final));
loop.RunUntilIdle();
}
void SendTranscriptionError() {
EXPECT_TRUE(fake_recognizer_->is_capturing_audio());
base::RunLoop loop;
fake_recognizer_->SendSpeechRecognitionError();
loop.RunUntilIdle();
}
void OnRecognizerBound(
speech::FakeSpeechRecognizer* bound_recognizer) override {
if (bound_recognizer->recognition_options()->recognizer_client_type ==
media::mojom::RecognizerClientType::kProjector) {
fake_recognizer_ = bound_recognizer->GetWeakPtr();
}
}
protected:
// Start speech recognition and verify on device to server based speech
// recognition fallback reason. If `expected_reason` is null, there will be no
// metric recorded because there is no fallback to server based recognition.
void MaybeReportToServerBasedFallbackReasonMetricAndVerify(
std::optional<ash::OnDeviceToServerSpeechRecognitionFallbackReason>
expected_reason,
int expected_count) {
ProjectorClientImpl* client =
static_cast<ProjectorClientImpl*>(projector_client_.get());
client->StartSpeechRecognition();
if (expected_reason.has_value()) {
histogram_tester_.ExpectBucketCount(kS3FallbackReasonMetricName,
/*sample=*/expected_reason.value(),
/*expected_count=*/expected_count);
} else {
EXPECT_TRUE(client->GetSpeechRecognitionAvailability().use_on_device);
}
client->OnSpeechRecognitionStopped();
}
base::HistogramTester histogram_tester_;
content::BrowserTaskEnvironment task_environment_;
raw_ptr<Profile, DanglingUntriaged> testing_profile_ = nullptr;
TestingProfileManager testing_profile_manager_{
TestingBrowserProcess::GetGlobal()};
MockProjectorController projector_controller_;
std::unique_ptr<ProjectorClient> projector_client_;
std::unique_ptr<speech::MockSodaInstaller> soda_installer_;
std::unique_ptr<MockAppClient> mock_app_client_;
std::unique_ptr<MockLocaleUpdateController> mock_locale_controller_;
raw_ptr<speech::FakeSpeechRecognitionService> fake_service_;
base::WeakPtr<speech::FakeSpeechRecognizer> fake_recognizer_;
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_P(ProjectorClientImplUnitTest, SpeechRecognitionResults) {
ProjectorClient* got_client = client();
ASSERT_TRUE(got_client);
got_client->StartSpeechRecognition();
base::RunLoop().RunUntilIdle();
EXPECT_CALL(projector_controller(),
OnTranscription(
media::SpeechRecognitionResult(kFirstSpeechResult, false)));
SendSpeechResult(kFirstSpeechResult, /*is_final=*/false);
EXPECT_CALL(projector_controller(),
OnTranscription(
media::SpeechRecognitionResult(kSecondSpeechResult, false)));
SendSpeechResult(kSecondSpeechResult, /*is_final=*/false);
EXPECT_CALL(projector_controller(), OnTranscriptionError());
SendTranscriptionError();
}
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
namespace {
const char kFrench[] = "fr";
const char kUnsupportedLanguage[] = "am";
const char kSpanishLatam[] = "es-419";
const char kSpanishMexican[] = "es-MX";
const char kEnglishNewZealand[] = "en-NZ";
bool IsEqualAvailability(const SpeechRecognitionAvailability& first,
const SpeechRecognitionAvailability& second) {
if (first.use_on_device != second.use_on_device) {
return false;
}
if (first.use_on_device) {
return first.on_device_availability == second.on_device_availability;
}
return first.server_based_availability == second.server_based_availability;
}
} // namespace
TEST_P(ProjectorClientImplUnitTest, SpeechRecognitionAvailability) {
const bool force_enable_server_based =
features::ShouldForceEnableServerSideSpeechRecognition();
const bool server_based_available =
features::IsInternalServerSideSpeechRecognitionEnabled();
SetLocale(kFrench);
SpeechRecognitionAvailability availability;
availability.use_on_device = false;
availability.server_based_availability =
ash::ServerBasedRecognitionAvailability::kAvailable;
if (server_based_available) {
EXPECT_TRUE(IsEqualAvailability(
projector_client_->GetSpeechRecognitionAvailability(), availability));
SetLocale(kSpanishLatam);
EXPECT_TRUE(IsEqualAvailability(
projector_client_->GetSpeechRecognitionAvailability(), availability));
SetLocale(kSpanishMexican);
EXPECT_TRUE(IsEqualAvailability(
projector_client_->GetSpeechRecognitionAvailability(), availability));
SetLocale(kEnglishNewZealand);
EXPECT_TRUE(IsEqualAvailability(
projector_client_->GetSpeechRecognitionAvailability(), availability));
} else {
availability.use_on_device = true;
availability.on_device_availability =
ash::OnDeviceRecognitionAvailability::kUserLanguageNotAvailable;
EXPECT_TRUE(IsEqualAvailability(
projector_client_->GetSpeechRecognitionAvailability(), availability));
}
SetLocale(kEnglishUS);
if (force_enable_server_based && server_based_available) {
availability.use_on_device = false;
availability.server_based_availability =
ash::ServerBasedRecognitionAvailability::kAvailable;
EXPECT_TRUE(IsEqualAvailability(
projector_client_->GetSpeechRecognitionAvailability(), availability));
} else {
availability.use_on_device = true;
availability.on_device_availability =
ash::OnDeviceRecognitionAvailability::kAvailable;
EXPECT_TRUE(IsEqualAvailability(
projector_client_->GetSpeechRecognitionAvailability(), availability));
}
SetLocale(kUnsupportedLanguage);
if (force_enable_server_based) {
availability.use_on_device = false;
availability.server_based_availability =
ash::ServerBasedRecognitionAvailability::kUserLanguageNotAvailable;
EXPECT_TRUE(IsEqualAvailability(
projector_client_->GetSpeechRecognitionAvailability(), availability));
} else {
availability.use_on_device = true;
availability.on_device_availability =
ash::OnDeviceRecognitionAvailability::kUserLanguageNotAvailable;
SetLocale(kUnsupportedLanguage);
EXPECT_TRUE(IsEqualAvailability(
projector_client_->GetSpeechRecognitionAvailability(), availability));
}
}
TEST_P(ProjectorClientImplUnitTest, FallbackReasonMetric) {
const bool force_enable_server_based =
features::ShouldForceEnableServerSideSpeechRecognition();
const bool server_based_available =
features::IsInternalServerSideSpeechRecognitionEnabled();
size_t expect_total_count = 0;
SetLocale(kFrench);
if (server_based_available) {
if (force_enable_server_based) {
MaybeReportToServerBasedFallbackReasonMetricAndVerify(
ash::OnDeviceToServerSpeechRecognitionFallbackReason::kEnforcedByFlag,
1);
expect_total_count++;
} else {
// French is not available for SODA:
MaybeReportToServerBasedFallbackReasonMetricAndVerify(
ash::OnDeviceToServerSpeechRecognitionFallbackReason::
kUserLanguageNotAvailableForSoda,
1);
// Set available language for SODA:
SetLocale(kEnglishUS);
// Verify metric reports for SODA error:
speech::SodaInstaller::GetInstance()->UninstallSodaForTesting();
speech::SodaInstaller::GetInstance()->NotifySodaErrorForTesting(
speech::LanguageCode::kEnUs,
speech::SodaInstaller::ErrorCode::kUnspecifiedError);
MaybeReportToServerBasedFallbackReasonMetricAndVerify(
ash::OnDeviceToServerSpeechRecognitionFallbackReason::
kSodaInstallationErrorUnspecified,
1);
speech::SodaInstaller::GetInstance()->NotifySodaErrorForTesting(
speech::LanguageCode::kEnUs,
speech::SodaInstaller::ErrorCode::kNeedsReboot);
MaybeReportToServerBasedFallbackReasonMetricAndVerify(
ash::OnDeviceToServerSpeechRecognitionFallbackReason::
kSodaInstallationErrorNeedsReboot,
1);
expect_total_count += 3;
}
} else {
if (!force_enable_server_based) {
// Set available language for SODA:
SetLocale(kEnglishUS);
// No metric reports if SODA is available:
MaybeReportToServerBasedFallbackReasonMetricAndVerify(std::nullopt, 0);
}
}
histogram_tester_.ExpectTotalCount(kS3FallbackReasonMetricName,
/*count=*/expect_total_count);
}
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
// TODO: dorianbrandon - Remove finch flag from disabled list. The finch
// flag currently sets the experiment to true for all languages. This isn't a
// problem since all ChromeOS languages are covered but it affects the
// structure of the language disabled test.
INSTANTIATE_TEST_SUITE_P(
ProjectorClientTestScenarios,
ProjectorClientImplUnitTest,
::testing::Values(
ProjectorClientTestScenario(
{features::kOnDeviceSpeechRecognition},
{features::kInternalServerSideSpeechRecognitionUSMModelFinch}),
ProjectorClientTestScenario(
{features::kOnDeviceSpeechRecognition,
features::kForceEnableServerSideSpeechRecognition},
{features::kInternalServerSideSpeechRecognitionUSMModelFinch}),
ProjectorClientTestScenario(
{features::kInternalServerSideSpeechRecognition,
features::kOnDeviceSpeechRecognition},
{features::kForceEnableServerSideSpeechRecognition,
features::kInternalServerSideSpeechRecognitionUSMModelFinch})));
} // namespace ash
|