summaryrefslogtreecommitdiffstats
path: root/intl/locale/tests/gtest/TestLocaleService.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'intl/locale/tests/gtest/TestLocaleService.cpp')
-rw-r--r--intl/locale/tests/gtest/TestLocaleService.cpp173
1 files changed, 173 insertions, 0 deletions
diff --git a/intl/locale/tests/gtest/TestLocaleService.cpp b/intl/locale/tests/gtest/TestLocaleService.cpp
new file mode 100644
index 0000000000..2cf19727d6
--- /dev/null
+++ b/intl/locale/tests/gtest/TestLocaleService.cpp
@@ -0,0 +1,173 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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 "gtest/gtest.h"
+#include "mozilla/Preferences.h"
+#include "mozilla/intl/Locale.h"
+#include "mozilla/intl/LocaleService.h"
+#include "mozilla/intl/Collator.h"
+
+using namespace mozilla::intl;
+
+TEST(Intl_Locale_LocaleService, CanonicalizeLanguageId)
+{
+ nsCString locale("en-US.POSIX");
+ ASSERT_TRUE(LocaleService::CanonicalizeLanguageId(locale));
+ ASSERT_TRUE(locale.EqualsLiteral("en-US"));
+
+ locale.AssignLiteral("en-US_POSIX");
+ ASSERT_TRUE(LocaleService::CanonicalizeLanguageId(locale));
+ ASSERT_TRUE(locale.EqualsLiteral("en-US-posix"));
+
+ locale.AssignLiteral("en-US-POSIX");
+ ASSERT_TRUE(LocaleService::CanonicalizeLanguageId(locale));
+ ASSERT_TRUE(locale.EqualsLiteral("en-US-posix"));
+
+ locale.AssignLiteral("C");
+ ASSERT_FALSE(LocaleService::CanonicalizeLanguageId(locale));
+ ASSERT_TRUE(locale.EqualsLiteral("und"));
+
+ locale.AssignLiteral("");
+ ASSERT_FALSE(LocaleService::CanonicalizeLanguageId(locale));
+ ASSERT_TRUE(locale.EqualsLiteral("und"));
+}
+
+TEST(Intl_Locale_LocaleService, GetAppLocalesAsBCP47)
+{
+ nsTArray<nsCString> appLocales;
+ LocaleService::GetInstance()->GetAppLocalesAsBCP47(appLocales);
+
+ ASSERT_FALSE(appLocales.IsEmpty());
+}
+
+TEST(Intl_Locale_LocaleService, GetAppLocalesAsLangTags)
+{
+ nsTArray<nsCString> appLocales;
+ LocaleService::GetInstance()->GetAppLocalesAsLangTags(appLocales);
+
+ ASSERT_FALSE(appLocales.IsEmpty());
+}
+
+TEST(Intl_Locale_LocaleService, GetAppLocalesAsLangTags_lastIsPresent)
+{
+ nsAutoCString lastFallbackLocale;
+ LocaleService::GetInstance()->GetLastFallbackLocale(lastFallbackLocale);
+
+ nsTArray<nsCString> appLocales;
+ LocaleService::GetInstance()->GetAppLocalesAsLangTags(appLocales);
+
+ ASSERT_TRUE(appLocales.Contains(lastFallbackLocale));
+}
+
+TEST(Intl_Locale_LocaleService, GetAppLocaleAsLangTag)
+{
+ nsTArray<nsCString> appLocales;
+ LocaleService::GetInstance()->GetAppLocalesAsLangTags(appLocales);
+
+ nsAutoCString locale;
+ LocaleService::GetInstance()->GetAppLocaleAsLangTag(locale);
+
+ ASSERT_TRUE(appLocales[0] == locale);
+}
+
+TEST(Intl_Locale_LocaleService, GetRegionalPrefsLocales)
+{
+ nsTArray<nsCString> rpLocales;
+ LocaleService::GetInstance()->GetRegionalPrefsLocales(rpLocales);
+
+ int32_t len = rpLocales.Length();
+ ASSERT_TRUE(len > 0);
+}
+
+TEST(Intl_Locale_LocaleService, GetWebExposedLocales)
+{
+ const nsTArray<nsCString> spoofLocale{"de"_ns};
+ LocaleService::GetInstance()->SetAvailableLocales(spoofLocale);
+ LocaleService::GetInstance()->SetRequestedLocales(spoofLocale);
+
+ nsTArray<nsCString> pvLocales;
+
+ mozilla::Preferences::SetInt("privacy.spoof_english", 0);
+ LocaleService::GetInstance()->GetWebExposedLocales(pvLocales);
+ ASSERT_TRUE(pvLocales.Length() > 0);
+ ASSERT_TRUE(pvLocales[0].Equals("de"_ns));
+
+ mozilla::Preferences::SetCString("intl.locale.privacy.web_exposed", "zh-TW");
+ LocaleService::GetInstance()->GetWebExposedLocales(pvLocales);
+ ASSERT_TRUE(pvLocales.Length() > 0);
+ ASSERT_TRUE(pvLocales[0].Equals("zh-TW"_ns));
+
+ mozilla::Preferences::SetInt("privacy.spoof_english", 2);
+ LocaleService::GetInstance()->GetWebExposedLocales(pvLocales);
+ ASSERT_EQ(1u, pvLocales.Length());
+ ASSERT_TRUE(pvLocales[0].Equals("en-US"_ns));
+}
+
+TEST(Intl_Locale_LocaleService, GetRequestedLocales)
+{
+ nsTArray<nsCString> reqLocales;
+ LocaleService::GetInstance()->GetRequestedLocales(reqLocales);
+
+ int32_t len = reqLocales.Length();
+ ASSERT_TRUE(len > 0);
+}
+
+TEST(Intl_Locale_LocaleService, GetAvailableLocales)
+{
+ nsTArray<nsCString> availableLocales;
+ LocaleService::GetInstance()->GetAvailableLocales(availableLocales);
+
+ int32_t len = availableLocales.Length();
+ ASSERT_TRUE(len > 0);
+}
+
+TEST(Intl_Locale_LocaleService, GetPackagedLocales)
+{
+ nsTArray<nsCString> packagedLocales;
+ LocaleService::GetInstance()->GetPackagedLocales(packagedLocales);
+
+ int32_t len = packagedLocales.Length();
+ ASSERT_TRUE(len > 0);
+}
+
+TEST(Intl_Locale_LocaleService, GetDefaultLocale)
+{
+ nsAutoCString locStr;
+ LocaleService::GetInstance()->GetDefaultLocale(locStr);
+
+ ASSERT_FALSE(locStr.IsEmpty());
+ Locale loc;
+ ASSERT_TRUE(LocaleParser::TryParse(locStr, loc).isOk());
+}
+
+TEST(Intl_Locale_LocaleService, IsAppLocaleRTL)
+{
+ mozilla::Preferences::SetCString("intl.l10n.pseudo", "bidi");
+ ASSERT_TRUE(LocaleService::GetInstance()->IsAppLocaleRTL());
+ mozilla::Preferences::ClearUser("intl.l10n.pseudo");
+}
+
+TEST(Intl_Locale_LocaleService, TryCreateComponent)
+{
+ {
+ // Create a Collator with the app locale.
+ auto result = LocaleService::GetInstance()->TryCreateComponent<Collator>();
+ ASSERT_TRUE(result.isOk());
+ }
+ {
+ // Create a Collator with the "en" locale.
+ auto result =
+ LocaleService::GetInstance()->TryCreateComponentWithLocale<Collator>(
+ "en");
+ ASSERT_TRUE(result.isOk());
+ }
+ {
+ // Fallback to the app locale when an invalid one is used.
+ auto result =
+ LocaleService::GetInstance()->TryCreateComponentWithLocale<Collator>(
+ "$invalidName");
+ ASSERT_TRUE(result.isOk());
+ }
+}