blob: ea6b09085b2355785f1a612a8312ea9483b4b996 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
<script>
function test()
{
function itemForRepresentedObject(x) {
return x;
}
function createTree(data) {
let node = {
...data,
parent: null,
children: data.children || [],
};
node.children = node.children.map((childData) => {
let childNode = createTree(childData);
childNode.parent = node;
return childNode;
});
return node;
}
function flatten(node) {
return [node, ...node.children.flatMap(flatten)];
}
function shuffle(array) {
let shuffled = [];
while (array.length)
shuffled.push(array.splice(Math.floor(Math.random() * array.length), 1)[0]);
return shuffled;
}
let suite = InspectorTest.createSyncSuite("SelectionController.createTreeComparator");
suite.addTestCase({
name: "SelectionController.createTreeComparator.ValidTree",
description: "Check that createTreeComparator works with a valid tree.",
test() {
let comparator = WI.SelectionController.createTreeComparator(itemForRepresentedObject);
InspectorTest.log("Creating tree...");
let root = createTree({
id: "root",
children: [
{
id: "1",
children: [
{
id: "1 > 1",
children: [
{id: "1 > 1 > 1"},
{id: "1 > 1 > 2"},
{id: "1 > 1 > 3"},
],
},
{
id: "1 > 2",
children: [
{id: "1 > 2 > 1"},
{id: "1 > 2 > 2"},
{id: "1 > 2 > 3"},
],
},
{
id: "1 > 3",
children: [
{id: "1 > 3 > 1"},
{id: "1 > 3 > 2"},
{id: "1 > 3 > 3"},
],
},
],
},
{
id: "2",
children: [
{
id: "2 > 1",
children: [
{id: "2 > 1 > 1"},
{id: "2 > 1 > 2"},
{id: "2 > 1 > 3"},
],
},
{
id: "2 > 2",
children: [
{id: "2 > 2 > 1"},
{id: "2 > 2 > 2"},
{id: "2 > 2 > 3"},
],
},
{
id: "2 > 3",
children: [
{id: "2 > 3 > 1"},
{id: "2 > 3 > 2"},
{id: "2 > 3 > 3"},
],
},
],
},
{
id: "3",
children: [
{
id: "3 > 1",
children: [
{id: "3 > 1 > 1"},
{id: "3 > 1 > 2"},
{id: "3 > 1 > 3"},
],
},
{
id: "3 > 2",
children: [
{id: "3 > 2 > 1"},
{id: "3 > 2 > 2"},
{id: "3 > 2 > 3"},
],
},
{
id: "3 > 3",
children: [
{id: "3 > 3 > 1"},
{id: "3 > 3 > 2"},
{id: "3 > 3 > 3"},
],
},
],
},
],
});
InspectorTest.log("Shuffling tree...");
let nodes = shuffle(flatten(root));
InspectorTest.log("Sorting tree...");
nodes.sort(comparator);
InspectorTest.newline();
for (let node of nodes)
InspectorTest.log(" " + node.id);
},
});
suite.runTestCasesAndFinish();
}
</script>
</head>
<body onLoad="runTest()">
<p>Tests for WI.SelectionController.createTreeComparator.</p>
</body>
</html>