190 lines
8.1 KiB
HTML
190 lines
8.1 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
|
|
<script>
|
|
function test()
|
|
{
|
|
let suite = InspectorTest.createSyncSuite("View.Basics");
|
|
|
|
suite.addTestCase({
|
|
name: "View.rootView",
|
|
test() {
|
|
let rootView = WI.View.rootView();
|
|
InspectorTest.expectThat(rootView.isAttached, "Root view should be attached by definition.");
|
|
InspectorTest.expectEqual(rootView.element, document.body, "Root view element should be document.body.");
|
|
InspectorTest.expectFalse(rootView.layoutPending, "Root view should not have a pending layout.");
|
|
InspectorTest.expectEqual(rootView.subviews.length, 0, "Root view should not have subviews.");
|
|
InspectorTest.expectEqual(WI.View.rootView(), WI.View.rootView(), "View.rootView() should always return the same view.");
|
|
|
|
return true;
|
|
}
|
|
});
|
|
|
|
suite.addTestCase({
|
|
name: "View.constructor",
|
|
test() {
|
|
let view = new WI.View;
|
|
InspectorTest.expectFalse(view.isAttached, "View should not be attached.");
|
|
InspectorTest.expectEqual(view.element.tagName, "DIV", "View element should be a <div>.");
|
|
InspectorTest.expectEqual(view.element.childNodes.length, 0, "View element should not have child nodes.");
|
|
InspectorTest.expectFalse(view.layoutPending, "View should not have a pending layout.");
|
|
InspectorTest.expectEqual(view.subviews.length, 0, "View should not have subviews.");
|
|
InspectorTest.expectNull(view.parentView, "View should not have a parent.");
|
|
|
|
let existingElement = document.createElement("ol");
|
|
let customView = new WI.View(existingElement);
|
|
InspectorTest.expectEqual(customView.element, existingElement, "View should be created with passed in element.");
|
|
|
|
return true;
|
|
}
|
|
});
|
|
|
|
suite.addTestCase({
|
|
name: "View.addSubview",
|
|
test() {
|
|
let parent = new WI.View;
|
|
let child = new WI.View;
|
|
parent.addSubview(child);
|
|
InspectorTest.expectEqual(child.parentView, parent, "Child should have the correct parent.");
|
|
InspectorTest.expectThat(child.isDescendantOf(parent), "Child should be a descendant of the parent.");
|
|
InspectorTest.expectThat(parent.subviews.includes(child), "Child should be included in the parent's subviews.");
|
|
|
|
let previousSubviews = parent.subviews.slice();
|
|
parent.addSubview(child);
|
|
InspectorTest.expectShallowEqual(previousSubviews, parent.subviews, "Adding a view multiple times should have no effect.");
|
|
|
|
let grandchild = new WI.View;
|
|
child.addSubview(grandchild);
|
|
InspectorTest.expectThat(grandchild.isDescendantOf(child.parentView), "Grandchild should be a descendant of it's grandparent.");
|
|
|
|
return true;
|
|
}
|
|
});
|
|
|
|
suite.addTestCase({
|
|
name: "View.removeSubview",
|
|
test() {
|
|
let parent = new WI.View;
|
|
let child = new WI.View;
|
|
parent.addSubview(child);
|
|
parent.removeSubview(child);
|
|
InspectorTest.expectNull(child.parentView, "Removed view should not have a parent.");
|
|
InspectorTest.expectThat(!child.isDescendantOf(parent), "Removed view should not be a descendant of the parent.");
|
|
InspectorTest.expectThat(!parent.subviews.includes(child), "Removed view should not be included in the parent's subviews.");
|
|
|
|
let previousSubviews = parent.subviews.slice();
|
|
parent.removeSubview(new WI.View);
|
|
InspectorTest.expectShallowEqual(previousSubviews, parent.subviews, "Removing a nonexistent view should have no effect.")
|
|
|
|
return true;
|
|
}
|
|
});
|
|
|
|
suite.addTestCase({
|
|
name: "View.removeSubview.IndirectDescendant",
|
|
test() {
|
|
let parent = new WI.View;
|
|
let middleElement = parent.element.appendChild(document.createElement("div"));
|
|
|
|
let child = new WI.View;
|
|
middleElement.appendChild(child.element);
|
|
|
|
parent.addSubview(child);
|
|
parent.removeSubview(child);
|
|
|
|
InspectorTest.expectFalse(child.element.parentNode, "Removed view should not be in the DOM.");
|
|
|
|
return true;
|
|
}
|
|
});
|
|
|
|
suite.addTestCase({
|
|
name: "View.insertSubviewBefore",
|
|
test() {
|
|
let parent = new WI.View;
|
|
let child1 = new WI.View;
|
|
let child2 = new WI.View;
|
|
parent.insertSubviewBefore(child1);
|
|
InspectorTest.expectEqual(parent.subviews[0], child1, "Inserting a view before `null` should append the view.");
|
|
|
|
parent.insertSubviewBefore(child2, child1);
|
|
InspectorTest.expectEqual(parent.subviews[0], child2, "child2 should be inserted before dhild1.");
|
|
InspectorTest.expectEqual(parent.subviews[1], child1, "child1 should be after child2.");
|
|
|
|
let previousSubviews = parent.subviews.slice();
|
|
parent.insertSubviewBefore(child2, new WI.View);
|
|
InspectorTest.expectShallowEqual(parent.subviews, previousSubviews, "Inserting a view before a nonexistent view should have no effect.");
|
|
|
|
return true;
|
|
}
|
|
});
|
|
|
|
suite.addTestCase({
|
|
name: "View.replaceSubview",
|
|
test() {
|
|
let parent = new WI.View;
|
|
let child1 = new WI.View;
|
|
let child2 = new WI.View;
|
|
parent.addSubview(child1);
|
|
parent.replaceSubview(child1, child2);
|
|
InspectorTest.expectNull(child1.parentView, "Replaced view should not have a parent.");
|
|
InspectorTest.expectEqual(child2.parentView, parent, "New view should have the correct parent.");
|
|
|
|
let previousSubviews = parent.subviews.slice();
|
|
parent.replaceSubview(child2, child2);
|
|
InspectorTest.expectShallowEqual(parent.subviews, previousSubviews, "Replacing a view with itself should have no effect.");
|
|
|
|
previousSubviews = parent.subviews;
|
|
parent.replaceSubview(new WI.View, child1);
|
|
InspectorTest.expectShallowEqual(parent.subviews, previousSubviews, "Replacing a nonexistent view should have no effect.");
|
|
|
|
return true;
|
|
}
|
|
});
|
|
|
|
suite.addTestCase({
|
|
name: "View.isAttached",
|
|
test() {
|
|
let view = new WI.View;
|
|
WI.View.rootView().addSubview(view);
|
|
InspectorTest.expectThat(view.isAttached, "View added to the root should be attached.");
|
|
|
|
WI.View.rootView().removeSubview(view);
|
|
InspectorTest.expectFalse(view.isAttached, "View removed from the root should not be attached.");
|
|
|
|
let parent = new WI.View;
|
|
let child = new WI.View;
|
|
parent.addSubview(child);
|
|
InspectorTest.expectFalse(child.isAttached, "View added to a detached parent should not be attached.");
|
|
WI.View.rootView().addSubview(parent);
|
|
InspectorTest.expectThat(child.isAttached, "Attaching a view to the root causes descendent views to be attached.");
|
|
WI.View.rootView().removeSubview(parent);
|
|
InspectorTest.expectFalse(child.isAttached, "Detaching a view from the root causes descendent views to be detached.");
|
|
|
|
return true;
|
|
}
|
|
});
|
|
|
|
suite.addTestCase({
|
|
name: "View.fromElement",
|
|
test() {
|
|
let view = new WI.View;
|
|
InspectorTest.expectEqual(WI.View.fromElement(view.element), view, "Should be able to lookup an existing view from its element.");
|
|
InspectorTest.expectNull(WI.View.fromElement(document.createElement("div")), "Should return null for an element not associated with any view.");
|
|
InspectorTest.expectNull(WI.View.fromElement({}), "Should return null for non-element.");
|
|
InspectorTest.expectNull(WI.View.fromElement(null), "Should return null for null element.");
|
|
|
|
return true;
|
|
}
|
|
});
|
|
|
|
suite.runTestCasesAndFinish();
|
|
}
|
|
</script>
|
|
</head>
|
|
<body onload="runTest()">
|
|
<p>Testing basic View operations: root view access, view creation, and subview management.</p>
|
|
</body>
|
|
</html>
|