OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Service Worker: registration end-to-end</title> |
| 3 <script src="../resources/testharness.js"></script> |
| 4 <script src="../resources/testharnessreport.js"></script> |
| 5 <script src="resources/test-helpers.js"></script> |
| 6 <script> |
| 7 var t = async_test('Registration: end-to-end'); |
| 8 t.step(function() { |
| 9 |
| 10 var serviceWorkerStates = []; |
| 11 var lastServiceWorkerState = ''; |
| 12 var receivedMessageFromPort = ''; |
| 13 var currentChangeCount = 0; |
| 14 |
| 15 assert_true(navigator.serviceWorker instanceof ServiceWorkerContainer); |
| 16 assert_equals(typeof navigator.serviceWorker.register, 'function'); |
| 17 assert_equals(typeof navigator.serviceWorker.unregister, 'function'); |
| 18 |
| 19 navigator.serviceWorker.oncurrentchange = function() { |
| 20 ++currentChangeCount; |
| 21 }; |
| 22 |
| 23 // This always tests fresh registration, so unregister it first. |
| 24 navigator.serviceWorker.unregister('/in-scope/*').then( |
| 25 t.step_func(doRegister), |
| 26 unreached_func(t, 'unregister should not reject') |
| 27 ); |
| 28 |
| 29 function doRegister() { |
| 30 navigator.serviceWorker.register( |
| 31 'resources/end-to-end-worker.js', |
| 32 {scope: '/in-scope/*'} |
| 33 ).then( |
| 34 t.step_func(onRegister) |
| 35 ).catch( |
| 36 unreached_func(t, 'Registration failed') |
| 37 ); |
| 38 } |
| 39 |
| 40 function sendMessagePort(worker, from) { |
| 41 var messageChannel = new MessageChannel(); |
| 42 worker.postMessage({from:from, port:messageChannel.port2}, [messageChann
el.port2]); |
| 43 return messageChannel.port1; |
| 44 } |
| 45 |
| 46 |
| 47 var sawMessage, sawActive; |
| 48 var messagePromise = new Promise(function(resolve) { sawMessage = resolve; }
); |
| 49 var activePromise = new Promise(function(resolve) { sawActive = resolve; }); |
| 50 |
| 51 function onRegister(sw) { |
| 52 serviceWorkerStates.push(sw.state); |
| 53 lastServiceWorkerState = sw.state; |
| 54 |
| 55 var sawMessage = new Promise(t.step_func(function(resolve) { |
| 56 sendMessagePort(sw, 'registering doc').onmessage = t.step_func(funct
ion (e) { |
| 57 receivedMessageFromPort = e.data; |
| 58 resolve(); |
| 59 }); |
| 60 })); |
| 61 |
| 62 var sawActive = new Promise(t.step_func(function(resolve) { |
| 63 sw.onstatechange = t.step_func(function() { |
| 64 serviceWorkerStates.push(sw.state); |
| 65 |
| 66 switch (sw.state) { |
| 67 case 'installing': |
| 68 assert_equals(lastServiceWorkerState, 'parsed'); |
| 69 break; |
| 70 case 'installed': |
| 71 assert_equals(lastServiceWorkerState, 'installing'); |
| 72 break; |
| 73 case 'activating': |
| 74 assert_equals(lastServiceWorkerState, 'installed'); |
| 75 break; |
| 76 case 'active': |
| 77 assert_equals(lastServiceWorkerState, 'activating'); |
| 78 break; |
| 79 default: |
| 80 unreached_func(t, 'Unexpected state: ' + sw.state); |
| 81 } |
| 82 |
| 83 lastServiceWorkerState = sw.state; |
| 84 if (sw.state === 'active') |
| 85 resolve(); |
| 86 }); |
| 87 })); |
| 88 |
| 89 Promise.all([sawMessage, sawActive]).then(t.step_func(function() { |
| 90 assert_array_equals(serviceWorkerStates, |
| 91 ['parsed', 'installing', 'installed', 'activatin
g', 'active'], |
| 92 'Service worker should pass through four states'
); |
| 93 |
| 94 assert_equals(currentChangeCount, 0, |
| 95 'Should not see current changes since document is out
of scope'); |
| 96 |
| 97 assert_equals(receivedMessageFromPort, 'Ack for: registering doc'); |
| 98 |
| 99 t.done(); |
| 100 })); |
| 101 } |
| 102 }); |
| 103 </script> |
OLD | NEW |