Bug 1130686 - Add test for service worker client.focus. r=nsm

This commit is contained in:
Catalin Badea 2015-04-07 16:25:08 +03:00
Родитель 1046704caa
Коммит 3c89238807
4 изменённых файлов: 145 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,15 @@
onmessage = function(e) {
if (!e.source) {
dump("ERROR: message doesn't have a source.");
}
// The client should be a window client
if (e.source instanceof WindowClient) {
// this will dispatch a focus event on the client
e.source.focus().then(function(client) {
client.postMessage(client.focused);
});
} else {
dump("ERROR: client should be a WindowClient");
}
};

Просмотреть файл

@ -17,6 +17,7 @@ support-files =
workerUpdate/update.html
sw_clients/simple.html
sw_clients/service_worker_controlled.html
sw_clients/focus_stealing_client.html
match_all_worker.js
match_all_advanced_worker.js
worker_unregister.js
@ -63,6 +64,7 @@ support-files =
redirect_serviceworker.sjs
importscript.sjs
importscript_worker.js
client_focus_worker.js
[test_unregister.html]
[test_installation_simple.html]
@ -89,3 +91,4 @@ skip-if = true # Bug 1136780
[test_sandbox_intercept.html]
[test_request_context.html]
[test_importscript.html]
[test_client_focus.html]

Просмотреть файл

@ -0,0 +1,33 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1130686 - Test service worker client.focus: client </title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<!--
We load this as an iframe to blur the main test window.
-->
</head>
<body>/
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">
if (!parent) {
info("error: sw_clients/focus_stealing_client.html shouldn't be launched directly!");
}
window.onload = function() {
navigator.serviceWorker.ready.then(function() {
parent.postMessage("READY", "*");
});
}
</script>
</pre>
</body>
</html>

Просмотреть файл

@ -0,0 +1,94 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1130686 - Test service worker client.focus </title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<!--
This test checks that client.focus is able to restore focus to the main window
when an iframe is holding the focus.
-->
</head>
<body>
<p id="display"></p>
<div id="content"></div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">
var registration;
var worker;
function start() {
return navigator.serviceWorker.register("client_focus_worker.js",
{ scope: "./sw_clients/focus_stealing_client.html" })
.then((swr) => registration = swr);
}
function unregister() {
return registration.unregister().then(function(result) {
ok(result, "Unregister should return true.");
});
}
function loseFocus() {
var p = new Promise(function(res, rej) {
window.onmessage = function(e) {
if (e.data == "READY") {
ok(true, "iframe created.");
iframe.contentWindow.focus();
}
}
window.onblur = function() {
ok(true, "blurred");
res();
}
});
content = document.getElementById("content");
ok(content, "parent exists.");
iframe = document.createElement("iframe");
content.appendChild(iframe);
iframe.setAttribute('src', "sw_clients/focus_stealing_client.html");
return p;
}
function testFocus() {
var p = new Promise(function(res, rej) {
navigator.serviceWorker.onmessage = function(e) {
ok(e.data, "client object is marked as focused.");
ok(document.hasFocus(), "document has focus.");
res();
}
});
ok(registration.active, "active worker exists.");
registration.active.postMessage("go");
return p;
}
function runTest() {
start()
.then(loseFocus)
.then(testFocus)
.then(unregister)
.catch(function(e) {
ok(false, "Some test failed with error " + e);
}).then(SimpleTest.finish);
}
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true],
]}, runTest);
</script>
</pre>
</body>
</html>