gecko-dev/docshell/test/browser/browser_browsingContext-02.js
Andreas Farre cd40874733 Bug 1515646 - Test BrowsingContext::FindWithName. r=peterv
Test that BrowsingContext::FindWithName finds the expected
BrowsingContexts and also that BrowsingContext::FindWithName finds the
same browsing contexts as if nsIDocShellTreeItem::FindItemWithName had
been used.

Differential Revision: https://phabricator.services.mozilla.com/D15191

--HG--
rename : docshell/test/browser/browser_browsingContext.js => docshell/test/browser/browser_browsingContext-01.js
extra : moz-landing-system : lando
2019-02-15 11:17:50 +00:00

119 lines
4.3 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
add_task(async function() {
await BrowserTestUtils.withNewTab(
{gBrowser, url: "about:blank"}, async function(browser) {
const BASE1 = getRootDirectory(gTestPath)
.replace("chrome://mochitests/content", "http://example.com");
const BASE2 = getRootDirectory(gTestPath)
.replace("chrome://mochitests/content", "http://test1.example.com");
const URL = BASE1 + "onload_message.html";
let sixth = BrowserTestUtils.waitForNewTab(gBrowser, URL + "#sixth", true, true);
let seventh = BrowserTestUtils.waitForNewTab(gBrowser, URL + "#seventh", true, true);
await ContentTask.spawn(browser, {base1: BASE1, base2: BASE2},
async function({base1, base2}) {
let top = content.window;
top.name = 'top';
top.location.href += "#top";
let contexts = {
top: top.location.href,
first: base1 + "dummy_page.html#first",
third: base2 + "dummy_page.html#third",
second: base1 + "dummy_page.html#second",
fourth: base2 + "dummy_page.html#fourth",
fifth: base1 + "dummy_page.html#fifth",
sixth: base1 + "onload_message.html#sixth",
seventh: base1 + "onload_message.html#seventh"
};
function addFrame(target, name) {
let doc = (target.contentWindow || target).document;
let frame = doc.createElement('iframe');
let p = new Promise(resolve => (frame.onload = () =>resolve(frame)));
doc.body.appendChild(frame);
frame.name = name;
frame.src = contexts[name];
return p;
}
function addWindow(target, name, {options, resolve}) {
var win = target.contentWindow.open(contexts[name], name, options);
if (resolve) {
return new Promise(
resolve => target.contentWindow.addEventListener(
'message', () => resolve(win)));
} else {
return Promise.resolve({name: name});
}
}
// We're going to create a tree that looks like the
// following.
//
// top sixth seventh
// / \
// / \ /
// first second
// / \ /
// / \
// third fourth - - -
// /
// /
// fifth
//
// The idea is to have one top level non-auxiliary browsing
// context, five nested, one top level auxiliary with an
// opener, and one top level without an opener. Given that
// set of related and one unrelated browsing contexts we
// wish to confirm that targeting is able to find
// appropriate browsing contexts.
function bc(frame) {
return (frame.contentWindow || frame).docShell.browsingContext;
}
function reachable(start, targets) {
for (let target of targets) {
is(bc(start).findWithName(target.name), bc(target),
[bc(start).name, 'can reach', target.name].join(' '));
}
}
function unreachable(start, target) {
is(bc(start).findWithName(target.name), null,
[bc(start).name, "can't reach", target.name].join(' '));
}
let first = await addFrame(top, 'first');
info('first');
let second = await addFrame(top, 'second');
info('second');
let third = await addFrame(first, 'third');
info('third');
let fourth = await addFrame(first, 'fourth');
info('fourth');
let fifth = await addFrame(fourth, 'fifth');
info('fifth');
let sixth = await addWindow(fourth, 'sixth', { resolve: true });
info('sixth');
let seventh = await addWindow(fourth, 'seventh', { options: ['noopener'] });
info('seventh');
let frames = [top, first, second, third, fourth, fifth, sixth];
for (let start of frames) {
reachable(start, frames);
unreachable(start, seventh);
}
});
for (let tab of await Promise.all([sixth, seventh])) {
BrowserTestUtils.removeTab(tab);
}
});
});