Bug 1265771 P3 Expand browser_force_refresh.js to verify Clients.matchAll() behavior on refresh. r=bz

This commit is contained in:
Ben Kelly 2016-04-22 00:50:13 -07:00
Родитель 9216655d15
Коммит da6964ef2b
3 изменённых файлов: 76 добавлений и 9 удалений

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

@ -8,9 +8,56 @@
</head>
<body>
<script type="text/javascript">
addEventListener('load', function(event) {
var custom = new Event('cached-load', { bubbles: true });
function ok(exp, msg) {
if (!exp) {
throw(msg);
}
}
function is(actual, expected, msg) {
if (actual !== expected) {
throw('got "' + actual + '", but expected "' + expected + '" - ' + msg);
}
}
function fail(err) {
var custom = new CustomEvent('cached-failure', {
bubbles: true,
detail: err
});
document.dispatchEvent(custom);
}
function getUncontrolledClients(sw) {
return new Promise(function(resolve, reject) {
navigator.serviceWorker.addEventListener('message', function onMsg(evt) {
if (evt.data.type === 'CLIENTS') {
navigator.serviceWorker.removeEventListener('message', onMsg);
resolve(evt.data.detail);
}
});
sw.postMessage({ type: 'GET_UNCONTROLLED_CLIENTS' })
});
}
addEventListener('load', function(event) {
if (!navigator.serviceWorker.controller) {
return fail(window.location.href + ' is not controlled!');
}
getUncontrolledClients(navigator.serviceWorker.controller)
.then(function(clientList) {
is(clientList.length, 1, 'should only have one client');
is(clientList[0].url, window.location.href,
'client url should match current window');
is(clientList[0].frameType, 'top-level',
'client should be a top-level window');
var custom = new Event('cached-load', { bubbles: true });
document.dispatchEvent(custom);
})
.catch(function(err) {
fail(err);
});
});
</script>
</body>

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

@ -14,7 +14,7 @@ function forceRefresh() {
function frameScript() {
function eventHandler(event) {
sendAsyncMessage("test:event", {type: event.type});
sendAsyncMessage("test:event", {type: event.type, detail: event.detail});
}
// These are tab-local, so no need to unregister them.
@ -22,6 +22,7 @@ function frameScript() {
addEventListener('base-register', eventHandler, true, true);
addEventListener('base-sw-ready', eventHandler, true, true);
addEventListener('cached-load', eventHandler, true, true);
addEventListener('cached-failure', eventHandler, true, true);
}
function test() {
@ -47,13 +48,14 @@ function test() {
executeSoon(finish);
}
var cachedLoad = false;
var maxCacheLoadCount = 3;
var cachedLoadCount = 0;
var baseLoadCount = 0;
function eventHandler(msg) {
if (msg.data.type === 'base-load') {
baseLoadCount += 1;
if (cachedLoad) {
if (cachedLoadCount === maxCacheLoadCount) {
is(baseLoadCount, 2, 'cached load should occur before second base load');
return done();
}
@ -62,17 +64,23 @@ function test() {
return done();
}
} else if (msg.data.type === 'base-register') {
ok(!cachedLoad, 'cached load should not occur before base register');
ok(!cachedLoadCount, 'cached load should not occur before base register');
is(baseLoadCount, 1, 'register should occur after first base load');
} else if (msg.data.type === 'base-sw-ready') {
ok(!cachedLoad, 'cached load should not occur before base ready');
ok(!cachedLoadCount, 'cached load should not occur before base ready');
is(baseLoadCount, 1, 'ready should occur after first base load');
refresh();
} else if (msg.data.type === 'cached-load') {
ok(!cachedLoad, 'cached load should not occur twice');
ok(cachedLoadCount < maxCacheLoadCount, 'cached load should not occur too many times');
is(baseLoadCount, 1, 'cache load occur after first base load');
cachedLoad = true;
cachedLoadCount += 1;
if (cachedLoadCount < maxCacheLoadCount) {
return refresh();
}
forceRefresh();
} else if (msg.data.type === 'cached-failure') {
ok(false, 'failure: ' + msg.data.detail);
done();
}
return;

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

@ -20,3 +20,15 @@ self.addEventListener('fetch', function (event) {
})
);
});
self.addEventListener('message', function (event) {
if (event.data.type === 'GET_UNCONTROLLED_CLIENTS') {
event.waitUntil(clients.matchAll({ includeUncontrolled: true })
.then(function(clientList) {
var resultList = clientList.map(function(c) {
return { url: c.url, frameType: c.frameType };
});
event.source.postMessage({ type: 'CLIENTS', detail: resultList });
}));
}
});