blob: 42a77c86ec5e89e1eff7ccb1b2ee248c10215ecc [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
<script src="../../resources/gc.js"></script>
<script>
async function checkDoesNotKeepObjectsAlive()
{
let one = [{value: 1}];
let two = [{value: 2}];
let three = [{value: 3}];
let iterableWeakSet = new IterableWeakSet([one[0], two[0], three[0], two[0]]);
function check(description, itemOrValue, present) {
if (typeof itemOrValue === "object")
TestPage.dispatchEventToFrontend("TestPage-checkDoesNotKeepObjectsAlive-log", {message: `${iterableWeakSet.has(itemOrValue) === present ? "PASS" : "FAIL"}: 'has' should return ${present ? "true" : "false"} for '${JSON.stringify(itemOrValue)}' after ${description}.`});
if (typeof itemOrValue === "number") {
let found = false;
for (let item of iterableWeakSet) {
if (item.value !== itemOrValue)
continue;
if (found) {
TestPage.dispatchEventToFrontend("TestPage-checkDoesNotKeepObjectsAlive-log", {message: `FAIL: Should not find '${JSON.stringify(itemOrValue)}' more than once after ${description}.`});
continue;
}
found = true;
}
TestPage.dispatchEventToFrontend("TestPage-checkDoesNotKeepObjectsAlive-log", {message: `${found === present ? "PASS" : "FAIL"}: Should ${present ? "" : "not"} contain '{"value": ${itemOrValue}}' after ${description}.`});
}
}
TestPage.dispatchEventToFrontend("TestPage-checkDoesNotKeepObjectsAlive-log", {message: JSON.stringify(iterableWeakSet)});
check("construction", one[0], true);
check("construction", two[0], true);
check("construction", three[0], true);
nukeArray(one);
await new Promise(setTimeout);
gc();
await new Promise(setTimeout);
TestPage.dispatchEventToFrontend("TestPage-checkDoesNotKeepObjectsAlive-log", {message: JSON.stringify(iterableWeakSet)});
check("`one = null`", 1, false);
check("`one = null`", two[0], true);
check("`one = null`", three[0], true);
nukeArray(two);
await new Promise(setTimeout);
gc();
await new Promise(setTimeout);
TestPage.dispatchEventToFrontend("TestPage-checkDoesNotKeepObjectsAlive-log", {message: JSON.stringify(iterableWeakSet)});
check("`two = null`", 1, false);
check("`two = null`", 2, false);
check("`two = null`", three[0], true);
nukeArray(three);
await new Promise(setTimeout);
gc();
await new Promise(setTimeout);
TestPage.dispatchEventToFrontend("TestPage-checkDoesNotKeepObjectsAlive-log", {message: JSON.stringify(iterableWeakSet)});
check("`three = null`", 1, false);
check("`three = null`", 2, false);
check("`three = null`", 3, false);
TestPage.dispatchEventToFrontend("TestPage-checkDoesNotKeepObjectsAlive-done");
}
function test()
{
let suite = InspectorTest.createAsyncSuite("IterableWeakSet");
suite.addTestCase({
name: "IterableWeakSet.prototype.constructor.Empty",
async test() {
let iterableWeakSet = new IterableWeakSet;
InspectorTest.log(iterableWeakSet);
},
});
suite.addTestCase({
name: "IterableWeakSet.prototype.constructor.NonEmpty",
async test() {
let one = {value: 1};
let two = {value: 2};
let three = {value: 3};
let iterableWeakSet = new IterableWeakSet([one, two, three, two]);
InspectorTest.log(iterableWeakSet);
},
});
suite.addTestCase({
name: "IterableWeakSet.prototype.has",
async test() {
let one = {value: 1};
let two = {value: 2};
let three = {value: 3};
let four = {value: 4};
let iterableWeakSet = new IterableWeakSet([one, two, three, two]);
InspectorTest.log(iterableWeakSet);
InspectorTest.expectTrue(iterableWeakSet.has(one), "'has' should return true if a key exists.");
InspectorTest.expectTrue(iterableWeakSet.has(two), "'has' should return true if a key exists (more than once).");
InspectorTest.expectTrue(iterableWeakSet.has(three), "'has' should return true if a key exists.");
InspectorTest.expectFalse(iterableWeakSet.has(four), "'has' should return false if a key doesn't exist.");
},
});
suite.addTestCase({
name: "IterableWeakSet.prototype.add",
async test() {
let one = {value: 1};
let two = {value: 2};
let three = {value: 3};
let iterableWeakSet = new IterableWeakSet;
InspectorTest.log(iterableWeakSet);
iterableWeakSet.add(one);
InspectorTest.log(iterableWeakSet);
iterableWeakSet.add(two);
InspectorTest.log(iterableWeakSet);
iterableWeakSet.add(three);
InspectorTest.log(iterableWeakSet);
iterableWeakSet.add(two);
InspectorTest.log(iterableWeakSet);
},
});
suite.addTestCase({
name: "IterableWeakSet.prototype.delete",
async test() {
let one = {value: 1};
let two = {value: 2};
let three = {value: 3};
let four = {value: 4};
let iterableWeakSet = new IterableWeakSet([one, two, three, two]);
InspectorTest.log(iterableWeakSet);
InspectorTest.expectTrue(iterableWeakSet.delete(one), "'delete' should return true for a known key.");
InspectorTest.log(iterableWeakSet);
InspectorTest.expectTrue(iterableWeakSet.delete(two), "'delete' should return true for a known key.");
InspectorTest.log(iterableWeakSet);
InspectorTest.expectFalse(iterableWeakSet.delete(two), "'delete' should return false for an already deleted key.");
InspectorTest.log(iterableWeakSet);
InspectorTest.expectTrue(iterableWeakSet.delete(three), "'delete' should return true for a known key.");
InspectorTest.log(iterableWeakSet);
InspectorTest.expectFalse(iterableWeakSet.delete(four), "'delete' should return false for an unknown key.");
InspectorTest.log(iterableWeakSet);
},
});
suite.addTestCase({
name: "IterableWeakSet.prototype.take",
async test() {
let one = {value: 1};
let two = {value: 2};
let three = {value: 3};
let four = {value: 4};
let iterableWeakSet = new IterableWeakSet([one, two, three, two]);
InspectorTest.log(iterableWeakSet);
InspectorTest.expectEqual(iterableWeakSet.take(one)?.value, 1, "'take' should return the key for a known key.");
InspectorTest.log(iterableWeakSet);
InspectorTest.expectEqual(iterableWeakSet.take(two)?.value, 2, "'take' should return the key for a known key.");
InspectorTest.log(iterableWeakSet);
InspectorTest.expectEqual(iterableWeakSet.take(two), undefined, "'take' should return undefined for an already deleted key.");
InspectorTest.log(iterableWeakSet);
InspectorTest.expectEqual(iterableWeakSet.take(three)?.value, 3, "'take' should return the key for a known key.");
InspectorTest.log(iterableWeakSet);
InspectorTest.expectEqual(iterableWeakSet.take(four), undefined, "'take' should return undefined for an unknown key.");
InspectorTest.log(iterableWeakSet);
},
});
suite.addTestCase({
name: "IterableWeakSet.prototype.clear",
async test() {
let one = {value: 1};
let two = {value: 2};
let three = {value: 3};
let iterableWeakSet = new IterableWeakSet([one, two, three, two]);
InspectorTest.log(iterableWeakSet);
iterableWeakSet.clear();
InspectorTest.log(iterableWeakSet);
},
});
suite.addTestCase({
name: "IterableWeakSet.prototype.keys",
async test() {
let one = {value: 1};
let two = {value: 2};
let three = {value: 3};
let iterableWeakSet = new IterableWeakSet([one, two, three, two]);
InspectorTest.log(Array.from(iterableWeakSet.keys()));
},
});
suite.addTestCase({
name: "IterableWeakSet.prototype.values",
async test() {
let one = {value: 1};
let two = {value: 2};
let three = {value: 3};
let iterableWeakSet = new IterableWeakSet([one, two, three, two]);
InspectorTest.log(Array.from(iterableWeakSet.values()));
},
});
suite.addTestCase({
name: "IterableWeakSet.prototype.copy",
async test() {
let one = {value: 1};
let two = {value: 2};
let three = {value: 3};
let four = {value: 4};
let iterableWeakSet = new IterableWeakSet([one, two, three, two]);
InspectorTest.log(iterableWeakSet);
let copy = iterableWeakSet.copy();
InspectorTest.expectNotEqual(iterableWeakSet, copy, "Copy should not return the same object.")
InspectorTest.expectEqual(JSON.stringify(iterableWeakSet), JSON.stringify(copy), "Copy should return a deep copy.");
iterableWeakSet.add(four);
InspectorTest.expectNotEqual(JSON.stringify(iterableWeakSet), JSON.stringify(copy), "Modifying the original should not modify the copy.");
},
});
suite.addTestCase({
name: "IterableWeakSet.DoesNotKeepObjectsAlive",
async test() {
// Send `IterableWeakSet` to the page so that `GCController` can be used.
InspectorTest.log("Evaluating `IterableWeakSet` source in the inspected page...");
await InspectorTest.evaluateInPage(String(IterableWeakSet));
InspectorTest.addEventListener("TestPage-checkDoesNotKeepObjectsAlive-log", (event) => {
InspectorTest.log(event.data.message);
});
InspectorTest.log("Testing `IterableWeakSet` in the inspected page...");
await Promise.all([
InspectorTest.awaitEvent("TestPage-checkDoesNotKeepObjectsAlive-done"),
InspectorTest.evaluateInPage(`checkDoesNotKeepObjectsAlive()`),
]);
},
});
suite.runTestCasesAndFinish();
}
</script>
</head>
<body onload="runTest()">
<p>Testing all methods of IterableWeakSet.</p>
</body>
</html>