1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/imptests/editing/selecttest/test_collapse.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +<!doctype html> 1.5 +<title>Selection.collapse() 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 testCollapse(range, point) { 1.14 + selection.removeAllRanges(); 1.15 + var addedRange; 1.16 + if (range) { 1.17 + addedRange = range.cloneRange(); 1.18 + selection.addRange(addedRange); 1.19 + } 1.20 + 1.21 + if (point[0].nodeType == Node.DOCUMENT_TYPE_NODE) { 1.22 + assert_throws("INVALID_NODE_TYPE_ERR", function() { 1.23 + selection.collapse(point[0], point[1]); 1.24 + }, "Must throw INVALID_NODE_TYPE_ERR when collapse()ing if the node is a DocumentType"); 1.25 + return; 1.26 + } 1.27 + 1.28 + if (point[1] < 0 || point[1] > getNodeLength(point[0])) { 1.29 + assert_throws("INDEX_SIZE_ERR", function() { 1.30 + selection.collapse(point[0], point[1]); 1.31 + }, "Must throw INDEX_SIZE_ERR when collapse()ing if the offset is negative or greater than the node's length"); 1.32 + return; 1.33 + } 1.34 + 1.35 + selection.collapse(point[0], point[1]); 1.36 + 1.37 + assert_equals(selection.rangeCount, 1, 1.38 + "selection.rangeCount must equal 1 after collapse()"); 1.39 + assert_equals(selection.focusNode, point[0], 1.40 + "focusNode must equal the node we collapse()d to"); 1.41 + assert_equals(selection.focusOffset, point[1], 1.42 + "focusOffset must equal the offset we collapse()d to"); 1.43 + assert_equals(selection.focusNode, selection.anchorNode, 1.44 + "focusNode and anchorNode must be equal after collapse()"); 1.45 + assert_equals(selection.focusOffset, selection.anchorOffset, 1.46 + "focusOffset and anchorOffset must be equal after collapse()"); 1.47 + if (range) { 1.48 + assert_equals(addedRange.startContainer, range.startContainer, 1.49 + "collapse() must not change the startContainer of a preexisting Range"); 1.50 + assert_equals(addedRange.endContainer, range.endContainer, 1.51 + "collapse() must not change the endContainer of a preexisting Range"); 1.52 + assert_equals(addedRange.startOffset, range.startOffset, 1.53 + "collapse() must not change the startOffset of a preexisting Range"); 1.54 + assert_equals(addedRange.endOffset, range.endOffset, 1.55 + "collapse() must not change the endOffset of a preexisting Range"); 1.56 + } 1.57 +} 1.58 + 1.59 +// Also test a selection with no ranges 1.60 +testRanges.unshift("[]"); 1.61 + 1.62 +// Don't want to eval() each point a bazillion times 1.63 +var testPointsCached = []; 1.64 +for (var i = 0; i < testPoints.length; i++) { 1.65 + testPointsCached.push(eval(testPoints[i])); 1.66 +} 1.67 + 1.68 +var tests = []; 1.69 +for (var i = 0; i < testRanges.length; i++) { 1.70 + var endpoints = eval(testRanges[i]); 1.71 + var range; 1.72 + test(function() { 1.73 + if (endpoints.length) { 1.74 + range = ownerDocument(endpoints[0]).createRange(); 1.75 + range.setStart(endpoints[0], endpoints[1]); 1.76 + range.setEnd(endpoints[2], endpoints[3]); 1.77 + } else { 1.78 + // Empty selection 1.79 + range = null; 1.80 + } 1.81 + }, "Set up range " + i + " " + testRanges[i]); 1.82 + for (var j = 0; j < testPoints.length; j++) { 1.83 + tests.push(["Range " + i + " " + testRanges[i] + ", point " + j + " " + testPoints[j], range, testPointsCached[j]]); 1.84 + } 1.85 +} 1.86 + 1.87 +generate_tests(testCollapse, tests); 1.88 + 1.89 +testDiv.style.display = "none"; 1.90 +</script>