test_weakRefs_collected_wrapper.html (1617B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test WeakRefs with DOM wrappers that get cycle collected</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <script type="application/javascript"> 8 let weakrefs = []; 9 10 function go() { 11 SimpleTest.waitForExplicitFinish(); 12 13 // 1. nsISupports-derived object. 14 let doc = document.implementation.createHTMLDocument(); 15 weakrefs.push(new WeakRef(doc)); 16 17 // 2. non-nsISupports-derived object. 18 let buffer = new AudioBuffer({length: 1, sampleRate: 8000}); 19 weakrefs.push(new WeakRef(buffer)); 20 21 // 3. nsISupports non-wrapper-cached object. 22 let image = new ImageData(1, 1); 23 weakrefs.push(new WeakRef(image)); 24 25 // 4. non-nsISupports non-wrapper-cached object. 26 let iterator = document.fonts.values(); 27 weakrefs.push(new WeakRef(iterator)); 28 29 for (let wr of weakrefs) { 30 isnot(wr.deref(), undefined, "Check that live wrapper is returned"); 31 } 32 33 // setTimeout will call its callback in a new task. 34 setTimeout(task2, 0); 35 } 36 37 function task2() { 38 // Trigger a GC and CC to collect the DOM objects, but no GC to 39 // collect the wrappers. 40 SpecialPowers.DOMWindowUtils.garbageCollect(); 41 SpecialPowers.DOMWindowUtils.cycleCollect(); 42 43 for (let wr of weakrefs) { 44 is(wr.deref(), undefined, "Check that stale wrapper is not exposed"); 45 } 46 47 SimpleTest.finish(); 48 } 49 </script> 50 </head> 51 <body onload="go()"></body> 52 </html>