1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // /////////////////////////////////////////////////////////////////////////
7 // Utilities for navigation tests
9 // /////////////////////////////////////////////////////////////////////////
11 var body = "This frame was navigated.";
12 var target_url = "navigation_target_url.html";
14 var popup_body = "This is a popup";
15 var target_popup_url = "navigation_target_popup_url.html";
17 // /////////////////////////////////////////////////////////////////////////
18 // Functions that navigate frames
19 // /////////////////////////////////////////////////////////////////////////
21 function navigateByLocation(wnd) {
23 wnd.location = target_url;
25 // We need to keep our finished frames count consistent.
26 // Oddly, this ends up simulating the behavior of IE7.
27 window.open(target_url, "_blank", "width=10,height=10");
31 function navigateByOpen(name) {
32 window.open(target_url, name, "width=10,height=10");
35 function navigateByForm(name) {
36 var form = document.createElement("form");
37 form.action = target_url;
40 document.body.appendChild(form);
44 var hyperlink_count = 0;
46 function navigateByHyperlink(name) {
47 var link = document.createElement("a");
48 link.href = target_url;
50 link.id = "navigation_hyperlink_" + hyperlink_count++;
51 document.body.appendChild(link);
52 sendMouseEvent({ type: "click" }, link.id);
55 // /////////////////////////////////////////////////////////////////////////
56 // Functions that call into Mochitest framework
57 // /////////////////////////////////////////////////////////////////////////
59 async function isNavigated(wnd, message) {
62 result = await SpecialPowers.spawn(wnd, [], () =>
63 this.content.document.body.innerHTML.trim()
68 is(result, body, message);
71 function isBlank(wnd, message) {
74 result = wnd.document.body.innerHTML.trim();
78 is(result, "This is a blank document.", message);
81 function isAccessible(wnd, message) {
83 wnd.document.body.innerHTML;
90 function isInaccessible(wnd, message) {
92 wnd.document.body.innerHTML;
99 function delay(msec) {
100 return new Promise(resolve => setTimeout(resolve, msec));
103 // /////////////////////////////////////////////////////////////////////////
104 // Functions that uses SpecialPowers.spawn
105 // /////////////////////////////////////////////////////////////////////////
107 async function waitForFinishedFrames(numFrames) {
108 SimpleTest.requestFlakyTimeout("Polling");
110 var finishedWindows = new Set();
112 async function searchForFinishedFrames(win) {
114 let { href, bodyText, readyState } = await SpecialPowers.spawn(
119 href: this.content.location.href,
121 this.content.document.body &&
122 this.content.document.body.textContent.trim(),
123 readyState: this.content.document.readyState,
129 (href.endsWith(target_url) || href.endsWith(target_popup_url)) &&
130 (bodyText == body || bodyText == popup_body) &&
131 readyState == "complete"
133 finishedWindows.add(SpecialPowers.getBrowsingContextID(win));
136 // This may throw if a frame is not fully initialized, in which
137 // case we'll handle it in a later iteration.
140 for (let i = 0; i < win.frames.length; i++) {
141 await searchForFinishedFrames(win.frames[i]);
145 while (finishedWindows.size < numFrames) {
148 for (let win of SpecialPowers.getGroupTopLevelWindows(window)) {
149 win = SpecialPowers.unwrap(win);
150 await searchForFinishedFrames(win);
154 if (finishedWindows.size > numFrames) {
155 throw new Error("Too many frames loaded.");
159 async function getFramesByName(name) {
161 for (let win of SpecialPowers.getGroupTopLevelWindows(window)) {
162 win = SpecialPowers.unwrap(win);
164 (await SpecialPowers.spawn(win, [], () => this.content.name)) === name
173 async function cleanupWindows() {
174 for (let win of SpecialPowers.getGroupTopLevelWindows(window)) {
175 win = SpecialPowers.unwrap(win);
182 href = await SpecialPowers.spawn(
186 this.content && this.content.location && this.content.location.href
189 // SpecialPowers.spawn(win, ...) throws if win is closed. We did
190 // our best to not call it on a closed window, but races happen.
198 (href.endsWith(target_url) || href.endsWith(target_popup_url))