dom/tests/mochitest/chrome/selectAtPoint.html

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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.

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

mercurial