bug1093686_inner.html (3047B)
1 <!DOCTYPE HTML> 2 <html id="html" style="height:100%"> 3 <head> 4 <title>Testing effect of listener on body</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script src="/tests/SimpleTest/EventUtils.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 8 <style> 9 .target { position:absolute; left:200px; top:200px; width:200px; height:200px; background:blue; } 10 </style> 11 </head> 12 <body id="body" onload="setTimeout(runTest, 0)" style="margin:0; width:100%; height:100%; overflow:hidden"> 13 <div id="content"> 14 <div class="target" id="t"></div> 15 </div> 16 <pre id="test"> 17 <script type="application/javascript"> 18 var eventTarget; 19 window.onmousedown = function(event) { eventTarget = event.target; }; 20 21 // Make sure the target div is "clickable" by adding a click listener on it. 22 document.getElementById('t').addEventListener('click', function(e) { 23 parent.ok(true, "target was clicked on"); 24 }); 25 26 // Helper functions 27 28 function testMouseClick(aX, aY, aExpectedId, aMsg) { 29 eventTarget = null; 30 synthesizeMouseAtPoint(aX, aY, {}); 31 try { 32 parent.is(eventTarget.id, aExpectedId, 33 "checking offset " + aX + "," + aY + " hit " + aExpectedId + " [" + aMsg + "]"); 34 } catch (ex) { 35 parent.ok(false, "checking offset " + aX + "," + aY + " hit " + aExpectedId + " [" + aMsg + "]; got " + eventTarget); 36 } 37 } 38 39 function testWithAndWithoutBodyListener(aX, aY, aExpectedId, aMsg) { 40 var func = function(e) { 41 // no-op function 42 parent.ok(true, "body was clicked on"); 43 }; 44 testMouseClick(aX, aY, aExpectedId, aMsg + " without listener on body"); 45 document.body.addEventListener("click", func); 46 testMouseClick(aX, aY, aExpectedId, aMsg + " with listener on body"); 47 document.body.removeEventListener("click", func); 48 } 49 50 // Main tests 51 52 var mm; 53 function runTest() { 54 mm = SpecialPowers.getDOMWindowUtils(parent).physicalMillimeterInCSSPixels; 55 parent.ok(4*mm >= 10, "WARNING: mm " + mm + " too small in this configuration. Test results will be bogus"); 56 57 // Test near the target, check it hits the target 58 testWithAndWithoutBodyListener(200 - 2*mm, 200 - 2*mm, "t", "basic click retargeting"); 59 // Test on the target, check it hits the target 60 testWithAndWithoutBodyListener(200 + 2*mm, 200 + 2*mm, "t", "direct click"); 61 // Test outside the target, check it hits the root 62 testWithAndWithoutBodyListener(40, 40, "body", "click way outside target"); 63 64 SpecialPowers.pushPrefEnv({"set": [["ui.mouse.radius.enabled", false]]}, runTest2); 65 } 66 67 function runTest2() { 68 // In this test, mouse event retargeting is disabled. 69 70 // Test near the target, check it hits the body 71 testWithAndWithoutBodyListener(200 - 2*mm, 200 - 2*mm, "body", "basic click retargeting"); 72 // Test on the target, check it hits the target 73 testWithAndWithoutBodyListener(200 + 2*mm, 200 + 2*mm, "t", "direct click"); 74 // Test outside the target, check it hits the root 75 testWithAndWithoutBodyListener(40, 40, "body", "click way outside target"); 76 77 parent.finishTest(); 78 } 79 80 </script> 81 </pre> 82 </body> 83 </html>