toolkit/content/tests/chrome/test_mousecapture.xul

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 <?xml version="1.0"?>
     2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
     5 <window title="Mouse Capture Tests" align="start"
     6         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
     8 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
    11 <script>
    12 <![CDATA[
    14 SimpleTest.expectAssertions(6);
    16 SimpleTest.waitForExplicitFinish();
    18 var captureRetargetMode = false;
    19 var cachedMouseDown = null;
    20 var previousWidth = 0, originalWidth = 0;
    21 var loadInWindow = false;
    23 function splitterCallback(adjustment)
    24 {
    25   var newWidth = Number($("leftbox").width); // getBoundingClientRect().width;
    26   var expectedWidth = previousWidth + adjustment;
    27   if (expectedWidth > $("splitterbox").getBoundingClientRect().width)
    28     expectedWidth = $("splitterbox").getBoundingClientRect().width - $("splitter").getBoundingClientRect().width;
    29   is(newWidth, expectedWidth, "splitter left box size (" + adjustment + ")");
    30   previousWidth = newWidth;
    31 }
    33 function selectionCallback(adjustment)
    34 {
    35   if (adjustment == 4000) {
    36     is(frames[0].getSelection().toString(), "This is some text", "selection after drag (" + adjustment + ")");
    37     ok(frames[0].scrollY > 40, "selection caused scroll down (" + adjustment + ")");
    38   }
    39   else {
    40     if (adjustment == 0) {
    41       is(frames[0].getSelection().toString(), ".", "selection after drag (" + adjustment + ")");
    42     }
    43     is(frames[0].scrollY, 0, "selection scrollY (" + adjustment + ")");
    44   }
    45 }
    47 function framesetCallback(adjustment)
    48 {
    49   var newWidth = frames[1].frames[0].document.documentElement.clientWidth;
    50   var expectedWidth = originalWidth + adjustment;
    51   if (adjustment == 0)
    52     expectedWidth = originalWidth - 12;
    53   else if (expectedWidth >= 4000)
    54     expectedWidth = originalWidth * 2 - 2;
    56   is(newWidth, expectedWidth, "frameset after drag (" + adjustment + ")");
    57 }
    59 var otherWindow = null;
    61 function selectionScrollCheck()
    62 {
    63   var element = otherWindow.document.documentElement;
    65   var count = 0;
    66   function selectionScrollDone() {
    67     // wait for 6 scroll events to occur
    68     if (count++ < 6)
    69       return;
    71     otherWindow.removeEventListener("scroll", selectionScrollDone, false);
    73     var selectedText = otherWindow.getSelection().toString().replace(/\r/g, "");
    74     is(selectedText, "One\n\nTwo", "text is selected");
    76     // should have scrolled 20 pixels from the mousemove above and six extra
    77     // times from the selection scroll timer for a total of 140
    78     var oldScrollY = otherWindow.scrollY;
    79     is(otherWindow.scrollY, 140, "selection scroll position after timer");
    81     synthesizeMouse(element, 4, otherWindow.innerHeight + 25, { type: "mouseup" }, otherWindow);
    82     disableNonTestMouseEvents(false);
    83     otherWindow.close();
    85     if (loadInWindow) {
    86       SimpleTest.finish();
    87     }
    88     else {
    89       // now try again, but open the page in a new window
    90       loadInWindow = true;
    91       synthesizeMouse(document.getElementById("custom"), 2, 2, { type: "mousedown" });
    93       // check to ensure that selection dragging scrolls the right scrollable area
    94       otherWindow = window.open("data:text/html,<html><p>One</p><p style='margin-top: 200px;'>Two</p><p style='margin-top: 4000px'>This is some text</p></html>", "_blank", "width=200,height=200,scrollbars=yes");
    95       otherWindow.addEventListener("load", function() { SimpleTest.waitForFocus(selectionScrollCheck, otherWindow); }, false);
    96     }
    97   }
    99   SimpleTest.executeSoon(function () {
   100     disableNonTestMouseEvents(true);
   101     synthesizeMouse(element, 2, 2, { type: "mousedown" }, otherWindow);
   102     synthesizeMouse(element, 100, otherWindow.innerHeight + 20, { type: "mousemove" }, otherWindow);
   103     otherWindow.addEventListener("scroll", selectionScrollDone, false);
   104   });
   105 }
   107 function runTests()
   108 {
   109   previousWidth = $("leftbox").getBoundingClientRect().width;
   110   runCaptureTest($("splitter"), splitterCallback);
   112   var custom = document.getElementById("custom");
   113   runCaptureTest(custom);
   115   synthesizeMouseExpectEvent($("rightbox"), 2, 2, { type: "mousemove" },
   116                              $("rightbox"), "mousemove", "setCapture and releaseCapture");
   118   custom.setCapture();
   119   synthesizeMouseExpectEvent($("leftbox"), 2, 2, { type: "mousemove" },
   120                              $("leftbox"), "mousemove", "setCapture fails on non mousedown");
   122   var custom2 = document.getElementById("custom2");
   123   synthesizeMouse(custom2, 2, 2, { type: "mousedown" });
   124   synthesizeMouseExpectEvent($("leftbox"), 2, 2, { type: "mousemove" },
   125                              $("leftbox"), "mousemove", "document.releaseCapture releases capture");
   127   var custom3 = document.getElementById("custom3");
   128   synthesizeMouse(custom3, 2, 2, { type: "mousedown" });
   129   synthesizeMouseExpectEvent($("leftbox"), 2, 2, { type: "mousemove" },
   130                              $("leftbox"), "mousemove", "element.releaseCapture releases capture");
   132   var custom4 = document.getElementById("custom4");
   133   synthesizeMouse(custom4, 2, 2, { type: "mousedown" });
   134   synthesizeMouseExpectEvent($("leftbox"), 2, 2, { type: "mousemove" },
   135                              custom4, "mousemove", "element.releaseCapture during mousemove before releaseCapture");
   136   synthesizeMouseExpectEvent($("leftbox"), 2, 2, { type: "mousemove" },
   137                              $("leftbox"), "mousemove", "element.releaseCapture during mousemove after releaseCapture");
   139   var custom5 = document.getElementById("custom5");
   140   runCaptureTest(custom5);
   141   captureRetargetMode = true;
   142   runCaptureTest(custom5);
   143   captureRetargetMode = false;
   145   var custom6 = document.getElementById("custom6");
   146   synthesizeMouse(custom6, 2, 2, { type: "mousedown" });
   147   synthesizeMouseExpectEvent($("leftbox"), 2, 2, { type: "mousemove" },
   148                              $("leftbox"), "mousemove", "setCapture only works on elements in documents");
   149   synthesizeMouse(custom6, 2, 2, { type: "mouseup" });
   151   // test that mousedown on an image with setCapture followed by a big enough
   152   // mouse move does not start a drag (bug 517737)
   153   var image = document.getElementById("image");
   154   synthesizeMouse(image, 2, 2, { type: "mousedown" });
   155   synthesizeMouseExpectEvent($("leftbox"), 2, 2, { type: "mousemove" },
   156                              image, "mousemove", "setCapture works on images");
   157   synthesizeMouse(image, 2, 2, { type: "mouseup" });
   159   // save scroll
   160   var scrollX = parent ? parent.scrollX : 0;
   161   var scrollY = parent ? parent.scrollY : 0;
   163   var b = frames[0].document.getElementById("b");
   164 //  runCaptureTest(b, selectionCallback);
   166   // restore scroll
   167   if (parent) parent.scroll(scrollX, scrollY);
   169 //  frames[0].getSelection().collapseToStart();
   171   var body = frames[0].document.body;
   172   var fixed = frames[0].document.getElementById("fixed");
   173   function captureOnBody() { body.setCapture() }
   174   body.addEventListener("mousedown", captureOnBody, true);
   175   synthesizeMouse(body, 8, 8, { type: "mousedown" }, frames[0]);
   176   body.removeEventListener("mousedown", captureOnBody, true);
   177   synthesizeMouseExpectEvent(fixed, 2, 2, { type: "mousemove" },
   178                              fixed, "mousemove", "setCapture on body retargets to root node", frames[0]);
   179   synthesizeMouse(body, 8, 8, { type: "mouseup" }, frames[0]);
   181   previousWidth = frames[1].frames[0].document.documentElement.clientWidth;
   182   originalWidth = previousWidth;
   183   runCaptureTest(frames[1].document.documentElement.lastChild, framesetCallback);
   185   // ensure that clicking on an element where the frame disappears doesn't crash
   186   synthesizeMouse(frames[2].document.getElementById("input"), 8, 8, { type: "mousedown" }, frames[2]);
   187   synthesizeMouse(frames[2].document.getElementById("input"), 8, 8, { type: "mouseup" }, frames[2]);
   189   synthesizeMouse(document.getElementById("option3"), 2, 2, { type: "mousedown" });
   190   synthesizeMouse(document.getElementById("option3"), 2, 1000, { type: "mousemove" });
   191   var select = document.getElementById("select");
   192   is(select.selectedIndex, 9, "scroll select");
   193   synthesizeMouse(document.getElementById("select"), 2, 2, { type: "mouseup" });
   195   synthesizeMouse(custom, 2, 2, { type: "mousedown" });
   197   // check to ensure that selection dragging scrolls the right scrollable area.
   198   // This should open the page in a new tab.
   200   var topPos = window.innerHeight;
   201   otherWindow = window.open("data:text/html,<html><p>One</p><p style='margin-top: " + topPos + "'>Two</p><p style='margin-top: 4000px'>This is some text</p></html>", "_blank");
   202   otherWindow.addEventListener("load", function() { SimpleTest.waitForFocus(selectionScrollCheck, otherWindow); }, false);
   203 }
   205 function runCaptureTest(element, callback)
   206 {
   207   var expectedTarget = null;
   209   var win = element.ownerDocument.defaultView;
   211   function mouseMoved(event) {
   212     is(event.originalTarget, expectedTarget,
   213        expectedTarget.id + " target for point " + event.clientX + "," + event.clientY);
   214   }
   215   win.addEventListener("mousemove", mouseMoved, false);
   217   expectedTarget = element;
   219   var basepoint = element.localName == "frameset" ? 50 : 2;
   220   synthesizeMouse(element, basepoint, basepoint, { type: "mousedown" }, win);
   222   // in setCapture(true) mode, all events should fire on custom5. In
   223   // setCapture(false) mode, events can fire at a descendant
   224   if (expectedTarget == $("custom5") && !captureRetargetMode)
   225     expectedTarget = $("custom5spacer");
   227   // releaseCapture should do nothing for an element which isn't capturing
   228   $("splitterbox").releaseCapture();
   230   synthesizeMouse(element, basepoint + 2, basepoint + 2, { type: "mousemove" }, win);
   231   if (callback)
   232     callback(2);
   234   if (expectedTarget == $("custom5spacer") && !captureRetargetMode)
   235     expectedTarget = $("custom5inner");
   237   if (element.id == "b") {
   238     var tooltip = document.getElementById("tooltip");
   239     tooltip.openPopup();
   240     tooltip.hidePopup();
   241   }
   243   synthesizeMouse(element, basepoint + 25, basepoint + 25, { type: "mousemove" }, win);
   244   if (callback)
   245     callback(25);
   247   expectedTarget = element.localName == "b" ? win.document.documentElement : element;
   248   synthesizeMouse(element, basepoint + 4000, basepoint + 4000, { type: "mousemove" }, win);
   249   if (callback)
   250     callback(4000);
   251   synthesizeMouse(element, basepoint - 12, basepoint - 12, { type: "mousemove" }, win);
   252   if (callback)
   253     callback(-12);
   255   expectedTarget = element.localName == "frameset" ? element : win.document.documentElement;
   256   synthesizeMouse(element, basepoint + 30, basepoint + 30, { type: "mouseup" }, win);
   257   synthesizeMouse(win.document.documentElement, 2, 2, { type: "mousemove" }, win);
   258   if (callback)
   259     callback(0);
   261   win.removeEventListener("mousemove", mouseMoved, false);
   262 }
   264 SimpleTest.waitForFocus(runTests);
   266 ]]>
   267 </script>
   269 <tooltip id="tooltip">
   270   <label value="Test"/>
   271 </tooltip>
   273 <hbox id="splitterbox" style="margin-top: 5px;" onmousedown="this.setCapture()">
   274   <hbox id="leftbox" width="100" flex="1"/>
   275   <splitter id="splitter" height="5"/>
   276   <hbox id="rightbox" width="100" flex="1"/>
   277 </hbox>
   279 <vbox id="custom" width="10" height="10" onmousedown="this.setCapture(); cachedMouseDown = event;"/>
   280 <vbox id="custom2" width="10" height="10" onmousedown="this.setCapture(); document.releaseCapture();"/>
   281 <vbox id="custom3" width="10" height="10" onmousedown="this.setCapture(); this.releaseCapture();"/>
   282 <vbox id="custom4" width="10" height="10" onmousedown="this.setCapture();"
   283                                           onmousemove="this.releaseCapture();"/>
   284 <hbox id="custom5" width="40" height="40"
   285       onmousedown="this.setCapture(captureRetargetMode);">
   286   <spacer id="custom5spacer" width="5"/>
   287   <hbox id="custom5inner" width="35" height="35"/>
   288 </hbox>
   289 <vbox id="custom6" width="10" height="10"
   290       onmousedown="document.createElement('hbox').setCapture();"/>
   292 <hbox>
   293   <iframe width="100" height="100"
   294           src="data:text/html,%3Cbody style%3D'font-size%3A 40pt%3B'%3E.%3Cb id%3D'b'%3EThis%3C/b%3E is some text%3Cdiv id='fixed' style='position: fixed; left: 55px; top: 5px; width: 10px; height: 10px'%3E.%3C/div%3E%3C/body%3E"/>
   296   <iframe width="100" height="100"
   297           src="data:text/html,%3Cframeset cols='50%, 50%'%3E%3Cframe src='about:blank'%3E%3Cframe src='about:blank'%3E%3C/frameset%3E"/>
   299   <iframe width="100" height="100"
   300           src="data:text/html,%3Cinput id='input' onfocus='this.style.display = &quot;none&quot;' style='float: left;'>"/>
   302   <select id="select" xmlns="http://www.w3.org/1999/xhtml" size="4">
   303     <option id="option1">One</option>
   304     <option id="option2">Two</option>
   305     <option id="option3">Three</option>
   306     <option id="option4">Four</option>
   307     <option id="option5">Five</option>
   308     <option id="option6">Six</option>
   309     <option id="option7">Seven</option>
   310     <option id="option8">Eight</option>
   311     <option id="option9">Nine</option>
   312     <option id="option10">Ten</option>
   313   </select>
   314 </hbox>
   316 <hbox>
   317   <img id="image" xmlns="http://www.w3.org/1999/xhtml"
   318        onmousedown="this.setCapture();" onmouseup="this.releaseCapture();"
   319        ondragstart="ok(false, 'should not get a drag when a setCapture is active');"
   320        src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAAG0lEQVR42mP8z0A%2BYKJA76jmUc2jmkc1U0EzACKcASfOgGoMAAAAAElFTkSuQmCC"/>
   321 </hbox>
   323 <body id="body" xmlns="http://www.w3.org/1999/xhtml">
   324   <p id="display"/><div id="content" style="display: none"/><pre id="test"/>
   325 </body>
   327 </window>

mercurial