<!DOCTYPE html>
<head>
<script src="../resources/gc.js"></script>
<script src="../resources/js-test.js"></script>
<script src="resources/netinfo_common.js"></script>
</head>
<body>
<script>
description('Tests that unused listeners are collected.');
shouldBe('typeof internals.observeGC', '"function"',
'this test requires window.internals');
let callback = function(e) {
testFailed("Should not get here.");
};
// Add a listener.
var callbackObserver;
// Do not pass the object directly to observeGC function. It may
// remain live on this function's stack preventing GC from collecting
// it. Accessing the object inside an inner function will prevent any
// unneeded references on this function's stack. (http://crbug.com/595672/)
(function() {
callbackObserver = internals.observeGC(callback);
connection.addEventListener('typechange', callback);
})();
asyncGC().then( () => {
shouldBeFalse('callbackObserver.wasCollected');
// Remove the listener and its callback reference.
// Access objects in an inner function to avoid references to objects
// remaining live on this function's stack frame (http://crbug.com/595672/).
(() => {connection.removeEventListener('typechange', callback);})();
callback = null;
return asyncGC();
}).then(() => {
shouldBeTrue('callbackObserver.wasCollected');
finishJSTest();
});
</script>
</body>
</html>