| <!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.addTestCase({ |
| name: "View.DirtyDescendantsCount", |
| test() { |
| let rootView = WI.View.rootView(); |
| let parent = new WI.View; |
| let child = new WI.View; |
| |
| // The root view may still be dirty from a previous test removing a subview. |
| rootView._dirty = false; |
| rootView._dirtyDescendantsCount = 0; |
| |
| InspectorTest.log("- Adding parent view to root view."); |
| rootView.addSubview(parent); |
| InspectorTest.expectEqual(rootView._dirtyDescendantsCount, 1, "Root view should have 1 dirty descendant."); |
| InspectorTest.expectFalse(rootView._dirty, "Root view should not be dirty."); |
| InspectorTest.expectTrue(parent._dirty, "Parent attached to root view should be dirty."); |
| |
| InspectorTest.log("- Adding child view to parent view."); |
| parent.addSubview(child); |
| InspectorTest.expectEqual(rootView._dirtyDescendantsCount, 2, "Root view should have 2 dirty descendants."); |
| InspectorTest.expectEqual(parent._dirtyDescendantsCount, 1, "Parent attached to root view should have 1 dirty descendant."); |
| InspectorTest.expectFalse(rootView._dirty, "Root view should not be dirty."); |
| InspectorTest.expectTrue(parent._dirty, "Parent attached to root view should be dirty."); |
| InspectorTest.expectTrue(child._dirty, "Child attached to parent view should be dirty."); |
| |
| InspectorTest.log("- Removing parent view from root view."); |
| rootView.removeSubview(parent); |
| InspectorTest.expectEqual(rootView._dirtyDescendantsCount, 0, "Root view should have 0 dirty descendants."); |
| InspectorTest.expectEqual(parent._dirtyDescendantsCount, 0, "Parent detached from root view should have 0 dirty descendants."); |
| InspectorTest.expectFalse(rootView._dirty, "Root view should not be dirty."); |
| InspectorTest.expectFalse(parent._dirty, "Parent detached from root view should not be dirty."); |
| InspectorTest.expectFalse(child._dirty, "Child attached to detached parent view should not be dirty."); |
| |
| InspectorTest.log("- Adding parent view to root view."); |
| rootView.addSubview(parent); |
| InspectorTest.expectEqual(rootView._dirtyDescendantsCount, 1, "Root view should have 1 dirty descendant."); |
| InspectorTest.expectEqual(parent._dirtyDescendantsCount, 0, "Parent detached from root view should have 0 dirty descendants."); |
| InspectorTest.expectFalse(rootView._dirty, "Root view should not be dirty."); |
| InspectorTest.expectTrue(parent._dirty, "Parent attached to root view should be dirty."); |
| InspectorTest.expectFalse(child._dirty, "Child attached to parent view should not be dirty."); |
| |
| // Clean up views for the next test. |
| rootView.removeSubview(parent); |
| |
| 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> |