tenfourfox/dom/requestsync/tests/test_wakeUp.html
Cameron Kaiser c9b2922b70 hello FPR
2017-04-19 00:56:45 -07:00

188 lines
5.6 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for requestSync - wakeUp</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="common_basic.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<div id="container"></div>
<script type="application/javascript;version=1.7">
var oneShotCounter = 0;
var multiShotCounter = 0;
function maybeDone() {
if (oneShotCounter == 1 && multiShotCounter == 5) {
runTests();
}
}
function registerPage() {
var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('system_message_chrome_script.js'));
gScript.addMessageListener("page-registered", function pageRegisteredHandler() {
gScript.removeMessageListener("page-registered", pageRegisteredHandler);
gScript.destroy();
runTests();
});
gScript.sendAsyncMessage("trigger-register-page",
{ type: "request-sync",
manifestURL: window.location.origin + "/manifest.webapp",
pageURL: window.location.href });
}
function setMessageHandler() {
navigator.mozSetMessageHandler('request-sync', function(e) {
ok(true, "One event has been received!");
if (e.task == "oneShot") {
is(e.data, 42, "e.data is correct");
is(e.lastSync, 0, "e.lastSync is correct");
is(e.oneShot, true, "e.oneShot is correct");
is(e.minInterval, 2, "e.minInterval is correct");
is(e.wifiOnly, false, "e.wifiOnly is correct");
is(++oneShotCounter, 1, "Only 1 shot should be received here");
maybeDone();
}
else if (e.task == "multiShots") {
is(e.data, 'hello world!', "e.data is correct");
if (multiShotCounter == 0) {
is(e.lastSync, 0, "e.lastSync is correct");
} else {
isnot(e.lastSync, 0, "e.lastSync is correct");
}
is(e.oneShot, false, "e.oneShot is correct");
is(e.minInterval, 3, "e.minInterval is correct");
is(e.wifiOnly, false, "e.wifiOnly is correct");
++multiShotCounter;
if (multiShotCounter == 1) {
info("Setting a promise object.");
navigator.mozSetMessageHandlerPromise(new Promise(function(a, b) {
setTimeout(a, 0);
}));
} else if (multiShotCounter == 2) {
// The second time we don't reply at all.
info("Setting a promise object without resolving it.");
navigator.mozSetMessageHandlerPromise(new Promise(function(a, b) {}));
} else if (multiShotCounter == 3) {
info("Throwing an exception.");
// Now we throw an exception
SimpleTest.expectUncaughtException();
throw "Booom!";
} else {
info("Setting a promise object and reject it.");
navigator.mozSetMessageHandlerPromise(new Promise(function(a, b) {
setTimeout(b, 0);
}));
}
maybeDone();
}
else {
ok(false, "Unknown event has been received!");
}
});
runTests();
}
function test_register_oneShot() {
navigator.sync.register('oneShot', { minInterval: 2,
oneShot: true,
data: 42,
wifiOnly: false,
wakeUpPage: location.href }).then(
function() {
ok(true, "navigator.sync.register() oneShot done");
runTests();
}, genericError);
}
function test_register_multiShots() {
navigator.sync.register('multiShots', { minInterval: 3,
oneShot: false,
data: 'hello world!',
wifiOnly: false,
wakeUpPage: location.href }).then(
function() {
ok(true, "navigator.sync.register() multiShots done");
runTests();
}, genericError);
}
function test_unregister_oneShot() {
navigator.sync.unregister('oneShot').then(
function() {
ok(true, "navigator.sync.unregister() oneShot done");
runTests();
}, genericError);
}
function test_unregister_multiShots() {
navigator.sync.unregister('multiShots').then(
function() {
ok(true, "navigator.sync.unregister() multiShots done");
runTests();
}, genericError);
}
function test_wait() {
// nothing to do here.
}
var tests = [
function() {
SpecialPowers.pushPrefEnv({"set": [["dom.requestSync.enabled", true],
["dom.requestSync.minInterval", 1],
["dom.requestSync.maxTaskTimeout", 10000 /* 10 seconds */],
["dom.ignore_webidl_scope_checks", true]]}, runTests);
},
function() {
SpecialPowers.pushPermissions(
[{ "type": "requestsync-manager", "allow": 1, "context": document } ], runTests);
},
function() {
SpecialPowers.importInMainProcess("resource://gre/modules/RequestSyncService.jsm");
runTests();
},
registerPage,
setMessageHandler,
test_register_oneShot,
test_register_multiShots,
test_wait,
test_unregister_oneShot,
test_unregister_multiShots,
];
function runTests() {
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
SimpleTest.waitForExplicitFinish();
runTests();
</script>
</body>
</html>