extendable-event-waituntil.https.html (4778B)
1 <!DOCTYPE html> 2 <title>ExtendableEvent: waitUntil</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="resources/test-helpers.sub.js"></script> 6 <script> 7 function runTest(test, scope, onRegister) { 8 var script = 'resources/extendable-event-waituntil.js?' + scope; 9 return service_worker_unregister_and_register(test, script, scope) 10 .then(function(registration) { 11 test.add_cleanup(function() { 12 return service_worker_unregister(test, scope); 13 }); 14 15 return onRegister(registration.installing); 16 }); 17 } 18 19 // Sends a SYN to the worker and asynchronously listens for an ACK; sets 20 // |obj.synced| to true once ack'd. 21 function syncWorker(worker, obj) { 22 var channel = new MessageChannel(); 23 worker.postMessage({port: channel.port2}, [channel.port2]); 24 return new Promise(function(resolve) { 25 channel.port1.onmessage = resolve; 26 }).then(function(e) { 27 var message = e.data; 28 assert_equals(message, 'SYNC', 29 'Should receive sync message from worker.'); 30 obj.synced = true; 31 channel.port1.postMessage('ACK'); 32 }); 33 } 34 35 promise_test(function(t) { 36 // Passing scope as the test switch for worker script. 37 var scope = 'resources/install-fulfilled'; 38 var onRegister = function(worker) { 39 var obj = {}; 40 41 return Promise.all([ 42 syncWorker(worker, obj), 43 wait_for_state(t, worker, 'installed') 44 ]).then(function() { 45 assert_true( 46 obj.synced, 47 'state should be "installed" after the waitUntil promise ' + 48 'for "oninstall" is fulfilled.'); 49 service_worker_unregister_and_done(t, scope); 50 }); 51 }; 52 return runTest(t, scope, onRegister); 53 }, 'Test install event waitUntil fulfilled'); 54 55 promise_test(function(t) { 56 var scope = 'resources/install-multiple-fulfilled'; 57 var onRegister = function(worker) { 58 var obj1 = {}; 59 var obj2 = {}; 60 61 return Promise.all([ 62 syncWorker(worker, obj1), 63 syncWorker(worker, obj2), 64 wait_for_state(t, worker, 'installed') 65 ]).then(function() { 66 assert_true( 67 obj1.synced && obj2.synced, 68 'state should be "installed" after all waitUntil promises ' + 69 'for "oninstall" are fulfilled.'); 70 }); 71 }; 72 return runTest(t, scope, onRegister); 73 }, 'Test ExtendableEvent multiple waitUntil fulfilled.'); 74 75 promise_test(function(t) { 76 var scope = 'resources/install-reject-precedence'; 77 var onRegister = function(worker) { 78 var obj1 = {}; 79 var obj2 = {}; 80 81 return Promise.all([ 82 syncWorker(worker, obj1) 83 .then(function() { 84 syncWorker(worker, obj2); 85 }), 86 wait_for_state(t, worker, 'redundant') 87 ]).then(function() { 88 assert_true( 89 obj1.synced, 90 'The "redundant" state was entered after the first "extend ' + 91 'lifetime promise" resolved.' 92 ); 93 assert_true( 94 obj2.synced, 95 'The "redundant" state was entered after the third "extend ' + 96 'lifetime promise" resolved.' 97 ); 98 }); 99 }; 100 return runTest(t, scope, onRegister); 101 }, 'Test ExtendableEvent waitUntil reject precedence.'); 102 103 promise_test(function(t) { 104 var scope = 'resources/activate-fulfilled'; 105 var onRegister = function(worker) { 106 var obj = {}; 107 return wait_for_state(t, worker, 'activating') 108 .then(function() { 109 return Promise.all([ 110 syncWorker(worker, obj), 111 wait_for_state(t, worker, 'activated') 112 ]); 113 }) 114 .then(function() { 115 assert_true( 116 obj.synced, 117 'state should be "activated" after the waitUntil promise ' + 118 'for "onactivate" is fulfilled.'); 119 }); 120 }; 121 return runTest(t, scope, onRegister); 122 }, 'Test activate event waitUntil fulfilled'); 123 124 promise_test(function(t) { 125 var scope = 'resources/install-rejected'; 126 var onRegister = function(worker) { 127 return wait_for_state(t, worker, 'redundant'); 128 }; 129 return runTest(t, scope, onRegister); 130 }, 'Test install event waitUntil rejected'); 131 132 promise_test(function(t) { 133 var scope = 'resources/activate-rejected'; 134 var onRegister = function(worker) { 135 return wait_for_state(t, worker, 'activated'); 136 }; 137 return runTest(t, scope, onRegister); 138 }, 'Test activate event waitUntil rejected.'); 139 140 </script>