browser_containerLoadingContent.js (3491B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const DIRPATH = getRootDirectory(gTestPath).replace( 7 "chrome://mochitests/content/", 8 "" 9 ); 10 11 const ORIGIN = "https://example.com"; 12 const CROSSORIGIN = "https://example.org"; 13 14 const TABURL = `${ORIGIN}/${DIRPATH}dummy_page.html`; 15 16 const IMAGEURL = `${ORIGIN}/${DIRPATH}image.png`; 17 const CROSSIMAGEURL = `${CROSSORIGIN}/${DIRPATH}image.png`; 18 19 const DOCUMENTURL = `${ORIGIN}/${DIRPATH}dummy_page.html`; 20 const CROSSDOCUMENTURL = `${CROSSORIGIN}/${DIRPATH}dummy_page.html`; 21 22 function getPids(browser) { 23 return browser.browsingContext.children.map( 24 child => child.currentWindowContext.osPid 25 ); 26 } 27 28 async function runTest(spec, tabUrl, imageurl, crossimageurl, check) { 29 await BrowserTestUtils.withNewTab(tabUrl, async browser => { 30 await SpecialPowers.spawn( 31 browser, 32 [spec, imageurl, crossimageurl], 33 async ({ element, attribute }, url1, url2) => { 34 for (let url of [url1, url2]) { 35 const object = content.document.createElement(element); 36 object[attribute] = url; 37 const onloadPromise = new Promise(res => { 38 object.onload = res; 39 }); 40 content.document.body.appendChild(object); 41 await onloadPromise; 42 } 43 } 44 ); 45 46 await check(browser); 47 }); 48 } 49 50 let iframe = { element: "iframe", attribute: "src" }; 51 let embed = { element: "embed", attribute: "src" }; 52 let object = { element: "object", attribute: "data" }; 53 54 async function checkImage(browser) { 55 let pids = getPids(browser); 56 is(pids.length, 2, "There should be two browsing contexts"); 57 ok(pids[0], "The first pid should have a sane value"); 58 ok(pids[1], "The second pid should have a sane value"); 59 isnot(pids[0], pids[1], "The two pids should be different"); 60 61 let images = []; 62 for (let context of browser.browsingContext.children) { 63 images.push( 64 await SpecialPowers.spawn(context, [], async () => { 65 let img = new URL(content.document.querySelector("img").src); 66 is( 67 `${img.protocol}//${img.host}`, 68 `${content.location.protocol}//${content.location.host}`, 69 "Images should be loaded in the same domain as the document" 70 ); 71 return img.href; 72 }) 73 ); 74 } 75 isnot(images[0], images[1], "The images should have different sources"); 76 } 77 78 function checkDocument(browser) { 79 let pids = getPids(browser); 80 is(pids.length, 2, "There should be two browsing contexts"); 81 ok(pids[0], "The first pid should have a sane value"); 82 ok(pids[1], "The second pid should have a sane value"); 83 isnot(pids[0], pids[1], "The two pids should be different"); 84 } 85 86 add_task(async function test_iframeImageDocument() { 87 await runTest(iframe, TABURL, IMAGEURL, CROSSIMAGEURL, checkImage); 88 }); 89 90 add_task(async function test_embedImageDocument() { 91 await runTest(embed, TABURL, IMAGEURL, CROSSIMAGEURL, checkImage); 92 }); 93 94 add_task(async function test_objectImageDocument() { 95 await runTest(object, TABURL, IMAGEURL, CROSSIMAGEURL, checkImage); 96 }); 97 98 add_task(async function test_iframeDocument() { 99 await runTest(iframe, TABURL, DOCUMENTURL, CROSSDOCUMENTURL, checkDocument); 100 }); 101 102 add_task(async function test_embedDocument() { 103 await runTest(embed, TABURL, DOCUMENTURL, CROSSDOCUMENTURL, checkDocument); 104 }); 105 106 add_task(async function test_objectDocument() { 107 await runTest(object, TABURL, DOCUMENTURL, CROSSDOCUMENTURL, checkDocument); 108 });