layout/generic/test/plugin_focus_helper.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/generic/test/plugin_focus_helper.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +<html>
     1.5 +<head>
     1.6 +  <title>Test that clicking on plugins transfers focus correctly</title>
     1.7 +  <style>
     1.8 +  embed { width:200px; height:200px; }
     1.9 +  </style>
    1.10 +</head>
    1.11 +<body>
    1.12 +
    1.13 +<p><input type="text" id="input"></input>
    1.14 +<p><embed id="p1" type="application/x-test" wmode="window" paintscript="didPaint('p1')">
    1.15 +   <embed id="p2" type="application/x-test" paintscript="didPaint('p2')"></p>
    1.16 +
    1.17 +<script type="text/javascript">
    1.18 +var SimpleTest = window.opener.SimpleTest;
    1.19 +var is = window.opener.is;
    1.20 +var ok = window.opener.ok;
    1.21 +var todo = window.opener.todo;
    1.22 +var info = window.opener.info;
    1.23 +
    1.24 +SimpleTest.waitForExplicitFinish();
    1.25 +
    1.26 +// We don't want to trigger native mouse events until the document is fully
    1.27 +// loaded and each plugin has painted once.
    1.28 +var expectPaints = 2;
    1.29 +var loaded = false;
    1.30 +function didPaint(id) {
    1.31 +  ok(--expectPaints >= 0, "Got plugin painted event from "+id);
    1.32 +  document.getElementById(id).setAttribute("paintscript", null);
    1.33 +  if (expectPaints == 0) {
    1.34 +    if (document.readyState == "complete") {
    1.35 +      theTest();
    1.36 +    } else {
    1.37 +      info("Waiting for document load to continue");
    1.38 +      window.addEventListener("load", function() { theTest(); });
    1.39 +    }
    1.40 +  }
    1.41 +}
    1.42 +
    1.43 +//
    1.44 +// Begin the test
    1.45 +//
    1.46 +function theTest() {
    1.47 +
    1.48 +const Cc = SpecialPowers.Cc;
    1.49 +const Ci = SpecialPowers.Ci;
    1.50 +var gWindowUtils = SpecialPowers.wrap(window)
    1.51 +                                .QueryInterface(Ci.nsIInterfaceRequestor)
    1.52 +                                .getInterface(Ci.nsIDOMWindowUtils);
    1.53 +
    1.54 +var nativeMouseDown;
    1.55 +var nativeMouseUp;
    1.56 +
    1.57 +function nativeClickElement(id) {
    1.58 +  return function() {
    1.59 +
    1.60 +    var element = document.getElementById(id);
    1.61 +    var bounds = element.getBoundingClientRect();
    1.62 +    var x = (bounds.left + window.mozInnerScreenX + 10);
    1.63 +    var y = (bounds.top + window.mozInnerScreenY + 10);
    1.64 +
    1.65 +    SimpleTest.executeSoon(function () {
    1.66 +      gWindowUtils.sendNativeMouseEvent(x, y, nativeMouseDown, 0, element);
    1.67 +      gWindowUtils.sendNativeMouseEvent(x, y, nativeMouseUp, 0, element);
    1.68 +    });
    1.69 +  };
    1.70 +}
    1.71 +
    1.72 +function done() {
    1.73 +  // Something about this test is losing focus to the OS intermittently, remove
    1.74 +  // the windowed plugins and wait for this window to be focused before
    1.75 +  // continuing. (Bug 874843)
    1.76 +  for (var p of [ "p1", "p2" ]) {
    1.77 +    p = document.getElementById(p);
    1.78 +    p.parentNode.removeChild(p);
    1.79 +    p = null;
    1.80 +  }
    1.81 +  SimpleTest.waitForFocus(window.opener.childDone, window);
    1.82 +}
    1.83 +
    1.84 +var step = 0;
    1.85 +var steps = [
    1.86 +  { event:"focus", id:"input", action:nativeClickElement("p1") },
    1.87 +  { event:"blur", id:"input" },
    1.88 +  { event:"focus", id:"p1", action:nativeClickElement("p2") },
    1.89 +  { event:"blur", id:"p1" },
    1.90 +  { event:"focus", id:"p2", action:nativeClickElement("input") },
    1.91 +  { event:"blur", id:"p2" },
    1.92 +  { event:"focus", id:"input", action:done }
    1.93 +];
    1.94 +
    1.95 +function handleEvent(event) {
    1.96 +  if (step >= steps.length)
    1.97 +    return;
    1.98 +
    1.99 +  var s = steps[step++];
   1.100 +  is(event.type, s.event, "Check event type");
   1.101 +  is(event.target.id, s.id, "Check event target");
   1.102 +
   1.103 +  if (s.action) {
   1.104 +    SimpleTest.executeSoon(s.action);
   1.105 +  }
   1.106 +}
   1.107 +
   1.108 +var elems = ["input", "p1", "p2"];
   1.109 +for (var i = 0; i < elems.length; ++i) {
   1.110 +  var e = document.getElementById(elems[i]);
   1.111 +  e.addEventListener("focus", handleEvent, false);
   1.112 +  e.addEventListener("blur", handleEvent, false);
   1.113 +}
   1.114 +
   1.115 +function doTest() {
   1.116 +  input.focus();
   1.117 +}
   1.118 +
   1.119 +if (navigator.platform.indexOf("Mac") >= 0) {
   1.120 +  nativeMouseDown = 1; // NSLeftMouseDown
   1.121 +  nativeMouseUp = 2; // NSLeftMouseUp
   1.122 +  SimpleTest.waitForFocus(doTest, window);
   1.123 +} else if (navigator.platform.indexOf("Win") >= 0) {
   1.124 +  nativeMouseDown = 2; // MOUSEEVENTF_LEFTDOWN
   1.125 +  nativeMouseUp = 4; // MOUSEEVENTF_LEFTUP
   1.126 +  SimpleTest.waitForFocus(doTest, window);
   1.127 +} else {
   1.128 +  // XXX(johns): our gtk2 sendNativeMouseEvent doesn't support clicks
   1.129 +  todo(false, "Platform not supported");
   1.130 +  done();
   1.131 +}
   1.132 +
   1.133 +} // function theTest
   1.134 +</script>
   1.135 +
   1.136 +</body>
   1.137 +</html>

mercurial