toolkit/components/extensions/test/mochitest/test_ext_contentscript.html
author Kris Maglione <maglione.k@gmail.com>
Tue, 22 Dec 2015 23:43:23 -0500
changeset 317182 a357c7e76527ac42fc450826d37f57c3c99ad97f
parent 310976 ec5135f71a220cad31af2cf93f039defeaa2b35d
child 339925 66ed3ef32ef656f7ca32209da496c1252dfa8421
permissions -rw-r--r--
Bug 1234755: [webext] Make JS in HTML files pass ESLint. r?billm This also changes the `type` attribute in our script tags to text/javascript, since that's currently all the eslint-html plugin supports.

<!DOCTYPE HTML>
<html>
<head>
  <title>Test for content script</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

add_task(function* test_contentscript() {
  function backgroundScript() {
    browser.runtime.onMessage.addListener(([msg, expectedState, readyState], sender) => {
      if (msg == "chrome-namespace-ok") {
        browser.test.sendMessage(msg);
        return;
      }

      browser.test.assertEq(msg, "script-run", "message type is correct");
      browser.test.assertEq(readyState, expectedState, "readyState is correct");
      browser.test.sendMessage("script-run-" + expectedState);
    });
  }

  function contentScriptStart() {
    browser.runtime.sendMessage(["script-run", "loading", document.readyState]);
  }
  function contentScriptEnd() {
    browser.runtime.sendMessage(["script-run", "interactive", document.readyState]);
  }
  function contentScriptIdle() {
    browser.runtime.sendMessage(["script-run", "complete", document.readyState]);
  }

  function contentScript() {
    let manifest = browser.runtime.getManifest();
    void manifest.applications.gecko.id;
    chrome.runtime.sendMessage(["chrome-namespace-ok"]);
  }

  let extensionData = {
    manifest: {
      content_scripts: [
        {
          "matches": ["http://mochi.test/*/file_sample.html"],
          "js": ["content_script_start.js"],
          "run_at": "document_start",
        },
        {
          "matches": ["http://mochi.test/*/file_sample.html"],
          "js": ["content_script_end.js"],
          "run_at": "document_end",
        },
        {
          "matches": ["http://mochi.test/*/file_sample.html"],
          "js": ["content_script_idle.js"],
          "run_at": "document_idle",
        },
        {
          "matches": ["http://mochi.test/*/file_sample.html"],
          "js": ["content_script.js"],
          "run_at": "document_idle",
        },
      ],
    },
    background: "(" + backgroundScript.toString() + ")()",

    files: {
      "content_script_start.js": "(" + contentScriptStart.toString() + ")()",
      "content_script_end.js": "(" + contentScriptEnd.toString() + ")()",
      "content_script_idle.js": "(" + contentScriptIdle.toString() + ")()",
      "content_script.js": "(" + contentScript.toString() + ")()",
    },
  };

  let extension = ExtensionTestUtils.loadExtension(extensionData);

  let loadingCount = 0;
  let interactiveCount = 0;
  let completeCount = 0;
  extension.onMessage("script-run-loading", () => { loadingCount++; });
  extension.onMessage("script-run-interactive", () => { interactiveCount++; });

  let completePromise = new Promise(resolve => {
    extension.onMessage("script-run-complete", () => { completeCount++; resolve(); });
  });

  let chromeNamespacePromise = extension.awaitMessage("chrome-namespace-ok");

  yield extension.startup();
  info("extension loaded");

  let win = window.open("file_sample.html");

  yield Promise.all([waitForLoad(win), completePromise, chromeNamespacePromise]);
  info("test page loaded");

  win.close();

  is(loadingCount, 1, "document_start script ran exactly once");
  is(interactiveCount, 1, "document_end script ran exactly once");
  is(completeCount, 1, "document_idle script ran exactly once");

  yield extension.unload();
  info("extension unloaded");
});
</script>

</body>
</html>