Commit 5a85f1bd authored by Alexandre Poirot's avatar Alexandre Poirot
Browse files

Bug 1743044 - [devtools] Expand source's introductionType coverage. r=bomsy

I'm adding one test for each Debugger.Source's introductionType
which was not covered yet.
Except for eventHandler and domTimer, which require some additional fix (see next changeset).

Differential Revision: https://phabricator.services.mozilla.com/D132217
parent 96786684
Loading
Loading
Loading
Loading
+72 −13
Original line number Diff line number Diff line
@@ -4,6 +4,9 @@
"use strict";

// Test the ResourceCommand API around SOURCE.
//
// We cover each Spidermonkey Debugger Source's `introductionType`:
// https://searchfox.org/mozilla-central/rev/4c184ca81b28f1ccffbfd08f465709b95bcb4aa1/js/src/doc/Debugger/Debugger.Source.md#172-213

const ResourceCommand = require("devtools/shared/commands/resource/resource-command");

@@ -31,7 +34,15 @@ add_task(async function() {
      targets.push(targetFront);
    },
  });
  is(targets.length, 3, "Got expected number of targets");
  if (isEveryFrameTargetEnabled()) {
    is(targets.length, 4, "Got expected number of targets");
  } else {
    is(
      targets.length,
      3,
      "Got expected number of targets (without fission, nor EFT)"
    );
  }

  info("Check already available resources");
  const availableResources = [];
@@ -41,26 +52,25 @@ add_task(async function() {

  const expectedExistingResources = [
    {
      description: "independent js file",
      description: "eval",
      sourceForm: {
        introductionType: "scriptElement",
        introductionType: "eval",
        sourceMapBaseURL:
          "https://example.com/browser/devtools/shared/commands/resource/tests/sources.js",
        url:
          "https://example.com/browser/devtools/shared/commands/resource/tests/sources.js",
          "https://example.com/browser/devtools/shared/commands/resource/tests/sources.html",
        url: null,
        isBlackBoxed: false,
        sourceMapURL: null,
        extensionName: null,
      },
      sourceContent: {
        contentType: "text/javascript",
        source: "/* eslint-disable */\nfunction scriptSource() {}\n",
        source: "this.global = function evalFunction() {}",
      },
    },
    {
      description: "eval",
      description: "new Function()",
      sourceForm: {
        introductionType: "eval",
        introductionType: "Function",
        sourceMapBaseURL:
          "https://example.com/browser/devtools/shared/commands/resource/tests/sources.html",
        url: null,
@@ -70,13 +80,29 @@ add_task(async function() {
      },
      sourceContent: {
        contentType: "text/javascript",
        source: "this.global = function evalFunction() {}",
        source: "return 42;",
      },
    },
    {
      description: "inline JS inserted at runtime",
      sourceForm: {
        introductionType: "scriptElement", // This is an injectedScript at SpiderMonkey level, but is translated into scriptElement by SourceActor.form()
        sourceMapBaseURL:
          "https://example.com/browser/devtools/shared/commands/resource/tests/sources.html",
        url: null,
        isBlackBoxed: false,
        sourceMapURL: null,
        extensionName: null,
      },
      sourceContent: {
        contentType: "text/javascript",
        source: "console.log('inline-script')",
      },
    },
    {
      description: "inline JS",
      sourceForm: {
        introductionType: "scriptElement",
        introductionType: "scriptElement", // This is an inlineScript at SpiderMonkey level, but is translated into scriptElement by SourceActor.form()
        sourceMapBaseURL:
          "https://example.com/browser/devtools/shared/commands/resource/tests/sources.html",
        url:
@@ -124,6 +150,40 @@ add_task(async function() {
        source: "/* eslint-disable */\nfunction serviceWorkerSource() {}\n",
      },
    },
    {
      description: "independent js file",
      sourceForm: {
        introductionType: "scriptElement", // This is an srcScript at SpiderMonkey level, but is translated into scriptElement by SourceActor.form()
        sourceMapBaseURL:
          "https://example.com/browser/devtools/shared/commands/resource/tests/sources.js",
        url:
          "https://example.com/browser/devtools/shared/commands/resource/tests/sources.js",
        isBlackBoxed: false,
        sourceMapURL: null,
        extensionName: null,
      },
      sourceContent: {
        contentType: "text/javascript",
        source: "/* eslint-disable */\nfunction scriptSource() {}\n",
      },
    },
    {
      description: "javascript URL",
      sourceForm: {
        introductionType: "javascriptURL",
        sourceMapBaseURL: isEveryFrameTargetEnabled()
          ? "about:blank"
          : "https://example.com/browser/devtools/shared/commands/resource/tests/sources.html",
        url: null,
        isBlackBoxed: false,
        sourceMapURL: null,
        extensionName: null,
      },
      sourceContent: {
        contentType: "text/javascript",
        source: "666",
      },
    },
  ];
  await assertResources(availableResources, expectedExistingResources);

@@ -149,8 +209,6 @@ async function assertResources(resources, expected) {
}

async function assertResource(source, expected) {
  info(`Checking resource "#${expected.description}"`);

  is(
    source.resourceType,
    ResourceCommand.TYPES.SOURCE,
@@ -173,6 +231,7 @@ async function assertResource(source, expected) {
    matchingExpected,
    `This source was expected with source content being "${sourceContent.source}"`
  );
  info(`Found "#${matchingExpected.description}"`);
  assertObject(
    sourceContent,
    matchingExpected.sourceContent,
+18 −1
Original line number Diff line number Diff line
@@ -6,17 +6,34 @@
    <meta charset="utf-8"/>
  </head>
  <body>
    <!-- introductionType=inlineScript mapped to scriptElement -->
    <script type="text/javascript">
      "use strict";
      /* eslint-disable */
      function inlineSource() {}

      // introductionType=eval
      // Assign it to a global in order to avoid it being GCed
      eval("this.global = function evalFunction() {}");

      // introductionType=Function
      // Also assign to a global to avoid being GCed
      this.global2 = new Function("return 42;");

      // introductionType=injectedScript mapped to scriptElement
      const script = document.createElement("script");
      script.textContent = "console.log('inline-script')";
      document.documentElement.appendChild(script);

      // introductionType=Worker, but ends up being null on SourceActor's form
      // Assign the worker to a global variable in order to avoid
      // having it be GCed.
      this.worker = new Worker("worker-sources.js");
      window.registrationPromise = navigator.serviceWorker.register("service-worker-sources.js");
    </script>
    <!-- introductionType=srcScript mapped to scriptElement -->
    <script src="sources.js"></script>
    <!-- introductionType=javascriptURL -->
    <iframe src="javascript:666"></iframe>
  </body>
</html>