summaryrefslogtreecommitdiffstats
path: root/intl/locale/tests/unit/test_localeService_negotiateLanguages.js
blob: f929bc3a4370bd2a409ce675943dcce7dabec61c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* 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/. */

const localeService = Services.locale;

const data = {
  filtering: {
    "exact match": [
      [["en"], ["en"], ["en"]],
      [["en-US"], ["en-US"], ["en-US"]],
      [["en-Latn-US"], ["en-Latn-US"], ["en-Latn-US"]],
      [["en-Latn-US-windows"], ["en-Latn-US-windows"], ["en-Latn-US-windows"]],
      [["fr-FR"], ["de", "it", "fr-FR"], ["fr-FR"]],
      [
        ["fr", "pl", "de-DE"],
        ["pl", "en-US", "de-DE"],
        ["pl", "de-DE"],
      ],
    ],
    "available as range": [
      [["en-US"], ["en"], ["en"]],
      [["en-Latn-US"], ["en-US"], ["en-US"]],
      [["en-US-windows"], ["en-US"], ["en-US"]],
      [
        ["fr-CA", "de-DE"],
        ["fr", "it", "de"],
        ["fr", "de"],
      ],
      [["ja-JP-windows"], ["ja"], ["ja"]],
      [
        ["en-Latn-GB", "en-Latn-IN"],
        ["en-IN", "en-GB"],
        ["en-GB", "en-IN"],
      ],
    ],
    "should match on likely subtag": [
      [["en"], ["en-GB", "de", "en-US"], ["en-US", "en-GB"]],
      [
        ["en"],
        ["en-Latn-GB", "de", "en-Latn-US"],
        ["en-Latn-US", "en-Latn-GB"],
      ],
      [["fr"], ["fr-CA", "fr-FR"], ["fr-FR", "fr-CA"]],
      [["az-IR"], ["az-Latn", "az-Arab"], ["az-Arab"]],
      [["sr-RU"], ["sr-Cyrl", "sr-Latn"], ["sr-Latn"]],
      [["sr"], ["sr-Latn", "sr-Cyrl"], ["sr-Cyrl"]],
      [["zh-GB"], ["zh-Hans", "zh-Hant"], ["zh-Hant"]],
      [["sr", "ru"], ["sr-Latn", "ru"], ["ru"]],
      [["sr-RU"], ["sr-Latn-RO", "sr-Cyrl"], ["sr-Latn-RO"]],
    ],
    "should match likelySubtag region over other regions": [
      [["en-CA"], ["en-ZA", "en-GB", "en-US"], ["en-US", "en-ZA", "en-GB"]],
    ],
    "should match cross-region": [
      [["en"], ["en-US"], ["en-US"]],
      [["en-US"], ["en-GB"], ["en-GB"]],
      [["en-Latn-US"], ["en-Latn-GB"], ["en-Latn-GB"]],
    ],
    "should match cross-variant": [
      [["en-US-linux"], ["en-US-windows"], ["en-US-windows"]],
    ],
    "should prioritize properly": [
      // exact match first
      [
        ["en-US"],
        ["en-US-windows", "en", "en-US"],
        ["en-US", "en", "en-US-windows"],
      ],
      // available as range second
      [["en-Latn-US"], ["en-GB", "en-US"], ["en-US", "en-GB"]],
      // likely subtags third
      [["en"], ["en-Cyrl-US", "en-Latn-US"], ["en-Latn-US"]],
      // variant range fourth
      [
        ["en-US-macos"],
        ["en-US-windows", "en-GB-macos"],
        ["en-US-windows", "en-GB-macos"],
      ],
      // regional range fifth
      [["en-US-macos"], ["en-GB-windows"], ["en-GB-windows"]],
      [["en-US"], ["en-GB", "en"], ["en", "en-GB"]],
      [
        ["fr-CA-macos", "de-DE"],
        ["de-DE", "fr-FR-windows"],
        ["fr-FR-windows", "de-DE"],
      ],
    ],
    "should handle default locale properly": [
      [["fr"], ["de", "it"], []],
      [["fr"], ["de", "it"], "en-US", ["en-US"]],
      [["fr"], ["de", "en-US"], "en-US", ["en-US"]],
      [
        ["fr", "de-DE"],
        ["de-DE", "fr-CA"],
        "en-US",
        ["fr-CA", "de-DE", "en-US"],
      ],
    ],
    "should handle all matches on the 1st higher than any on the 2nd": [
      [
        ["fr-CA-macos", "de-DE"],
        ["de-DE", "fr-FR-windows"],
        ["fr-FR-windows", "de-DE"],
      ],
    ],
    "should handle cases and underscores, returning the form given in the 'available' list":
      [
        [["fr_FR"], ["fr-FR"], ["fr-FR"]],
        [["fr_fr"], ["fr-FR"], ["fr-FR"]],
        [["fr_Fr"], ["fr-fR"], ["fr-fR"]],
        [["fr_lAtN_fr"], ["fr-Latn-FR"], ["fr-Latn-FR"]],
        [["fr_FR"], ["fr_FR"], ["fr_FR"]],
        [["fr-FR"], ["fr_FR"], ["fr_FR"]],
        [["fr_Cyrl_FR_macos"], ["fr_Cyrl_fr-macos"], ["fr_Cyrl_fr-macos"]],
      ],
    "should handle mozilla specific 3-letter variants": [
      [
        ["ja-JP-mac", "de-DE"],
        ["ja-JP-mac", "de-DE"],
        ["ja-JP-mac", "de-DE"],
      ],
    ],
    "should not crash on invalid input": [
      [["fą-FŻ"], ["ór_Fń"], []],
      [["2"], ["ąóżł"], []],
      [[[""]], ["fr-FR"], []],
    ],
    "should not match on invalid input": [[["en"], ["ťŮ"], []]],
  },
  matching: {
    "should match only one per requested": [
      [
        ["fr", "en"],
        ["en-US", "fr-FR", "en", "fr"],
        null,
        localeService.langNegStrategyMatching,
        ["fr", "en"],
      ],
    ],
  },
  lookup: {
    "should match only one": [
      [
        ["fr-FR", "en"],
        ["en-US", "fr-FR", "en", "fr"],
        "en-US",
        localeService.langNegStrategyLookup,
        ["fr-FR"],
      ],
    ],
  },
};

