Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 <!doctype html>
2 <title>getSelection() tests</title>
3 <div id=log></div>
4 <script src=/resources/testharness.js></script>
5 <script src=/resources/testharnessreport.js></script>
6 <script>
7 "use strict";
9 // TODO: Figure out more places where defaultView is or is not guaranteed to be
10 // null, and test whether getSelection() is null.
11 //
12 // TODO: Figure out a good way to test display: none iframes.
14 test(function() {
15 // Sanity checks like this are to flag known browser bugs with clearer
16 // error messages, instead of throwing inscrutable exceptions.
17 assert_true("Selection" in window,
18 "Sanity check: window must have Selection property");
20 assert_true(window.getSelection() instanceof Selection);
21 }, "window.getSelection() instanceof Selection");
23 test(function() {
24 assert_equals(window.getSelection(), window.getSelection());
25 }, "window.getSelection() === window.getSelection()");
27 test(function() {
28 assert_true("Selection" in window,
29 "Sanity check: window must have Selection property");
30 // This sanity check (which occurs a number of times below, too) is because
31 // document.getSelection() is supposed to return null if defaultView is
32 // null, so we need to figure out whether defaultView is null or not before
33 // we can make correct assertions about getSelection().
34 assert_not_equals(document.defaultView, null,
35 "Sanity check: document.defaultView must not be null");
37 assert_equals(typeof document.getSelection(), "object",
38 "document.getSelection() must be an object");
39 assert_true(document.getSelection() instanceof Selection);
40 }, "document.getSelection() instanceof Selection");
42 test(function() {
43 assert_not_equals(document.defaultView, null,
44 "Sanity check: document.defaultView must not be null");
45 assert_equals(document.getSelection(), document.getSelection());
46 }, "document.getSelection() === document.getSelection()");
48 test(function() {
49 assert_not_equals(document.defaultView, null,
50 "Sanity check: document.defaultView must not be null");
51 assert_equals(window.getSelection(), document.getSelection());
52 }, "window.getSelection() === document.getSelection()");
54 // "Each selection is associated with a single range, which may be null and is
55 // initially null."
56 //
57 // "The rangeCount attribute must return 0 if the context object's range is
58 // null, otherwise 1."
59 test(function() {
60 assert_equals(window.getSelection().rangeCount, 0,
61 "window.getSelection().rangeCount must initially be 0");
62 assert_equals(typeof document.getSelection(), "object",
63 "Sanity check: document.getSelection() must be an object");
64 assert_equals(document.getSelection().rangeCount, 0,
65 "document.getSelection().rangeCount must initially be 0");
66 }, "Selection's range must initially be null");
68 test(function() {
69 var doc = document.implementation.createHTMLDocument("");
70 assert_equals(doc.defaultView, null,
71 "Sanity check: defaultView of created HTML document must be null");
72 assert_equals(doc.getSelection(), null);
73 }, "getSelection() on HTML document with null defaultView must be null");
75 test(function() {
76 var xmlDoc = document.implementation.createDocument(null, "", null);
78 assert_true("getSelection" in xmlDoc, "XML document must have getSelection()");
80 assert_equals(xmlDoc.defaultView, null,
81 "Sanity check: defaultView of created XML document must be null");
82 assert_equals(xmlDoc.getSelection(), null);
83 }, "getSelection() on XML document with null defaultView must be null");
86 // Run a bunch of iframe tests, once immediately after the iframe is appended
87 // to the document and once onload. This makes a difference, because browsers
88 // differ (at the time of this writing) in whether they load about:blank in
89 // iframes synchronously or not. Per the HTML spec, there must be a browsing
90 // context associated with the iframe as soon as it's appended to the document,
91 // so there should be a selection too.
92 var iframe = document.createElement("iframe");
93 add_completion_callback(function() {
94 document.body.removeChild(iframe);
95 });
97 var testDescs = [];
98 var testFuncs = [];
99 testDescs.push("window.getSelection() instanceof Selection in an iframe");
100 testFuncs.push(function() {
101 assert_true("Selection" in iframe.contentWindow,
102 "Sanity check: window must have Selection property");
103 assert_not_equals(iframe.contentWindow.document.defaultView, null,
104 "Sanity check: document.defaultView must not be null");
105 assert_not_equals(iframe.contentWindow.getSelection(), null,
106 "window.getSelection() must not be null");
107 assert_true(iframe.contentWindow.getSelection() instanceof iframe.contentWindow.Selection);
108 });
110 testDescs.push("document.getSelection() instanceof Selection in an iframe");
111 testFuncs.push(function() {
112 assert_true("Selection" in iframe.contentWindow,
113 "Sanity check: window must have Selection property");
114 assert_not_equals(iframe.contentDocument.defaultView, null,
115 "Sanity check: document.defaultView must not be null");
116 assert_not_equals(iframe.contentDocument.getSelection(), null,
117 "document.getSelection() must not be null");
118 assert_equals(typeof iframe.contentDocument.getSelection(), "object",
119 "document.getSelection() must be an object");
120 assert_true(iframe.contentDocument.getSelection() instanceof iframe.contentWindow.Selection);
121 });
123 testDescs.push("window.getSelection() === document.getSelection() in an iframe");
124 testFuncs.push(function() {
125 assert_not_equals(iframe.contentDocument.defaultView, null,
126 "Sanity check: document.defaultView must not be null");
127 assert_equals(iframe.contentWindow.getSelection(), iframe.contentDocument.getSelection());
128 });
130 testDescs.push("getSelection() inside and outside iframe must return different objects");
131 testFuncs.push(function() {
132 assert_not_equals(iframe.contentWindow.getSelection(), getSelection());
133 });
135 testDescs.push("getSelection() on HTML document with null defaultView must be null inside an iframe");
136 testFuncs.push(function() {
137 var doc = iframe.contentDocument.implementation.createHTMLDocument("");
138 assert_equals(doc.defaultView, null,
139 "Sanity check: defaultView of created HTML document must be null");
140 assert_equals(doc.getSelection(), null);
141 });
143 var asyncTests = [];
144 testDescs.forEach(function(desc) {
145 asyncTests.push(async_test(desc + " onload"));
146 });
148 iframe.onload = function() {
149 asyncTests.forEach(function(t, i) {
150 t.step(testFuncs[i]);
151 t.done();
152 });
153 };
155 document.body.appendChild(iframe);
157 testDescs.forEach(function(desc, i) {
158 test(testFuncs[i], desc + " immediately after appendChild");
159 });
160 </script>