summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/unit/test_defineESModuleGetters_options.js
blob: b01580dfa12ed280a931430408494f7d4b8d635c (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
/* 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/. */

add_task(async function testShared() {
  const lazy1 = {};
  const lazy2 = {};

  ChromeUtils.defineESModuleGetters(lazy1, {
    GetX: "resource://test/esm_lazy-1.sys.mjs",
  });

  ChromeUtils.defineESModuleGetters(lazy2, {
    GetX: "resource://test/esm_lazy-1.sys.mjs",
  }, {
    global: "shared",
  });

  Assert.equal(lazy1.GetX, lazy2.GetX);

  const ns = ChromeUtils.importESModule("resource://test/esm_lazy-1.sys.mjs");

  Assert.equal(ns.GetX, lazy1.GetX);
  Assert.equal(ns.GetX, lazy2.GetX);
});

add_task(async function testDevTools() {
  const lazy = {};

  ChromeUtils.defineESModuleGetters(lazy, {
    GetX: "resource://test/esm_lazy-1.sys.mjs",
  }, {
    global: "devtools",
  });

  lazy.GetX; // delazify before import.

  const ns = ChromeUtils.importESModule("resource://test/esm_lazy-1.sys.mjs", {
    global: "devtools",
  });

  Assert.equal(ns.GetX, lazy.GetX);
});

add_task(async function testSandbox() {
  const uri = "http://example.com/";
  const window = createContentWindow(uri);
  const sandboxOpts = {
    sandboxPrototype: window,
    wantGlobalProperties: ["ChromeUtils"],
  };
  const sb = new Cu.Sandbox(uri, sandboxOpts);

  const result = Cu.evalInSandbox(`
  const lazy = {};

  ChromeUtils.defineESModuleGetters(lazy, {
    GetX: "resource://test/esm_lazy-1.sys.mjs",
  }, {
    global: "current",
  });

  lazy.GetX; // delazify before import.

  const ns = ChromeUtils.importESModule("resource://test/esm_lazy-1.sys.mjs", {
    global: "current",
  });

  ns.GetX == lazy.GetX;
`, sb);

  Assert.ok(result);
});

add_task(async function testWindow() {
  const win1 = createChromeWindow();

  const result = win1.eval(`
  const lazy = {};

  ChromeUtils.defineESModuleGetters(lazy, {
    GetX: "resource://test/esm_lazy-1.sys.mjs",
  }, {
    global: "current",
  });

  lazy.GetX; // delazify before import.

  const ns = ChromeUtils.importESModule("resource://test/esm_lazy-1.sys.mjs", {
    global: "current",
  });

  ns.GetX == lazy.GetX;
`);

  Assert.ok(result);
});