Chromium Code Reviews| Index: LayoutTests/http/tests/serviceworker/resources/support.js | 
| diff --git a/LayoutTests/http/tests/serviceworker/resources/support.js b/LayoutTests/http/tests/serviceworker/resources/support.js | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..2da0a354615c668c6970ddc1688f8d97645a6067 | 
| --- /dev/null | 
| +++ b/LayoutTests/http/tests/serviceworker/resources/support.js | 
| @@ -0,0 +1,65 @@ | 
| +// Service Worker helpers for testharness.js-based tests. | 
| + | 
| +// Use with unexpected event handlers or Promise rejection. Will | 
| +// fail the test with a name/message is present on the error. | 
| +// E.g.: | 
| +// onbadevent = fail(t, 'Should only see good events'); | 
| +// Promise.then(...).catch(fail(t, 'Rejection is never fun')); | 
| +function fail(test, desc) { | 
| + desc = desc || 'Failure'; | 
| + return test.step_func(function(reason) { | 
| + if (reason && 'name' in reason && 'message' in reason) { | 
| + assert_unreached(desc + ': ' + reason.name + ' - ' + reason.message); | 
| + } else if (reason && 'name' in reason) { | 
| + assert_unreached(desc + ': ' + reason.name); | 
| + } else { | 
| + assert_unreached(desc); | 
| + } | 
| + }); | 
| +} | 
| + | 
| +// Sends a message of {port, from} to the specified worker, and returns the | 
| +// port at the local end of the channel. | 
| +function sendMessagePort(worker, from) { | 
| + var messageChannel = new MessageChannel(); | 
| + worker.postMessage({from:from, port:messageChannel.port2}, [messageChannel.port2]); | 
| + return messageChannel.port1; | 
| +} | 
| + | 
| + | 
| +function recordStateChanges(obj) { | 
| 
 
kinuko
2014/04/16 03:28:38
I think we can remove this for now, it's basically
 
jsbell
2014/04/16 17:04:19
Done.
 
 | 
| + obj = obj || {}; | 
| + | 
| + obj.activeChangeCount = 0; | 
| + obj.activeStateChangeCount = 0; | 
| + obj.pendingChangeCount = 0; | 
| + obj.pendingStateChangeCount = 0; | 
| + | 
| + if (!navigator.serviceWorker) | 
| + return obj; | 
| + | 
| + navigator.serviceWorker.onpendingchange = function() { | 
| + ++obj.pendingChangeCount; | 
| + setStateChangeCounter(navigator.serviceWorker.pending, | 
| + function() { ++obj.pendingStateChangeCount; }); | 
| + }; | 
| + | 
| + navigator.serviceWorker.onactivechange = function() { | 
| + ++obj.activeChangeCount; | 
| + setStateChangeCounter(navigator.serviceWorker.active, | 
| + function() { ++obj.activeStateChangeCount; }); | 
| + }; | 
| + | 
| + setStateChangeCounter(navigator.serviceWorker.pending, | 
| + function() { ++obj.pendingStateChangeCount; }); | 
| + | 
| + setStateChangeCounter(navigator.serviceWorker.active, | 
| + function() { ++obj.activeStateChangeCount; }); | 
| + | 
| + function setStateChangeCounter(worker, callback) { | 
| + if (worker) | 
| + worker.onstatechange = callback; | 
| + } | 
| + | 
| + return obj; | 
| +} |