update-recovery.https.html (2505B)
1 <!DOCTYPE html> 2 <title>Service Worker: recovery by navigation update</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="resources/testharness-helpers.js"></script> 6 <script src="resources/test-helpers.sub.js"></script> 7 <script> 8 promise_test(function(t) { 9 var scope = 'resources/simple.txt'; 10 var worker_url = 'resources/update-recovery-worker.py'; 11 var expected_url = normalizeURL(worker_url); 12 var registration; 13 14 function with_bad_iframe(url) { 15 return new Promise(function(resolve, reject) { 16 var frame = document.createElement('iframe'); 17 18 // There is no cross-browser event to listen for to detect an 19 // iframe that fails to load due to a bad interception. Unfortunately 20 // we have to use a timeout. 21 var timeout = setTimeout(function() { 22 frame.remove(); 23 resolve(); 24 }, 5000); 25 26 // If we do get a load event, though, we know something went wrong. 27 frame.addEventListener('load', function() { 28 clearTimeout(timeout); 29 frame.remove(); 30 reject('expected bad iframe should not fire a load event!'); 31 }); 32 33 frame.src = url; 34 document.body.appendChild(frame); 35 }); 36 } 37 38 function with_update(t) { 39 return new Promise(function(resolve, reject) { 40 registration.addEventListener('updatefound', function onUpdate() { 41 registration.removeEventListener('updatefound', onUpdate); 42 wait_for_state(t, registration.installing, 'activated').then(function() { 43 resolve(); 44 }); 45 }); 46 }); 47 } 48 49 return service_worker_unregister_and_register(t, worker_url, scope) 50 .then(function(r) { 51 t.add_cleanup(function() { 52 return service_worker_unregister(t, scope); 53 }); 54 55 registration = r; 56 return wait_for_state(t, registration.installing, 'activated'); 57 }) 58 .then(function() { 59 return Promise.all([ 60 with_update(t), 61 with_bad_iframe(scope) 62 ]); 63 }) 64 .then(function() { 65 return with_iframe(scope); 66 }) 67 .then(function(frame) { 68 assert_equals(frame.contentWindow.navigator.serviceWorker.controller.scriptURL, 69 expected_url); 70 frame.remove(); 71 }); 72 }, 'Recover from a bad service worker by updating after a failed navigation.'); 73 </script>