Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright 2012 Mozilla Corporation. All rights reserved. |
michael@0 | 2 | // This code is governed by the BSD license found in the LICENSE file. |
michael@0 | 3 | |
michael@0 | 4 | /** |
michael@0 | 5 | * @description Tests that the set of options for the date and time components is processed correctly. |
michael@0 | 6 | * @author Norbert Lindenberg |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | $INCLUDE("testIntl.js"); |
michael@0 | 10 | |
michael@0 | 11 | var locales = [[], ["zh-Hans-CN"], ["hi-IN"], ["en-US"], ["id-ID"]]; |
michael@0 | 12 | var dates = [new Date(), new Date(0), new Date(Date.parse("1989-11-09T17:57:00Z"))]; |
michael@0 | 13 | |
michael@0 | 14 | function testWithDateTimeFormat(options, expected) { |
michael@0 | 15 | locales.forEach(function (locales) { |
michael@0 | 16 | var format = new Intl.DateTimeFormat(locales, options); |
michael@0 | 17 | var resolvedOptions = format.resolvedOptions(); |
michael@0 | 18 | getDateTimeComponents().forEach(function (component) { |
michael@0 | 19 | if (resolvedOptions.hasOwnProperty(component)) { |
michael@0 | 20 | if (!expected.hasOwnProperty(component)) { |
michael@0 | 21 | $ERROR("Unrequested component " + component + |
michael@0 | 22 | " added to expected subset " + JSON.stringify(expected) + |
michael@0 | 23 | "; locales " + locales + ", options " + |
michael@0 | 24 | (options ? JSON.stringify(options) : options) + "."); |
michael@0 | 25 | } |
michael@0 | 26 | } else { |
michael@0 | 27 | if (expected.hasOwnProperty(component)) { |
michael@0 | 28 | $ERROR("Missing component " + component + |
michael@0 | 29 | " from expected subset " + JSON.stringify(expected) + |
michael@0 | 30 | "; locales " + locales + ", options " + |
michael@0 | 31 | (options ? JSON.stringify(options) : options) + "."); |
michael@0 | 32 | } |
michael@0 | 33 | } |
michael@0 | 34 | }); |
michael@0 | 35 | }); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | function testWithToLocale(f, options, expected) { |
michael@0 | 39 | // expected can be either one subset or an array of possible subsets |
michael@0 | 40 | if (expected.length === undefined) { |
michael@0 | 41 | expected = [expected]; |
michael@0 | 42 | } |
michael@0 | 43 | locales.forEach(function (locales) { |
michael@0 | 44 | dates.forEach(function (date) { |
michael@0 | 45 | var formatted = Date.prototype[f].call(date, locales, options); |
michael@0 | 46 | var expectedStrings = []; |
michael@0 | 47 | expected.forEach(function (expected) { |
michael@0 | 48 | var referenceFormat = new Intl.DateTimeFormat(locales, expected); |
michael@0 | 49 | expectedStrings.push(referenceFormat.format(date)); |
michael@0 | 50 | }); |
michael@0 | 51 | if (expectedStrings.indexOf(formatted) === -1) { |
michael@0 | 52 | $ERROR("Function " + f + " did not return expected string for locales " + |
michael@0 | 53 | locales + ", options " + (options? JSON.stringify(options) : options) + |
michael@0 | 54 | "; expected " + |
michael@0 | 55 | (expectedStrings.length === 1 ? expectedStrings[0] : "one of " + expectedStrings) + |
michael@0 | 56 | ", got " + formatted + "."); |
michael@0 | 57 | } |
michael@0 | 58 | }); |
michael@0 | 59 | }); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | // any/date: steps 5a, 6a, 7a |
michael@0 | 63 | testWithDateTimeFormat(undefined, {year: "numeric", month: "numeric", day: "numeric"}); |
michael@0 | 64 | |
michael@0 | 65 | // any/date: steps 5a, 6a |
michael@0 | 66 | testWithDateTimeFormat({year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); |
michael@0 | 67 | |
michael@0 | 68 | // any/date: steps 5a, 6a |
michael@0 | 69 | testWithDateTimeFormat({hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); |
michael@0 | 70 | |
michael@0 | 71 | // any/all: steps 5a, 6a, 7a, 8a |
michael@0 | 72 | testWithToLocale("toLocaleString", undefined, [ |
michael@0 | 73 | // the first one is not guaranteed to be supported; the second one is |
michael@0 | 74 | {year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}, |
michael@0 | 75 | {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"} |
michael@0 | 76 | ]); |
michael@0 | 77 | |
michael@0 | 78 | // any/all: steps 5a, 6a |
michael@0 | 79 | testWithToLocale("toLocaleString", {year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); |
michael@0 | 80 | |
michael@0 | 81 | // any/all: steps 5a, 6a |
michael@0 | 82 | testWithToLocale("toLocaleString", {hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); |
michael@0 | 83 | |
michael@0 | 84 | // date/date: steps 5a, 7a |
michael@0 | 85 | testWithToLocale("toLocaleDateString", undefined, {year: "numeric", month: "numeric", day: "numeric"}); |
michael@0 | 86 | |
michael@0 | 87 | // date/date: steps 5a |
michael@0 | 88 | testWithToLocale("toLocaleDateString", {year: "numeric", month: "numeric"}, {year: "numeric", month: "numeric"}); |
michael@0 | 89 | |
michael@0 | 90 | // date/date: steps 5a, 7a |
michael@0 | 91 | testWithToLocale("toLocaleDateString", {hour: "numeric", minute: "numeric", second: "numeric"}, [ |
michael@0 | 92 | // the first one is not guaranteed to be supported; the second one is |
michael@0 | 93 | {year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}, |
michael@0 | 94 | {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"} |
michael@0 | 95 | ]); |
michael@0 | 96 | |
michael@0 | 97 | // time/time: steps 6a, 8a |
michael@0 | 98 | testWithToLocale("toLocaleTimeString", undefined, {hour: "numeric", minute: "numeric", second: "numeric"}); |
michael@0 | 99 | |
michael@0 | 100 | // time/time: steps 6a, 8a |
michael@0 | 101 | testWithToLocale("toLocaleTimeString", {weekday: "short", year: "numeric", month: "numeric", day: "numeric"}, |
michael@0 | 102 | {weekday: "short", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric"}); |
michael@0 | 103 | |
michael@0 | 104 | // time/time: steps 6a |
michael@0 | 105 | testWithToLocale("toLocaleTimeString", {hour: "numeric", minute: "numeric"}, {hour: "numeric", minute: "numeric"}); |
michael@0 | 106 | |
michael@0 | 107 |