Test selectAllChildren() better
authorAryeh Gregor <ayg@aryeh.name>
Thu, 12 Jan 2012 12:02:26 -0700
changeset 692 bf41eff7558d
parent 691 51785ce8f38d
child 693 4dc4d65cc87e
Test selectAllChildren() better

Now tests that existing ranges are replaced, not mutated. (This is true
in practice in browsers.)
selecttest/selectAllChildren.html
--- a/selecttest/selectAllChildren.html	Thu Jan 12 11:49:54 2012 -0700
+++ b/selecttest/selectAllChildren.html	Thu Jan 12 12:02:26 2012 -0700
@@ -6,36 +6,46 @@
 <script>
 "use strict";
 
-// TODO: Test what happens if there's an existing range in the selection.  Make
-// sure a new range is created, rather than the existing range being replaced.
-
-for (var i = 0; i < testNodes.length; i++) {
-	test(function() {
-		selection.removeAllRanges();
-
-		var node = eval(testNodes[i]);
+testRanges.unshift("[]");
 
-		if (node.nodeType == Node.DOCUMENT_TYPE_NODE) {
-			assert_throws("INVALID_NODE_TYPE_ERR", function() {
-				selection.selectAllChildren(node);
-			}, "selectAllChildren() on a DocumentType must throw InvalidNodeTypeError");
-			return;
-		}
+for (var i = 0; i < testRanges.length; i++) {
+	var endpoints = eval(testRanges[i]);
 
-		selection.selectAllChildren(node);
-		// This implicitly tests that the selection is forwards, by using
-		// anchorOffset/focusOffset instead of getRangeAt.
-		assert_equals(selection.rangeCount, 1,
-			"After selectAllChildren, rangeCount must be 1");
-		assert_equals(selection.anchorNode, node,
-			"After selectAllChildren, anchorNode must be the given node");
-		assert_equals(selection.anchorOffset, 0,
-			"After selectAllChildren, anchorOffset must be 0");
-		assert_equals(selection.focusNode, node,
-			"After selectAllChildren, focusNode must be the given node");
-		assert_equals(selection.focusOffset, node.childNodes.length,
-			"After selectAllChildren, focusOffset must be the given node's number of children");
-	}, "Node " + i + " " + testNodes[i]);
+	for (var j = 0; j < testNodes.length; j++) {
+		var node = eval(testNodes[j]);
+
+		test(function() {
+			setSelectionForwards(endpoints);
+			var originalRange = getSelection().rangeCount
+				? getSelection().getRangeAt(0)
+				: null;
+
+			if (node.nodeType == Node.DOCUMENT_TYPE_NODE) {
+				assert_throws("INVALID_NODE_TYPE_ERR", function() {
+					selection.selectAllChildren(node);
+				}, "selectAllChildren() on a DocumentType must throw InvalidNodeTypeError");
+				return;
+			}
+
+			selection.selectAllChildren(node);
+			// This implicitly tests that the selection is forwards, by using
+			// anchorOffset/focusOffset instead of getRangeAt.
+			assert_equals(selection.rangeCount, 1,
+				"After selectAllChildren, rangeCount must be 1");
+			assert_equals(selection.anchorNode, node,
+				"After selectAllChildren, anchorNode must be the given node");
+			assert_equals(selection.anchorOffset, 0,
+				"After selectAllChildren, anchorOffset must be 0");
+			assert_equals(selection.focusNode, node,
+				"After selectAllChildren, focusNode must be the given node");
+			assert_equals(selection.focusOffset, node.childNodes.length,
+				"After selectAllChildren, focusOffset must be the given node's number of children");
+			if (originalRange) {
+				assert_not_equals(getSelection().getRangeAt(0), originalRange,
+					"selectAllChildren must replace any existing range, not mutate it");
+			}
+		}, "Range " + i + " " + testRanges[i] + ", node " + j + " " + testNodes[j]);
+	}
 }
 
 testDiv.style.display = "none";