Refactor test scoped default locale

This commit is contained in:
Terry Mancey
2022-10-12 11:56:24 -05:00
parent 3c6c8badda
commit 398bf5ef0f
28 changed files with 216 additions and 174 deletions
+4 -5
View File
@@ -26,7 +26,7 @@
#include "brave/components/brave_rewards/browser/rewards_service_impl.h"
#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_util.h"
#include "brave/components/constants/brave_paths.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h"
@@ -201,7 +201,7 @@ class BraveAdsBrowserTest : public InProcessBrowserTest,
}
scoped_default_locale_ =
std::make_unique<brave_l10n::ScopedDefaultLocaleForTesting>(it->second);
std::make_unique<brave_l10n::test::ScopedDefaultLocale>(it->second);
}
void MaybeMockLocaleForBraveAdsUpgradePath() {
@@ -239,7 +239,7 @@ class BraveAdsBrowserTest : public InProcessBrowserTest,
}
scoped_default_locale_ =
std::make_unique<brave_l10n::ScopedDefaultLocaleForTesting>(locale);
std::make_unique<brave_l10n::test::ScopedDefaultLocale>(locale);
}
void MaybeMockUserProfilePreferencesForBraveAdsUpgradePath() {
@@ -343,8 +343,7 @@ class BraveAdsBrowserTest : public InProcessBrowserTest,
std::string wallet_;
std::string parameters_;
std::unique_ptr<brave_l10n::ScopedDefaultLocaleForTesting>
scoped_default_locale_;
std::unique_ptr<brave_l10n::test::ScopedDefaultLocale> scoped_default_locale_;
};
IN_PROC_BROWSER_TEST_F(BraveAdsBrowserTest, BraveAdsLocaleIsSupported) {
+1
View File
@@ -454,6 +454,7 @@ source_set("brave_ads_unit_tests") {
"//brave/components/brave_rewards/test:brave_rewards_unit_tests",
"//brave/components/challenge_bypass_ristretto",
"//brave/components/l10n/common",
"//brave/components/l10n/common:test_support",
"//brave/components/version_info:version_info",
"//brave/vendor/bat-native-ads",
"//brave/vendor/bat-native-ledger",
@@ -19,7 +19,7 @@
#include "brave/components/brave_rewards/common/pref_names.h"
#include "brave/components/brave_rewards/common/rewards_flags.h"
#include "brave/components/greaselion/browser/buildflags/buildflags.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/prefs/pref_service.h"
@@ -43,7 +43,7 @@ class RewardsServiceJPTest : public testing::Test {
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
scoped_default_locale_ =
std::make_unique<brave_l10n::ScopedDefaultLocaleForTesting>("ja_JP");
std::make_unique<brave_l10n::test::ScopedDefaultLocale>("ja_JP");
profile_ = CreateBraveRewardsProfile(temp_dir_.GetPath());
ASSERT_TRUE(profile_);
#if BUILDFLAG(ENABLE_GREASELION)
@@ -92,8 +92,7 @@ class RewardsServiceJPTest : public testing::Test {
std::unique_ptr<Profile> profile_;
raw_ptr<RewardsServiceImpl> rewards_service_ = nullptr;
base::ScopedTempDir temp_dir_;
std::unique_ptr<brave_l10n::ScopedDefaultLocaleForTesting>
scoped_default_locale_;
std::unique_ptr<brave_l10n::test::ScopedDefaultLocale> scoped_default_locale_;
};
#if BUILDFLAG(ENABLE_GEMINI_WALLET)
@@ -18,7 +18,7 @@
#include "brave/components/brave_rewards/common/features.h"
#include "brave/components/brave_rewards/common/pref_names.h"
#include "brave/components/greaselion/browser/buildflags/buildflags.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/prefs/pref_service.h"
@@ -74,7 +74,7 @@ class RewardsServiceTest : public testing::Test {
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
scoped_default_locale_ =
std::make_unique<brave_l10n::ScopedDefaultLocaleForTesting>("en_US");
std::make_unique<brave_l10n::test::ScopedDefaultLocale>("en_US");
profile_ = CreateBraveRewardsProfile(temp_dir_.GetPath());
ASSERT_TRUE(profile_.get());
#if BUILDFLAG(ENABLE_GREASELION)
@@ -124,8 +124,7 @@ class RewardsServiceTest : public testing::Test {
raw_ptr<RewardsServiceImpl> rewards_service_ = nullptr;
std::unique_ptr<MockRewardsServiceObserver> observer_;
base::ScopedTempDir temp_dir_;
std::unique_ptr<brave_l10n::ScopedDefaultLocaleForTesting>
scoped_default_locale_;
std::unique_ptr<brave_l10n::test::ScopedDefaultLocale> scoped_default_locale_;
};
#if BUILDFLAG(ENABLE_GEMINI_WALLET)
+1 -1
View File
@@ -25,7 +25,7 @@ source_set("brave_rewards_unit_tests") {
"//brave/components/brave_rewards/resources:static_resources_grit",
"//brave/components/challenge_bypass_ristretto",
"//brave/components/greaselion/browser/buildflags:buildflags",
"//brave/components/l10n/common",
"//brave/components/l10n/common:test_support",
"//brave/vendor/bat-native-ledger",
"//brave/vendor/bat-native-ledger:publishers_proto",
"//brave/vendor/bat-native-rapidjson",
@@ -19,7 +19,7 @@
#include "brave/components/brave_search/common/features.h"
#include "brave/components/brave_search_conversion/features.h"
#include "brave/components/brave_search_conversion/utils.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
@@ -152,7 +152,7 @@ TEST_F(BraveSearchDefaultHostTest, DisallowsAfterMaxTimesAsked) {
TEST_F(BraveSearchDefaultHostTest, CanSetDefaultAlwaysTestWithSearchPromotion) {
base::test::ScopedFeatureList feature_list;
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"en_US"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_US"};
auto host = GetAPIHost("search.test.com");
// Add a search provider for the host
+1 -1
View File
@@ -27,7 +27,7 @@ source_set("brave_news_unit_tests") {
"//brave/components/brave_today/browser",
"//brave/components/brave_today/common",
"//brave/components/brave_today/common:mojom",
"//brave/components/l10n/common",
"//brave/components/l10n/common:test_support",
"//chrome/browser",
"//chrome/test:test_support",
"//content/test:test_support",
+10 -10
View File
@@ -8,7 +8,7 @@
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "brave/components/brave_today/common/features.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
@@ -22,14 +22,14 @@ TEST_F(BraveNewsUrlsTest, BraveNewsV2IsDisabled) {
TEST_F(BraveNewsUrlsTest, BraveNewsUsesV1ByDefault) {
{
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"en_US"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_US"};
std::string region = brave_today::GetRegionUrlPart();
EXPECT_EQ(brave_today::GetV1RegionUrlPart(), region);
EXPECT_EQ("", region);
}
{
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"ja_JP"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"ja_JP"};
std::string region = brave_today::GetRegionUrlPart();
EXPECT_EQ(brave_today::GetV1RegionUrlPart(), region);
EXPECT_EQ("ja", region);
@@ -37,7 +37,7 @@ TEST_F(BraveNewsUrlsTest, BraveNewsUsesV1ByDefault) {
// Unknown/unsupported locale.
{
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"na_NA"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"na_NA"};
std::string region = brave_today::GetRegionUrlPart();
EXPECT_EQ(brave_today::GetV1RegionUrlPart(), region);
EXPECT_EQ("", region);
@@ -49,17 +49,17 @@ TEST_F(BraveNewsUrlsTest, BraveNewsV2FlagUsesGlobalFeeds) {
features.InitAndEnableFeature(brave_today::features::kBraveNewsV2Feature);
{
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"en_US"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_US"};
EXPECT_EQ("global.", brave_today::GetRegionUrlPart());
}
{
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"ja_JP"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"ja_JP"};
EXPECT_EQ("global.", brave_today::GetRegionUrlPart());
}
{
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"na_NA"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"na_NA"};
EXPECT_EQ("global.", brave_today::GetRegionUrlPart());
}
}
@@ -69,17 +69,17 @@ TEST_F(BraveNewsUrlsTest, BraveNewsV2FlagDoesNotAffectV1Region) {
features.InitAndEnableFeature(brave_today::features::kBraveNewsV2Feature);
{
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"en_US"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_US"};
EXPECT_EQ("", brave_today::GetV1RegionUrlPart());
}
{
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"ja-JP"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"ja-JP"};
EXPECT_EQ("ja", brave_today::GetV1RegionUrlPart());
}
{
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"na-NA"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"na-NA"};
EXPECT_EQ("", brave_today::GetV1RegionUrlPart());
}
}
+21 -1
View File
@@ -1,5 +1,8 @@
static_library("common") {
friend = [ ":unit_tests" ]
friend = [
":test_support",
":unit_tests",
]
public = [
"locale_util.h",
@@ -9,6 +12,8 @@ static_library("common") {
]
sources = [
"default_locale.cc",
"default_locale.h",
"locale_subtag_info.cc",
"locale_subtag_info.h",
"locale_subtag_parser_util.cc",
@@ -33,6 +38,20 @@ static_library("common") {
]
}
static_library("test_support") {
testonly = true
sources = [
"test/scoped_default_locale.cc",
"test/scoped_default_locale.h",
]
deps = [
"//base",
"//brave/components/l10n/common",
]
}
source_set("unit_tests") {
testonly = true
@@ -47,6 +66,7 @@ source_set("unit_tests") {
deps = [
"//base",
"//brave/components/l10n/common",
"//brave/components/l10n/common:test_support",
"//brave/components/resources:strings_grit",
"//testing/gmock",
"//testing/gtest",
+33
View File
@@ -0,0 +1,33 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "brave/components/l10n/common/default_locale.h"
#include "base/no_destructor.h"
#include "brave/components/l10n/browser/default_locale_util.h"
namespace brave_l10n {
namespace {
constexpr char kFallbackLocale[] = "en_US";
std::string& MutableDefaultLocaleString() {
static base::NoDestructor<std::string> locale(
MaybeGetDefaultLocaleString().value_or(kFallbackLocale));
return *locale;
}
} // namespace
const std::string& DefaultLocaleString() {
return MutableDefaultLocaleString();
}
std::string& MutableDefaultLocaleStringForTesting() {
return MutableDefaultLocaleString();
}
} // namespace brave_l10n
+18
View File
@@ -0,0 +1,18 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef BRAVE_COMPONENTS_L10N_COMMON_DEFAULT_LOCALE_H_
#define BRAVE_COMPONENTS_L10N_COMMON_DEFAULT_LOCALE_H_
#include <string>
namespace brave_l10n {
const std::string& DefaultLocaleString();
std::string& MutableDefaultLocaleStringForTesting();
} // namespace brave_l10n
#endif // BRAVE_COMPONENTS_L10N_COMMON_DEFAULT_LOCALE_H_
+3 -62
View File
@@ -5,8 +5,7 @@
#include "brave/components/l10n/common/locale_util.h"
#include "base/no_destructor.h"
#include "brave/components/l10n/browser/default_locale_util.h"
#include "brave/components/l10n/common/default_locale.h"
#include "brave/components/l10n/common/locale_subtag_info.h"
#include "brave/components/l10n/common/locale_subtag_parser_util.h"
@@ -14,41 +13,13 @@ namespace brave_l10n {
namespace {
constexpr char kFallbackLocale[] = "en_US";
constexpr char kFallbackLanguageCode[] = "en";
constexpr char kFallbackCountryCode[] = "US";
absl::optional<std::string>& DefaultLocaleForTesting() {
static base::NoDestructor<absl::optional<std::string>> locale;
return *locale;
}
} // namespace
ScopedDefaultLocaleForTesting::ScopedDefaultLocaleForTesting(
const std::string& locale) {
last_locale_ = DefaultLocaleForTesting();
DefaultLocaleForTesting() = locale;
}
ScopedDefaultLocaleForTesting::~ScopedDefaultLocaleForTesting() {
DefaultLocaleForTesting() = last_locale_;
}
std::string GetDefaultLocaleString() {
const absl::optional<std::string> locale_for_testing =
DefaultLocaleForTesting();
if (locale_for_testing) {
return *locale_for_testing;
}
const absl::optional<std::string> default_locale =
MaybeGetDefaultLocaleString();
if (!default_locale) {
return kFallbackLocale;
}
return *default_locale;
const std::string& GetDefaultLocaleString() {
return DefaultLocaleString();
}
std::string GetISOLanguageCode(const std::string& locale) {
@@ -61,12 +32,6 @@ std::string GetISOLanguageCode(const std::string& locale) {
}
std::string GetDefaultISOLanguageCodeString() {
const absl::optional<std::string> locale_for_testing =
DefaultLocaleForTesting();
if (locale_for_testing) {
return GetISOLanguageCode(*locale_for_testing);
}
return GetISOLanguageCode(GetDefaultLocaleString());
}
@@ -80,12 +45,6 @@ absl::optional<std::string> GetISOScriptCode(const std::string& locale) {
}
absl::optional<std::string> GetDefaultISOScriptCodeString() {
const absl::optional<std::string> locale_for_testing =
DefaultLocaleForTesting();
if (locale_for_testing) {
return GetISOScriptCode(*locale_for_testing);
}
return GetISOScriptCode(GetDefaultLocaleString());
}
@@ -99,12 +58,6 @@ std::string GetISOCountryCode(const std::string& locale) {
}
std::string GetDefaultISOCountryCodeString() {
const absl::optional<std::string> locale_for_testing =
DefaultLocaleForTesting();
if (locale_for_testing) {
return GetISOCountryCode(*locale_for_testing);
}
return GetISOCountryCode(GetDefaultLocaleString());
}
@@ -118,12 +71,6 @@ absl::optional<std::string> GetCharSet(const std::string& locale) {
}
absl::optional<std::string> GetDefaultCharSetString() {
const absl::optional<std::string> locale_for_testing =
DefaultLocaleForTesting();
if (locale_for_testing) {
return GetCharSet(*locale_for_testing);
}
return GetCharSet(GetDefaultLocaleString());
}
@@ -137,12 +84,6 @@ absl::optional<std::string> GetVariant(const std::string& locale) {
}
absl::optional<std::string> GetDefaultVariantString() {
const absl::optional<std::string> locale_for_testing =
DefaultLocaleForTesting();
if (locale_for_testing) {
return GetVariant(*locale_for_testing);
}
return GetVariant(GetDefaultLocaleString());
}
+1 -13
View File
@@ -12,24 +12,12 @@
namespace brave_l10n {
// Set the current default locale for testing, restoring the original locale
// upon destruction.
class ScopedDefaultLocaleForTesting final {
public:
explicit ScopedDefaultLocaleForTesting(const std::string& locale);
~ScopedDefaultLocaleForTesting();
private:
absl::optional<std::string> last_locale_;
};
// IMPORTANT: When the locale should match the application locale or an eligible
// string pack for localization use the canonicalized
// l10n_util::GetApplicationLocale.
// Returns the current default locale of the device as a string.
std::string GetDefaultLocaleString();
const std::string& GetDefaultLocaleString();
// Returns a lowercase two-letter ISO 639-1 language code for the given locale,
// falling back to "en" if the locale does not contain a language code. See
+22 -21
View File
@@ -5,6 +5,7 @@
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -12,12 +13,12 @@
namespace brave_l10n {
TEST(LocaleUtilTest, ScopedDefaultLocaleForTesting) {
const ScopedDefaultLocaleForTesting scoped_default_locale_us("en_KY");
TEST(LocaleUtilTest, ScopedDefaultLocale) {
const test::ScopedDefaultLocale scoped_default_locale_us("en_KY");
ASSERT_EQ("en_KY", GetDefaultLocaleString());
{
const ScopedDefaultLocaleForTesting scoped_default_locale_gb("en_GB");
const test::ScopedDefaultLocale scoped_default_locale_gb("en_GB");
ASSERT_EQ("en_GB", GetDefaultLocaleString());
}
@@ -25,37 +26,37 @@ TEST(LocaleUtilTest, ScopedDefaultLocaleForTesting) {
}
TEST(LocaleUtilTest, GetDefaultLocaleString) {
const ScopedDefaultLocaleForTesting scoped_default_locale("en_Latn_US.UTF-8");
const test::ScopedDefaultLocale scoped_default_locale("en_Latn_US.UTF-8");
EXPECT_EQ("en_Latn_US.UTF-8", GetDefaultLocaleString());
}
TEST(LocaleUtilTest, GetDefaultISOLanguageCodeString) {
const ScopedDefaultLocaleForTesting scoped_default_locale("en_Latn_US.UTF-8");
const test::ScopedDefaultLocale scoped_default_locale("en_Latn_US.UTF-8");
EXPECT_EQ("en", GetDefaultISOLanguageCodeString());
}
TEST(LocaleUtilTest, GetFallbackDefaultISOLanguageCodeString) {
const ScopedDefaultLocaleForTesting scoped_default_locale({});
const test::ScopedDefaultLocale scoped_default_locale({});
EXPECT_EQ("en", GetDefaultISOLanguageCodeString());
}
TEST(LocaleUtilTest, GetISOLanguageCodeForLocale) {
const ScopedDefaultLocaleForTesting scoped_default_locale("ja_JP");
const test::ScopedDefaultLocale scoped_default_locale("ja_JP");
EXPECT_EQ("en", GetISOLanguageCode("en_Latn_US.UTF-8@currency=USD"));
}
TEST(LocaleUtilTest, GetDefaultISOScriptCodeString) {
const ScopedDefaultLocaleForTesting scoped_default_locale("en_Latn_US.UTF-8");
const test::ScopedDefaultLocale scoped_default_locale("en_Latn_US.UTF-8");
EXPECT_EQ("Latn", GetDefaultISOScriptCodeString());
}
TEST(LocaleUtilTest, GetUnspecifiedDefaultISOScriptCodeString) {
const ScopedDefaultLocaleForTesting scoped_default_locale({});
const test::ScopedDefaultLocale scoped_default_locale({});
EXPECT_FALSE(GetDefaultISOScriptCodeString());
}
TEST(LocaleUtilTest, GetISOScriptCodeForLocale) {
const ScopedDefaultLocaleForTesting scoped_default_locale("ja_JP");
const test::ScopedDefaultLocale scoped_default_locale("ja_JP");
EXPECT_EQ("Latn", GetISOScriptCode("en_Latn_US.UTF-8@currency=USD"));
}
@@ -64,42 +65,42 @@ TEST(LocaleUtilTest, GetUnspecifiedISOScriptCodeForLocale) {
}
TEST(LocaleUtilTest, GetDefaultISOCountryCodeString) {
const ScopedDefaultLocaleForTesting scoped_default_locale("en_Latn_US.UTF-8");
const test::ScopedDefaultLocale scoped_default_locale("en_Latn_US.UTF-8");
EXPECT_EQ("US", GetDefaultISOCountryCodeString());
}
TEST(LocaleUtilTest, GetFallbackDefaultISOCountryCodeString) {
const ScopedDefaultLocaleForTesting scoped_default_locale({});
const test::ScopedDefaultLocale scoped_default_locale({});
EXPECT_EQ("US", GetDefaultISOCountryCodeString());
}
TEST(LocaleUtilTest, GetISOCountryCodeForLocale) {
const ScopedDefaultLocaleForTesting scoped_default_locale("ja_JP");
const test::ScopedDefaultLocale scoped_default_locale("ja_JP");
EXPECT_EQ("US", GetISOCountryCode("en_Latn_US.UTF-8@currency=USD"));
}
TEST(LocaleUtilTest, GetDefaultUNM49CodeString) {
const ScopedDefaultLocaleForTesting scoped_default_locale("en_001");
const test::ScopedDefaultLocale scoped_default_locale("en_001");
EXPECT_EQ("001", GetDefaultISOCountryCodeString());
}
TEST(LocaleUtilTest, GetUNM49CodeForLocaleString) {
const ScopedDefaultLocaleForTesting scoped_default_locale("ja_JP");
const test::ScopedDefaultLocale scoped_default_locale("ja_JP");
EXPECT_EQ("001", GetISOCountryCode("en_001"));
}
TEST(LocaleUtilTest, GetDefaultCharSetString) {
const ScopedDefaultLocaleForTesting scoped_default_locale("en_Latn_US.UTF-8");
const test::ScopedDefaultLocale scoped_default_locale("en_Latn_US.UTF-8");
EXPECT_EQ("UTF-8", GetDefaultCharSetString());
}
TEST(LocaleUtilTest, GetUnspecifiedDefaultCharSetString) {
const ScopedDefaultLocaleForTesting scoped_default_locale({});
const test::ScopedDefaultLocale scoped_default_locale({});
EXPECT_FALSE(GetDefaultCharSetString());
}
TEST(LocaleUtilTest, GetCharSetForLocale) {
const ScopedDefaultLocaleForTesting scoped_default_locale("ja_JP");
const test::ScopedDefaultLocale scoped_default_locale("ja_JP");
EXPECT_EQ("UTF-8", GetCharSet("en_Latn_US.UTF-8@currency=USD"));
}
@@ -108,18 +109,18 @@ TEST(LocaleUtilTest, GetUnspecifiedCharSetForLocale) {
}
TEST(LocaleUtilTest, GetDefaultVariantString) {
const ScopedDefaultLocaleForTesting scoped_default_locale(
const test::ScopedDefaultLocale scoped_default_locale(
"en_Latn_US.UTF-8@currency=USD");
EXPECT_EQ("currency=USD", GetDefaultVariantString());
}
TEST(LocaleUtilTest, GetUnspecifiedDefaultVariantString) {
const ScopedDefaultLocaleForTesting scoped_default_locale({});
const test::ScopedDefaultLocale scoped_default_locale({});
EXPECT_FALSE(GetDefaultVariantString());
}
TEST(LocaleUtilTest, GetVariantForLocale) {
const ScopedDefaultLocaleForTesting scoped_default_locale("ja_JP");
const test::ScopedDefaultLocale scoped_default_locale("ja_JP");
EXPECT_EQ("currency=USD", GetVariant("en_Latn_US.UTF-8@currency=USD"));
}
@@ -0,0 +1,21 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "brave/components/l10n/common/default_locale.h"
namespace brave_l10n::test {
ScopedDefaultLocale::ScopedDefaultLocale(const std::string& locale) {
last_locale_ = MutableDefaultLocaleStringForTesting();
MutableDefaultLocaleStringForTesting() = locale;
}
ScopedDefaultLocale::~ScopedDefaultLocale() {
MutableDefaultLocaleStringForTesting() = last_locale_;
}
} // namespace brave_l10n::test
@@ -0,0 +1,33 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef BRAVE_COMPONENTS_L10N_COMMON_TEST_SCOPED_DEFAULT_LOCALE_H_
#define BRAVE_COMPONENTS_L10N_COMMON_TEST_SCOPED_DEFAULT_LOCALE_H_
#include <string>
namespace brave_l10n::test {
// Set the current default locale for testing, restoring the original locale
// upon destruction.
class ScopedDefaultLocale final {
public:
explicit ScopedDefaultLocale(const std::string& locale);
ScopedDefaultLocale(const ScopedDefaultLocale& other) = delete;
ScopedDefaultLocale& operator=(const ScopedDefaultLocale& other) = delete;
ScopedDefaultLocale(ScopedDefaultLocale&& other) noexcept = delete;
ScopedDefaultLocale& operator=(ScopedDefaultLocale&& other) noexcept = delete;
~ScopedDefaultLocale();
private:
std::string last_locale_;
};
} // namespace brave_l10n::test
#endif // BRAVE_COMPONENTS_L10N_COMMON_TEST_SCOPED_DEFAULT_LOCALE_H_
@@ -7,7 +7,7 @@
#include "brave/components/ntp_widget_utils/browser/ntp_widget_utils_region.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/country_codes/country_codes.h"
@@ -112,20 +112,20 @@ TEST_F(NTPWidgetUtilsRegionUtilTest, TestRegionUnAllowedDenyList) {
TEST_F(NTPWidgetUtilsRegionUtilTest, TestFindLocaleInListOne) {
{
// Base test with default english locale
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"en_US"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_US"};
EXPECT_EQ(::ntp_widget_utils::FindLocale({"en", "fr", "ja"}, "en"), "en");
}
{
// Test that FindLocale returns set locale if it's in the provided list
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"ja_JP"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"ja_JP"};
EXPECT_EQ(::ntp_widget_utils::FindLocale({"en", "fr", "ja"}, "en"), "ja");
}
{
// Test that FindLocale returns the provided default locale if it's not in
// the provided list
brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{"ar_DZ"};
brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"ar_DZ"};
EXPECT_EQ(::ntp_widget_utils::FindLocale({"en", "fr", "ja"}, "en"), "en");
}
}
+1 -1
View File
@@ -25,7 +25,7 @@ source_set("unit_tests") {
"//base/test:test_support",
"//brave/components/brave_search_conversion",
"//brave/components/constants",
"//brave/components/l10n/common",
"//brave/components/l10n/common:test_support",
"//brave/components/search_engines",
"//chrome/test:test_support",
"//components/bookmarks/browser",
@@ -11,7 +11,7 @@
#include "brave/components/brave_search_conversion/types.h"
#include "brave/components/brave_search_conversion/utils.h"
#include "brave/components/constants/pref_names.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "brave/components/omnibox/browser/brave_omnibox_prefs.h"
#include "brave/components/omnibox/browser/promotion_provider.h"
#include "brave/components/omnibox/browser/promotion_utils.h"
@@ -48,7 +48,7 @@ class OmniboxPromotionTest : public testing::Test {
false);
scoped_default_locale_ =
std::make_unique<brave_l10n::ScopedDefaultLocaleForTesting>("en_US");
std::make_unique<brave_l10n::test::ScopedDefaultLocale>("en_US");
}
void CreateController(bool incognito) {
@@ -97,8 +97,7 @@ class OmniboxPromotionTest : public testing::Test {
TestSchemeClassifier classifier_;
TestingPrefServiceSimple pref_service_;
std::unique_ptr<AutocompleteController> controller_;
std::unique_ptr<brave_l10n::ScopedDefaultLocaleForTesting>
scoped_default_locale_;
std::unique_ptr<brave_l10n::test::ScopedDefaultLocale> scoped_default_locale_;
};
// Promotion match should not be added for private profile.
+2
View File
@@ -235,6 +235,7 @@ test("brave_unit_tests") {
"//brave/components/ipfs/test:brave_ipfs_unit_tests",
"//brave/components/json:brave_json_unit_tests",
"//brave/components/l10n/common",
"//brave/components/l10n/common:test_support",
"//brave/components/l10n/common:unit_tests",
"//brave/components/ntp_background_images/browser",
"//brave/components/ntp_background_images/buildflags",
@@ -904,6 +905,7 @@ test("brave_browser_tests") {
"//brave/components/debounce/common",
"//brave/components/ipfs/buildflags",
"//brave/components/l10n/common",
"//brave/components/l10n/common:test_support",
"//brave/components/ntp_background_images/buildflags",
"//brave/components/playlist",
"//brave/components/playlist/buildflags",
@@ -8,7 +8,7 @@
#include "base/test/values_test_util.h"
#include "bat/ads/internal/base/unittest/unittest_base.h"
#include "bat/ads/internal/base/unittest/unittest_mock_util.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
// npm run test -- brave_unit_tests --filter=BatAds*
@@ -49,8 +49,7 @@ TEST_F(BatAdsLocaleUserDataTest, GetLocaleForCountryNotInAnonymitySet) {
// Arrange
MockBuildChannel(BuildChannelType::kRelease);
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_MC"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_MC"};
// Act
const base::Value::Dict user_data = GetLocale();
@@ -67,8 +66,7 @@ TEST_F(BatAdsLocaleUserDataTest,
// Arrange
MockBuildChannel(BuildChannelType::kRelease);
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_CX"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_CX"};
// Act
const base::Value::Dict user_data = GetLocale();
@@ -12,7 +12,7 @@
#include "bat/ads/internal/flags/flag_manager_util.h"
#include "bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens_unittest_util.h"
#include "bat/ads/sys_info.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "url/gurl.h"
// npm run test -- brave_unit_tests --filter=BatAds*
@@ -71,8 +71,7 @@ TEST_F(BatAdsCreateConfirmationUrlRequestBuilderTest,
MockBuildChannel(BuildChannelType::kRelease);
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_AS"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_AS"};
const absl::optional<ConfirmationInfo> confirmation = BuildConfirmation();
ASSERT_TRUE(confirmation);
@@ -105,8 +104,7 @@ TEST_F(BatAdsCreateConfirmationUrlRequestBuilderTest,
MockBuildChannel(BuildChannelType::kRelease);
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_KY"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_KY"};
const absl::optional<ConfirmationInfo> confirmation = BuildConfirmation();
ASSERT_TRUE(confirmation);
@@ -170,8 +168,7 @@ TEST_F(BatAdsCreateConfirmationUrlRequestBuilderTest,
MockBuildChannel(BuildChannelType::kNightly);
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_AS"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_AS"};
const absl::optional<ConfirmationInfo> confirmation = BuildConfirmation();
ASSERT_TRUE(confirmation);
@@ -204,8 +201,7 @@ TEST_F(BatAdsCreateConfirmationUrlRequestBuilderTest,
MockBuildChannel(BuildChannelType::kNightly);
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_KY"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_KY"};
const absl::optional<ConfirmationInfo> confirmation = BuildConfirmation();
ASSERT_TRUE(confirmation);
@@ -269,8 +265,7 @@ TEST_F(BatAdsCreateConfirmationUrlRequestBuilderTest,
MockBuildChannel(BuildChannelType::kRelease);
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_AS"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_AS"};
const absl::optional<ConfirmationInfo> confirmation = BuildConfirmation();
ASSERT_TRUE(confirmation);
@@ -303,8 +298,7 @@ TEST_F(BatAdsCreateConfirmationUrlRequestBuilderTest,
MockBuildChannel(BuildChannelType::kRelease);
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_KY"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_KY"};
const absl::optional<ConfirmationInfo> confirmation = BuildConfirmation();
ASSERT_TRUE(confirmation);
@@ -368,8 +362,7 @@ TEST_F(BatAdsCreateConfirmationUrlRequestBuilderTest,
MockBuildChannel(BuildChannelType::kNightly);
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_AS"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_AS"};
const absl::optional<ConfirmationInfo> confirmation = BuildConfirmation();
ASSERT_TRUE(confirmation);
@@ -402,8 +395,7 @@ TEST_F(BatAdsCreateConfirmationUrlRequestBuilderTest,
MockBuildChannel(BuildChannelType::kNightly);
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_KY"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_KY"};
const absl::optional<ConfirmationInfo> confirmation = BuildConfirmation();
ASSERT_TRUE(confirmation);
@@ -12,7 +12,7 @@
#include "bat/ads/internal/base/unittest/unittest_mock_util.h"
#include "bat/ads/internal/geographic/subdivision/subdivision_targeting.h"
#include "bat/ads/pref_names.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "net/http/http_status_code.h"
// npm run test -- brave_unit_tests --filter=BatAds*
@@ -41,7 +41,7 @@ class BatAdsSubdivisionTargetingExclusionRuleTest
UnitTestBase::SetUp();
scoped_default_locale_ =
std::make_unique<brave_l10n::ScopedDefaultLocaleForTesting>(
std::make_unique<brave_l10n::test::ScopedDefaultLocale>(
base::StrCat({"en", "_", GetParam().country}));
subdivision_targeting_ =
@@ -81,8 +81,7 @@ class BatAdsSubdivisionTargetingExclusionRuleTest
return base::StrCat({GetParam().country, "-XX"});
}
std::unique_ptr<brave_l10n::ScopedDefaultLocaleForTesting>
scoped_default_locale_;
std::unique_ptr<brave_l10n::test::ScopedDefaultLocale> scoped_default_locale_;
std::unique_ptr<geographic::SubdivisionTargeting> subdivision_targeting_;
std::unique_ptr<SubdivisionTargetingExclusionRule> exclusion_rule_;
@@ -17,6 +17,7 @@
#include "bat/ads/internal/base/unittest/unittest_mock_util.h"
#include "bat/ads/internal/base/unittest/unittest_time_util.h"
#include "bat/ads/pref_names.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
using ::testing::NiceMock;
@@ -27,7 +28,7 @@ UnitTestBase::UnitTestBase()
ads_client_mock_(std::make_unique<NiceMock<AdsClientMock>>()),
platform_helper_mock_(std::make_unique<NiceMock<PlatformHelperMock>>()),
scoped_default_locale_(
std::make_unique<brave_l10n::ScopedDefaultLocaleForTesting>(
std::make_unique<brave_l10n::test::ScopedDefaultLocale>(
kDefaultLocale)) {
CHECK(temp_dir_.CreateUniqueTempDir());
}
@@ -30,7 +30,6 @@
#include "bat/ads/internal/tabs/tab_manager.h"
#include "bat/ads/internal/user_interaction/idle_detection/idle_detection_manager.h"
#include "bat/ads/internal/user_interaction/user_activity/user_activity_manager.h"
#include "brave/components/l10n/common/locale_util.h"
#include "testing/gmock/include/gmock/gmock.h" // IWYU pragma: keep
#include "testing/gtest/include/gtest/gtest.h" // IWYU pragma: keep
@@ -39,6 +38,10 @@ class Time;
class TimeDelta;
} // namespace base
namespace brave_l10n::test {
class ScopedDefaultLocale;
} // namespace brave_l10n::test
namespace ads {
class Database;
@@ -149,8 +152,7 @@ class UnitTestBase : public testing::Test {
bool setup_called_ = false;
bool teardown_called_ = false;
std::unique_ptr<brave_l10n::ScopedDefaultLocaleForTesting>
scoped_default_locale_;
std::unique_ptr<brave_l10n::test::ScopedDefaultLocale> scoped_default_locale_;
std::unique_ptr<AdsImpl> ads_;
@@ -16,7 +16,7 @@
#include "bat/ads/internal/diagnostics/entries/last_unidle_time_diagnostic_util.h"
#include "bat/ads/pref_names.h"
#include "bat/ads/sys_info.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
// npm run test -- brave_unit_tests --filter=BatAds.*
@@ -34,8 +34,7 @@ TEST_F(BatAdsDiagnosticManagerTest, DiagnosticManager) {
SysInfo().device_id =
"21b4677de1a9b4a197ab671a1481d3fcb24f826a4358a05aafbaee5a9a51b57e";
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_KY"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_KY"};
SetCatalogId("da5dd0e8-71e9-4607-a45b-13e28b607a81");
SetCatalogLastUpdated(Now());
@@ -7,7 +7,7 @@
#include "bat/ads/internal/base/unittest/unittest_base.h"
#include "bat/ads/internal/diagnostics/diagnostic_entry_types.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
// npm run test -- brave_unit_tests --filter=BatAds.*
@@ -17,8 +17,7 @@ class BatAdsLocaleDiagnosticEntryTest : public UnitTestBase {};
TEST_F(BatAdsLocaleDiagnosticEntryTest, GetValue) {
// Arrange
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_KY"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_KY"};
const LocaleDiagnosticEntry diagnostic_entry;
@@ -10,7 +10,7 @@
#include "bat/ads/internal/base/unittest/unittest_base.h"
#include "bat/ads/internal/base/unittest/unittest_mock_util.h"
#include "bat/ads/pref_names.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/l10n/common/test/scoped_default_locale.h"
#include "net/http/http_status_code.h"
// npm run test -- brave_unit_tests --filter=BatAds*
@@ -85,8 +85,7 @@ TEST_F(BatAdsSubdivisionTargetingTest,
TEST_F(BatAdsSubdivisionTargetingTest,
MaybeFetchSubdivisionTargetingNotSupportedLocale) {
// Arrange
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_KY"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_KY"};
// Act
subdivision_targeting_->MaybeFetch();
@@ -100,8 +99,7 @@ TEST_F(BatAdsSubdivisionTargetingTest,
TEST_F(BatAdsSubdivisionTargetingTest,
MaybeAllowSubdivisionTargetingNotSupportedLocale) {
// Arrange
const brave_l10n::ScopedDefaultLocaleForTesting scoped_default_locale{
"en_KY"};
const brave_l10n::test::ScopedDefaultLocale scoped_default_locale{"en_KY"};
// Act
subdivision_targeting_->MaybeAllow();