dom/imptests/editing/selecttest/test_addRange.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/imptests/editing/selecttest/test_addRange.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,177 @@
     1.4 +<!doctype html>
     1.5 +<title>Selection.addRange() tests</title>
     1.6 +<div id=log></div>
     1.7 +<script src=/resources/testharness.js></script>
     1.8 +<script src=/resources/testharnessreport.js></script>
     1.9 +<script src=common.js></script>
    1.10 +<script>
    1.11 +"use strict";
    1.12 +
    1.13 +function testAddRange(exception, range, endpoints, qualifier, testName) {
    1.14 +	test(function() {
    1.15 +		assert_equals(exception, null, "Test setup must not throw exceptions");
    1.16 +
    1.17 +		selection.addRange(range);
    1.18 +
    1.19 +		assert_equals(range.startContainer, endpoints[0],
    1.20 +			"addRange() must not modify the startContainer of the Range it's given");
    1.21 +		assert_equals(range.startOffset, endpoints[1],
    1.22 +			"addRange() must not modify the startOffset of the Range it's given");
    1.23 +		assert_equals(range.endContainer, endpoints[2],
    1.24 +			"addRange() must not modify the endContainer of the Range it's given");
    1.25 +		assert_equals(range.endOffset, endpoints[3],
    1.26 +			"addRange() must not modify the endOffset of the Range it's given");
    1.27 +	}, testName + ": " + qualifier + " addRange() must not throw exceptions or modify the range it's given");
    1.28 +
    1.29 +	test(function() {
    1.30 +		assert_equals(exception, null, "Test setup must not throw exceptions");
    1.31 +
    1.32 +		assert_equals(selection.rangeCount, 1, "rangeCount must be 1");
    1.33 +	}, testName + ": " + qualifier + " addRange() must result in rangeCount being 1");
    1.34 +
    1.35 +	// From here on out we check selection.getRangeAt(selection.rangeCount - 1)
    1.36 +	// so as not to double-fail Gecko.
    1.37 +
    1.38 +	test(function() {
    1.39 +		assert_equals(exception, null, "Test setup must not throw exceptions");
    1.40 +		assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0");
    1.41 +
    1.42 +		var newRange = selection.getRangeAt(selection.rangeCount - 1);
    1.43 +
    1.44 +		assert_not_equals(newRange, null,
    1.45 +			"getRangeAt(rangeCount - 1) must not return null");
    1.46 +		assert_equals(typeof newRange, "object",
    1.47 +			"getRangeAt(rangeCount - 1) must return an object");
    1.48 +
    1.49 +		assert_equals(newRange.startContainer, range.startContainer,
    1.50 +			"startContainer of the Selection's last Range must match the added Range");
    1.51 +		assert_equals(newRange.startOffset, range.startOffset,
    1.52 +			"startOffset of the Selection's last Range must match the added Range");
    1.53 +		assert_equals(newRange.endContainer, range.endContainer,
    1.54 +			"endContainer of the Selection's last Range must match the added Range");
    1.55 +		assert_equals(newRange.endOffset, range.endOffset,
    1.56 +			"endOffset of the Selection's last Range must match the added Range");
    1.57 +	}, testName + ": " + qualifier + " addRange() must result in the selection's last range having the specified endpoints");
    1.58 +
    1.59 +	test(function() {
    1.60 +		assert_equals(exception, null, "Test setup must not throw exceptions");
    1.61 +		assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0");
    1.62 +
    1.63 +		assert_equals(selection.getRangeAt(selection.rangeCount - 1), range,
    1.64 +			"getRangeAt(rangeCount - 1) must return the same object we added");
    1.65 +	}, testName + ": " + qualifier + " addRange() must result in the selection's last range being the same object we added");
    1.66 +
    1.67 +	// Let's not test many different modifications -- one should be enough.
    1.68 +	test(function() {
    1.69 +		assert_equals(exception, null, "Test setup must not throw exceptions");
    1.70 +		assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0");
    1.71 +
    1.72 +		if (range.startContainer == paras[0].firstChild
    1.73 +		&& range.startOffset == 0
    1.74 +		&& range.endContainer == paras[0].firstChild
    1.75 +		&& range.endOffset == 2) {
    1.76 +			// Just in case . . .
    1.77 +			range.setStart(paras[0].firstChild, 1);
    1.78 +		} else {
    1.79 +			range.setStart(paras[0].firstChild, 0);
    1.80 +			range.setEnd(paras[0].firstChild, 2);
    1.81 +		}
    1.82 +
    1.83 +		var newRange = selection.getRangeAt(selection.rangeCount - 1);
    1.84 +
    1.85 +		assert_equals(newRange.startContainer, range.startContainer,
    1.86 +			"After mutating the " + qualifier + " added Range, startContainer of the Selection's last Range must match the added Range");
    1.87 +		assert_equals(newRange.startOffset, range.startOffset,
    1.88 +			"After mutating the " + qualifier + " added Range, startOffset of the Selection's last Range must match the added Range");
    1.89 +		assert_equals(newRange.endContainer, range.endContainer,
    1.90 +			"After mutating the " + qualifier + " added Range, endContainer of the Selection's last Range must match the added Range");
    1.91 +		assert_equals(newRange.endOffset, range.endOffset,
    1.92 +			"After mutating the " + qualifier + " added Range, endOffset of the Selection's last Range must match the added Range");
    1.93 +	}, testName + ": modifying the " + qualifier + " added range must modify the Selection's last Range");
    1.94 +
    1.95 +	// Now test the other way too.
    1.96 +	test(function() {
    1.97 +		assert_equals(exception, null, "Test setup must not throw exceptions");
    1.98 +		assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0");
    1.99 +
   1.100 +		var newRange = selection.getRangeAt(selection.rangeCount - 1);
   1.101 +
   1.102 +		if (newRange.startContainer == paras[0].firstChild
   1.103 +		&& newRange.startOffset == 4
   1.104 +		&& newRange.endContainer == paras[0].firstChild
   1.105 +		&& newRange.endOffset == 6) {
   1.106 +			newRange.setStart(paras[0].firstChild, 5);
   1.107 +		} else {
   1.108 +			newRange.setStart(paras[0].firstChild, 4);
   1.109 +			newRange.setStart(paras[0].firstChild, 6);
   1.110 +		}
   1.111 +
   1.112 +		assert_equals(newRange.startContainer, range.startContainer,
   1.113 +			"After " + qualifier + " addRange(), after mutating the Selection's last Range, startContainer of the Selection's last Range must match the added Range");
   1.114 +		assert_equals(newRange.startOffset, range.startOffset,
   1.115 +			"After " + qualifier + " addRange(), after mutating the Selection's last Range, startOffset of the Selection's last Range must match the added Range");
   1.116 +		assert_equals(newRange.endContainer, range.endContainer,
   1.117 +			"After " + qualifier + " addRange(), after mutating the Selection's last Range, endContainer of the Selection's last Range must match the added Range");
   1.118 +		assert_equals(newRange.endOffset, range.endOffset,
   1.119 +			"After " + qualifier + " addRange(), after mutating the Selection's last Range, endOffset of the Selection's last Range must match the added Range");
   1.120 +	}, testName + ": modifying the Selection's last Range must modify the " + qualifier + " added Range");
   1.121 +}
   1.122 +
   1.123 +// Do only n evals, not n^2
   1.124 +var testRangesEvaled = testRanges.map(eval);
   1.125 +
   1.126 +for (var i = 0; i < testRanges.length; i++) {
   1.127 +	for (var j = 0; j < testRanges.length; j++) {
   1.128 +		var testName = "Range " + i + " " + testRanges[i]
   1.129 +			+ " followed by Range " + j + " " + testRanges[j];
   1.130 +
   1.131 +		var exception = null;
   1.132 +		try {
   1.133 +			selection.removeAllRanges();
   1.134 +
   1.135 +			var endpoints1 = testRangesEvaled[i];
   1.136 +			var range1 = ownerDocument(endpoints1[0]).createRange();
   1.137 +			range1.setStart(endpoints1[0], endpoints1[1]);
   1.138 +			range1.setEnd(endpoints1[2], endpoints1[3]);
   1.139 +
   1.140 +			if (range1.startContainer !== endpoints1[0]) {
   1.141 +				throw "Sanity check: the first Range we created must have the desired startContainer";
   1.142 +			}
   1.143 +			if (range1.startOffset !== endpoints1[1]) {
   1.144 +				throw "Sanity check: the first Range we created must have the desired startOffset";
   1.145 +			}
   1.146 +			if (range1.endContainer !== endpoints1[2]) {
   1.147 +				throw "Sanity check: the first Range we created must have the desired endContainer";
   1.148 +			}
   1.149 +			if (range1.endOffset !== endpoints1[3]) {
   1.150 +				throw "Sanity check: the first Range we created must have the desired endOffset";
   1.151 +			}
   1.152 +
   1.153 +			var endpoints2 = testRangesEvaled[j];
   1.154 +			var range2 = ownerDocument(endpoints2[0]).createRange();
   1.155 +			range2.setStart(endpoints2[0], endpoints2[1]);
   1.156 +			range2.setEnd(endpoints2[2], endpoints2[3]);
   1.157 +
   1.158 +			if (range2.startContainer !== endpoints2[0]) {
   1.159 +				throw "Sanity check: the second Range we created must have the desired startContainer";
   1.160 +			}
   1.161 +			if (range2.startOffset !== endpoints2[1]) {
   1.162 +				throw "Sanity check: the second Range we created must have the desired startOffset";
   1.163 +			}
   1.164 +			if (range2.endContainer !== endpoints2[2]) {
   1.165 +				throw "Sanity check: the second Range we created must have the desired endContainer";
   1.166 +			}
   1.167 +			if (range2.endOffset !== endpoints2[3]) {
   1.168 +				throw "Sanity check: the second Range we created must have the desired endOffset";
   1.169 +			}
   1.170 +		} catch (e) {
   1.171 +			exception = e;
   1.172 +		}
   1.173 +
   1.174 +		testAddRange(exception, range1, endpoints1, "first", testName);
   1.175 +		testAddRange(exception, range2, endpoints2, "second", testName);
   1.176 +	}
   1.177 +}
   1.178 +
   1.179 +testDiv.style.display = "none";
   1.180 +</script>

mercurial