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
|
// 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/accessibility/soda_installer_impl.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/live_caption/pref_names.h"
#include "components/prefs/testing_pref_service.h"
#include "components/soda/constants.h"
#include "components/soda/pref_names.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const speech::LanguageCode kEnglishLocale = speech::LanguageCode::kEnUs;
const speech::LanguageCode kJapaneseLocale = speech::LanguageCode::kJaJp;
const base::TimeDelta kSodaUninstallTime = base::Days(30);
constexpr char kSodaEnglishLanguageInstallationResult[] =
"SodaInstaller.Language.en-US.InstallationResult";
} // namespace
namespace speech {
class MockSodaInstallerImpl : public SodaInstallerImpl {
public:
MockSodaInstallerImpl() = default;
~MockSodaInstallerImpl() override = default;
void InstallSoda(PrefService* global_prefs) override {
OnSodaBinaryInstalled();
}
void InstallLanguage(const std::string& language,
PrefService* global_prefs) override {
OnSodaLanguagePackInstalled(speech::GetLanguageCode(language));
}
};
class SodaInstallerImplTest : public testing::Test {
protected:
void SetUp() override {
soda_installer_impl_ = std::make_unique<MockSodaInstallerImpl>();
pref_service_ = std::make_unique<TestingPrefServiceSimple>();
soda_installer_impl_->RegisterLocalStatePrefs(pref_service_->registry());
pref_service_->registry()->RegisterBooleanPref(prefs::kLiveCaptionEnabled,
true);
pref_service_->registry()->RegisterStringPref(
prefs::kLiveCaptionLanguageCode, kUsEnglishLocale);
}
void TearDown() override {
soda_installer_impl_.reset();
pref_service_.reset();
}
SodaInstallerImpl* GetInstance() { return soda_installer_impl_.get(); }
bool IsSodaInstalled(
speech::LanguageCode language_code = speech::LanguageCode::kEnUs) {
return soda_installer_impl_->IsSodaInstalled(language_code);
}
bool IsLanguagePackInstalled(speech::LanguageCode language_code) {
return soda_installer_impl_->IsLanguageInstalled(language_code);
}
bool IsSodaDownloading(
speech::LanguageCode language_code = speech::LanguageCode::kEnUs) {
return soda_installer_impl_->IsSodaDownloading(language_code);
}
void InstallLanguage(const std::string& language) {
return soda_installer_impl_->InstallLanguage(language, pref_service_.get());
}
void UninstallLanguage(const std::string& language) {
return soda_installer_impl_->UninstallLanguage(language,
pref_service_.get());
}
void Init() {
soda_installer_impl_->Init(pref_service_.get(), pref_service_.get());
task_environment_.RunUntilIdle();
}
void SetUninstallTimer() {
soda_installer_impl_->SetUninstallTimer(pref_service_.get(),
pref_service_.get());
}
void FastForwardBy(base::TimeDelta delta) {
task_environment_.FastForwardBy(delta);
}
void SetLiveCaptionEnabled(bool enabled) {
pref_service_->SetManagedPref(prefs::kLiveCaptionEnabled,
std::make_unique<base::Value>(enabled));
}
void SetSodaInstallerInitialized(bool initialized) {
soda_installer_impl_->soda_installer_initialized_ = initialized;
}
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
std::unique_ptr<MockSodaInstallerImpl> soda_installer_impl_;
std::unique_ptr<TestingPrefServiceSimple> pref_service_;
};
TEST_F(SodaInstallerImplTest, IsSodaInstalled) {
base::HistogramTester histogram_tester;
ASSERT_FALSE(IsSodaInstalled());
Init();
ASSERT_TRUE(IsSodaInstalled());
// SODA binary and english language installation never failed.
histogram_tester.ExpectBucketCount(kSodaBinaryInstallationResult, 0, 0);
histogram_tester.ExpectBucketCount(kSodaEnglishLanguageInstallationResult, 0,
0);
// SODA binary and english language installation succeeded once.
histogram_tester.ExpectBucketCount(kSodaBinaryInstallationResult, 1, 1);
histogram_tester.ExpectBucketCount(kSodaEnglishLanguageInstallationResult, 1,
1);
}
TEST_F(SodaInstallerImplTest, IsDownloading) {
ASSERT_FALSE(IsSodaDownloading());
Init();
ASSERT_FALSE(IsSodaDownloading());
}
TEST_F(SodaInstallerImplTest, IsLanguagePackInstalled) {
ASSERT_FALSE(IsLanguagePackInstalled(kEnglishLocale));
Init();
ASSERT_TRUE(IsLanguagePackInstalled(kEnglishLocale));
ASSERT_FALSE(IsLanguagePackInstalled(kJapaneseLocale));
}
TEST_F(SodaInstallerImplTest, UninstallLanguagePacks) {
ASSERT_FALSE(IsLanguagePackInstalled(kEnglishLocale));
ASSERT_FALSE(IsLanguagePackInstalled(kJapaneseLocale));
Init();
ASSERT_TRUE(IsLanguagePackInstalled(kEnglishLocale));
ASSERT_FALSE(IsLanguagePackInstalled(kJapaneseLocale));
InstallLanguage(speech::GetLanguageName(kJapaneseLocale));
ASSERT_TRUE(IsLanguagePackInstalled(kEnglishLocale));
ASSERT_TRUE(IsLanguagePackInstalled(kJapaneseLocale));
UninstallLanguage(speech::GetLanguageName(kEnglishLocale));
ASSERT_FALSE(IsLanguagePackInstalled(kEnglishLocale));
ASSERT_TRUE(IsLanguagePackInstalled(kJapaneseLocale));
}
TEST_F(SodaInstallerImplTest, AvailableLanguagesTest) {
auto actual_available_langs = soda_installer_impl_->GetAvailableLanguages();
auto expected_available_langs =
soda_installer_impl_->GetLiveCaptionEnabledLanguages();
EXPECT_THAT(actual_available_langs,
::testing::UnorderedElementsAreArray(expected_available_langs));
}
TEST_F(SodaInstallerImplTest, UninstallSodaAfterThirtyDays) {
Init();
ASSERT_TRUE(IsSodaInstalled());
// Turn off features that use SODA so that the uninstall timer can be set.
SetLiveCaptionEnabled(false);
SetUninstallTimer();
ASSERT_TRUE(IsSodaInstalled());
// If 30 days pass without the uninstall time being reset, SODA will be
// uninstalled the next time Init() is called.
// Set SodaInstaller initialized state to false to mimic a browser shutdown.
SetSodaInstallerInitialized(false);
FastForwardBy(kSodaUninstallTime);
ASSERT_TRUE(IsSodaInstalled());
// The uninstallation process doesn't start until Init() is called again.
Init();
ASSERT_FALSE(IsSodaInstalled());
}
TEST_F(SodaInstallerImplTest, ReregisterSodaWithinThirtyDays) {
Init();
ASSERT_TRUE(IsSodaInstalled());
// Turn off features that use SODA so that the uninstall timer can be set.
SetLiveCaptionEnabled(false);
SetUninstallTimer();
ASSERT_TRUE(IsSodaInstalled());
// Fast forward SODA and manually uninstall SODA to simulate a browser
// restart.
SetSodaInstallerInitialized(false);
FastForwardBy(base::Days(1));
GetInstance()->UninstallSodaForTesting();
ASSERT_FALSE(IsSodaInstalled());
// SODA should be registered because so it recently used within the last 30
// days.
Init();
ASSERT_TRUE(IsSodaInstalled());
}
// Tests that SODA stays installed if thirty days pass and a feature using SODA
// is enabled.
TEST_F(SodaInstallerImplTest,
SodaStaysInstalledAfterThirtyDaysIfFeatureEnabled) {
Init();
ASSERT_TRUE(IsSodaInstalled());
SetUninstallTimer();
ASSERT_TRUE(IsSodaInstalled());
// Set SodaInstaller initialized state to false to mimic a browser shutdown.
FastForwardBy(kSodaUninstallTime);
ASSERT_TRUE(IsSodaInstalled());
Init();
ASSERT_TRUE(IsSodaInstalled());
}
// Tests that SODA can be reinstalled after previously being uninstalled.
TEST_F(SodaInstallerImplTest, ReinstallSoda) {
Init();
ASSERT_TRUE(IsSodaInstalled());
// Turn off features that use SODA so that the uninstall timer can be set.
SetLiveCaptionEnabled(false);
SetUninstallTimer();
ASSERT_TRUE(IsSodaInstalled());
// If 30 days pass without the uninstall time being pushed, SODA will be
// uninstalled the next time Init() is called.
// Set SodaInstaller initialized state to false to mimic a browser shutdown.
FastForwardBy(kSodaUninstallTime);
ASSERT_TRUE(IsSodaInstalled());
// Set SodaInstaller initialized state to false to mimic a browser shutdown.
SetSodaInstallerInitialized(false);
// The uninstallation process doesn't start until Init() is called again.
Init();
ASSERT_FALSE(IsSodaInstalled());
SetLiveCaptionEnabled(true);
Init();
ASSERT_TRUE(IsSodaInstalled());
}
} // namespace speech
|