OLD | NEW |
---|---|
(Empty) | |
1 // Service Worker helpers for testharness.js-based tests. | |
2 | |
3 // Use with unexpected event handlers or Promise rejection. Will | |
4 // fail the test with a name/message is present on the error. | |
5 // E.g.: | |
6 // onbadevent = fail(t, 'Should only see good events'); | |
7 // Promise.then(...).catch(fail(t, 'Rejection is never fun')); | |
8 function fail(test, desc) { | |
9 desc = desc || 'Failure'; | |
10 return test.step_func(function(reason) { | |
11 if (reason && 'name' in reason && 'message' in reason) { | |
12 assert_unreached(desc + ': ' + reason.name + ' - ' + reason.message) ; | |
13 } else if (reason && 'name' in reason) { | |
14 assert_unreached(desc + ': ' + reason.name); | |
15 } else { | |
16 assert_unreached(desc); | |
17 } | |
18 }); | |
19 } | |
20 | |
21 // Sends a message of {port, from} to the specified worker, and returns the | |
22 // port at the local end of the channel. | |
23 function sendMessagePort(worker, from) { | |
24 var messageChannel = new MessageChannel(); | |
25 worker.postMessage({from:from, port:messageChannel.port2}, [messageChannel.p ort2]); | |
26 return messageChannel.port1; | |
27 } | |
28 | |
29 | |
30 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.
| |
31 obj = obj || {}; | |
32 | |
33 obj.activeChangeCount = 0; | |
34 obj.activeStateChangeCount = 0; | |
35 obj.pendingChangeCount = 0; | |
36 obj.pendingStateChangeCount = 0; | |
37 | |
38 if (!navigator.serviceWorker) | |
39 return obj; | |
40 | |
41 navigator.serviceWorker.onpendingchange = function() { | |
42 ++obj.pendingChangeCount; | |
43 setStateChangeCounter(navigator.serviceWorker.pending, | |
44 function() { ++obj.pendingStateChangeCount; }); | |
45 }; | |
46 | |
47 navigator.serviceWorker.onactivechange = function() { | |
48 ++obj.activeChangeCount; | |
49 setStateChangeCounter(navigator.serviceWorker.active, | |
50 function() { ++obj.activeStateChangeCount; }); | |
51 }; | |
52 | |
53 setStateChangeCounter(navigator.serviceWorker.pending, | |
54 function() { ++obj.pendingStateChangeCount; }); | |
55 | |
56 setStateChangeCounter(navigator.serviceWorker.active, | |
57 function() { ++obj.activeStateChangeCount; }); | |
58 | |
59 function setStateChangeCounter(worker, callback) { | |
60 if (worker) | |
61 worker.onstatechange = callback; | |
62 } | |
63 | |
64 return obj; | |
65 } | |
OLD | NEW |