1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/imptests/editing/selecttest/test_getSelection.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,160 @@ 1.4 +<!doctype html> 1.5 +<title>getSelection() 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> 1.10 +"use strict"; 1.11 + 1.12 +// TODO: Figure out more places where defaultView is or is not guaranteed to be 1.13 +// null, and test whether getSelection() is null. 1.14 +// 1.15 +// TODO: Figure out a good way to test display: none iframes. 1.16 + 1.17 +test(function() { 1.18 + // Sanity checks like this are to flag known browser bugs with clearer 1.19 + // error messages, instead of throwing inscrutable exceptions. 1.20 + assert_true("Selection" in window, 1.21 + "Sanity check: window must have Selection property"); 1.22 + 1.23 + assert_true(window.getSelection() instanceof Selection); 1.24 +}, "window.getSelection() instanceof Selection"); 1.25 + 1.26 +test(function() { 1.27 + assert_equals(window.getSelection(), window.getSelection()); 1.28 +}, "window.getSelection() === window.getSelection()"); 1.29 + 1.30 +test(function() { 1.31 + assert_true("Selection" in window, 1.32 + "Sanity check: window must have Selection property"); 1.33 + // This sanity check (which occurs a number of times below, too) is because 1.34 + // document.getSelection() is supposed to return null if defaultView is 1.35 + // null, so we need to figure out whether defaultView is null or not before 1.36 + // we can make correct assertions about getSelection(). 1.37 + assert_not_equals(document.defaultView, null, 1.38 + "Sanity check: document.defaultView must not be null"); 1.39 + 1.40 + assert_equals(typeof document.getSelection(), "object", 1.41 + "document.getSelection() must be an object"); 1.42 + assert_true(document.getSelection() instanceof Selection); 1.43 +}, "document.getSelection() instanceof Selection"); 1.44 + 1.45 +test(function() { 1.46 + assert_not_equals(document.defaultView, null, 1.47 + "Sanity check: document.defaultView must not be null"); 1.48 + assert_equals(document.getSelection(), document.getSelection()); 1.49 +}, "document.getSelection() === document.getSelection()"); 1.50 + 1.51 +test(function() { 1.52 + assert_not_equals(document.defaultView, null, 1.53 + "Sanity check: document.defaultView must not be null"); 1.54 + assert_equals(window.getSelection(), document.getSelection()); 1.55 +}, "window.getSelection() === document.getSelection()"); 1.56 + 1.57 +// "Each selection is associated with a single range, which may be null and is 1.58 +// initially null." 1.59 +// 1.60 +// "The rangeCount attribute must return 0 if the context object's range is 1.61 +// null, otherwise 1." 1.62 +test(function() { 1.63 + assert_equals(window.getSelection().rangeCount, 0, 1.64 + "window.getSelection().rangeCount must initially be 0"); 1.65 + assert_equals(typeof document.getSelection(), "object", 1.66 + "Sanity check: document.getSelection() must be an object"); 1.67 + assert_equals(document.getSelection().rangeCount, 0, 1.68 + "document.getSelection().rangeCount must initially be 0"); 1.69 +}, "Selection's range must initially be null"); 1.70 + 1.71 +test(function() { 1.72 + var doc = document.implementation.createHTMLDocument(""); 1.73 + assert_equals(doc.defaultView, null, 1.74 + "Sanity check: defaultView of created HTML document must be null"); 1.75 + assert_equals(doc.getSelection(), null); 1.76 +}, "getSelection() on HTML document with null defaultView must be null"); 1.77 + 1.78 +test(function() { 1.79 + var xmlDoc = document.implementation.createDocument(null, "", null); 1.80 + 1.81 + assert_true("getSelection" in xmlDoc, "XML document must have getSelection()"); 1.82 + 1.83 + assert_equals(xmlDoc.defaultView, null, 1.84 + "Sanity check: defaultView of created XML document must be null"); 1.85 + assert_equals(xmlDoc.getSelection(), null); 1.86 +}, "getSelection() on XML document with null defaultView must be null"); 1.87 + 1.88 + 1.89 +// Run a bunch of iframe tests, once immediately after the iframe is appended 1.90 +// to the document and once onload. This makes a difference, because browsers 1.91 +// differ (at the time of this writing) in whether they load about:blank in 1.92 +// iframes synchronously or not. Per the HTML spec, there must be a browsing 1.93 +// context associated with the iframe as soon as it's appended to the document, 1.94 +// so there should be a selection too. 1.95 +var iframe = document.createElement("iframe"); 1.96 +add_completion_callback(function() { 1.97 + document.body.removeChild(iframe); 1.98 +}); 1.99 + 1.100 +var testDescs = []; 1.101 +var testFuncs = []; 1.102 +testDescs.push("window.getSelection() instanceof Selection in an iframe"); 1.103 +testFuncs.push(function() { 1.104 + assert_true("Selection" in iframe.contentWindow, 1.105 + "Sanity check: window must have Selection property"); 1.106 + assert_not_equals(iframe.contentWindow.document.defaultView, null, 1.107 + "Sanity check: document.defaultView must not be null"); 1.108 + assert_not_equals(iframe.contentWindow.getSelection(), null, 1.109 + "window.getSelection() must not be null"); 1.110 + assert_true(iframe.contentWindow.getSelection() instanceof iframe.contentWindow.Selection); 1.111 +}); 1.112 + 1.113 +testDescs.push("document.getSelection() instanceof Selection in an iframe"); 1.114 +testFuncs.push(function() { 1.115 + assert_true("Selection" in iframe.contentWindow, 1.116 + "Sanity check: window must have Selection property"); 1.117 + assert_not_equals(iframe.contentDocument.defaultView, null, 1.118 + "Sanity check: document.defaultView must not be null"); 1.119 + assert_not_equals(iframe.contentDocument.getSelection(), null, 1.120 + "document.getSelection() must not be null"); 1.121 + assert_equals(typeof iframe.contentDocument.getSelection(), "object", 1.122 + "document.getSelection() must be an object"); 1.123 + assert_true(iframe.contentDocument.getSelection() instanceof iframe.contentWindow.Selection); 1.124 +}); 1.125 + 1.126 +testDescs.push("window.getSelection() === document.getSelection() in an iframe"); 1.127 +testFuncs.push(function() { 1.128 + assert_not_equals(iframe.contentDocument.defaultView, null, 1.129 + "Sanity check: document.defaultView must not be null"); 1.130 + assert_equals(iframe.contentWindow.getSelection(), iframe.contentDocument.getSelection()); 1.131 +}); 1.132 + 1.133 +testDescs.push("getSelection() inside and outside iframe must return different objects"); 1.134 +testFuncs.push(function() { 1.135 + assert_not_equals(iframe.contentWindow.getSelection(), getSelection()); 1.136 +}); 1.137 + 1.138 +testDescs.push("getSelection() on HTML document with null defaultView must be null inside an iframe"); 1.139 +testFuncs.push(function() { 1.140 + var doc = iframe.contentDocument.implementation.createHTMLDocument(""); 1.141 + assert_equals(doc.defaultView, null, 1.142 + "Sanity check: defaultView of created HTML document must be null"); 1.143 + assert_equals(doc.getSelection(), null); 1.144 +}); 1.145 + 1.146 +var asyncTests = []; 1.147 +testDescs.forEach(function(desc) { 1.148 + asyncTests.push(async_test(desc + " onload")); 1.149 +}); 1.150 + 1.151 +iframe.onload = function() { 1.152 + asyncTests.forEach(function(t, i) { 1.153 + t.step(testFuncs[i]); 1.154 + t.done(); 1.155 + }); 1.156 +}; 1.157 + 1.158 +document.body.appendChild(iframe); 1.159 + 1.160 +testDescs.forEach(function(desc, i) { 1.161 + test(testFuncs[i], desc + " immediately after appendChild"); 1.162 +}); 1.163 +</script>