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