summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_l10n.js
blob: 96cc1243480cd899137ece64a3c91f97a898280c (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
"use strict";

const { FileUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/FileUtils.sys.mjs"
);

add_setup(async function setup() {
  // Add a test .ftl file
  // (Note: other tests do this by patching L10nRegistry.load() but in
  // this test L10nRegistry is also loaded in the extension process --
  // just adding a new resource is easier than trying to patch
  // L10nRegistry in all processes)
  let dir = FileUtils.getDir("TmpD", ["l10ntest"]);
  dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);

  await IOUtils.writeUTF8(
    PathUtils.join(dir.path, "test.ftl"),
    "key = value\n"
  );

  let target = Services.io.newFileURI(dir);
  let resProto = Services.io
    .getProtocolHandler("resource")
    .QueryInterface(Ci.nsIResProtocolHandler);

  resProto.setSubstitution("l10ntest", target);

  const source = new L10nFileSource(
    "test",
    "app",
    Services.locale.requestedLocales,
    "resource://l10ntest/"
  );
  L10nRegistry.getInstance().registerSources([source]);
});

// Test that privileged extensions can use fluent to get strings from
// language packs (and that unprivileged extensions cannot)
add_task(async function test_l10n_dom() {
  const PAGE = `<!DOCTYPE html>
                <html><head>
                  <meta charset="utf8">
                  <link rel="localization" href="test.ftl"/>
                  <script src="page.js"></script>
                </head></html>`;

  function SCRIPT() {
    window.addEventListener(
      "load",
      async () => {
        try {
          await document.l10n.ready;
          let result = await document.l10n.formatValue("key");
          browser.test.sendMessage("result", { success: true, result });
        } catch (err) {
          browser.test.sendMessage("result", {
            success: false,
            msg: err.message,
          });
        }
      },
      { once: true }
    );
  }

  async function runTest(isPrivileged) {
    let extension = ExtensionTestUtils.loadExtension({
      background() {
        browser.test.sendMessage("ready", browser.runtime.getURL("page.html"));
      },
      manifest: {
        web_accessible_resources: ["page.html"],
      },
      isPrivileged,
      files: {
        "page.html": PAGE,
        "page.js": SCRIPT,
      },
    });

    await extension.startup();
    let url = await extension.awaitMessage("ready");
    let page = await ExtensionTestUtils.loadContentPage(url, { extension });
    let results = await extension.awaitMessage("result");
    await page.close();
    await extension.unload();

    return results;
  }

  // Everything should work for a privileged extension
  let results = await runTest(true);
  equal(results.success, true, "Translation succeeded in privileged extension");
  equal(results.result, "value", "Translation got the right value");

  // In an unprivileged extension, document.l10n shouldn't show up
  results = await runTest(false);
  equal(results.success, false, "Translation failed in unprivileged extension");
  equal(
    results.msg.endsWith("document.l10n is undefined"),
    true,
    "Translation failed due to missing document.l10n"
  );
});

add_task(async function test_l10n_manifest() {
  // Fluent can't be used to localize properties that the AddonManager
  // reads (see comment inside ExtensionData.parseManifest for details)
  // so test by localizing a property that only the extension framework
  // cares about: page_action.  This means we can only do this test from
  // browser.
  if (AppConstants.MOZ_BUILD_APP != "browser") {
    return;
  }

  AddonTestUtils.initializeURLPreloader();

  async function runTest({
    isPrivileged = false,
    temporarilyInstalled = false,
  } = {}) {
    let extension = ExtensionTestUtils.loadExtension({
      isPrivileged,
      temporarilyInstalled,
      manifest: {
        l10n_resources: ["test.ftl"],
        page_action: {
          default_title: "__MSG_key__",
        },
      },
    });

    if (temporarilyInstalled && !isPrivileged) {
      ExtensionTestUtils.failOnSchemaWarnings(false);
      await Assert.rejects(
        extension.startup(),
        /Using 'l10n_resources' requires a privileged add-on/,
        "startup failed without privileged api access"
      );
      ExtensionTestUtils.failOnSchemaWarnings(true);
      return;
    }
    await extension.startup();
    let title = extension.extension.manifest.page_action.default_title;
    await extension.unload();
    return title;
  }

  let title = await runTest({ isPrivileged: true });
  equal(
    title,
    "value",
    "Manifest key localized with fluent in privileged extension"
  );

  title = await runTest();
  equal(
    title,
    "__MSG_key__",
    "Manifest key not localized in unprivileged extension"
  );

  title = await runTest({ temporarilyInstalled: true });
  equal(title, undefined, "Startup fails with temporarilyInstalled extension");
});