|
1 <!doctype html> |
|
2 <title>Selection.addRange() tests</title> |
|
3 <div id=log></div> |
|
4 <script src=/resources/testharness.js></script> |
|
5 <script src=/resources/testharnessreport.js></script> |
|
6 <script src=common.js></script> |
|
7 <script> |
|
8 "use strict"; |
|
9 |
|
10 function testAddRange(exception, range, endpoints, qualifier, testName) { |
|
11 test(function() { |
|
12 assert_equals(exception, null, "Test setup must not throw exceptions"); |
|
13 |
|
14 selection.addRange(range); |
|
15 |
|
16 assert_equals(range.startContainer, endpoints[0], |
|
17 "addRange() must not modify the startContainer of the Range it's given"); |
|
18 assert_equals(range.startOffset, endpoints[1], |
|
19 "addRange() must not modify the startOffset of the Range it's given"); |
|
20 assert_equals(range.endContainer, endpoints[2], |
|
21 "addRange() must not modify the endContainer of the Range it's given"); |
|
22 assert_equals(range.endOffset, endpoints[3], |
|
23 "addRange() must not modify the endOffset of the Range it's given"); |
|
24 }, testName + ": " + qualifier + " addRange() must not throw exceptions or modify the range it's given"); |
|
25 |
|
26 test(function() { |
|
27 assert_equals(exception, null, "Test setup must not throw exceptions"); |
|
28 |
|
29 assert_equals(selection.rangeCount, 1, "rangeCount must be 1"); |
|
30 }, testName + ": " + qualifier + " addRange() must result in rangeCount being 1"); |
|
31 |
|
32 // From here on out we check selection.getRangeAt(selection.rangeCount - 1) |
|
33 // so as not to double-fail Gecko. |
|
34 |
|
35 test(function() { |
|
36 assert_equals(exception, null, "Test setup must not throw exceptions"); |
|
37 assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0"); |
|
38 |
|
39 var newRange = selection.getRangeAt(selection.rangeCount - 1); |
|
40 |
|
41 assert_not_equals(newRange, null, |
|
42 "getRangeAt(rangeCount - 1) must not return null"); |
|
43 assert_equals(typeof newRange, "object", |
|
44 "getRangeAt(rangeCount - 1) must return an object"); |
|
45 |
|
46 assert_equals(newRange.startContainer, range.startContainer, |
|
47 "startContainer of the Selection's last Range must match the added Range"); |
|
48 assert_equals(newRange.startOffset, range.startOffset, |
|
49 "startOffset of the Selection's last Range must match the added Range"); |
|
50 assert_equals(newRange.endContainer, range.endContainer, |
|
51 "endContainer of the Selection's last Range must match the added Range"); |
|
52 assert_equals(newRange.endOffset, range.endOffset, |
|
53 "endOffset of the Selection's last Range must match the added Range"); |
|
54 }, testName + ": " + qualifier + " addRange() must result in the selection's last range having the specified endpoints"); |
|
55 |
|
56 test(function() { |
|
57 assert_equals(exception, null, "Test setup must not throw exceptions"); |
|
58 assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0"); |
|
59 |
|
60 assert_equals(selection.getRangeAt(selection.rangeCount - 1), range, |
|
61 "getRangeAt(rangeCount - 1) must return the same object we added"); |
|
62 }, testName + ": " + qualifier + " addRange() must result in the selection's last range being the same object we added"); |
|
63 |
|
64 // Let's not test many different modifications -- one should be enough. |
|
65 test(function() { |
|
66 assert_equals(exception, null, "Test setup must not throw exceptions"); |
|
67 assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0"); |
|
68 |
|
69 if (range.startContainer == paras[0].firstChild |
|
70 && range.startOffset == 0 |
|
71 && range.endContainer == paras[0].firstChild |
|
72 && range.endOffset == 2) { |
|
73 // Just in case . . . |
|
74 range.setStart(paras[0].firstChild, 1); |
|
75 } else { |
|
76 range.setStart(paras[0].firstChild, 0); |
|
77 range.setEnd(paras[0].firstChild, 2); |
|
78 } |
|
79 |
|
80 var newRange = selection.getRangeAt(selection.rangeCount - 1); |
|
81 |
|
82 assert_equals(newRange.startContainer, range.startContainer, |
|
83 "After mutating the " + qualifier + " added Range, startContainer of the Selection's last Range must match the added Range"); |
|
84 assert_equals(newRange.startOffset, range.startOffset, |
|
85 "After mutating the " + qualifier + " added Range, startOffset of the Selection's last Range must match the added Range"); |
|
86 assert_equals(newRange.endContainer, range.endContainer, |
|
87 "After mutating the " + qualifier + " added Range, endContainer of the Selection's last Range must match the added Range"); |
|
88 assert_equals(newRange.endOffset, range.endOffset, |
|
89 "After mutating the " + qualifier + " added Range, endOffset of the Selection's last Range must match the added Range"); |
|
90 }, testName + ": modifying the " + qualifier + " added range must modify the Selection's last Range"); |
|
91 |
|
92 // Now test the other way too. |
|
93 test(function() { |
|
94 assert_equals(exception, null, "Test setup must not throw exceptions"); |
|
95 assert_not_equals(selection.rangeCount, 0, "Cannot proceed with tests if rangeCount is 0"); |
|
96 |
|
97 var newRange = selection.getRangeAt(selection.rangeCount - 1); |
|
98 |
|
99 if (newRange.startContainer == paras[0].firstChild |
|
100 && newRange.startOffset == 4 |
|
101 && newRange.endContainer == paras[0].firstChild |
|
102 && newRange.endOffset == 6) { |
|
103 newRange.setStart(paras[0].firstChild, 5); |
|
104 } else { |
|
105 newRange.setStart(paras[0].firstChild, 4); |
|
106 newRange.setStart(paras[0].firstChild, 6); |
|
107 } |
|
108 |
|
109 assert_equals(newRange.startContainer, range.startContainer, |
|
110 "After " + qualifier + " addRange(), after mutating the Selection's last Range, startContainer of the Selection's last Range must match the added Range"); |
|
111 assert_equals(newRange.startOffset, range.startOffset, |
|
112 "After " + qualifier + " addRange(), after mutating the Selection's last Range, startOffset of the Selection's last Range must match the added Range"); |
|
113 assert_equals(newRange.endContainer, range.endContainer, |
|
114 "After " + qualifier + " addRange(), after mutating the Selection's last Range, endContainer of the Selection's last Range must match the added Range"); |
|
115 assert_equals(newRange.endOffset, range.endOffset, |
|
116 "After " + qualifier + " addRange(), after mutating the Selection's last Range, endOffset of the Selection's last Range must match the added Range"); |
|
117 }, testName + ": modifying the Selection's last Range must modify the " + qualifier + " added Range"); |
|
118 } |
|
119 |
|
120 // Do only n evals, not n^2 |
|
121 var testRangesEvaled = testRanges.map(eval); |
|
122 |
|
123 for (var i = 0; i < testRanges.length; i++) { |
|
124 for (var j = 0; j < testRanges.length; j++) { |
|
125 var testName = "Range " + i + " " + testRanges[i] |
|
126 + " followed by Range " + j + " " + testRanges[j]; |
|
127 |
|
128 var exception = null; |
|
129 try { |
|
130 selection.removeAllRanges(); |
|
131 |
|
132 var endpoints1 = testRangesEvaled[i]; |
|
133 var range1 = ownerDocument(endpoints1[0]).createRange(); |
|
134 range1.setStart(endpoints1[0], endpoints1[1]); |
|
135 range1.setEnd(endpoints1[2], endpoints1[3]); |
|
136 |
|
137 if (range1.startContainer !== endpoints1[0]) { |
|
138 throw "Sanity check: the first Range we created must have the desired startContainer"; |
|
139 } |
|
140 if (range1.startOffset !== endpoints1[1]) { |
|
141 throw "Sanity check: the first Range we created must have the desired startOffset"; |
|
142 } |
|
143 if (range1.endContainer !== endpoints1[2]) { |
|
144 throw "Sanity check: the first Range we created must have the desired endContainer"; |
|
145 } |
|
146 if (range1.endOffset !== endpoints1[3]) { |
|
147 throw "Sanity check: the first Range we created must have the desired endOffset"; |
|
148 } |
|
149 |
|
150 var endpoints2 = testRangesEvaled[j]; |
|
151 var range2 = ownerDocument(endpoints2[0]).createRange(); |
|
152 range2.setStart(endpoints2[0], endpoints2[1]); |
|
153 range2.setEnd(endpoints2[2], endpoints2[3]); |
|
154 |
|
155 if (range2.startContainer !== endpoints2[0]) { |
|
156 throw "Sanity check: the second Range we created must have the desired startContainer"; |
|
157 } |
|
158 if (range2.startOffset !== endpoints2[1]) { |
|
159 throw "Sanity check: the second Range we created must have the desired startOffset"; |
|
160 } |
|
161 if (range2.endContainer !== endpoints2[2]) { |
|
162 throw "Sanity check: the second Range we created must have the desired endContainer"; |
|
163 } |
|
164 if (range2.endOffset !== endpoints2[3]) { |
|
165 throw "Sanity check: the second Range we created must have the desired endOffset"; |
|
166 } |
|
167 } catch (e) { |
|
168 exception = e; |
|
169 } |
|
170 |
|
171 testAddRange(exception, range1, endpoints1, "first", testName); |
|
172 testAddRange(exception, range2, endpoints2, "second", testName); |
|
173 } |
|
174 } |
|
175 |
|
176 testDiv.style.display = "none"; |
|
177 </script> |