blob: 0953d57d68edbfe463c3e228dd56050935d82aa7 [file] [log] [blame]
<!DOCTYPE html>
<script src="../../resources/js-test.js"></script>
<script>
description('Tests for leaks caused by reference cycles that pass through the DOM implementation');
function checkForNodeLeaks(testFunction, underlyingClass)
{
// Bump this number as high as we need to, to get reproducible results.
const repetitions = 20;
gc();
const beforeCount = internals.numberOfLiveNodes();
for (var i = 0; i < repetitions; ++i)
testFunction();
gc();
const leaks = internals.numberOfLiveNodes() - beforeCount;
if (leaks == repetitions)
return "leaked";
if (leaks < repetitions / 10)
return "did not leak";
return "leaked an unexpected number of nodes: " + leaks + " leaks in " + repetitions + " runs";
}
function emptyFunction()
{
}
function createNode()
{
document.createTextNode("");
}
function createEventListenerCycle()
{
const leakDetectionNode = document.createTextNode("");
leakDetectionNode.addEventListener("x", function () { return leakDetectionNode; });
}
function createPromiseCycle()
{
const leakDetectionNode = document.createTextNode("");
const promise = new Promise(function (resolve, reject) { });
promise.cycle = promise;
promise.leakDetectionNode = leakDetectionNode;
}
function createTreeWalkerNodeCycle()
{
const leakDetectionNode = document.createTextNode("");
leakDetectionNode.treeWalker = document.createTreeWalker(leakDetectionNode);
}
function createTreeWalkerFilterCycle()
{
const leakDetectionNode = document.createTextNode("");
const filter = { leakDetectionNode: leakDetectionNode, acceptNode: function(node) { return leakDetectionNode; } };
leakDetectionNode.treeWalker = document.createTreeWalker(document, 0, filter);
}
function createCustomEventDetailsCycle()
{
const leakDetectionNode = document.createTextNode("");
leakDetectionNode.event = new CustomEvent("x", { detail: leakDetectionNode });
}
function createErrorEventDataCycle()
{
const leakDetectionNode = document.createTextNode("");
leakDetectionNode.event = new ErrorEvent("x", { error: leakDetectionNode });
}
function createExtendableMessageEventDataCycle()
{
const leakDetectionNode = document.createTextNode("");
leakDetectionNode.event = new ExtendableMessageEvent("x", { data: leakDetectionNode });
}
function createMessageEventDataCycle()
{
const leakDetectionNode = document.createTextNode("");
leakDetectionNode.event = new MessageEvent("x", { data: leakDetectionNode });
}
function createPromiseRejectionEventPromiseCycle()
{
const leakDetectionNode = document.createTextNode("");
const promise = new Promise(function (resolve, reject) { });
promise.leakDetectionNode = leakDetectionNode;
leakDetectionNode.event = new PromiseRejectionEvent("x", { promise: promise });
}
function createPromiseRejectionEventPromiseFunctionCycle()
{
const leakDetectionNode = document.createTextNode("");
const promise = new Promise(function (resolve, reject) { return leakDetectionNode; });
leakDetectionNode.event = new PromiseRejectionEvent("x", { promise: promise });
}
function createPromiseRejectionEventReasonCycle()
{
const leakDetectionNode = document.createTextNode("");
const promise = new Promise(function (resolve, reject) { });
leakDetectionNode.event = new PromiseRejectionEvent("x", { promise: promise, reason: leakDetectionNode });
}
function createPopStateEventStateCycle()
{
const leakDetectionNode = document.createTextNode("");
leakDetectionNode.event = new PopStateEvent("x", { state: leakDetectionNode });
}
function createIDBRequestResultCycle()
{
// FIXME: Need to write this test and reorganize so it can be asynchronous.
// Get an IDBRequest with a result that is a structured clone (see IDBTransaction::didGetRecordOnServer).
// Add a property to the result object that references the request.
// Add another property to the result object that references a leak detection node.
}
function createPaymentResponseDetailsCycle()
{
// FIXME: Need to write this test and reorganize so it can be asynchronous.
// Get a PaymentResponse, requires simulating a successful payment.
// Add a property to the details object that references the PaymentResponse.
// Add another property to the details object that references a leak detection node.
}
function createRTCStatsReportCycle()
{
// FIXME: Need to write this test and reorganize so it can be asynchronous.
// Get an RTCStatsReport.
// Get one of the objects from the map.
// Add a property to that object that references the report.
// Add another property to that object that references a leak detection node.
}
function runLeakTest(testFunctionName, underlyingClassName)
{
if (underlyingClassName && !(underlyingClassName in window))
debug('---- Did not test ' + underlyingClassName + ' because it is not enabled.');
else
shouldBeEqualToString('checkForNodeLeaks(' + testFunctionName + ')', 'did not leak');
}
function startTest()
{
if (!window.internals || !internals.numberOfLiveNodes) {
testFailed('Test requires windows.internals, so must be run inside WebKitTestRunner');
return;
}
runLeakTest('emptyFunction');
runLeakTest('createNode');
runLeakTest('createEventListenerCycle');
runLeakTest('createTreeWalkerNodeCycle');
runLeakTest('createTreeWalkerFilterCycle');
runLeakTest('createPromiseCycle');
runLeakTest('createCustomEventDetailsCycle');
runLeakTest('createErrorEventDataCycle');
runLeakTest('createExtendableMessageEventDataCycle', 'ExtendableMessageEvent');
runLeakTest('createMessageEventDataCycle');
runLeakTest('createPopStateEventStateCycle');
runLeakTest('createPromiseRejectionEventPromiseCycle');
runLeakTest('createPromiseRejectionEventPromiseFunctionCycle');
runLeakTest('createPromiseRejectionEventReasonCycle');
}
startTest();
</script>