Chromium Code Reviews| 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/support.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() { | |
|
jsbell
2014/04/16 17:43:38
I reintroduced this as it seems like a valid asser
| |
| 20 ++currentChangeCount; | |
| 21 }; | |
| 22 | |
| 23 // This always tests fresh registration, so unregister it first. | |
| 24 | |
| 25 // unregister is non-idempotent, and rejects if not found. | |
| 26 // https://github.com/slightlyoff/ServiceWorker/issues/233 | |
| 27 navigator.serviceWorker.unregister('/in-scope/*').then( | |
| 28 t.step_func(doRegister), t.step_func(doRegister)); | |
|
jsbell
2014/04/16 17:43:38
Having unregister() reject if there was no match s
kinuko
2014/04/17 11:32:54
It's the behavior currently spec'ed, not the test'
| |
| 29 | |
| 30 function doRegister() { | |
| 31 navigator.serviceWorker.register( | |
| 32 'resources/end-to-end-worker.js', | |
| 33 {scope: '/in-scope/*'} | |
| 34 ).then( | |
| 35 t.step_func(onRegister) | |
|
jsbell
2014/04/16 17:43:38
On github, the registration.html calls onRegister
kinuko
2014/04/17 11:32:54
It was a bug, it's fixed now (by dominicc's pull r
| |
| 36 ).catch( | |
| 37 fail(t, 'Registration failed') | |
| 38 ); | |
| 39 } | |
| 40 | |
| 41 function onRegister(sw) { | |
| 42 serviceWorkerStates.push(sw.state); | |
| 43 lastServiceWorkerState = sw.state; | |
| 44 | |
| 45 sendMessagePort(sw, 'registering doc').onmessage = t.step_func(function (e) { | |
| 46 receivedMessageFromPort = e.data; | |
| 47 if (lastServiceWorkerState === 'active') | |
| 48 onTestFinished(); | |
|
jsbell
2014/04/16 17:43:38
Having two exit paths makes me sad. The test shoul
kinuko
2014/04/17 11:32:54
Yup agreed, btw feel free to fix these flaws here
| |
| 49 }); | |
| 50 | |
| 51 sw.onstatechange = t.step_func(function() { | |
| 52 serviceWorkerStates.push(sw.state); | |
| 53 | |
| 54 switch (sw.state) { | |
| 55 case 'installing': | |
| 56 // FIXME: Not currently seen. | |
| 57 assert_equals(lastServiceWorkerState, 'parsed'); | |
|
jsbell
2014/04/16 17:43:38
This case currently never matches, but it's in git
kinuko
2014/04/17 11:32:54
I wasn't very sure which is expected behavior, whe
| |
| 58 break; | |
| 59 case 'installed': | |
| 60 assert_equals(lastServiceWorkerState, 'installing'); | |
| 61 break; | |
| 62 case 'activating': | |
| 63 assert_equals(lastServiceWorkerState, 'installed'); | |
| 64 break; | |
| 65 case 'active': | |
| 66 assert_equals(lastServiceWorkerState, 'activating'); | |
| 67 break; | |
| 68 default: | |
| 69 fail(t, 'Unexpected state: ' + sw.state); | |
| 70 } | |
| 71 | |
| 72 lastServiceWorkerState = sw.state; | |
| 73 if (sw.state === 'active' && receivedMessageFromPort) | |
| 74 onTestFinished(); | |
|
falken
2014/04/22 14:03:08
FYI, I think the flaky timeouts are caused by acti
| |
| 75 }); | |
| 76 } | |
| 77 | |
| 78 function onTestFinished() { | |
| 79 assert_array_equals(serviceWorkerStates, | |
| 80 ['installing', 'installed', 'activating', 'active'], | |
| 81 'Service worker should pass through four states'); | |
| 82 | |
| 83 assert_equals(currentChangeCount, 0, | |
| 84 'Should not see current changes since document is out of s cope'); | |
| 85 | |
| 86 assert_equals(receivedMessageFromPort, 'Ack for: registering doc'); | |
| 87 | |
| 88 t.done(); | |
| 89 } | |
| 90 }); | |
| 91 </script> | |
| OLD | NEW |