-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathhelpers.js
67 lines (56 loc) · 2.45 KB
/
helpers.js
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
window.waitForLoad = (t, iframe, urlRelativeToThisDocument) => {
return new Promise(resolve => {
iframe.addEventListener("load", t.step_func(() => {
assert_equals(iframe.contentWindow.location.href, (new URL(urlRelativeToThisDocument, location.href)).href);
// Wait a bit longer to ensure all history stuff has settled, e.g. the document is "completely loaded"
// (which happens from a queued task).
setTimeout(resolve, 0);
}), { once: true });
});
};
window.waitForLoadAllowingIntermediateLoads = (t, iframe, urlRelativeToThisDocument) => {
return new Promise(resolve => {
const handler = t.step_func(() => {
if (iframe.contentWindow.location.href === (new URL(urlRelativeToThisDocument, location.href)).href) {
// Wait a bit longer to ensure all history stuff has settled, e.g. the document is "completely loaded"
// (which happens from a queued task).
setTimeout(resolve, 0);
iframe.removeEventListener("load", handler);
}
});
iframe.addEventListener("load", handler);
});
};
window.waitForMessage = (t, expected) => {
return new Promise(resolve => {
window.addEventListener("message", t.step_func(e => {
assert_equals(e.data, expected);
resolve();
}), { once: true });
});
};
window.setupSentinelIframe = async (t) => {
// If this iframe gets navigated by history.back(), then the iframe under test did not, so we did a replace.
const sentinelIframe = document.createElement("iframe");
sentinelIframe.src = "/common/blank.html?sentinelstart";
document.body.append(sentinelIframe);
t.add_cleanup(() => sentinelIframe.remove());
await waitForLoad(t, sentinelIframe, "/common/blank.html?sentinelstart");
sentinelIframe.src = "/common/blank.html?sentinelend";
await waitForLoad(t, sentinelIframe, "/common/blank.html?sentinelend");
return sentinelIframe;
};
window.checkSentinelIframe = async (t, sentinelIframe) => {
// Go back. Since iframe should have done a replace, this should move sentinelIframe back, not iframe.
history.back();
await waitForLoad(t, sentinelIframe, "/common/blank.html?sentinelstart");
};
window.insertIframe = (t, url) => {
const iframe = document.createElement("iframe");
iframe.src = url;
document.body.append(iframe);
// Intentionally not including the following:
// t.add_cleanup(() => iframe.remove());
// Doing so breaks some of the testdriver.js tests with "cannot find window" errors.
return iframe;
};