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

const server = createHttpServer({ hosts: ["example.com"] });
server.registerDirectory("/data/", do_get_file("data"));

add_task(async function test_contentscript_exportHelpers() {
  function contentScript() {
    browser.test.assertTrue(typeof cloneInto === "function");
    browser.test.assertTrue(typeof createObjectIn === "function");
    browser.test.assertTrue(typeof exportFunction === "function");

    /* globals exportFunction, precisePi, reportPi */
    let value = 3.14;
    exportFunction(() => value, window, { defineAs: "precisePi" });

    browser.test.assertEq(
      "undefined",
      typeof precisePi,
      "exportFunction should export to the page's scope only"
    );

    browser.test.assertEq(
      "undefined",
      typeof window.precisePi,
      "exportFunction should export to the page's scope only"
    );

    let results = [];
    exportFunction(pi => results.push(pi), window, { defineAs: "reportPi" });

    let s = document.createElement("script");
    s.textContent = `(${function () {
      let result1 = "unknown 1";
      let result2 = "unknown 2";
      try {
        result1 = precisePi();
      } catch (e) {
        result1 = "err:" + e;
      }
      try {
        result2 = window.precisePi();
      } catch (e) {
        result2 = "err:" + e;
      }
      reportPi(result1);
      reportPi(result2);
    }})();`;

    document.documentElement.appendChild(s);
    // Inline script ought to run synchronously.

    browser.test.assertEq(
      3.14,
      results[0],
      "exportFunction on window should define a global function"
    );
    browser.test.assertEq(
      3.14,
      results[1],
      "exportFunction on window should export a property to window."
    );

    browser.test.assertEq(
      2,
      results.length,
      "Expecting the number of results to match the number of method calls"
    );

    browser.test.notifyPass("export helper test completed");
  }

  let extensionData = {
    manifest: {
      content_scripts: [
        {
          js: ["contentscript.js"],
          matches: ["http://example.com/data/file_sample.html"],
          run_at: "document_start",
        },
      ],
    },

    files: {
      "contentscript.js": contentScript,
    },
  };

  let extension = ExtensionTestUtils.loadExtension(extensionData);
  await extension.startup();

  let contentPage = await ExtensionTestUtils.loadContentPage(
    "http://example.com/data/file_sample.html"
  );

  await extension.awaitFinish("export helper test completed");
  await contentPage.close();
  await extension.unload();
});