tor-browser

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

test_javascript_logging.js (1966B)


      1 "use strict";
      2 
      3 const { JSObjectsTestUtils } = ChromeUtils.importESModule(
      4  "resource://testing-common/JSObjectsTestUtils.sys.mjs"
      5 );
      6 JSObjectsTestUtils.init(this);
      7 
      8 // Note that XPCShellContentUtils will already be initialized by JSObjectsTestUtils
      9 const { XPCShellContentUtils } = ChromeUtils.importESModule(
     10  "resource://testing-common/XPCShellContentUtils.sys.mjs"
     11 );
     12 
     13 const EXPECTED_VALUES_FILE = "test_javascript_logging.snapshot.mjs";
     14 
     15 add_task(async function () {
     16  // Create a content page in order to better simulate a real world page
     17  const contentPage = await XPCShellContentUtils.loadContentPage(
     18    "http://example.com/"
     19  );
     20 
     21  await JSObjectsTestUtils.runTest(EXPECTED_VALUES_FILE, async function (arg) {
     22    // Because the test page runs in a content process, we have to execute most of the test logic via `spawn`
     23    return contentPage.spawn([arg], async ({ context, expression }) => {
     24      const { CONTEXTS } = ChromeUtils.importESModule(
     25        "resource://testing-common/AllJavascriptTypes.mjs"
     26      );
     27      const { JSTracer } = ChromeUtils.importESModule(
     28        "resource://devtools/server/tracer/tracer.sys.mjs"
     29      );
     30      const systemPrincipal =
     31        Services.scriptSecurityManager.getSystemPrincipal();
     32      const chromeSandbox = Cu.Sandbox(systemPrincipal);
     33 
     34      let ref;
     35      try {
     36        if (context == CONTEXTS.CHROME) {
     37          ref = Cu.evalInSandbox(
     38            expression,
     39            chromeSandbox,
     40            null,
     41            "test sandbox"
     42          );
     43        } else {
     44          ref = this.content.eval(expression);
     45        }
     46      } catch (e) {
     47        ref = e;
     48      }
     49 
     50      const str = JSTracer.objectToString(ref);
     51 
     52      // Silence any async rejection
     53      if (ref instanceof this.content.Promise) {
     54        // eslint-disable-next-line max-nested-callbacks
     55        ref.catch(function () {});
     56      }
     57 
     58      return str;
     59    });
     60  });
     61 
     62  info("Close content page");
     63  await contentPage.close();
     64 });