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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const URL = "about:blank";
async function getBrowsingContextId(browser, id) {
return SpecialPowers.spawn(browser, [id], async function(id) {
let contextId = content.window.docShell.browsingContext.id;
let frames = [content.window];
while (frames.length) {
let frame = frames.pop();
let target = frame.document.getElementById(id);
if (target) {
contextId = target.docShell.browsingContext.id;
break;
}
frames = frames.concat(Array.from(frame.frames));
}
return contextId;
});
}
async function addFrame(browser, id, parentId) {
return SpecialPowers.spawn(browser, [{ parentId, id }], async function({
parentId,
id,
}) {
let parent = null;
if (parentId) {
let frames = [content.window];
while (frames.length) {
let frame = frames.pop();
let target = frame.document.getElementById(parentId);
if (target) {
parent = target.contentWindow.document.body;
break;
}
frames = frames.concat(Array.from(frame.frames));
}
} else {
parent = content.document.body;
}
let frame = await new Promise(resolve => {
let frame = content.document.createElement("iframe");
frame.id = id || "";
frame.url = "about:blank";
frame.onload = () => resolve(frame);
parent.appendChild(frame);
});
return frame.contentWindow.docShell.browsingContext.id;
});
}
async function removeFrame(browser, id) {
return SpecialPowers.spawn(browser, [id], async function(id) {
let frames = [content.window];
while (frames.length) {
let frame = frames.pop();
let target = frame.document.getElementById(id);
if (target) {
target.remove();
break;
}
frames = frames.concat(Array.from(frame.frames));
}
});
}
function getBrowsingContextById(id) {
return BrowsingContext.get(id);
}
add_task(async function() {
await BrowserTestUtils.withNewTab({ gBrowser, url: URL }, async function(
browser
) {
let topId = await getBrowsingContextId(browser, "");
let topContext = getBrowsingContextById(topId);
isnot(topContext, null);
is(topContext.parent, null);
is(
topId,
browser.browsingContext.id,
"<browser> has the correct browsingContext"
);
is(
browser.browserId,
topContext.browserId,
"browsing context should have a correct <browser> id"
);
let id0 = await addFrame(browser, "frame0");
let browsingContext0 = getBrowsingContextById(id0);
isnot(browsingContext0, null);
is(browsingContext0.parent, topContext);
await removeFrame(browser, "frame0");
is(topContext.children.indexOf(browsingContext0), -1);
// TODO(farre): Handle browsingContext removal [see Bug 1486719].
todo_isnot(browsingContext0.parent, topContext);
});
});
add_task(async function() {
await BrowserTestUtils.withNewTab(
{
gBrowser,
url:
getRootDirectory(gTestPath).replace(
"chrome://mochitests/content",
"http://example.com"
) + "dummy_page.html",
},
async function(browser) {
let path = getRootDirectory(gTestPath).replace(
"chrome://mochitests/content",
"http://example.com"
);
await SpecialPowers.spawn(browser, [path], async function(path) {
function waitForMessage(command) {
let r;
let p = new Promise(resolve => {
content.window.addEventListener(
"message",
e => resolve({ result: r, event: e }),
{ once: true }
);
});
r = command();
return p;
}
// Open a new window and wait for the message.
let { result: win, event: e1 } = await waitForMessage(_ =>
content.window.open(path + "onpageshow_message.html")
);
is(e1.data, "pageshow");
{
// Create, attach and load an iframe into the window's document.
let frame = win.document.createElement("iframe");
win.document.body.appendChild(frame);
frame.src = "dummy_page.html";
await ContentTaskUtils.waitForEvent(frame, "load");
}
is(win.frames.length, 1, "Here we should have an iframe");
// The frame should have expected browsing context and docshell.
let frameBC = win.frames[0].docShell.browsingContext;
let winDocShell = win.frames[0].docShell;
// Navigate the window and wait for the message.
let { event: e2 } = await waitForMessage(
_ => (win.location = path + "onload_message.html")
);
is(e2.data, "load");
is(win.frames.length, 0, "Here there shouldn't be an iframe");
// Return to the previous document. N.B. we expect to trigger
// BFCache here, hence we wait for pageshow.
let { event: e3 } = await waitForMessage(_ => win.history.back());
is(e3.data, "pageshow");
is(win.frames.length, 1, "And again there should be an iframe");
is(winDocShell, win.frames[0].docShell, "BF cache cached docshell");
is(
frameBC,
win.frames[0].docShell.browsingContext,
"BF cache cached BC"
);
is(
frameBC.id,
win.frames[0].docShell.browsingContext.id,
"BF cached BC's have same id"
);
is(
win.docShell.browsingContext.children[0],
frameBC,
"BF cached BC's should still be a child of its parent"
);
is(
win.docShell.browsingContext,
frameBC.parent,
"BF cached BC's should still be a connected to its parent"
);
win.close();
});
}
);
});
|