summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_contentscript_cross_docGroup_adoption_xhr.js
blob: 274efb71f30e5feb208ae2069f3ab63e54094003 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

add_task(async function test_cross_docGroup_adoption() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.content_web_accessible.enabled", true]],
  });

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://example.com/"
  );

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      content_scripts: [
        {
          matches: ["http://example.com/"],
          js: ["content-script.js"],
        },
      ],
      web_accessible_resources: ["blank.html"],
    },

    files: {
      "blank.html": "<html>data</html>",
      "content-script.js": function () {
        let xhr = new XMLHttpRequest();
        xhr.responseType = "document";
        xhr.open("GET", browser.runtime.getURL("blank.html"));

        xhr.onload = function () {
          let doc = xhr.response;
          try {
            let node = doc.body.cloneNode(true);
            document.body.appendChild(node);
            browser.test.notifyPass("nodeAdopted");
          } catch (SecurityError) {
            browser.test.assertTrue(
              false,
              "The above node adoption should not fail"
            );
          }
        };
        xhr.send();
      },
    },
  });

  await extension.startup();
  await extension.awaitFinish("nodeAdopted");
  await extension.unload();

  BrowserTestUtils.removeTab(tab);
});