diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /toolkit/components/mozintl/test | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/mozintl/test')
5 files changed, 373 insertions, 0 deletions
diff --git a/toolkit/components/mozintl/test/.eslintrc.js b/toolkit/components/mozintl/test/.eslintrc.js new file mode 100644 index 0000000000..69e89d0054 --- /dev/null +++ b/toolkit/components/mozintl/test/.eslintrc.js @@ -0,0 +1,5 @@ +"use strict"; + +module.exports = { + extends: ["plugin:mozilla/xpcshell-test"], +}; diff --git a/toolkit/components/mozintl/test/test_mozintl.js b/toolkit/components/mozintl/test/test_mozintl.js new file mode 100644 index 0000000000..9a118ff4b6 --- /dev/null +++ b/toolkit/components/mozintl/test/test_mozintl.js @@ -0,0 +1,169 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); + +function run_test() { + test_methods_presence(); + test_methods_calling(); + test_constructors(); + test_rtf_formatBestUnit(); + test_datetimeformat(); + + ok(true); +} + +function test_methods_presence() { + equal(Services.intl.getCalendarInfo instanceof Function, true); + equal(Services.intl.getDisplayNames instanceof Function, true); + equal(Services.intl.getLocaleInfo instanceof Function, true); + equal(Services.intl.getLocaleDisplayNames instanceof Function, true); +} + +function test_methods_calling() { + Services.intl.getCalendarInfo("pl"); + Services.intl.getDisplayNames("ar"); + Services.intl.getLocaleInfo("de"); + new Services.intl.DateTimeFormat("fr"); + new Services.intl.ListFormat("fr"); + new Services.intl.Locale("fr"); + new Services.intl.RelativeTimeFormat("fr"); + ok(true); +} + +function test_constructors() { + let constructors = [ + "Collator", + "DateTimeFormat", + "ListFormat", + "NumberFormat", + "PluralRules", + ]; + + constructors.forEach(constructor => { + let obj = new Intl[constructor](); + let obj2 = new Services.intl[constructor](); + + equal(typeof obj, typeof obj2); + }); +} + +function testRTFBestUnit(anchor, value, expected) { + let rtf = new Services.intl.RelativeTimeFormat("en-US"); + deepEqual(rtf.formatBestUnit(new Date(value), { now: anchor }), expected); +} + +function test_rtf_formatBestUnit() { + { + // format seconds-distant dates + let anchor = new Date("2016-04-10 12:00:00"); + testRTFBestUnit(anchor, "2016-04-10 11:59:01", "59 seconds ago"); + testRTFBestUnit(anchor, "2016-04-10 12:00:00", "now"); + testRTFBestUnit(anchor, "2016-04-10 12:00:59", "in 59 seconds"); + } + + { + // format minutes-distant dates + let anchor = new Date("2016-04-10 12:00:00"); + testRTFBestUnit(anchor, "2016-04-10 11:01:00", "59 minutes ago"); + testRTFBestUnit(anchor, "2016-04-10 11:59", "1 minute ago"); + testRTFBestUnit(anchor, "2016-04-10 12:01", "in 1 minute"); + testRTFBestUnit(anchor, "2016-04-10 12:01:59", "in 1 minute"); + testRTFBestUnit(anchor, "2016-04-10 12:59:59", "in 59 minutes"); + } + + { + // format hours-distant dates + let anchor = new Date("2016-04-10 12:00:00"); + testRTFBestUnit(anchor, "2016-04-10 00:00", "12 hours ago"); + testRTFBestUnit(anchor, "2016-04-10 13:00", "in 1 hour"); + testRTFBestUnit(anchor, "2016-04-10 13:59:59", "in 1 hour"); + testRTFBestUnit(anchor, "2016-04-10 23:59:59", "in 11 hours"); + + anchor = new Date("2016-04-10 01:00"); + testRTFBestUnit(anchor, "2016-04-09 19:00", "6 hours ago"); + testRTFBestUnit(anchor, "2016-04-09 18:00", "yesterday"); + + anchor = new Date("2016-04-10 23:00"); + testRTFBestUnit(anchor, "2016-04-11 05:00", "in 6 hours"); + testRTFBestUnit(anchor, "2016-04-11 06:00", "tomorrow"); + + anchor = new Date("2016-01-31 23:00"); + testRTFBestUnit(anchor, "2016-02-01 05:00", "in 6 hours"); + testRTFBestUnit(anchor, "2016-02-01 07:00", "tomorrow"); + + anchor = new Date("2016-12-31 23:00"); + testRTFBestUnit(anchor, "2017-01-01 05:00", "in 6 hours"); + testRTFBestUnit(anchor, "2017-01-01 07:00", "tomorrow"); + } + + { + // format days-distant dates + let anchor = new Date("2016-04-10 12:00:00"); + testRTFBestUnit(anchor, "2016-04-01 00:00", "9 days ago"); + testRTFBestUnit(anchor, "2016-04-09 18:00", "yesterday"); + testRTFBestUnit(anchor, "2016-04-11 09:00", "tomorrow"); + testRTFBestUnit(anchor, "2016-04-30 23:59", "in 20 days"); + testRTFBestUnit(anchor, "2016-03-31 23:59", "last month"); + testRTFBestUnit(anchor, "2016-05-01 00:00", "next month"); + + anchor = new Date("2016-04-06 12:00"); + testRTFBestUnit(anchor, "2016-03-31 23:59", "6 days ago"); + + anchor = new Date("2016-04-25 23:00"); + testRTFBestUnit(anchor, "2016-05-01 00:00", "in 6 days"); + } + + { + // format months-distant dates + let anchor = new Date("2016-04-10 12:00:00"); + testRTFBestUnit(anchor, "2016-01-01 00:00", "3 months ago"); + testRTFBestUnit(anchor, "2016-03-01 00:00", "last month"); + testRTFBestUnit(anchor, "2016-05-01 00:00", "next month"); + testRTFBestUnit(anchor, "2016-12-01 23:59", "in 8 months"); + + anchor = new Date("2017-01-12 18:30"); + testRTFBestUnit(anchor, "2016-12-29 18:30", "last month"); + + anchor = new Date("2016-12-29 18:30"); + testRTFBestUnit(anchor, "2017-01-12 18:30", "next month"); + + anchor = new Date("2016-02-28 12:00"); + testRTFBestUnit(anchor, "2015-12-31 23:59", "2 months ago"); + } + + { + // format year-distant dates + let anchor = new Date("2016-04-10 12:00:00"); + testRTFBestUnit(anchor, "2014-04-01 00:00", "2 years ago"); + testRTFBestUnit(anchor, "2015-03-01 00:00", "last year"); + testRTFBestUnit(anchor, "2017-05-01 00:00", "next year"); + testRTFBestUnit(anchor, "2024-12-01 23:59", "in 8 years"); + + anchor = new Date("2017-01-12 18:30"); + testRTFBestUnit(anchor, "2016-01-01 18:30", "last year"); + testRTFBestUnit(anchor, "2015-12-29 18:30", "2 years ago"); + + anchor = new Date("2016-12-29 18:30"); + testRTFBestUnit(anchor, "2017-07-12 18:30", "next year"); + testRTFBestUnit(anchor, "2017-02-12 18:30", "in 2 months"); + testRTFBestUnit(anchor, "2018-01-02 18:30", "in 2 years"); + + testRTFBestUnit(anchor, "2098-01-02 18:30", "in 82 years"); + } +} + +function test_datetimeformat() { + Services.prefs.setStringPref( + "intl.date_time.pattern_override.date_long", + "yyyy年M月d日" + ); + + let formatted = new Services.intl.DateTimeFormat("ja", { + dateStyle: "long", + }).format(new Date("2020-12-08 21:00:05")); + + equal(formatted, "2020年12月8日"); + + Services.prefs.clearUserPref("intl.date_time.pattern_override.date_long"); +} diff --git a/toolkit/components/mozintl/test/test_mozintl_getLocaleDisplayNames.js b/toolkit/components/mozintl/test/test_mozintl_getLocaleDisplayNames.js new file mode 100644 index 0000000000..4b6e4e76e0 --- /dev/null +++ b/toolkit/components/mozintl/test/test_mozintl_getLocaleDisplayNames.js @@ -0,0 +1,132 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { L10nRegistry, FileSource } = ChromeUtils.import( + "resource://gre/modules/L10nRegistry.jsm" +); + +const fs = { + "toolkit/intl/languageNames.ftl": ` +language-name-en = English + `, + "toolkit/intl/regionNames.ftl": ` +region-name-us = United States +region-name-ru = Russia + `, +}; + +L10nRegistry.loadSync = function(url) { + if (!fs.hasOwnProperty(url)) { + return false; + } + return fs[url]; +}; + +let locales = Services.locale.packagedLocales; +const mockSource = new FileSource("mock", locales, ""); +L10nRegistry.registerSources([mockSource]); + +const gLangDN = Services.intl.getLanguageDisplayNames.bind( + Services.intl, + undefined +); +const gAvLocDN = Services.intl.getAvailableLocaleDisplayNames.bind( + Services.intl +); +const gRegDN = Services.intl.getRegionDisplayNames.bind( + Services.intl, + undefined +); +const gLocDN = Services.intl.getLocaleDisplayNames.bind( + Services.intl, + undefined +); + +add_test(function test_valid_language_tag() { + deepEqual(gLocDN([]), []); + deepEqual(gLocDN(["en"]), ["English"]); + deepEqual(gLocDN(["und"]), ["und"]); + run_next_test(); +}); + +add_test(function test_valid_region_tag() { + deepEqual(gLocDN(["en-US"]), ["English (United States)"]); + deepEqual(gLocDN(["en-XY"]), ["English (XY)"]); + run_next_test(); +}); + +add_test(function test_valid_script_tag() { + deepEqual(gLocDN(["en-Cyrl"]), ["English (Cyrl)"]); + deepEqual(gLocDN(["en-Cyrl-RU"]), ["English (Cyrl, Russia)"]); + run_next_test(); +}); + +add_test(function test_valid_variants_tag() { + deepEqual(gLocDN(["en-Cyrl-macos"]), ["English (Cyrl, macos)"]); + deepEqual(gLocDN(["en-Cyrl-RU-macos"]), ["English (Cyrl, Russia, macos)"]); + deepEqual(gLocDN(["en-Cyrl-RU-macos-modern"]), [ + "English (Cyrl, Russia, macos, modern)", + ]); + run_next_test(); +}); + +add_test(function test_other_subtags_ignored() { + deepEqual(gLocDN(["en-x-ignore"]), ["English"]); + deepEqual(gLocDN(["en-t-en-latn"]), ["English"]); + deepEqual(gLocDN(["en-u-hc-h24"]), ["English"]); + run_next_test(); +}); + +add_test(function test_invalid_locales() { + deepEqual(gLocDN(["2"]), ["2"]); + deepEqual(gLocDN([""]), [""]); + Assert.throws(() => gLocDN([2]), /All locale codes must be strings/); + Assert.throws(() => gLocDN([{}]), /All locale codes must be strings/); + Assert.throws(() => gLocDN([true]), /All locale codes must be strings/); + run_next_test(); +}); + +add_test(function test_language_only() { + deepEqual(gLangDN([]), []); + deepEqual(gLangDN(["en"]), ["English"]); + deepEqual(gLangDN(["und"]), ["und"]); + run_next_test(); +}); + +add_test(function test_invalid_languages() { + deepEqual(gLangDN(["2"]), ["2"]); + deepEqual(gLangDN([""]), [""]); + Assert.throws(() => gLangDN([2]), /All language codes must be strings/); + Assert.throws(() => gLangDN([{}]), /All language codes must be strings/); + Assert.throws(() => gLangDN([true]), /All language codes must be strings/); + run_next_test(); +}); + +add_test(function test_region_only() { + deepEqual(gRegDN([]), []); + deepEqual(gRegDN(["US"]), ["United States"]); + deepEqual(gRegDN(["und"]), ["UND"]); + run_next_test(); +}); + +add_test(function test_invalid_regions() { + deepEqual(gRegDN(["2"]), ["2"]); + deepEqual(gRegDN([""]), [""]); + Assert.throws(() => gRegDN([2]), /All region codes must be strings/); + Assert.throws(() => gRegDN([{}]), /All region codes must be strings/); + Assert.throws(() => gRegDN([true]), /All region codes must be strings/); + run_next_test(); +}); + +add_test(function test_availableLocaleDisplayNames() { + let langCodes = gAvLocDN("language"); + equal( + !!langCodes.length, + true, + "There should be some language codes available" + ); + let regCodes = gAvLocDN("region"); + equal(!!regCodes.length, true, "There should be some region codes available"); + run_next_test(); +}); diff --git a/toolkit/components/mozintl/test/test_mozintlhelper.js b/toolkit/components/mozintl/test/test_mozintlhelper.js new file mode 100644 index 0000000000..3023a15cee --- /dev/null +++ b/toolkit/components/mozintl/test/test_mozintlhelper.js @@ -0,0 +1,61 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +function run_test() { + const miHelper = Cc["@mozilla.org/mozintlhelper;1"].getService( + Ci.mozIMozIntlHelper + ); + + test_this_global(miHelper); + test_cross_global(miHelper); + test_methods_presence(miHelper); + + ok(true); +} + +function test_this_global(miHelper) { + let x = {}; + + miHelper.addGetCalendarInfo(x); + equal(x.getCalendarInfo instanceof Function, true); + equal(x.getCalendarInfo() instanceof Object, true); +} + +function test_cross_global(miHelper) { + var global = new Cu.Sandbox("https://example.com/"); + var x = global.Object(); + + miHelper.addGetCalendarInfo(x); + var waivedX = Cu.waiveXrays(x); + equal(waivedX.getCalendarInfo instanceof Function, false); + equal( + waivedX.getCalendarInfo instanceof Cu.waiveXrays(global.Function), + true + ); + equal(waivedX.getCalendarInfo() instanceof Object, false); + equal( + waivedX.getCalendarInfo() instanceof Cu.waiveXrays(global.Object), + true + ); +} + +function test_methods_presence(miHelper) { + equal(miHelper.addGetCalendarInfo instanceof Function, true); + equal(miHelper.addGetDisplayNames instanceof Function, true); + equal(miHelper.addGetLocaleInfo instanceof Function, true); + equal(miHelper.addDateTimeFormatConstructor instanceof Function, true); + + let x = {}; + + miHelper.addGetCalendarInfo(x); + equal(x.getCalendarInfo instanceof Function, true); + + miHelper.addGetDisplayNames(x); + equal(x.getDisplayNames instanceof Function, true); + + miHelper.addGetLocaleInfo(x); + equal(x.getLocaleInfo instanceof Function, true); + + miHelper.addDateTimeFormatConstructor(x); + equal(x.DateTimeFormat instanceof Function, true); +} diff --git a/toolkit/components/mozintl/test/xpcshell.ini b/toolkit/components/mozintl/test/xpcshell.ini new file mode 100644 index 0000000000..b524252bba --- /dev/null +++ b/toolkit/components/mozintl/test/xpcshell.ini @@ -0,0 +1,6 @@ +[DEFAULT] +head = + +[test_mozintl.js] +[test_mozintl_getLocaleDisplayNames.js] +[test_mozintlhelper.js] |