gecko-dev/dom/workers/test/test_suspend.html

135 строки
3.3 KiB
HTML

<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for DOM Worker Threads</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
const BLANK_URI = location.href.replace("test_suspend.html", "blank.html");
var lastCount;
var suspended = false;
var resumed = false;
var finished = false;
var interval;
var oldMessageCount;
var waitCount = 0;
var testWin = window.open("suspend_window.html", "testWin");
testWin.onload = testWinLoaded;
window.addEventListener("message", msg => {
if (suspended) {
badOnloadCallback();
} else {
suspendCallback();
}
})
function finishTest() {
if (finished) {
return;
}
finished = true;
testWin.terminateWorker();
testWin.close();
SimpleTest.finish();
}
function waitInterval() {
if (finished) {
return;
}
is(testWin.location.href, BLANK_URI, "Wrong url!");
is(suspended, true, "Not suspended?");
is(resumed, false, "Already resumed?!");
is(lastCount, oldMessageCount, "Received a message while suspended!");
if (++waitCount == 5) {
clearInterval(interval);
resumed = true;
testWin.history.back();
}
}
function badOnloadCallback() {
if (finished) {
return;
}
ok(false, "We don't want suspend_window.html to fire a new load event, we want it to come out of the bfcache!");
finishTest();
}
function suspendCallback() {
if (finished) {
return;
}
is(testWin.location.href, BLANK_URI, "Wrong url!");
is(suspended, false, "Already suspended?");
is(resumed, false, "Already resumed?");
suspended = true;
oldMessageCount = lastCount;
interval = setInterval(waitInterval, 1000);
}
function messageCallback(data) {
if (finished) {
return;
}
if (!suspended) {
ok(lastCount === undefined || lastCount == data - 1,
"Got good data, lastCount = " + lastCount + ", data = " + data);
lastCount = data;
if (lastCount == 25) {
testWin.location = "blank.html";
// We want suspend_window.html to go into bfcache, so we need to flush
// out all pending notifications. Otherwise, if they're flushed too
// late, they could kick us out of the bfcache again.
testWin.document.body.offsetTop;
}
return;
}
var newLocation = location.href.replace("test_suspend.html",
"suspend_window.html");
is(testWin.location.href, newLocation, "Wrong url!");
is(resumed, true, "Got message before resumed!");
is(lastCount, data - 1, "Missed a message, suspend failed!");
finishTest();
}
function errorCallback(data) {
if (finished) {
return;
}
ok(false, "testWin had an error: '" + data + "'");
finishTest();
}
function testWinLoaded() {
if (finished) {
return;
}
testWin.startWorker(messageCallback, errorCallback);
}
</script>
</pre>
</body>
</html>