Bug 1265795 P2 Add a web-platform-test for the window navigation case. r=bz

This commit is contained in:
Ben Kelly 2016-04-21 16:10:40 -07:00
parent e07a0186e8
commit d7ee43813a
4 changed files with 133 additions and 0 deletions

View File

@ -35276,6 +35276,12 @@
"url": "/html/webappapis/scripting/processing-model-2/window-onerror-with-cross-frame-event-listeners-4.html"
}
],
"service-workers/service-worker/navigate-window.https.html": [
{
"path": "service-workers/service-worker/navigate-window.https.html",
"url": "/service-workers/service-worker/navigate-window.https.html"
}
],
"web-animations/timing-model/active-time.html": [
{
"path": "web-animations/timing-model/active-time.html",

View File

@ -0,0 +1,97 @@
<!DOCTYPE html>
<title>Service Worker: Navigate a Window</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
var host_info = get_host_info();
var BASE_URL = host_info['HTTPS_ORIGIN'] + base_path();
function wait_for_message(msg) {
return new Promise(function(resolve, reject) {
window.addEventListener('message', function onMsg(evt) {
if (evt.data.type === msg) {
resolve();
}
});
});
}
function with_window(url) {
var win = window.open(url);
return wait_for_message('LOADED').then(_ => win);
}
function navigate_window(win, url) {
win.location = url;
return wait_for_message('LOADED').then(_ => win);
}
function go_back(win) {
win.history.back();
return wait_for_message('PAGESHOW').then(_ => win);
}
function go_forward(win) {
win.history.forward();
return wait_for_message('PAGESHOW').then(_ => win);
}
function get_clients(win, sw, opts) {
return new Promise((resolve, reject) => {
win.navigator.serviceWorker.addEventListener('message', function onMsg(evt) {
win.navigator.serviceWorker.removeEventListener('message', onMsg);
if (evt.data.type === 'success') {
resolve(evt.data.detail);
} else {
reject(evt.data.detail);
}
});
sw.postMessage({ type: 'GET_CLIENTS', opts: (opts || {}) });
});
}
function validate_window(win, url, opts) {
return win.navigator.serviceWorker.getRegistration(url)
.then(reg => {
// In order to compare service worker instances we need to
// make sure the DOM object is owned by the same global; the
// opened window in this case.
assert_equals(win.navigator.serviceWorker.controller, reg.active,
'window should be controlled by service worker');
return get_clients(win, reg.active);
})
.then(resultList => {
assert_equals(resultList.length, 1, 'there should only be one client');
assert_equals(resultList[0].url, url,
'client should be our opened window');
assert_equals(resultList[0].frameType, 'auxiliary',
'window.open() should create a client with an auxiliary frame type');
return win;
})
}
async_test(function(t) {
var worker = BASE_URL + 'resources/navigate-window-worker.js';
var scope = BASE_URL + 'resources/loaded.html?navigate-window';
var url1 = scope + '&q=1';
var url2 = scope + '&q=2';
service_worker_unregister_and_register(t, worker, scope)
.then(reg => wait_for_state(t, reg.installing, 'activated') )
.then(___ => with_window(url1))
.then(win => validate_window(win, url1, { includeUncontrolled: false }))
.then(win => navigate_window(win, url2))
.then(win => validate_window(win, url2, { includeUncontrolled: false }))
.then(win => go_back(win))
.then(win => validate_window(win, url1, { includeUncontrolled: false }))
.then(win => go_forward(win))
.then(win => validate_window(win, url2, { includeUncontrolled: false }))
.then(win => win.close())
.catch(unreached_rejection(t))
.then(___ => service_worker_unregister_and_done(t, scope))
}, 'Clients.matchAll() should not show an old window as controlled after ' +
'it navigates.');
</script>
</body>

View File

@ -0,0 +1,9 @@
<script>
addEventListener('load', function() {
opener.postMessage({ type: 'LOADED' }, '*');
});
addEventListener('pageshow', function() {
opener.postMessage({ type: 'PAGESHOW' }, '*');
});
</script>

View File

@ -0,0 +1,21 @@
addEventListener('message', function(evt) {
if (evt.data.type === 'GET_CLIENTS') {
clients.matchAll(evt.data.opts).then(function(clientList) {
var resultList = clientList.map(function(c) {
return { url: c.url, frameType: c.frameType, id: c.id };
});
evt.source.postMessage({ type: 'success', detail: resultList });
}).catch(function(err) {
evt.source.postMessage({
type: 'failure',
detail: 'matchAll() rejected with "' + err + '"'
});
});
return;
}
evt.source.postMessage({
type: 'failure',
detail: 'Unexpected message type "' + evt.data.type + '"'
});
});