tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_defineESModuleGetters_options.js (2224B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 add_task(async function testShared() {
      6  const lazy1 = {};
      7  const lazy2 = {};
      8 
      9  ChromeUtils.defineESModuleGetters(lazy1, {
     10    GetX: "resource://test/esm_lazy-1.sys.mjs",
     11  });
     12 
     13  ChromeUtils.defineESModuleGetters(lazy2, {
     14    GetX: "resource://test/esm_lazy-1.sys.mjs",
     15  }, {
     16    global: "shared",
     17  });
     18 
     19  Assert.equal(lazy1.GetX, lazy2.GetX);
     20 
     21  const ns = ChromeUtils.importESModule("resource://test/esm_lazy-1.sys.mjs");
     22 
     23  Assert.equal(ns.GetX, lazy1.GetX);
     24  Assert.equal(ns.GetX, lazy2.GetX);
     25 });
     26 
     27 add_task(async function testDevTools() {
     28  const lazy = {};
     29 
     30  ChromeUtils.defineESModuleGetters(lazy, {
     31    GetX: "resource://test/esm_lazy-1.sys.mjs",
     32  }, {
     33    global: "devtools",
     34  });
     35 
     36  lazy.GetX; // delazify before import.
     37 
     38  const ns = ChromeUtils.importESModule("resource://test/esm_lazy-1.sys.mjs", {
     39    global: "devtools",
     40  });
     41 
     42  Assert.equal(ns.GetX, lazy.GetX);
     43 });
     44 
     45 add_task(async function testSandbox() {
     46  const uri = "http://example.com/";
     47  const window = createContentWindow(uri);
     48  const sandboxOpts = {
     49    sandboxPrototype: window,
     50    wantGlobalProperties: ["ChromeUtils"],
     51  };
     52  const sb = new Cu.Sandbox(uri, sandboxOpts);
     53 
     54  const result = Cu.evalInSandbox(`
     55  const lazy = {};
     56 
     57  ChromeUtils.defineESModuleGetters(lazy, {
     58    GetX: "resource://test/esm_lazy-1.sys.mjs",
     59  }, {
     60    global: "current",
     61  });
     62 
     63  lazy.GetX; // delazify before import.
     64 
     65  const ns = ChromeUtils.importESModule("resource://test/esm_lazy-1.sys.mjs", {
     66    global: "current",
     67  });
     68 
     69  ns.GetX == lazy.GetX;
     70 `, sb);
     71 
     72  Assert.ok(result);
     73 });
     74 
     75 add_task(async function testWindow() {
     76  const win1 = createChromeWindow();
     77 
     78  const result = win1.eval(`
     79  const lazy = {};
     80 
     81  ChromeUtils.defineESModuleGetters(lazy, {
     82    GetX: "resource://test/esm_lazy-1.sys.mjs",
     83  }, {
     84    global: "current",
     85  });
     86 
     87  lazy.GetX; // delazify before import.
     88 
     89  const ns = ChromeUtils.importESModule("resource://test/esm_lazy-1.sys.mjs", {
     90    global: "current",
     91  });
     92 
     93  ns.GetX == lazy.GetX;
     94 `);
     95 
     96  Assert.ok(result);
     97 });