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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/regional_capabilities/regional_capabilities_service.h"
#include <memory>
#include <optional>
#include "base/check_deref.h"
#include "base/memory/weak_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "components/country_codes/country_codes.h"
#include "components/regional_capabilities/regional_capabilities_metrics.h"
#include "components/regional_capabilities/regional_capabilities_prefs.h"
#include "components/regional_capabilities/regional_capabilities_switches.h"
#include "components/regional_capabilities/regional_capabilities_test_utils.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "regional_capabilities_country_id.h"
#include "regional_capabilities_service.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::country_codes::CountryId;
namespace regional_capabilities {
namespace {
class AsyncRegionalCapabilitiesServiceClient
: public RegionalCapabilitiesService::Client {
public:
explicit AsyncRegionalCapabilitiesServiceClient(
CountryId fallback_country_id = CountryId())
: fallback_country_id_(fallback_country_id) {}
~AsyncRegionalCapabilitiesServiceClient() override = default;
CountryId GetFallbackCountryId() override { return fallback_country_id_; }
CountryId GetVariationsLatestCountryId() override { return CountryId(); }
void FetchCountryId(CountryIdCallback country_id_fetched_callback) override {
ASSERT_FALSE(cached_country_id_callback_) << "Test setup error";
if (fetched_country_id_.has_value()) {
std::move(country_id_fetched_callback).Run(fetched_country_id_.value());
} else {
// To be run next time we run `SetFetchedCountry()`;
cached_country_id_callback_ = std::move(country_id_fetched_callback);
}
}
void SetFetchedCountry(std::optional<CountryId> fetched_country_id) {
fetched_country_id_ = fetched_country_id;
if (cached_country_id_callback_ && fetched_country_id_.has_value()) {
std::move(cached_country_id_callback_).Run(fetched_country_id_.value());
}
}
base::WeakPtr<AsyncRegionalCapabilitiesServiceClient> AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
const CountryId fallback_country_id_;
std::optional<CountryId> fetched_country_id_;
CountryIdCallback cached_country_id_callback_;
base::WeakPtrFactory<AsyncRegionalCapabilitiesServiceClient>
weak_ptr_factory_{this};
};
constexpr char kBelgiumCountryCode[] = "BE";
constexpr CountryId kBelgiumCountryId = CountryId("BE");
CountryId GetCountryId(RegionalCapabilitiesService& service) {
return service.GetCountryId().GetForTesting();
}
class RegionalCapabilitiesServiceTest : public ::testing::Test {
public:
RegionalCapabilitiesServiceTest() {
prefs::RegisterProfilePrefs(pref_service_.registry());
}
~RegionalCapabilitiesServiceTest() override = default;
void ClearCommandLineCountry() {
base::CommandLine::ForCurrentProcess()->RemoveSwitch(
switches::kSearchEngineChoiceCountry);
}
void SetCommandLineCountry(std::string_view country_code) {
ClearCommandLineCountry();
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kSearchEngineChoiceCountry, country_code);
}
std::optional<int> GetPrefSerializedCountry() {
if (!pref_service().HasPrefPath(prefs::kCountryIDAtInstall)) {
return std::nullopt;
}
return pref_service().GetInteger(prefs::kCountryIDAtInstall);
}
void SetPrefCountry(CountryId country_id) {
pref_service().SetInteger(prefs::kCountryIDAtInstall,
country_id.Serialize());
}
std::unique_ptr<RegionalCapabilitiesService> InitService(
CountryId fallback_country_id = CountryId()) {
auto client = std::make_unique<AsyncRegionalCapabilitiesServiceClient>(
fallback_country_id);
weak_client_ = client->AsWeakPtr();
return std::make_unique<RegionalCapabilitiesService>(pref_service_,
std::move(client));
}
sync_preferences::TestingPrefServiceSyncable& pref_service() {
return pref_service_;
}
base::HistogramTester& histogram_tester() { return histogram_tester_; }
base::WeakPtr<AsyncRegionalCapabilitiesServiceClient> client() {
return weak_client_;
}
private:
sync_preferences::TestingPrefServiceSyncable pref_service_;
TestingPrefServiceSimple local_state_;
base::WeakPtr<AsyncRegionalCapabilitiesServiceClient> weak_client_;
base::HistogramTester histogram_tester_;
};
TEST_F(RegionalCapabilitiesServiceTest, GetCountryIdCommandLineOverride) {
// The command line value bypasses the country ID cache and does not
// require recreating the service.
std::unique_ptr<RegionalCapabilitiesService> service = InitService();
SetCommandLineCountry(kBelgiumCountryCode);
EXPECT_EQ(GetCountryId(*service), kBelgiumCountryId);
// When the command line value is not two uppercase basic Latin alphabet
// characters, the country code should not be valid.
SetCommandLineCountry("??");
EXPECT_FALSE(GetCountryId(*service).IsValid());
SetCommandLineCountry("us");
EXPECT_FALSE(GetCountryId(*service).IsValid());
SetCommandLineCountry("USA");
EXPECT_FALSE(GetCountryId(*service).IsValid());
histogram_tester().ExpectTotalCount(
"RegionalCapabilities.FetchedCountryMatching", 0);
histogram_tester().ExpectTotalCount(
"RegionalCapabilities.FallbackCountryMatching", 0);
histogram_tester().ExpectTotalCount(
"RegionalCapabilities.PersistedCountryMatching", 0);
histogram_tester().ExpectTotalCount(
"RegionalCapabilities.LoadedCountrySource", 0);
}
TEST_F(RegionalCapabilitiesServiceTest, GetCountryId_FetchedSync) {
const auto kFallbackCountryId = CountryId("FR");
std::unique_ptr<RegionalCapabilitiesService> service =
InitService(kFallbackCountryId);
client()->SetFetchedCountry(kBelgiumCountryId);
// The fetched country is available synchronously, before `GetCountryId` was
// invoked for the first time this run, so the new value should be used
// right away.
EXPECT_EQ(GetCountryId(*service), kBelgiumCountryId);
// The pref should be updated as well.
EXPECT_EQ(GetPrefSerializedCountry(), kBelgiumCountryId.Serialize());
histogram_tester().ExpectUniqueSample(
"RegionalCapabilities.FetchedCountryMatching", 2, 1);
histogram_tester().ExpectTotalCount(
"RegionalCapabilities.FallbackCountryMatching", 0);
histogram_tester().ExpectUniqueSample(
"RegionalCapabilities.PersistedCountryMatching", 1, 1);
histogram_tester().ExpectUniqueSample(
"RegionalCapabilities.LoadedCountrySource",
static_cast<int>(LoadedCountrySource::kCurrentOnly), 1);
}
TEST_F(RegionalCapabilitiesServiceTest, GetCountryId_FetchedAsyncUsesFallback) {
const auto kFallbackCountryId = CountryId("FR");
std::unique_ptr<RegionalCapabilitiesService> service =
InitService(kFallbackCountryId);
// We didn't get a response from the device API call before `GetCountryId`
// was invoked, so the fallback country should be used.
EXPECT_EQ(GetCountryId(*service), kFallbackCountryId);
// The pref should not be updated.
EXPECT_EQ(GetPrefSerializedCountry(), std::nullopt);
// Simulate a response arriving after the first `GetCountryId` call.
client()->SetFetchedCountry(kBelgiumCountryId);
// The pref should be updated so the new country can be used the next run.
EXPECT_EQ(GetPrefSerializedCountry(), kBelgiumCountryId.Serialize());
// However, the `GetCountryId()` result shouldn't change until the next run.
EXPECT_EQ(GetCountryId(*service), kFallbackCountryId);
histogram_tester().ExpectTotalCount(
"RegionalCapabilities.FetchedCountryMatching", 0);
histogram_tester().ExpectUniqueSample(
"RegionalCapabilities.FallbackCountryMatching",
2 /* kVariationsCountryMissing */, 1);
histogram_tester().ExpectUniqueSample(
"RegionalCapabilities.PersistedCountryMatching", 1 /* kCountryMissing */,
1);
histogram_tester().ExpectUniqueSample(
"RegionalCapabilities.LoadedCountrySource",
static_cast<int>(LoadedCountrySource::kCurrentOnly), 1);
}
TEST_F(RegionalCapabilitiesServiceTest, GetCountryId_PrefAlreadyWritten) {
const auto kFallbackCountryId = CountryId("FR");
const auto kFetchedCountryId = CountryId("US");
std::unique_ptr<RegionalCapabilitiesService> service =
InitService(kFallbackCountryId);
client()->SetFetchedCountry(kFetchedCountryId);
SetPrefCountry(kBelgiumCountryId);
// The value set from the pref should be used instead of the ones from the
// client.
EXPECT_EQ(GetCountryId(*service), kBelgiumCountryId);
// The fetched value from the client does not overwrite the prefs either.
EXPECT_EQ(GetPrefSerializedCountry(), kBelgiumCountryId.Serialize());
histogram_tester().ExpectUniqueSample(
"RegionalCapabilities.FetchedCountryMatching", 2, 1);
histogram_tester().ExpectTotalCount(
"RegionalCapabilities.FallbackCountryMatching", 0);
histogram_tester().ExpectUniqueSample(
"RegionalCapabilities.PersistedCountryMatching", 2, 1);
histogram_tester().ExpectUniqueSample(
"RegionalCapabilities.LoadedCountrySource",
static_cast<int>(LoadedCountrySource::kPersistedPreferred), 1);
}
TEST_F(RegionalCapabilitiesServiceTest, GetCountryId_PrefChangesAfterReading) {
const auto kFallbackCountryId = CountryId("FR");
std::unique_ptr<RegionalCapabilitiesService> service =
InitService(kFallbackCountryId);
// The value set from the pref should be used.
SetPrefCountry(kBelgiumCountryId);
EXPECT_EQ(GetCountryId(*service), kBelgiumCountryId);
// Change the value in pref.
SetPrefCountry(CountryId("US"));
// The value returned by `GetCountryId` shouldn't change.
EXPECT_EQ(GetCountryId(*service), kBelgiumCountryId);
}
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
TEST_F(RegionalCapabilitiesServiceTest, ClearPrefForUnknownCountry) {
base::test::ScopedFeatureList scoped_feature_list{
switches::kClearPrefForUnknownCountry};
SetPrefCountry(CountryId());
std::unique_ptr<RegionalCapabilitiesService> service =
InitService(kBelgiumCountryId);
// The fetch needs to succeed, otherwise the obtained value is the fallback
// one and the pref will not be persisted.
client()->SetFetchedCountry(kBelgiumCountryId);
histogram_tester().ExpectTotalCount(
"Search.ChoiceDebug.UnknownCountryIdStored", 0);
EXPECT_EQ(GetCountryId(*service), kBelgiumCountryId);
histogram_tester().ExpectUniqueSample(
"Search.ChoiceDebug.UnknownCountryIdStored", 2 /* kClearedPref */, 1);
EXPECT_EQ(GetPrefSerializedCountry(), kBelgiumCountryId.Serialize());
}
TEST_F(RegionalCapabilitiesServiceTest, ClearPrefForUnknownCountry_Disabled) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(
switches::kClearPrefForUnknownCountry);
SetPrefCountry(CountryId());
std::unique_ptr<RegionalCapabilitiesService> service =
InitService(kBelgiumCountryId);
histogram_tester().ExpectTotalCount(
"Search.ChoiceDebug.UnknownCountryIdStored", 0);
EXPECT_EQ(GetCountryId(*service), country_codes::CountryId());
histogram_tester().ExpectUniqueSample(
"Search.ChoiceDebug.UnknownCountryIdStored",
1 /* kDontClearInvalidCountry */, 1);
EXPECT_EQ(GetPrefSerializedCountry(), country_codes::CountryId().Serialize());
}
TEST_F(RegionalCapabilitiesServiceTest, ClearPrefForUnknownCountry_Valid) {
base::test::ScopedFeatureList scoped_feature_list{
switches::kClearPrefForUnknownCountry};
SetPrefCountry(kBelgiumCountryId);
std::unique_ptr<RegionalCapabilitiesService> service = InitService();
histogram_tester().ExpectTotalCount(
"Search.ChoiceDebug.UnknownCountryIdStored", 0);
EXPECT_EQ(GetCountryId(*service), kBelgiumCountryId);
histogram_tester().ExpectUniqueSample(
"Search.ChoiceDebug.UnknownCountryIdStored", 0 /* kValidCountryId */, 1);
EXPECT_EQ(GetPrefSerializedCountry(), kBelgiumCountryId.Serialize());
}
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) ||
// BUILDFLAG(IS_LINUX)
TEST_F(RegionalCapabilitiesServiceTest, IsInEeaCountry) {
std::unique_ptr<RegionalCapabilitiesService> service =
InitService(kBelgiumCountryId);
EXPECT_TRUE(service->IsInEeaCountry());
SetCommandLineCountry("US");
EXPECT_FALSE(service->IsInEeaCountry());
SetCommandLineCountry(kBelgiumCountryCode);
EXPECT_TRUE(service->IsInEeaCountry());
// When --search-engine-choice-country is set to DEFAULT_EEA or EEA_ALL, the
// country is always considered as being in the EEA.
SetCommandLineCountry(switches::kDefaultListCountryOverride);
EXPECT_TRUE(service->IsInEeaCountry());
SetCommandLineCountry(switches::kEeaListCountryOverride);
EXPECT_TRUE(service->IsInEeaCountry());
}
} // namespace
} // namespace regional_capabilities
|