detached-iframe.window.js (1732B)
1 // META: title=MessageChannel in a detached iframe test 2 // META: script=/service-workers/service-worker/resources/test-helpers.sub.js 3 // META: script=/common/gc.js 4 // Pull in the with_iframe helper function from the service worker tests 5 6 7 const IframeAction = { 8 REMOVE_BEFORE_CREATION: 'remove-before-creation', 9 REMOVE_AFTER_CREATION: 'remove-after-creation', 10 }; 11 12 async function detached_frame_test(t, action) { 13 const iframe = await with_iframe('about:blank'); 14 const iframe_MessageChannel = iframe.contentWindow.MessageChannel; 15 16 if (action === IframeAction.REMOVE_BEFORE_CREATION) { 17 iframe.remove(); 18 } 19 20 (() => { 21 const mc = new iframe_MessageChannel(); 22 mc.port1.postMessage("boo"); 23 mc.port2.onmessage = t.unreached_func("message event received"); 24 mc.port2.onmessageerror = t.unreached_func("message event received"); 25 })(); 26 27 if (action === IframeAction.REMOVE_AFTER_CREATION) { 28 iframe.remove(); 29 } 30 31 await garbageCollect(); 32 33 // We are testing that neither of the above two events fire. We assume that a 2 second timeout 34 // is good enough. We can't use any other API for an end condition because each MessagePort has 35 // its own independent port message queue, which has no ordering guarantees relative to other 36 // APIs. 37 await new Promise(resolve => t.step_timeout(resolve, 2000)); 38 } 39 40 promise_test(async (t) => { 41 return detached_frame_test(t, IframeAction.REMOVE_AFTER_CREATION); 42 }, 'MessageChannel created from a detached iframe should not send messages (remove after create)'); 43 44 promise_test(async (t) => { 45 return detached_frame_test(t, IframeAction.REMOVE_BEFORE_CREATION); 46 }, 'MessageChannel created from a detached iframe should not send messages (remove before create)');