Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(630)

Unified Diff: LayoutTests/http/tests/serviceworker/resources/install-phase-event-waituntil.js

Issue 352423005: Add ServiceWorker InstallPhaseEvent.waitUntil() layout test. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: review update Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: LayoutTests/http/tests/serviceworker/resources/install-phase-event-waituntil.js
diff --git a/LayoutTests/http/tests/serviceworker/resources/install-phase-event-waituntil.js b/LayoutTests/http/tests/serviceworker/resources/install-phase-event-waituntil.js
new file mode 100644
index 0000000000000000000000000000000000000000..6738409b546347e309ede8db63232cc4b0b91663
--- /dev/null
+++ b/LayoutTests/http/tests/serviceworker/resources/install-phase-event-waituntil.js
@@ -0,0 +1,76 @@
+var pendingPorts = [];
+var portResolves = [];
+
+onmessage = function(e) {
+ var message = e.data;
+ if ('port' in message) {
+ var resolve = self.portResolves.shift();
+ if (resolve)
+ resolve(message.port);
+ else
+ self.pendingPorts.push(message.port);
+ }
+};
+
+function fulfillPromise() {
+ return new Promise(function(resolve) {
+ // Make sure oninstall/onactivate callback finishes first.
jsbell 2014/07/01 16:16:48 Is it really necessary for this to run in a subseq
xiang 2014/07/02 05:10:42 Yes, microtask is sufficient here, thanks!
+ setTimeout(function() {
+ var port = self.pendingPorts.shift();
+ if (port)
+ resolve(port);
+ else
+ self.portResolves.push(resolve);
+ }, 0);
+ }).then(function(port) {
+ port.postMessage('SYNC');
+ return new Promise(function(resolve) {
+ port.onmessage = function(e) {
+ if (e.data == 'ACK')
+ resolve();
+ };
+ });
+ });
+}
+
+function rejectPromise() {
+ return new Promise(function(resolve, reject) {
+ setTimeout(reject, 0);
jsbell 2014/07/01 16:16:48 Add comment similar to the above, i.e. why `return
xiang 2014/07/02 05:10:42 will add.
+ });
+}
+
+function stripScopeName(scope) {
+ return scope.split('/').slice(-1)[0];
+}
+
+oninstall = function(e) {
+ switch (stripScopeName(self.scope)) {
+ case 'install-fulfilled':
+ e.waitUntil(fulfillPromise());
+ break;
+ case 'install-rejected':
+ e.waitUntil(rejectPromise());
+ break;
+ }
+};
+
+onactivate = function(e) {
+ switch (stripScopeName(self.scope)) {
+ case 'activate-fulfilled':
+ e.waitUntil(fulfillPromise());
+ break;
+ case 'activate-rejected':
+ e.waitUntil(rejectPromise());
+ break;
+ case 'activate-multiple-fulfilled':
+ e.waitUntil(fulfillPromise());
+ e.waitUntil(fulfillPromise());
+ break;
+ case 'activate-reject-precedence':
+ e.waitUntil(fulfillPromise());
+ e.waitUntil(new Promise(function(resolve, reject) {
jsbell 2014/07/01 16:16:48 Why not rejectPromise() here?
xiang 2014/07/02 05:10:42 Sure, I will change it.
+ setTimeout(reject, 0);
+ }));
+ break;
+ }
+};

Powered by Google App Engine
This is Rietveld 408576698