Skip to content

Commit

Permalink
Refine wpt/service-workers/service-worker/clients-get.https.html
Browse files Browse the repository at this point in the history
Used async functions, arrow functions, and const, and added some comments.

Bug: 778497
Change-Id: I03f23c6adddbb0e317edb197dc8af197597b2381
Reviewed-on: https://chromium-review.googlesource.com/c/1343416
Commit-Queue: Makoto Shimazu <[email protected]>
Reviewed-by: Matt Falkenhagen <[email protected]>
Cr-Commit-Position: refs/heads/master@{#609693}
  • Loading branch information
makotoshimazu authored and chromium-wpt-export-bot committed Nov 21, 2018
1 parent d5dcb9f commit 0ce9a88
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 265 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
var shared_worker_url = scope + '-shared-worker.js';
var worker_url = scope + '-worker.js';
var client_ids = [];
var registration;
var frame;
promise_test(function(t) {
return service_worker_unregister_and_register(
t, 'resources/clients-get-worker.js', scope)
.then(function(registration) {
.then(function(r) {
registration = r;
add_completion_callback(function() { registration.unregister(); });
return wait_for_state(t, registration.installing, 'activated');
})
Expand Down Expand Up @@ -59,12 +61,10 @@
})
.then(function(client_id) {
client_ids.push(client_id);
var channel = new MessageChannel();
var saw_message = new Promise(function(resolve) {
channel.port1.onmessage = resolve;
navigator.serviceWorker.onmessage = resolve;
});
frame.contentWindow.navigator.serviceWorker.controller.postMessage(
{port: channel.port2, clientIds: client_ids}, [channel.port2]);
registration.active.postMessage({clientIds: client_ids});
return saw_message;
})
.then(function(e) {
Expand Down
348 changes: 139 additions & 209 deletions service-workers/service-worker/clients-get.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,221 +4,151 @@
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
var scope = 'resources/clients-get-frame.html';
var client_ids = [];
var frame;
promise_test(function(t) {
return service_worker_unregister_and_register(
t, 'resources/clients-get-worker.js', scope)
.then(function(registration) {
add_completion_callback(function() { registration.unregister(); });
return wait_for_state(t, registration.installing, 'activated');
})
.then(function() {
return with_iframe(scope + '#1');
})
.then(function(frame1) {
add_completion_callback(function() { frame1.remove(); });
frame1.focus();
return wait_for_clientId();
})
.then(function(client_id) {
client_ids.push(client_id);
return with_iframe(scope + '#2');
})
.then(function(frame2) {
frame = frame2;
add_completion_callback(function() { frame2.remove(); });
return wait_for_clientId();
})
.then(function(client_id) {
client_ids.push(client_id, 'invalid-id');
var channel = new MessageChannel();
var saw_message = new Promise(function(resolve) {
channel.port1.onmessage = resolve;
});
frame.contentWindow.navigator.serviceWorker.controller.postMessage(
{port: channel.port2, clientIds: client_ids}, [channel.port2]);
return saw_message;
})
.then(function(e) {
assert_equals(e.data.length, 3);
assert_array_equals(e.data[0], expected[0]);
assert_array_equals(e.data[1], expected[1]);
assert_equals(e.data[2], expected[2]);
});
}, 'Test Clients.get()');

promise_test((t) => {
let frame = null;
const scope = 'resources/simple.html';
const outerSwContainer = navigator.serviceWorker;
let innerSwReg = null;
let innerSw = null;

return service_worker_unregister_and_register(
t, 'resources/clients-get-resultingClientId-worker.js', scope)
.then((registration) => {
innerSwReg = registration;
add_completion_callback(function() { registration.unregister(); });
return wait_for_state(t, registration.installing, 'activated');
})
.then(() => {
// load frame and get resulting client id
let channel = new MessageChannel();
innerSw = innerSwReg.active;

let p = new Promise(resolve => {
function getResultingClientId(e) {
if (e.data.msg == 'getResultingClientId') {
const { resultingClientId } = e.data;

channel.port1.removeEventListener('message', getResultingClientId);

resolve({ resultingClientId, port: channel.port1 });
}
}

channel.port1.onmessage = getResultingClientId;
});


return with_iframe(scope).then((iframe) => {
innerSw.postMessage(
{ port: channel.port2, msg: 'getResultingClientId' },
[channel.port2],
);

frame = iframe;
frame.focus();
add_completion_callback(() => iframe.remove());

return p;
});
})
.then(({ resultingClientId, port }) => {
// query service worker for clients.get(resultingClientId)
let channel = new MessageChannel();

let p = new Promise(resolve => {
function getIsResultingClientUndefined(e) {
if (e.data.msg == 'getIsResultingClientUndefined') {
let { isResultingClientUndefined } = e.data;

port.removeEventListener('message', getIsResultingClientUndefined);

resolve(isResultingClientUndefined);
}
}

port.onmessage = getIsResultingClientUndefined;
});

innerSw.postMessage(
{ port: channel.port2, msg: 'getIsResultingClientUndefined', resultingClientId },
[channel.port2],
);

return p;
})
.then((isResultingClientUndefined) => {
assert_false(isResultingClientUndefined, 'Clients.get(FetchEvent.resultingClientId) resolved with a Client');
});
}, 'Test successful Clients.get(FetchEvent.resultingClientId)');

promise_test((t) => {
const scope = 'resources/simple.html?fail';
const outerSwContainer = navigator.serviceWorker;
let innerSwReg = null;
let innerSw = null;

return service_worker_unregister_and_register(
t, 'resources/clients-get-resultingClientId-worker.js', scope)
.then((registration) => {
innerSwReg = registration;
add_completion_callback(function() { registration.unregister(); });
return wait_for_state(t, registration.installing, 'activated');
})
.then(() => {
// load frame, destroying it while loading, and get resulting client id
innerSw = innerSwReg.active;

let iframe = document.createElement('iframe');
iframe.className = 'test-iframe';
iframe.src = scope;

function destroyIframe(e) {
if (e.data.msg == 'destroyResultingClient') {
iframe.remove();
iframe = null;

innerSw.postMessage({ msg: 'resultingClientDestroyed' });
}
}

outerSwContainer.addEventListener('message', destroyIframe);

let p = new Promise(resolve => {
function resultingClientDestroyedAck(e) {
if (e.data.msg == 'resultingClientDestroyedAck') {
let { resultingDestroyedClientId } = e.data;

outerSwContainer.removeEventListener('message', resultingClientDestroyedAck);
resolve(resultingDestroyedClientId);
}
}

outerSwContainer.addEventListener('message', resultingClientDestroyedAck);
});

document.body.appendChild(iframe);

return p;
})
.then((resultingDestroyedClientId) => {
// query service worker for clients.get(resultingDestroyedClientId)
let channel = new MessageChannel();

let p = new Promise((resolve, reject) => {
function getIsResultingClientUndefined(e) {
if (e.data.msg == 'getIsResultingClientUndefined') {
let { isResultingClientUndefined } = e.data;

channel.port1.removeEventListener('message', getIsResultingClientUndefined);

resolve(isResultingClientUndefined);
}
}

channel.port1.onmessage = getIsResultingClientUndefined;
});

innerSw.postMessage(
{ port: channel.port2, msg: 'getIsResultingClientUndefined', resultingClientId: resultingDestroyedClientId },
[channel.port2],
);

return p;
})
.then((isResultingClientUndefined) => {
assert_true(isResultingClientUndefined, 'Clients.get(FetchEvent.resultingClientId) resolved with `undefined`');
});
}, 'Test unsuccessful Clients.get(FetchEvent.resultingClientId)');

function wait_for_clientId() {
return new Promise(function(resolve, reject) {
function get_client_id(e) {
window.removeEventListener('message', get_client_id);
resolve(e.data.clientId);
}
window.addEventListener('message', get_client_id, false);
});
window.onmessage = e => {
resolve(e.data.clientId);
};
});
}

var expected = [
promise_test(async t => {
// Register service worker.
const scope = 'resources/clients-get-frame.html';
const client_ids = [];
const registration = await service_worker_unregister_and_register(
t, 'resources/clients-get-worker.js', scope);
t.add_cleanup(() => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');

// Prepare for test cases.
// Case 1: frame1 which is focused.
const frame1 = await with_iframe(scope + '#1');
t.add_cleanup(() => frame1.remove());
frame1.focus();
client_ids.push(await wait_for_clientId());
// Case 2: frame2 which is not focused.
const frame2 = await with_iframe(scope + '#2');
t.add_cleanup(() => frame2.remove());
client_ids.push(await wait_for_clientId());
// Case 3: invalid id.
client_ids.push('invalid-id');

// Call clients.get() for each id on the service worker.
const message_event = await new Promise(resolve => {
navigator.serviceWorker.onmessage = resolve;
registration.active.postMessage({clientIds: client_ids});
});

const expected = [
// visibilityState, focused, url, type, frameType
['visible', true, normalizeURL(scope) + '#1', 'window', 'nested'],
['visible', false, normalizeURL(scope) + '#2', 'window', 'nested'],
undefined
];
];
assert_equals(message_event.data.length, 3);
assert_array_equals(message_event.data[0], expected[0]);
assert_array_equals(message_event.data[1], expected[1]);
assert_equals(message_event.data[2], expected[2]);
}, 'Test Clients.get()');

promise_test(async t => {
// Register service worker.
const scope = 'resources/simple.html';
const registration = await service_worker_unregister_and_register(
t, 'resources/clients-get-resultingClientId-worker.js', scope)
t.add_cleanup(() => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');
const worker = registration.active;

// Load frame within the scope.
const frame = await with_iframe(scope);
t.add_cleanup(() => frame.remove());
frame.focus();

// Get resulting client id.
const resultingClientId = await new Promise(resolve => {
navigator.serviceWorker.onmessage = e => {
if (e.data.msg == 'getResultingClientId') {
resolve(e.data.resultingClientId);
}
};
worker.postMessage({msg: 'getResultingClientId'});
});

// Query service worker for clients.get(resultingClientId).
const isResultingClientUndefined = await new Promise(resolve => {
navigator.serviceWorker.onmessage = e => {
if (e.data.msg == 'getIsResultingClientUndefined') {
resolve(e.data.isResultingClientUndefined);
}
};
worker.postMessage({msg: 'getIsResultingClientUndefined',
resultingClientId});
});

assert_false(
isResultingClientUndefined,
'Clients.get(FetchEvent.resultingClientId) resolved with a Client');
}, 'Test successful Clients.get(FetchEvent.resultingClientId)');

promise_test(async t => {
// Register service worker.
const scope = 'resources/simple.html?fail';
const registration = await service_worker_unregister_and_register(
t, 'resources/clients-get-resultingClientId-worker.js', scope);
t.add_cleanup(() => registration.unregister());
await wait_for_state(t, registration.installing, 'activated');

// Load frame, and destroy it while loading.
const worker = registration.active;
let frame = document.createElement('iframe');
frame.src = scope;
t.add_cleanup(() => {
if (frame) {
frame.remove();
}
});

await new Promise(resolve => {
navigator.serviceWorker.onmessage = e => {
// The service worker posts a message to remove the iframe during fetch
// event.
if (e.data.msg == 'destroyResultingClient') {
frame.remove();
frame = null;
worker.postMessage({msg: 'resultingClientDestroyed'});
resolve();
}
};
document.body.appendChild(frame);
});

resultingDestroyedClientId = await new Promise(resolve => {
navigator.serviceWorker.onmessage = e => {
// The worker sends a message back when it receives the message
// 'resultingClientDestroyed' with the resultingClientId.
if (e.data.msg == 'resultingClientDestroyedAck') {
assert_equals(frame, null, 'Frame should be destroyed at this point.');
resolve(e.data.resultingDestroyedClientId);
}
};
});

// Query service worker for clients.get(resultingDestroyedClientId).
const isResultingClientUndefined = await new Promise(resolve => {
navigator.serviceWorker.onmessage = e => {
if (e.data.msg == 'getIsResultingClientUndefined') {
resolve(e.data.isResultingClientUndefined);
}
};
worker.postMessage({msg: 'getIsResultingClientUndefined',
resultingClientId: resultingDestroyedClientId });
});

assert_true(
isResultingClientUndefined,
'Clients.get(FetchEvent.resultingClientId) resolved with `undefined`');
}, 'Test unsuccessful Clients.get(FetchEvent.resultingClientId)');

</script>
Loading

0 comments on commit 0ce9a88

Please sign in to comment.