dom/tests/mochitest/chrome/selectAtPoint.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <title>nsIDOMWindowUtils::selectAtPoint test</title>
michael@0 5 <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
michael@0 7
michael@0 8 <script type="application/javascript;version=1.8">
michael@0 9 let SimpleTest = window.opener.SimpleTest;
michael@0 10 let Ci = Components.interfaces;
michael@0 11
michael@0 12 function ok() { window.opener.ok.apply(window.opener, arguments); }
michael@0 13 function done() { window.opener.done.apply(window.opener, arguments); }
michael@0 14
michael@0 15 function dumpLn() {
michael@0 16 for (let idx = 0; idx < arguments.length; idx++)
michael@0 17 dump(arguments[idx] + " ");
michael@0 18 dump("\n");
michael@0 19 }
michael@0 20
michael@0 21 function getCharacterDims() {
michael@0 22 let span = document.getElementById("measure");
michael@0 23 let rect = span.getBoundingClientRect();
michael@0 24 return { width: rect.right - rect.left,
michael@0 25 height: rect.bottom - rect.top };
michael@0 26 }
michael@0 27
michael@0 28 function setStart(aDWU, aX, aY, aSelectType)
michael@0 29 {
michael@0 30 // Clear any existing selection
michael@0 31 let selection = document.getSelection();
michael@0 32 selection.removeAllRanges();
michael@0 33
michael@0 34 // Select text
michael@0 35 let result = aDWU.selectAtPoint(aX, aY, aSelectType);
michael@0 36 ok(result == true, "selectAtPoint secceeded?");
michael@0 37 }
michael@0 38
michael@0 39 function setEnd(aDWU, aX, aY, aSelectType)
michael@0 40 {
michael@0 41 // Select text
michael@0 42 let result = aDWU.selectAtPoint(aX, aY, aSelectType);
michael@0 43 ok(result == true, "selectAtPoint secceeded?");
michael@0 44 }
michael@0 45
michael@0 46 function setSingle(aDWU, aX, aY, aSelectType, aSelectTypeStr, aExpectedSelectionText) {
michael@0 47 // Clear any existing selection
michael@0 48 let selection = document.getSelection();
michael@0 49 selection.removeAllRanges();
michael@0 50
michael@0 51 // Select text
michael@0 52 let result = aDWU.selectAtPoint(aX, aY, aSelectType);
michael@0 53 ok(result == true, "selectAtPoint secceeded?");
michael@0 54 }
michael@0 55
michael@0 56 function checkSelection(aDoc, aSelectTypeStr, aExpectedSelectionText) {
michael@0 57 // Retrieve text selected
michael@0 58 let selection = aDoc.getSelection();
michael@0 59 let text = selection.toString();
michael@0 60
michael@0 61 // Test
michael@0 62 let result = (text == aExpectedSelectionText);
michael@0 63 ok(result, aSelectTypeStr + " selection text matches?");
michael@0 64 if (!result) {
michael@0 65 dumpLn(aSelectTypeStr + " selection text:", "[" + text + "] expected:[" + aExpectedSelectionText + "]" );
michael@0 66 }
michael@0 67 }
michael@0 68
michael@0 69 function doTest() {
michael@0 70 let dwu = window.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 71 .getInterface(Ci.nsIDOMWindowUtils);
michael@0 72
michael@0 73 let os = Components.classes["@mozilla.org/xre/app-info;1"]
michael@0 74 .getService(Ci.nsIXULRuntime).OS;
michael@0 75 let isLinux = (os == "Linux");
michael@0 76 let isMac = (os == "Darwin");
michael@0 77 let isWindows = (os == "WINNT");
michael@0 78
michael@0 79 if (!isLinux && !isMac && !isWindows) {
michael@0 80 done();
michael@0 81 return;
michael@0 82 }
michael@0 83
michael@0 84 window.scrollTo(0, 0);
michael@0 85
michael@0 86 // Trick to get character spacing - get the bounds around a
michael@0 87 // single character trapped in a div.
michael@0 88 let charDims = getCharacterDims();
michael@0 89 // dumpLn("character dims:", charDims.width, charDims.height);
michael@0 90
michael@0 91 //
michael@0 92 // Root frame selection
michael@0 93 //
michael@0 94
michael@0 95 // First div in the main page
michael@0 96
michael@0 97 let div = document.getElementById("div1");
michael@0 98 let rect = div.getBoundingClientRect();
michael@0 99
michael@0 100 // Centered on the first character in the sentence div
michael@0 101 let targetPoint = { xPos: rect.left + (charDims.width / 2),
michael@0 102 yPos: rect.top + (charDims.height / 2) };
michael@0 103
michael@0 104 setSingle(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_WORDNOSPACE);
michael@0 105 checkSelection(document, "SELECT_WORDNOSPACE", "ttestselection1");
michael@0 106 setSingle(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_WORD);
michael@0 107 if (isLinux || isMac) {
michael@0 108 checkSelection(document, "SELECT_WORD", "ttestselection1");
michael@0 109 } else if (isWindows) {
michael@0 110 checkSelection(document, "SELECT_WORD", "ttestselection1 ");
michael@0 111 }
michael@0 112 setSingle(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_PARAGRAPH);
michael@0 113 checkSelection(document, "SELECT_PARAGRAPH", "ttestselection1 Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut. Ne labore incorrupte vix. Cu copiosae postulant tincidunt ius, in illud appetere contentiones eos. Ei munere officiis assentior pro, nibh decore ius at.");
michael@0 114
michael@0 115 // Centered on the second character in the sentence div
michael@0 116 let targetPoint = { xPos: rect.left + (charDims.width + (charDims.width / 2)),
michael@0 117 yPos: rect.top + (charDims.height / 2) };
michael@0 118 setSingle(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_CHARACTER);
michael@0 119 checkSelection(document, "SELECT_CHARACTER", "te");
michael@0 120 setSingle(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_CLUSTER);
michael@0 121 checkSelection(document, "SELECT_CLUSTER", "te");
michael@0 122
michael@0 123 // Separate character blocks in a word 't(te)s(ts)election1'
michael@0 124 let targetPoint = { xPos: rect.left + (charDims.width + (charDims.width / 2)),
michael@0 125 yPos: rect.top + (charDims.height / 2) };
michael@0 126 setStart(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_CHARACTER);
michael@0 127 let targetPoint = { xPos: rect.left + ((charDims.width * 4) + (charDims.width / 2)),
michael@0 128 yPos: rect.top + (charDims.height / 2) };
michael@0 129 setEnd(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_CHARACTER);
michael@0 130 if (isLinux || isMac) {
michael@0 131 // XXX I think this is a bug, the right hand selection is 4.5 characters over with a
michael@0 132 // monspaced font. what we want: t(te)s(ts)election1 what we get: t(te)st(se)lection1
michael@0 133 checkSelection(document, "split selection", "tese");
michael@0 134 } else if (isWindows) {
michael@0 135 checkSelection(document, "split selection", "tets");
michael@0 136 }
michael@0 137
michael@0 138 // Trying to select where there's no text, should fail but not throw
michael@0 139 let result = dwu.selectAtPoint(rect.left - 20, rect.top - 20, Ci.nsIDOMWindowUtils.SELECT_CHARACTER, false);
michael@0 140 ok(result == false, "couldn't select?");
michael@0 141
michael@0 142 // Second div in the main page
michael@0 143
michael@0 144 let div = document.getElementById("div2");
michael@0 145 let rect = div.getBoundingClientRect();
michael@0 146
michael@0 147 // Centered on the first line, first character in the paragraph div
michael@0 148 let targetPoint = { xPos: rect.left + (charDims.width / 2),
michael@0 149 yPos: rect.top + (charDims.height / 2) };
michael@0 150 setSingle(dwu, targetPoint.xPos + 50, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_PARAGRAPH);
michael@0 151 checkSelection(document, "SELECT_PARAGRAPH", "Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut. Ne labore incorrupte vix. Cu copiosae postulant tincidunt ius, in illud appetere contentiones eos.");
michael@0 152
michael@0 153 //
michael@0 154 // Inner IFRAME selection tests
michael@0 155 //
michael@0 156
michael@0 157 let frame = document.getElementById("frame1");
michael@0 158 let dwuFrame = frame.contentDocument
michael@0 159 .defaultView
michael@0 160 .QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 161 .getInterface(Ci.nsIDOMWindowUtils);
michael@0 162
michael@0 163 frame.contentWindow.scrollTo(0, 0);
michael@0 164
michael@0 165 let rect = frame.getBoundingClientRect();
michael@0 166
michael@0 167 let targetPoint = { xPos: charDims.width / 2,
michael@0 168 yPos: charDims.height / 2 };
michael@0 169 setSingle(dwuFrame, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_WORDNOSPACE);
michael@0 170 checkSelection(frame.contentWindow.document, "SELECT_WORDNOSPACE", "ttestselection2");
michael@0 171 setSingle(dwuFrame, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_WORD);
michael@0 172 if (isLinux || isMac) {
michael@0 173 checkSelection(frame.contentWindow.document, "SELECT_WORD", "ttestselection2");
michael@0 174 } else if (isWindows) {
michael@0 175 checkSelection(frame.contentWindow.document, "SELECT_WORD", "ttestselection2 ");
michael@0 176 }
michael@0 177 setSingle(dwuFrame, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_PARAGRAPH);
michael@0 178 checkSelection(frame.contentWindow.document, "SELECT_PARAGRAPH", "ttestselection2 Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut.");
michael@0 179
michael@0 180 // Outside the frame should throw. This is a failure in coordinate setup of
michael@0 181 // nsDOMWindowUtils::SelectAtPoint.
michael@0 182 let thr = false;
michael@0 183 try {
michael@0 184 dwuFrame.selectAtPoint(rect.right + 50, rect.top, Ci.nsIDOMWindowUtils.SELECT_WORD, false);
michael@0 185 } catch (ex) { thr = true; }
michael@0 186 ok(thr == true, "selectAtPoint expected throw?");
michael@0 187
michael@0 188 done();
michael@0 189 }
michael@0 190
michael@0 191 let frameLoad = false;
michael@0 192 let pageLoad = false;
michael@0 193 let painted = false;
michael@0 194 function testReady() {
michael@0 195 if (frameLoad && pageLoad && painted)
michael@0 196 doTest();
michael@0 197 }
michael@0 198
michael@0 199 function onFrameLoad() {
michael@0 200 // Exit the onload handler before trying the test, because we need
michael@0 201 // to ensure that paint unsupression has happened.
michael@0 202 setTimeout(function() {
michael@0 203 frameLoad = true;
michael@0 204 testReady();
michael@0 205 }, 0);
michael@0 206 }
michael@0 207
michael@0 208 function onPageLoad() {
michael@0 209 // Exit the onload handler before trying the test, because we need
michael@0 210 // to ensure that paint unsupression has happened
michael@0 211 // XXXroc why do we need to separately test for the loading of the frame
michael@0 212 // and a paint? That should not be necessary for this test.
michael@0 213 setTimeout(function() {
michael@0 214 pageLoad = true;
michael@0 215 testReady();
michael@0 216 }, 0);
michael@0 217 }
michael@0 218
michael@0 219 function onPaint() {
michael@0 220 window.removeEventListener("MozAfterPaint", onPaint, false);
michael@0 221 painted = true;
michael@0 222 testReady();
michael@0 223 }
michael@0 224
michael@0 225 window.addEventListener("MozAfterPaint", onPaint, false);
michael@0 226 </script>
michael@0 227
michael@0 228 <style type="text/css">
michael@0 229
michael@0 230 body {
michael@0 231 font-family: monospace;
michael@0 232 margin-left: 40px;
michael@0 233 margin-top: 40px;
michael@0 234 padding: 0;
michael@0 235 }
michael@0 236
michael@0 237 #div1 {
michael@0 238 border: 1px solid red;
michael@0 239 width: 400px;
michael@0 240 height: 100px;
michael@0 241 }
michael@0 242
michael@0 243 #frame1 {
michael@0 244 display: block;
michael@0 245 height: 100px;
michael@0 246 width: 300px;
michael@0 247 border: 1px solid blue;
michael@0 248 padding: 0;
michael@0 249 margin: 0;
michael@0 250 }
michael@0 251
michael@0 252 #div2 {
michael@0 253 border: 1px solid green;
michael@0 254 }
michael@0 255
michael@0 256 #measure {
michael@0 257 padding: 0px;
michael@0 258 margin: 0px;
michael@0 259 border: 1px solid red;
michael@0 260 }
michael@0 261
michael@0 262 </style>
michael@0 263 </head>
michael@0 264 <body id="body" onload="onPageLoad();">
michael@0 265
michael@0 266 <div id="div1">ttestselection1 Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut. Ne labore incorrupte vix. Cu copiosae postulant tincidunt ius, in illud appetere contentiones eos. Ei munere officiis assentior pro, nibh decore ius at.</div>
michael@0 267
michael@0 268 <br />
michael@0 269
michael@0 270 <iframe id="frame1" src="data:text/html,<html><body style='margin: 0; padding: 0; font-family: monospace;' onload='window.parent.onFrameLoad();'><div id='sel2'>ttestselection2 Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut.</div><br/><br/></body></html>"></iframe>
michael@0 271
michael@0 272 <br/>
michael@0 273
michael@0 274 <div id="div2">Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut. Ne labore incorrupte vix. Cu copiosae postulant tincidunt ius, in illud appetere contentiones eos.</div>
michael@0 275
michael@0 276 <br />
michael@0 277
michael@0 278 <span id="measure">t</span>
michael@0 279
michael@0 280 </body>
michael@0 281 </html>

mercurial