test_onmessageerror.html (4282B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Test onmessageerror event handlers</title> 5 </head> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <script type="text/javascript" src="utils.js"></script> 8 <script> 9 /** 10 * Test that ServiceWorkerGlobalScope and ServiceWorkerContainer handle 11 * `messageerror` events, using a test helper class `StructuredCloneTester`. 12 * Intances of this class can be configured to fail to serialize or 13 * deserialize, as it's difficult to artificially create the case where an 14 * object successfully serializes but fails to deserialize (which can be 15 * caused by out-of-memory failures or the target global not supporting a 16 * serialized interface). 17 */ 18 19 let registration = null; 20 let serviceWorker = null; 21 let serviceWorkerContainer = null; 22 const swScript = 'onmessageerror_worker.js'; 23 24 add_task(async () => { 25 await SpecialPowers.pushPrefEnv({ 26 set: [ 27 ['dom.serviceWorkers.enabled', true], 28 ['dom.serviceWorkers.testing.enabled', true], 29 ['dom.testing.structuredclonetester.enabled', true], 30 ], 31 }); 32 33 swContainer = navigator.serviceWorker; 34 35 registration = await swContainer.register(swScript); 36 ok(registration, 'Service Worker regsisters'); 37 38 serviceWorker = registration.installing; 39 await waitForState(serviceWorker, 'activated'); 40 }); // setup 41 42 add_task(async () => { 43 const serializable = true; 44 const deserializable = true; 45 let sct = new StructuredCloneTester(serializable, deserializable); 46 47 const p = new Promise((resolve, reject) => { 48 function onMessage(e) { 49 const expectedBehavior = 'Serializable and deserializable ' + 50 'StructuredCloneTester serializes and deserializes'; 51 52 is(e.data.received, 'message', expectedBehavior); 53 swContainer.removeEventListener('message', onMessage); 54 resolve(); 55 } 56 57 swContainer.addEventListener('message', onMessage); 58 }); 59 60 serviceWorker.postMessage({ serializable, deserializable, sct }); 61 62 await p; 63 }); 64 65 add_task(async () => { 66 const serializable = false; 67 // if it's not serializable, being deserializable or not doesn't matter 68 const deserializable = false; 69 let sct = new StructuredCloneTester(serializable, deserializable); 70 71 try { 72 serviceWorker.postMessage({ serializable, deserializable, sct }); 73 ok(false, 'StructuredCloneTester serialization should have thrown -- ' + 74 'this line should not have been reached.'); 75 } catch (e) { 76 const expectedBehavior = 'Unserializable StructuredCloneTester fails ' + 77 `to send, with exception name: ${e.name}`; 78 is(e.name, 'DataCloneError', expectedBehavior); 79 } 80 }); 81 82 add_task(async () => { 83 const serializable = true; 84 const deserializable = false; 85 let sct = new StructuredCloneTester(serializable, deserializable); 86 87 const p = new Promise((resolve, reject) => { 88 function onMessage(e) { 89 const expectedBehavior = 'ServiceWorkerGlobalScope handles ' + 90 'messageerror events'; 91 92 is(e.data.received, 'messageerror', expectedBehavior); 93 swContainer.removeEventListener('message', onMessage); 94 resolve(); 95 } 96 97 swContainer.addEventListener('message', onMessage); 98 }); 99 100 serviceWorker.postMessage({ serializable, deserializable, sct }); 101 102 await p; 103 }); // test ServiceWorkerGlobalScope onmessageerror 104 105 add_task(async () => { 106 const p = new Promise((resolve, reject) => { 107 function onMessageError(e) { 108 ok(true, 'ServiceWorkerContainer handles messageerror events'); 109 swContainer.removeEventListener('messageerror', onMessageError); 110 resolve(); 111 } 112 113 swContainer.addEventListener('messageerror', onMessageError); 114 }); 115 116 serviceWorker.postMessage('send-bad-message'); 117 118 await p; 119 }); // test ServiceWorkerContainer onmessageerror 120 121 add_task(async () => { 122 await SpecialPowers.popPrefEnv(); 123 ok(await registration.unregister(), 'Service Worker unregisters'); 124 }); // teardown 125 </script> 126 <body> 127 </body> 128 </html>