diff options
Diffstat (limited to '')
-rw-r--r-- | intl/locale/tests/gtest/TestCollation.cpp | 157 | ||||
-rw-r--r-- | intl/locale/tests/gtest/TestDateTimeFormat.cpp | 240 | ||||
-rw-r--r-- | intl/locale/tests/gtest/TestLocaleService.cpp | 154 | ||||
-rw-r--r-- | intl/locale/tests/gtest/TestLocaleServiceNegotiate.cpp | 53 | ||||
-rw-r--r-- | intl/locale/tests/gtest/TestMozLocale.cpp | 123 | ||||
-rw-r--r-- | intl/locale/tests/gtest/TestOSPreferences.cpp | 203 | ||||
-rw-r--r-- | intl/locale/tests/gtest/moz.build | 16 |
7 files changed, 946 insertions, 0 deletions
diff --git a/intl/locale/tests/gtest/TestCollation.cpp b/intl/locale/tests/gtest/TestCollation.cpp new file mode 100644 index 0000000000..4085f6433f --- /dev/null +++ b/intl/locale/tests/gtest/TestCollation.cpp @@ -0,0 +1,157 @@ +/* -*- 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 "nsCollationCID.h" +#include "nsComponentManagerUtils.h" +#include "nsCOMPtr.h" +#include "nsICollation.h" +#include "nsString.h" +#include "nsTArray.h" + +TEST(Collation, AllocateRowSortKey) +{ + nsCOMPtr<nsICollationFactory> colFactory = + do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID); + ASSERT_TRUE(colFactory); + + // Don't throw error even if locale name is invalid + nsCOMPtr<nsICollation> collator; + nsresult rv = colFactory->CreateCollationForLocale("$languageName"_ns, + getter_AddRefs(collator)); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + nsTArray<uint8_t> sortKey1; + // Don't throw error even if locale name is invalid + rv = collator->AllocateRawSortKey(nsICollation::kCollationStrengthDefault, + u"ABC"_ns, sortKey1); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + nsTArray<uint8_t> sortKey2; + // Don't throw error even if locale name is invalid + rv = collator->AllocateRawSortKey(nsICollation::kCollationStrengthDefault, + u"DEF"_ns, sortKey2); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + int32_t result; + rv = collator->CompareRawSortKey(sortKey1, sortKey2, &result); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + ASSERT_TRUE(result < 0); +} + +class CollationComparator final { + public: + explicit CollationComparator(nsICollation* aCollation) + : mCollation(aCollation) {} + + bool Equals(const nsString& a, const nsString& b) const { + int32_t result = 0; + mCollation->CompareString(nsICollation::kCollationStrengthDefault, a, b, + &result); + return result == 0; + } + + bool LessThan(const nsString& a, const nsString& b) const { + int32_t result = 0; + mCollation->CompareString(nsICollation::kCollationStrengthDefault, a, b, + &result); + return result < 0; + } + + private: + nsCOMPtr<nsICollation> mCollation; +}; + +TEST(Collation, CompareString) +{ + nsTArray<nsString> input; + input.AppendElement(u"Argentina"_ns); + input.AppendElement(u"Oerlikon"_ns); + input.AppendElement(u"Offenbach"_ns); + input.AppendElement(u"Sverige"_ns); + input.AppendElement(u"Vaticano"_ns); + input.AppendElement(u"Zimbabwe"_ns); + input.AppendElement(u"la France"_ns); + input.AppendElement(u"\u00a1viva Espa\u00f1a!"_ns); + input.AppendElement(u"\u00d6sterreich"_ns); + input.AppendElement(u"\u4e2d\u56fd"_ns); + input.AppendElement(u"\u65e5\u672c"_ns); + input.AppendElement(u"\ud55c\uad6d"_ns); + + nsCOMPtr<nsICollationFactory> colFactory = + do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID); + ASSERT_TRUE(colFactory); + + // Locale en-US; default options. + nsCOMPtr<nsICollation> collation; + colFactory->CreateCollationForLocale("en-US"_ns, getter_AddRefs(collation)); + ASSERT_TRUE(collation); + + { + CollationComparator comparator(collation); + input.Sort(comparator); + + ASSERT_TRUE(input[0].Equals(u"\u00a1viva Espa\u00f1a!"_ns)); + ASSERT_TRUE(input[1].Equals(u"Argentina"_ns)); + ASSERT_TRUE(input[2].Equals(u"la France"_ns)); + ASSERT_TRUE(input[3].Equals(u"Oerlikon"_ns)); + ASSERT_TRUE(input[4].Equals(u"Offenbach"_ns)); + ASSERT_TRUE(input[5].Equals(u"\u00d6sterreich"_ns)); + ASSERT_TRUE(input[6].Equals(u"Sverige"_ns)); + ASSERT_TRUE(input[7].Equals(u"Vaticano"_ns)); + ASSERT_TRUE(input[8].Equals(u"Zimbabwe"_ns)); + ASSERT_TRUE(input[9].Equals(u"\ud55c\uad6d"_ns)); + ASSERT_TRUE(input[10].Equals(u"\u4e2d\u56fd"_ns)); + ASSERT_TRUE(input[11].Equals(u"\u65e5\u672c"_ns)); + } + + // Locale sv-SE; default options. + // Swedish treats "Ö" as a separate character, which sorts after "Z". + colFactory->CreateCollationForLocale("sv-SE"_ns, getter_AddRefs(collation)); + ASSERT_TRUE(collation); + + { + CollationComparator comparator(collation); + input.Sort(comparator); + + ASSERT_TRUE(input[0].Equals(u"\u00a1viva Espa\u00f1a!"_ns)); + ASSERT_TRUE(input[1].Equals(u"Argentina"_ns)); + ASSERT_TRUE(input[2].Equals(u"la France"_ns)); + ASSERT_TRUE(input[3].Equals(u"Oerlikon"_ns)); + ASSERT_TRUE(input[4].Equals(u"Offenbach"_ns)); + ASSERT_TRUE(input[5].Equals(u"Sverige"_ns)); + ASSERT_TRUE(input[6].Equals(u"Vaticano"_ns)); + ASSERT_TRUE(input[7].Equals(u"Zimbabwe"_ns)); + ASSERT_TRUE(input[8].Equals(u"\u00d6sterreich"_ns)); + ASSERT_TRUE(input[9].Equals(u"\ud55c\uad6d"_ns)); + ASSERT_TRUE(input[10].Equals(u"\u4e2d\u56fd"_ns)); + ASSERT_TRUE(input[11].Equals(u"\u65e5\u672c"_ns)); + } + + // Locale de-DE; default options. + // In German standard sorting, umlauted characters are treated as variants + // of their base characters: ä ≅ a, ö ≅ o, ü ≅ u. + colFactory->CreateCollationForLocale("de-DE"_ns, getter_AddRefs(collation)); + ASSERT_TRUE(collation); + + { + CollationComparator comparator(collation); + input.Sort(comparator); + + ASSERT_TRUE(input[0].Equals(u"\u00a1viva Espa\u00f1a!"_ns)); + ASSERT_TRUE(input[1].Equals(u"Argentina"_ns)); + ASSERT_TRUE(input[2].Equals(u"la France"_ns)); + ASSERT_TRUE(input[3].Equals(u"Oerlikon"_ns)); + ASSERT_TRUE(input[4].Equals(u"Offenbach"_ns)); + ASSERT_TRUE(input[5].Equals(u"\u00d6sterreich"_ns)); + ASSERT_TRUE(input[6].Equals(u"Sverige"_ns)); + ASSERT_TRUE(input[7].Equals(u"Vaticano"_ns)); + ASSERT_TRUE(input[8].Equals(u"Zimbabwe"_ns)); + ASSERT_TRUE(input[9].Equals(u"\ud55c\uad6d"_ns)); + ASSERT_TRUE(input[10].Equals(u"\u4e2d\u56fd"_ns)); + ASSERT_TRUE(input[11].Equals(u"\u65e5\u672c"_ns)); + } +} diff --git a/intl/locale/tests/gtest/TestDateTimeFormat.cpp b/intl/locale/tests/gtest/TestDateTimeFormat.cpp new file mode 100644 index 0000000000..e2e56014cf --- /dev/null +++ b/intl/locale/tests/gtest/TestDateTimeFormat.cpp @@ -0,0 +1,240 @@ +#include "gtest/gtest.h" +#include "DateTimeFormat.h" + +namespace mozilla { + +TEST(DateTimeFormat, FormatPRExplodedTime) +{ + PRTime prTime = 0; + PRExplodedTime prExplodedTime; + PR_ExplodeTime(prTime, PR_GMTParameters, &prExplodedTime); + + mozilla::DateTimeFormat::mLocale = new nsCString("en-US"); + mozilla::DateTimeFormat::DeleteCache(); + + nsAutoString formattedTime; + nsresult rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("January") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("12:00:00 AM") != kNotFound || + formattedTime.Find("00:00:00") != kNotFound); + + prExplodedTime = {0, 0, 19, 0, 1, 0, 1970, 4, 0, {(19 * 60), 0}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("January") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("12:19:00 AM") != kNotFound || + formattedTime.Find("00:19:00") != kNotFound); + + prExplodedTime = {0, 0, 0, 7, 1, + 0, 1970, 4, 0, {(6 * 60 * 60), (1 * 60 * 60)}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("January") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("7:00:00 AM") != kNotFound || + formattedTime.Find("07:00:00") != kNotFound); + + prExplodedTime = { + 0, 0, 29, 11, 1, + 0, 1970, 4, 0, {(10 * 60 * 60) + (29 * 60), (1 * 60 * 60)}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("January") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("11:29:00 AM") != kNotFound || + formattedTime.Find("11:29:00") != kNotFound); + + prExplodedTime = {0, 0, 37, 23, 31, 11, 1969, 3, 364, {-(23 * 60), 0}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("December") != kNotFound); + ASSERT_TRUE(formattedTime.Find("31") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("11:37:00 PM") != kNotFound || + formattedTime.Find("23:37:00") != kNotFound); + + prExplodedTime = {0, 0, 0, 17, 31, 11, 1969, 3, 364, {-(7 * 60 * 60), 0}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("December") != kNotFound); + ASSERT_TRUE(formattedTime.Find("31") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("5:00:00 PM") != kNotFound || + formattedTime.Find("17:00:00") != kNotFound); + + prExplodedTime = { + 0, 0, 47, 14, 31, + 11, 1969, 3, 364, {-((10 * 60 * 60) + (13 * 60)), (1 * 60 * 60)}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("December") != kNotFound); + ASSERT_TRUE(formattedTime.Find("31") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("2:47:00 PM") != kNotFound || + formattedTime.Find("14:47:00") != kNotFound); +} + +TEST(DateTimeFormat, DateFormatSelectors) +{ + PRTime prTime = 0; + PRExplodedTime prExplodedTime; + PR_ExplodeTime(prTime, PR_GMTParameters, &prExplodedTime); + + mozilla::DateTimeFormat::mLocale = new nsCString("en-US"); + mozilla::DateTimeFormat::DeleteCache(); + + nsAutoString formattedTime; + nsresult rv = mozilla::DateTimeFormat::FormatDateTime( + &prExplodedTime, DateTimeFormat::Skeleton::yyyyMM, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_STREQ("01/1970", NS_ConvertUTF16toUTF8(formattedTime).get()); + + rv = mozilla::DateTimeFormat::FormatDateTime( + &prExplodedTime, DateTimeFormat::Skeleton::yyyyMMMM, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_STREQ("January 1970", NS_ConvertUTF16toUTF8(formattedTime).get()); + + rv = mozilla::DateTimeFormat::GetCalendarSymbol( + mozilla::DateTimeFormat::Field::Month, + mozilla::DateTimeFormat::Style::Wide, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_STREQ("January", NS_ConvertUTF16toUTF8(formattedTime).get()); + + rv = mozilla::DateTimeFormat::GetCalendarSymbol( + mozilla::DateTimeFormat::Field::Weekday, + mozilla::DateTimeFormat::Style::Abbreviated, &prExplodedTime, + formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_STREQ("Thu", NS_ConvertUTF16toUTF8(formattedTime).get()); +} + +TEST(DateTimeFormat, FormatPRExplodedTimeForeign) +{ + PRTime prTime = 0; + PRExplodedTime prExplodedTime; + PR_ExplodeTime(prTime, PR_GMTParameters, &prExplodedTime); + + mozilla::DateTimeFormat::mLocale = new nsCString("de-DE"); + mozilla::DateTimeFormat::DeleteCache(); + + nsAutoString formattedTime; + nsresult rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("1.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Januar") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("12:00:00 AM") != kNotFound || + formattedTime.Find("00:00:00") != kNotFound); + + prExplodedTime = {0, 0, 19, 0, 1, 0, 1970, 4, 0, {(19 * 60), 0}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("1.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Januar") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("12:19:00 AM") != kNotFound || + formattedTime.Find("00:19:00") != kNotFound); + + prExplodedTime = {0, 0, 0, 7, 1, + 0, 1970, 4, 0, {(6 * 60 * 60), (1 * 60 * 60)}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("1.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Januar") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("7:00:00 AM") != kNotFound || + formattedTime.Find("07:00:00") != kNotFound); + + prExplodedTime = { + 0, 0, 29, 11, 1, + 0, 1970, 4, 0, {(10 * 60 * 60) + (29 * 60), (1 * 60 * 60)}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("1.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Januar") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1970") != kNotFound); + ASSERT_TRUE(formattedTime.Find("11:29:00 AM") != kNotFound || + formattedTime.Find("11:29:00") != kNotFound); + + prExplodedTime = {0, 0, 37, 23, 31, 11, 1969, 3, 364, {-(23 * 60), 0}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("31.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Dezember") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("11:37:00 PM") != kNotFound || + formattedTime.Find("23:37:00") != kNotFound); + + prExplodedTime = {0, 0, 0, 17, 31, 11, 1969, 3, 364, {-(7 * 60 * 60), 0}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("31.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Dezember") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("5:00:00 PM") != kNotFound || + formattedTime.Find("17:00:00") != kNotFound); + + prExplodedTime = { + 0, 0, 47, 14, 31, + 11, 1969, 3, 364, {-((10 * 60 * 60) + (13 * 60)), (1 * 60 * 60)}}; + rv = mozilla::DateTimeFormat::FormatPRExplodedTime( + kDateFormatLong, kTimeFormatLong, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(formattedTime.Find("31.") != kNotFound); + ASSERT_TRUE(formattedTime.Find("Dezember") != kNotFound); + ASSERT_TRUE(formattedTime.Find("1969") != kNotFound); + ASSERT_TRUE(formattedTime.Find("2:47:00 PM") != kNotFound || + formattedTime.Find("14:47:00") != kNotFound); +} + +TEST(DateTimeFormat, DateFormatSelectorsForeign) +{ + PRTime prTime = 0; + PRExplodedTime prExplodedTime; + PR_ExplodeTime(prTime, PR_GMTParameters, &prExplodedTime); + + mozilla::DateTimeFormat::mLocale = new nsCString("de-DE"); + mozilla::DateTimeFormat::DeleteCache(); + + nsAutoString formattedTime; + nsresult rv = mozilla::DateTimeFormat::FormatDateTime( + &prExplodedTime, DateTimeFormat::Skeleton::yyyyMM, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_STREQ("01.1970", NS_ConvertUTF16toUTF8(formattedTime).get()); + + rv = mozilla::DateTimeFormat::FormatDateTime( + &prExplodedTime, DateTimeFormat::Skeleton::yyyyMMMM, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_STREQ("Januar 1970", NS_ConvertUTF16toUTF8(formattedTime).get()); + + rv = mozilla::DateTimeFormat::GetCalendarSymbol( + mozilla::DateTimeFormat::Field::Month, + mozilla::DateTimeFormat::Style::Wide, &prExplodedTime, formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_STREQ("Januar", NS_ConvertUTF16toUTF8(formattedTime).get()); + + rv = mozilla::DateTimeFormat::GetCalendarSymbol( + mozilla::DateTimeFormat::Field::Weekday, + mozilla::DateTimeFormat::Style::Abbreviated, &prExplodedTime, + formattedTime); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_STREQ("Do", NS_ConvertUTF16toUTF8(formattedTime).get()); +} + +} // namespace mozilla diff --git a/intl/locale/tests/gtest/TestLocaleService.cpp b/intl/locale/tests/gtest/TestLocaleService.cpp new file mode 100644 index 0000000000..32bdff61d6 --- /dev/null +++ b/intl/locale/tests/gtest/TestLocaleService.cpp @@ -0,0 +1,154 @@ +/* -*- 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/LocaleService.h" +#include "mozilla/intl/MozLocale.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()); + ASSERT_TRUE(Locale(locStr).IsWellFormed()); +} + +TEST(Intl_Locale_LocaleService, IsAppLocaleRTL) +{ + mozilla::Preferences::SetCString("intl.l10n.pseudo", "bidi"); + ASSERT_TRUE(LocaleService::GetInstance()->IsAppLocaleRTL()); + mozilla::Preferences::ClearUser("intl.l10n.pseudo"); + + mozilla::Preferences::SetInt("intl.uidirection", 0); + ASSERT_FALSE(LocaleService::GetInstance()->IsAppLocaleRTL()); + mozilla::Preferences::SetInt("intl.uidirection", 1); + ASSERT_TRUE(LocaleService::GetInstance()->IsAppLocaleRTL()); + mozilla::Preferences::SetInt("intl.uidirection", -1); +} diff --git a/intl/locale/tests/gtest/TestLocaleServiceNegotiate.cpp b/intl/locale/tests/gtest/TestLocaleServiceNegotiate.cpp new file mode 100644 index 0000000000..c428e81c8d --- /dev/null +++ b/intl/locale/tests/gtest/TestLocaleServiceNegotiate.cpp @@ -0,0 +1,53 @@ +/* -*- 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/intl/LocaleService.h" + +using namespace mozilla::intl; + +TEST(Intl_Locale_LocaleService, Negotiate) +{ + nsTArray<nsCString> requestedLocales; + nsTArray<nsCString> availableLocales; + nsTArray<nsCString> supportedLocales; + nsAutoCString defaultLocale("en-US"); + int32_t strategy = LocaleService::kLangNegStrategyFiltering; + + requestedLocales.AppendElement("sr"_ns); + + availableLocales.AppendElement("sr-Cyrl"_ns); + availableLocales.AppendElement("sr-Latn"_ns); + + LocaleService::GetInstance()->NegotiateLanguages( + requestedLocales, availableLocales, defaultLocale, strategy, + supportedLocales); + + ASSERT_TRUE(supportedLocales.Length() == 2); + ASSERT_TRUE(supportedLocales[0].EqualsLiteral("sr-Cyrl")); + ASSERT_TRUE(supportedLocales[1].EqualsLiteral("en-US")); +} + +TEST(Intl_Locale_LocaleService, UseLSDefaultLocale) +{ + nsTArray<nsCString> requestedLocales; + nsTArray<nsCString> availableLocales; + nsTArray<nsCString> supportedLocales; + nsAutoCString defaultLocale("en-US"); + int32_t strategy = LocaleService::kLangNegStrategyLookup; + + requestedLocales.AppendElement("sr"_ns); + + availableLocales.AppendElement("de"_ns); + + LocaleService::GetInstance()->NegotiateLanguages( + requestedLocales, availableLocales, defaultLocale, strategy, + supportedLocales); + + nsAutoCString lsDefaultLocale; + LocaleService::GetInstance()->GetDefaultLocale(lsDefaultLocale); + ASSERT_TRUE(supportedLocales.Length() == 1); + ASSERT_TRUE(supportedLocales[0].Equals(lsDefaultLocale)); +} diff --git a/intl/locale/tests/gtest/TestMozLocale.cpp b/intl/locale/tests/gtest/TestMozLocale.cpp new file mode 100644 index 0000000000..c56bc14eb3 --- /dev/null +++ b/intl/locale/tests/gtest/TestMozLocale.cpp @@ -0,0 +1,123 @@ +/* -*- 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/intl/MozLocale.h" + +using namespace mozilla::intl; + +TEST(Intl_Locale_Locale, Locale) +{ + Locale loc = Locale("en-US"); + + ASSERT_TRUE(loc.GetLanguage().Equals("en")); + ASSERT_TRUE(loc.GetRegion().Equals("US")); +} + +TEST(Intl_Locale_Locale, AsString) +{ + Locale loc = Locale("ja-jp-windows"); + + ASSERT_TRUE(loc.AsString().Equals("ja-JP-windows")); +} + +TEST(Intl_Locale_Locale, GetSubTags) +{ + Locale loc = Locale("en-latn-us-macos"); + + ASSERT_TRUE(loc.GetLanguage().Equals("en")); + ASSERT_TRUE(loc.GetScript().Equals("Latn")); + ASSERT_TRUE(loc.GetRegion().Equals("US")); + + nsTArray<nsCString> variants; + loc.GetVariants(variants); + ASSERT_TRUE(variants.Length() == 1); + ASSERT_TRUE(variants[0].Equals("macos")); +} + +TEST(Intl_Locale_Locale, Matches) +{ + Locale loc = Locale("en-US"); + + Locale loc2 = Locale("en-GB"); + ASSERT_FALSE(loc == loc2); + + Locale loc3 = Locale("en-US"); + ASSERT_TRUE(loc == loc3); + + Locale loc4 = Locale("En_us"); + ASSERT_TRUE(loc == loc4); +} + +TEST(Intl_Locale_Locale, MatchesRange) +{ + Locale loc = Locale("en-US"); + + Locale loc2 = Locale("en-Latn-US"); + ASSERT_FALSE(loc == loc2); + ASSERT_TRUE(loc.Matches(loc2, true, false)); + ASSERT_FALSE(loc.Matches(loc2, false, true)); + ASSERT_FALSE(loc.Matches(loc2, false, false)); + ASSERT_TRUE(loc.Matches(loc2, true, true)); + + Locale loc3 = Locale("en"); + ASSERT_FALSE(loc == loc3); + ASSERT_TRUE(loc.Matches(loc3, false, true)); + ASSERT_FALSE(loc.Matches(loc3, true, false)); + ASSERT_FALSE(loc.Matches(loc3, false, false)); + ASSERT_TRUE(loc.Matches(loc3, true, true)); +} + +TEST(Intl_Locale_Locale, Variants) +{ + Locale loc = Locale("en-US-UniFon-BasicEng"); + + // Make sure that we canonicalize and sort variant tags + ASSERT_TRUE(loc.AsString().Equals("en-US-basiceng-unifon")); +} + +TEST(Intl_Locale_Locale, InvalidLocale) +{ + Locale loc = Locale("en-verylongsubtag"); + ASSERT_FALSE(loc.IsWellFormed()); + + Locale loc2 = Locale("p-te"); + ASSERT_FALSE(loc2.IsWellFormed()); +} + +TEST(Intl_Locale_Locale, ClearRegion) +{ + Locale loc = Locale("en-US"); + loc.ClearRegion(); + ASSERT_TRUE(loc.AsString().Equals("en")); +} + +TEST(Intl_Locale_Locale, ClearVariants) +{ + Locale loc = Locale("en-US-windows"); + loc.ClearVariants(); + ASSERT_TRUE(loc.AsString().Equals("en-US")); +} + +TEST(Intl_Locale_Locale, jaJPmac) +{ + Locale loc = Locale("ja-JP-mac"); + ASSERT_TRUE(loc.AsString().Equals("ja-JP-macos")); +} + +TEST(Intl_Locale_Locale, Maximize) +{ + Locale loc = Locale("en"); + + ASSERT_TRUE(loc.GetLanguage().Equals("en")); + ASSERT_TRUE(loc.GetScript().IsEmpty()); + ASSERT_TRUE(loc.GetRegion().IsEmpty()); + + ASSERT_TRUE(loc.Maximize()); + + ASSERT_TRUE(loc.GetLanguage().Equals("en")); + ASSERT_TRUE(loc.GetScript().Equals("Latn")); + ASSERT_TRUE(loc.GetRegion().Equals("US")); +} diff --git a/intl/locale/tests/gtest/TestOSPreferences.cpp b/intl/locale/tests/gtest/TestOSPreferences.cpp new file mode 100644 index 0000000000..605b301c09 --- /dev/null +++ b/intl/locale/tests/gtest/TestOSPreferences.cpp @@ -0,0 +1,203 @@ +/* -*- 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/ArrayUtils.h" +#include "mozilla/Preferences.h" +#include "mozilla/intl/OSPreferences.h" + +using namespace mozilla::intl; + +/** + * We test that on all platforms we test against (irrelevant of the tier), + * we will be able to retrieve at least a single locale out of the system. + * + * In theory, that may not be true, but if we encounter such platform we should + * decide how to handle this and special case and this test should make + * it not happen without us noticing. + */ +TEST(Intl_Locale_OSPreferences, GetSystemLocales) +{ + nsTArray<nsCString> systemLocales; + ASSERT_TRUE(NS_SUCCEEDED( + OSPreferences::GetInstance()->GetSystemLocales(systemLocales))); + + ASSERT_FALSE(systemLocales.IsEmpty()); +} + +/** + * We test that on all platforms we test against (irrelevant of the tier), + * we will be able to retrieve at least a single locale out of the system. + * + * In theory, that may not be true, but if we encounter such platform we should + * decide how to handle this and special case and this test should make + * it not happen without us noticing. + */ +TEST(Intl_Locale_OSPreferences, GetRegionalPrefsLocales) +{ + nsTArray<nsCString> rgLocales; + ASSERT_TRUE(NS_SUCCEEDED( + OSPreferences::GetInstance()->GetRegionalPrefsLocales(rgLocales))); + + ASSERT_FALSE(rgLocales.IsEmpty()); +} + +/** + * We test that on all platforms we test against, + * we will be able to retrieve a date and time pattern. + * + * This may come back empty on platforms where we don't have platforms + * bindings for, so effectively, we're testing for crashes. We should + * never crash. + */ +TEST(Intl_Locale_OSPreferences, GetDateTimePattern) +{ + nsAutoCString pattern; + OSPreferences* osprefs = OSPreferences::GetInstance(); + + struct Test { + int dateStyle; + int timeStyle; + const char* locale; + }; + Test tests[] = {{0, 0, ""}, {1, 0, "pl"}, {2, 0, "de-DE"}, {3, 0, "fr"}, + {4, 0, "ar"}, + + {0, 1, ""}, {0, 2, "it"}, {0, 3, ""}, {0, 4, "ru"}, + + {4, 1, ""}, {3, 2, "cs"}, {2, 3, ""}, {1, 4, "ja"}}; + + for (unsigned i = 0; i < mozilla::ArrayLength(tests); i++) { + const Test& t = tests[i]; + if (NS_SUCCEEDED(osprefs->GetDateTimePattern( + t.dateStyle, t.timeStyle, nsDependentCString(t.locale), pattern))) { + ASSERT_TRUE((t.dateStyle == 0 && t.timeStyle == 0) || !pattern.IsEmpty()); + } + } + + // If the locale is not specified, we should get the pattern corresponding to + // the first regional prefs locale. + AutoTArray<nsCString, 10> rpLocales; + LocaleService::GetInstance()->GetRegionalPrefsLocales(rpLocales); + ASSERT_TRUE(rpLocales.Length() > 0); + + nsAutoCString rpLocalePattern; + ASSERT_TRUE(NS_SUCCEEDED( + osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleLong, + mozIOSPreferences::dateTimeFormatStyleLong, + rpLocales[0], rpLocalePattern))); + ASSERT_TRUE(NS_SUCCEEDED( + osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleLong, + mozIOSPreferences::dateTimeFormatStyleLong, + nsDependentCString(""), pattern))); + ASSERT_EQ(rpLocalePattern, pattern); +} + +/** + * Test that is possible to override the OS defaults through a pref. + */ +TEST(Intl_Locale_OSPreferences, GetDateTimePatternPrefOverrides) +{ + nsresult nr; + nsAutoCString default_pattern, pattern; + OSPreferences* osprefs = OSPreferences::GetInstance(); + + struct { + const char* DatePref; + const char* TimePref; + int32_t DateTimeFormatStyle; + } configs[] = {{"intl.date_time.pattern_override.date_short", + "intl.date_time.pattern_override.time_short", + mozIOSPreferences::dateTimeFormatStyleShort}, + {"intl.date_time.pattern_override.date_medium", + "intl.date_time.pattern_override.time_medium", + mozIOSPreferences::dateTimeFormatStyleMedium}, + {"intl.date_time.pattern_override.date_long", + "intl.date_time.pattern_override.time_long", + mozIOSPreferences::dateTimeFormatStyleLong}, + {"intl.date_time.pattern_override.date_full", + "intl.date_time.pattern_override.time_full", + mozIOSPreferences::dateTimeFormatStyleFull}}; + + for (const auto& config : configs) { + // Get default value for the OS + nr = osprefs->GetDateTimePattern(config.DateTimeFormatStyle, + mozIOSPreferences::dateTimeFormatStyleNone, + nsDependentCString(""), default_pattern); + ASSERT_TRUE(NS_SUCCEEDED(nr)); + + // Override date format + mozilla::Preferences::SetCString(config.DatePref, "yy-MM"); + nr = osprefs->GetDateTimePattern(config.DateTimeFormatStyle, + mozIOSPreferences::dateTimeFormatStyleNone, + nsDependentCString(""), pattern); + ASSERT_TRUE(NS_SUCCEEDED(nr)); + ASSERT_TRUE(pattern.EqualsASCII("yy-MM")); + + // Override time format + mozilla::Preferences::SetCString(config.TimePref, "HH:mm"); + nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleNone, + config.DateTimeFormatStyle, + nsDependentCString(""), pattern); + ASSERT_TRUE(NS_SUCCEEDED(nr)); + ASSERT_TRUE(pattern.EqualsASCII("HH:mm")); + + // Override both + nr = osprefs->GetDateTimePattern(config.DateTimeFormatStyle, + config.DateTimeFormatStyle, + nsDependentCString(""), pattern); + ASSERT_TRUE(NS_SUCCEEDED(nr)); + ASSERT_TRUE(pattern.Find("yy-MM") != kNotFound); + ASSERT_TRUE(pattern.Find("HH:mm") != kNotFound); + + // Clear overrides, we should get the default value back. + mozilla::Preferences::ClearUser(config.DatePref); + mozilla::Preferences::ClearUser(config.TimePref); + nr = osprefs->GetDateTimePattern(config.DateTimeFormatStyle, + mozIOSPreferences::dateTimeFormatStyleNone, + nsDependentCString(""), pattern); + ASSERT_TRUE(NS_SUCCEEDED(nr)); + ASSERT_EQ(default_pattern, pattern); + } + + // Test overriding connector + nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort, + mozIOSPreferences::dateTimeFormatStyleShort, + nsDependentCString(""), default_pattern); + + mozilla::Preferences::SetCString("intl.date_time.pattern_override.date_short", + "yyyy-MM-dd"); + mozilla::Preferences::SetCString("intl.date_time.pattern_override.time_short", + "HH:mm:ss"); + mozilla::Preferences::SetCString( + "intl.date_time.pattern_override.date_time_short", "{1} {0}"); + nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort, + mozIOSPreferences::dateTimeFormatStyleShort, + nsDependentCString(""), pattern); + ASSERT_TRUE(NS_SUCCEEDED(nr)); + ASSERT_TRUE(pattern.EqualsASCII("yyyy-MM-dd HH:mm:ss")); + + // Reset to date and time to defaults + mozilla::Preferences::ClearUser("intl.date_time.pattern_override.date_short"); + mozilla::Preferences::ClearUser("intl.date_time.pattern_override.time_short"); + + // Invalid patterns are ignored + mozilla::Preferences::SetCString( + "intl.date_time.pattern_override.date_time_short", "hello, world!"); + nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort, + mozIOSPreferences::dateTimeFormatStyleShort, + nsDependentCString(""), pattern); + ASSERT_TRUE(NS_SUCCEEDED(nr)); + ASSERT_EQ(default_pattern, pattern); + + // Clearing the override results in getting the default pattern back. + mozilla::Preferences::ClearUser( + "intl.date_time.pattern_override.date_time_short"); + nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort, + mozIOSPreferences::dateTimeFormatStyleShort, + nsDependentCString(""), pattern); + ASSERT_TRUE(NS_SUCCEEDED(nr)); + ASSERT_EQ(default_pattern, pattern); +} diff --git a/intl/locale/tests/gtest/moz.build b/intl/locale/tests/gtest/moz.build new file mode 100644 index 0000000000..e23c828f26 --- /dev/null +++ b/intl/locale/tests/gtest/moz.build @@ -0,0 +1,16 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +UNIFIED_SOURCES += [ + "TestCollation.cpp", + "TestDateTimeFormat.cpp", + "TestLocaleService.cpp", + "TestLocaleServiceNegotiate.cpp", + "TestMozLocale.cpp", + "TestOSPreferences.cpp", +] + +FINAL_LIBRARY = "xul-gtest" |