function run_test() {
  const nl = localeService.negotiateLanguages;

  const json = JSON.stringify;
  for (const strategy in data) {
    for (const groupName in data[strategy]) {
      const group = data[strategy][groupName];
      for (const test of group) {
        const requested = test[0];
        const available = test[1];
        const defaultLocale = test.length > 3 ? test[2] : undefined;
        const strategyInner = test.length > 4 ? test[3] : undefined;
        const supported = test[test.length - 1];

        const result = nl(test[0], test[1], defaultLocale, strategyInner);
        deepEqual(
          result,
          supported,
          `\nExpected ${json(requested)} * ${json(available)} = ${json(
            supported
          )}.\n`
        );
      }
    }
  }

  // Verify that we error out when requested or available is not an array.
  for (const [req, avail] in [
    [null, ["fr-FR"]],
    [undefined, ["fr-FR"]],
    [2, ["fr-FR"]],
    ["fr-FR", ["fr-FR"]],
    [["fr-FR"], null],
    [["fr-FR"], undefined],
    [["fr-FR"], 2],
    [["fr-FR"], "fr-FR"],
    [[null], []],
    [[undefined], []],
    [[undefined], [null]],
    [[undefined], [undefined]],
    [[null], [null]],
    [[[]], [[2]]],
  ]) {
    Assert.throws(
      () => {
        nl(req, avail);
      },
      err => err.result == Cr.NS_ERROR_XPC_CANT_CONVERT_PRIMITIVE_TO_ARRAY
    );
  }
}