test_browsing_context_structured_clone.js (2319B)
1 "use strict"; 2 3 add_task(async function test_BrowsingContext_structured_clone() { 4 let browser = Services.appShell.createWindowlessBrowser(false); 5 6 let frame = browser.document.createElement("iframe"); 7 8 await new Promise(r => { 9 frame.onload = () => r(); 10 browser.document.body.appendChild(frame); 11 }); 12 13 let { browsingContext } = frame; 14 15 let sch = new StructuredCloneHolder("debug name", "<anonymized> debug name", { 16 browsingContext, 17 }); 18 19 let deserialize = () => sch.deserialize({}, true); 20 21 // Check that decoding a live browsing context produces the correct 22 // object. 23 equal( 24 deserialize().browsingContext, 25 browsingContext, 26 "Got correct browsing context from StructuredClone deserialize" 27 ); 28 29 // Check that decoding a second time still succeeds. 30 equal( 31 deserialize().browsingContext, 32 browsingContext, 33 "Got correct browsing context from second StructuredClone deserialize" 34 ); 35 36 // Destroy the browsing context and make sure that the decode fails 37 // with a DataCloneError. 38 // 39 // Making sure the BrowsingContext is actually destroyed by the time 40 // we do the second decode is a bit tricky. We obviously have clear 41 // our local references to it, and give the GC a chance to reap them. 42 // And we also, of course, have to destroy the frame that it belongs 43 // to, or its frame loader and window global would hold it alive. 44 // 45 // Beyond that, we don't *have* to reload or destroy the parent 46 // document, but we do anyway just to be safe. 47 // 48 49 frame.remove(); 50 frame = null; 51 browsingContext = null; 52 53 browser.document.location.reload(); 54 browser.close(); 55 56 // We will schedule a precise GC and do both GC and CC a few times, to make 57 // sure we have completely destroyed the WindowGlobal actors (which keep 58 // references to their BrowsingContexts) in order 59 // to allow their (now snow-white) references to be collected. 60 await schedulePreciseGCAndForceCC(3); 61 62 // OK. We can be fairly confident that the BrowsingContext object 63 // stored in our structured clone data has been destroyed. Make sure 64 // that attempting to decode it again leads to the appropriate error. 65 Assert.throws( 66 deserialize, 67 e => e.name === "DataCloneError", 68 "Should get a DataCloneError when trying to decode a dead BrowsingContext" 69 ); 70 });