diff --git a/intl/locale/tests/gtest/TestCollation.cpp b/intl/locale/tests/gtest/TestCollation.cpp index 95f7df5d40572..4085f6433f512 100644 --- a/intl/locale/tests/gtest/TestCollation.cpp +++ b/intl/locale/tests/gtest/TestCollation.cpp @@ -41,3 +41,117 @@ TEST(Collation, AllocateRowSortKey) 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 mCollation; +}; + +TEST(Collation, CompareString) +{ + nsTArray 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 colFactory = + do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID); + ASSERT_TRUE(colFactory); + + // Locale en-US; default options. + nsCOMPtr 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/unit/test_collation.js b/intl/locale/tests/unit/test_collation.js deleted file mode 100644 index 1eb3fd2dfe0dd..0000000000000 --- a/intl/locale/tests/unit/test_collation.js +++ /dev/null @@ -1,78 +0,0 @@ -ChromeUtils.import("resource://gre/modules/Services.jsm"); - -function run_test() { - var input = [ - "Argentina", - "Oerlikon", - "Offenbach", - "Sverige", - "Vaticano", - "Zimbabwe", - "la France", - "¡viva España!", - "Österreich", - "中国", - "日本", - "한국", - ]; - - function test(locale, expected) { - var collator = Cc["@mozilla.org/intl/collation-factory;1"] - .createInstance(Ci.nsICollationFactory) - .CreateCollationForLocale(locale); - var strength = Ci.nsICollation.kCollationStrengthDefault; - var actual = input.sort((x, y) => collator.compareString(strength, x, y)); - deepEqual(actual, expected, locale); - } - - // Locale en-US; default options. - test("en-US", [ - "¡viva España!", - "Argentina", - "la France", - "Oerlikon", - "Offenbach", - "Österreich", - "Sverige", - "Vaticano", - "Zimbabwe", - "한국", - "中国", - "日本", - ]); - - // Locale sv-SE; default options. - // Swedish treats "Ö" as a separate character, which sorts after "Z". - test("sv-SE", [ - "¡viva España!", - "Argentina", - "la France", - "Oerlikon", - "Offenbach", - "Sverige", - "Vaticano", - "Zimbabwe", - "Österreich", - "한국", - "中国", - "日本", - ]); - - // Locale de-DE; default options. - // In German standard sorting, umlauted characters are treated as variants - // of their base characters: ä ≅ a, ö ≅ o, ü ≅ u. - test("de-DE", [ - "¡viva España!", - "Argentina", - "la France", - "Oerlikon", - "Offenbach", - "Österreich", - "Sverige", - "Vaticano", - "Zimbabwe", - "한국", - "中国", - "日本", - ]); -} diff --git a/intl/locale/tests/unit/xpcshell.ini b/intl/locale/tests/unit/xpcshell.ini index f17a481b3e372..75ab01343430c 100644 --- a/intl/locale/tests/unit/xpcshell.ini +++ b/intl/locale/tests/unit/xpcshell.ini @@ -7,9 +7,6 @@ support-files = [test_bug22310.js] skip-if = toolkit != "windows" && toolkit != "cocoa" -[test_collation.js] -skip-if = toolkit == "android" # ICU might be turned off - [test_bug1086527.js] [test_intl_on_workers.js] skip-if = toolkit == "android" # bug 1309447