summaryrefslogtreecommitdiffstats
path: root/intl/l10n/test/test_pseudo.js
blob: 23404aab128a73740845811e51cf07b018bca8e5 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */


const originalValues = {
  requested: Services.locale.requestedLocales,
};

const l10nReg = new L10nRegistry();

function getMockRegistry() {
  const mockSource = L10nFileSource.createMock("test", "app", ["en-US"], "/localization/{locale}/", [
    {
      path: "/localization/en-US/mock.ftl",
      source: `
key = This is a single message
   .tooltip = This is a tooltip
   .accesskey = f
`
    }
  ]);
  let registry = new L10nRegistry();
  registry.registerSources([mockSource]);
  return registry;
}

function getAttributeByName(attributes, name) {
  return attributes.find(attr => attr.name === name);
}

/**
 * This test verifies that as we switch between
 * different pseudo strategies, the Localization object
 * follows and formats using the given strategy.
 *
 * We test values and attributes and make sure that
 * a single-character attributes, commonly used for access keys
 * don't get transformed.
 */
add_task(async function test_pseudo_works() {
  Services.prefs.setStringPref("intl.l10n.pseudo", "");

  let mockRegistry = getMockRegistry();

  const l10n = new Localization([
    "mock.ftl",
  ], false, mockRegistry);

  {
    // 1. Start with no pseudo

    let message = (await l10n.formatMessages([{id: "key"}]))[0];

    ok(message.value.includes("This is a single message"));
    let attr0 = getAttributeByName(message.attributes, "tooltip");
    ok(attr0.value.includes("This is a tooltip"));
    let attr1 = getAttributeByName(message.attributes, "accesskey");
    equal(attr1.value, "f");
  }

  {
    // 2. Set Accented Pseudo

    Services.prefs.setStringPref("intl.l10n.pseudo", "accented");
    let message = (await l10n.formatMessages([{id: "key"}]))[0];

    ok(message.value.includes("[Ŧħiş iş aa şiƞɠŀee ḿeeşşaaɠee]"));
    let attr0 = getAttributeByName(message.attributes, "tooltip");
    ok(attr0.value.includes("[Ŧħiş iş aa ŧooooŀŧiƥ]"));
    let attr1 = getAttributeByName(message.attributes, "accesskey");
    equal(attr1.value, "f");
  }

  {
    // 3. Set Bidi Pseudo

    Services.prefs.setStringPref("intl.l10n.pseudo", "bidi");
    let message = (await l10n.formatMessages([{id: "key"}]))[0];

    ok(message.value.includes("iş a şiƞɠŀe ḿeşşaɠe"));
    let attr0 = getAttributeByName(message.attributes, "tooltip");
    ok(attr0.value.includes("Ŧħiş iş a ŧooŀŧiƥ"));
    let attr1 = getAttributeByName(message.attributes, "accesskey");
    equal(attr1.value, "f");
  }

  {
    // 4. Remove pseudo

    Services.prefs.setStringPref("intl.l10n.pseudo", "");
    let message = (await l10n.formatMessages([{id: "key"}]))[0];

    ok(message.value.includes("This is a single message"));
    let attr0 = getAttributeByName(message.attributes, "tooltip");
    ok(attr0.value.includes("This is a tooltip"));
    let attr1 = getAttributeByName(message.attributes, "accesskey");
    equal(attr1.value, "f");
  }

  Services.locale.requestedLocales = originalValues.requested;
});

/**
 * This test verifies that setting a bogus pseudo locale
 * strategy doesn't break anything.
 */
add_task(async function test_unavailable_strategy_works() {
  Services.prefs.setStringPref("intl.l10n.pseudo", "");

  let mockRegistry = getMockRegistry();

  const l10n = new Localization([
    "mock.ftl",
  ], false, mockRegistry);

  {
    // 1. Set unavailable pseudo strategy
    Services.prefs.setStringPref("intl.l10n.pseudo", "unknown-strategy");

    let message = (await l10n.formatMessages([{id: "key"}]))[0];

    ok(message.value.includes("This is a single message"));
    let attr0 = getAttributeByName(message.attributes, "tooltip");
    ok(attr0.value.includes("This is a tooltip"));
    let attr1 = getAttributeByName(message.attributes, "accesskey");
    equal(attr1.value, "f");
  }

  Services.prefs.setStringPref("intl.l10n.pseudo", "");
  Services.locale.requestedLocales = originalValues.requested;
});