summaryrefslogtreecommitdiffstats
path: root/dom/html/test/browser_containerLoadingContent.js
blob: c0b52ffe8ab4e0edcd9b42a1aed0666f2341cbac (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const syntheticBrowsingContexts = SpecialPowers.getBoolPref(
  "browser.opaqueResponseBlocking.syntheticBrowsingContext",
  false
);

const DIRPATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content/",
  ""
);

const ORIGIN = "https://example.com";
const CROSSORIGIN = "https://example.org";

const TABURL = `${ORIGIN}/${DIRPATH}dummy_page.html`;

const IMAGEURL = `${ORIGIN}/${DIRPATH}image.png`;
const CROSSIMAGEURL = `${CROSSORIGIN}/${DIRPATH}image.png`;

const DOCUMENTURL = `${ORIGIN}/${DIRPATH}dummy_page.html`;
const CROSSDOCUMENTURL = `${CROSSORIGIN}/${DIRPATH}dummy_page.html`;

async function createElements({ element, attribute }, url1, url2) {
  for (let url of [url1, url2]) {
    const object = content.document.createElement(element);
    object[attribute] = url;
    const onloadPromise = new Promise(res => {
      object.onload = res;
    });
    content.document.body.appendChild(object);
    await onloadPromise;
    return object;
  }
}

function getPids(browser) {
  return browser.browsingContext.children.map(
    child => child.currentWindowContext.osPid
  );
}

async function runTest(spec, tabUrl, imageurl, crossimageurl, check) {
  await BrowserTestUtils.withNewTab(tabUrl, async browser => {
    await SpecialPowers.spawn(
      browser,
      [spec, imageurl, crossimageurl],
      async ({ element, attribute }, url1, url2) => {
        for (let url of [url1, url2]) {
          const object = content.document.createElement(element);
          object[attribute] = url;
          const onloadPromise = new Promise(res => {
            object.onload = res;
          });
          content.document.body.appendChild(object);
          await onloadPromise;
        }
      }
    );

    await check(browser);
  });
}

let iframe = { element: "iframe", attribute: "src" };
let embed = { element: "embed", attribute: "src" };
let object = { element: "object", attribute: "data" };

async function checkImage(browser) {
  let pids = getPids(browser);
  is(pids.length, 2, "There should be two browsing contexts");
  ok(pids[0], "The first pid should have a sane value");
  ok(pids[1], "The second pid should have a sane value");
  isnot(pids[0], pids[1], "The two pids should be different");

  let images = [];
  for (let context of browser.browsingContext.children) {
    images.push(
      await SpecialPowers.spawn(context, [], async () => {
        let img = new URL(content.document.querySelector("img").src);
        is(
          `${img.protocol}//${img.host}`,
          `${content.location.protocol}//${content.location.host}`,
          "Images should be loaded in the same domain as the document"
        );
        return img.href;
      })
    );
  }
  isnot(images[0], images[1], "The images should have different sources");
}

function checkDocument(browser) {
  let pids = getPids(browser);
  is(pids.length, 2, "There should be two browsing contexts");
  ok(pids[0], "The first pid should have a sane value");
  ok(pids[1], "The second pid should have a sane value");
  isnot(pids[0], pids[1], "The two pids should be different");
}

add_task(async function test_iframeImageDocument() {
  await runTest(iframe, TABURL, IMAGEURL, CROSSIMAGEURL, checkImage);
});

if (syntheticBrowsingContexts) {
  add_task(async function test_embedImageDocument() {
    await runTest(embed, TABURL, IMAGEURL, CROSSIMAGEURL, checkImage);
  });

  add_task(async function test_objectImageDocument() {
    await runTest(object, TABURL, IMAGEURL, CROSSIMAGEURL, checkImage);
  });
}

add_task(async function test_iframeDocument() {
  await runTest(iframe, TABURL, DOCUMENTURL, CROSSDOCUMENTURL, checkDocument);
});

if (syntheticBrowsingContexts) {
  add_task(async function test_embedDocument() {
    await runTest(embed, TABURL, DOCUMENTURL, CROSSDOCUMENTURL, checkDocument);
  });

  add_task(async function test_objectDocument() {
    await runTest(object, TABURL, DOCUMENTURL, CROSSDOCUMENTURL, checkDocument);
  });
}