layout/generic/test/plugin_focus_helper.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <html>
     2 <head>
     3   <title>Test that clicking on plugins transfers focus correctly</title>
     4   <style>
     5   embed { width:200px; height:200px; }
     6   </style>
     7 </head>
     8 <body>
    10 <p><input type="text" id="input"></input>
    11 <p><embed id="p1" type="application/x-test" wmode="window" paintscript="didPaint('p1')">
    12    <embed id="p2" type="application/x-test" paintscript="didPaint('p2')"></p>
    14 <script type="text/javascript">
    15 var SimpleTest = window.opener.SimpleTest;
    16 var is = window.opener.is;
    17 var ok = window.opener.ok;
    18 var todo = window.opener.todo;
    19 var info = window.opener.info;
    21 SimpleTest.waitForExplicitFinish();
    23 // We don't want to trigger native mouse events until the document is fully
    24 // loaded and each plugin has painted once.
    25 var expectPaints = 2;
    26 var loaded = false;
    27 function didPaint(id) {
    28   ok(--expectPaints >= 0, "Got plugin painted event from "+id);
    29   document.getElementById(id).setAttribute("paintscript", null);
    30   if (expectPaints == 0) {
    31     if (document.readyState == "complete") {
    32       theTest();
    33     } else {
    34       info("Waiting for document load to continue");
    35       window.addEventListener("load", function() { theTest(); });
    36     }
    37   }
    38 }
    40 //
    41 // Begin the test
    42 //
    43 function theTest() {
    45 const Cc = SpecialPowers.Cc;
    46 const Ci = SpecialPowers.Ci;
    47 var gWindowUtils = SpecialPowers.wrap(window)
    48                                 .QueryInterface(Ci.nsIInterfaceRequestor)
    49                                 .getInterface(Ci.nsIDOMWindowUtils);
    51 var nativeMouseDown;
    52 var nativeMouseUp;
    54 function nativeClickElement(id) {
    55   return function() {
    57     var element = document.getElementById(id);
    58     var bounds = element.getBoundingClientRect();
    59     var x = (bounds.left + window.mozInnerScreenX + 10);
    60     var y = (bounds.top + window.mozInnerScreenY + 10);
    62     SimpleTest.executeSoon(function () {
    63       gWindowUtils.sendNativeMouseEvent(x, y, nativeMouseDown, 0, element);
    64       gWindowUtils.sendNativeMouseEvent(x, y, nativeMouseUp, 0, element);
    65     });
    66   };
    67 }
    69 function done() {
    70   // Something about this test is losing focus to the OS intermittently, remove
    71   // the windowed plugins and wait for this window to be focused before
    72   // continuing. (Bug 874843)
    73   for (var p of [ "p1", "p2" ]) {
    74     p = document.getElementById(p);
    75     p.parentNode.removeChild(p);
    76     p = null;
    77   }
    78   SimpleTest.waitForFocus(window.opener.childDone, window);
    79 }
    81 var step = 0;
    82 var steps = [
    83   { event:"focus", id:"input", action:nativeClickElement("p1") },
    84   { event:"blur", id:"input" },
    85   { event:"focus", id:"p1", action:nativeClickElement("p2") },
    86   { event:"blur", id:"p1" },
    87   { event:"focus", id:"p2", action:nativeClickElement("input") },
    88   { event:"blur", id:"p2" },
    89   { event:"focus", id:"input", action:done }
    90 ];
    92 function handleEvent(event) {
    93   if (step >= steps.length)
    94     return;
    96   var s = steps[step++];
    97   is(event.type, s.event, "Check event type");
    98   is(event.target.id, s.id, "Check event target");
   100   if (s.action) {
   101     SimpleTest.executeSoon(s.action);
   102   }
   103 }
   105 var elems = ["input", "p1", "p2"];
   106 for (var i = 0; i < elems.length; ++i) {
   107   var e = document.getElementById(elems[i]);
   108   e.addEventListener("focus", handleEvent, false);
   109   e.addEventListener("blur", handleEvent, false);
   110 }
   112 function doTest() {
   113   input.focus();
   114 }
   116 if (navigator.platform.indexOf("Mac") >= 0) {
   117   nativeMouseDown = 1; // NSLeftMouseDown
   118   nativeMouseUp = 2; // NSLeftMouseUp
   119   SimpleTest.waitForFocus(doTest, window);
   120 } else if (navigator.platform.indexOf("Win") >= 0) {
   121   nativeMouseDown = 2; // MOUSEEVENTF_LEFTDOWN
   122   nativeMouseUp = 4; // MOUSEEVENTF_LEFTUP
   123   SimpleTest.waitForFocus(doTest, window);
   124 } else {
   125   // XXX(johns): our gtk2 sendNativeMouseEvent doesn't support clicks
   126   todo(false, "Platform not supported");
   127   done();
   128 }
   130 } // function theTest
   131 </script>
   133 </body>
   134 </html>

mercurial