test_bug741666.html (6086B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=741666 5 --> 6 <head> 7 <title>Test for Bug 741666</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <script src="/tests/SimpleTest/EventUtils.js"></script> 10 <script src="/tests/SimpleTest/paint_listener.js"></script> 11 <script src="/tests/gfx/layers/apz/test/mochitest/apz_test_utils.js"></script> 12 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 13 </head> 14 <body> 15 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=741666">Mozilla Bug 741666</a> 16 <p id="display"></p> 17 <div id="content" style="display: none"> 18 19 </div> 20 <pre id="test"> 21 <script class="testbody" type="application/javascript"> 22 23 /** Test for Bug 306008 - Touch events with a reference held should retain their touch lists */ 24 25 let tests = [], testTarget, parent; 26 27 let touch = { 28 id: 0, 29 point: {x: 0, y: 0}, 30 radius: {x: 0, y: 0}, 31 rotation: 0, 32 force: 0.5, 33 target: null 34 } 35 36 function nextTest() { 37 if (tests.length) 38 SimpleTest.executeSoon(tests.shift()); 39 } 40 41 function checkEvent(aFakeEvent, aTouches) { 42 return function(aEvent, aTrusted) { 43 is(aFakeEvent.ctrlKey, aEvent.ctrlKey, "Correct ctrlKey"); 44 is(aFakeEvent.altKey, aEvent.altKey, "Correct altKey"); 45 is(aFakeEvent.shiftKey, aEvent.shiftKey, "Correct shiftKey"); 46 is(aFakeEvent.metaKey, aEvent.metaKey, "Correct metaKey"); 47 is(aEvent.isTrusted, aTrusted, "Event is trusted"); 48 checkTouches(aFakeEvent[aTouches], aEvent[aTouches]); 49 } 50 } 51 52 function checkTouches(aTouches1, aTouches2) { 53 is(aTouches1.length, aTouches2.length, "Correct touches length"); 54 for (var i = 0; i < aTouches1.length; i++) { 55 checkTouch(aTouches1[i], aTouches2[i]); 56 } 57 } 58 59 function checkTouch(aFakeTouch, aTouch) { 60 is(aFakeTouch.identifier, aTouch.identifier, "Touch has correct identifier"); 61 is(aFakeTouch.target, aTouch.target, "Touch has correct target"); 62 is(aFakeTouch.page.x, aTouch.pageX, "Touch has correct pageX"); 63 is(aFakeTouch.page.y, aTouch.pageY, "Touch has correct pageY"); 64 is(aFakeTouch.page.x + Math.round(window.mozInnerScreenX), aTouch.screenX, "Touch has correct screenX"); 65 is(aFakeTouch.page.y + Math.round(window.mozInnerScreenY), aTouch.screenY, "Touch has correct screenY"); 66 is(aFakeTouch.page.x, aTouch.clientX, "Touch has correct clientX"); 67 is(aFakeTouch.page.y, aTouch.clientY, "Touch has correct clientY"); 68 is(aFakeTouch.radius.x, aTouch.radiusX, "Touch has correct radiusX"); 69 is(aFakeTouch.radius.y, aTouch.radiusY, "Touch has correct radiusY"); 70 is(aFakeTouch.rotationAngle, aTouch.rotationAngle, "Touch has correct rotationAngle"); 71 is(aFakeTouch.force, aTouch.force, "Touch has correct force"); 72 } 73 74 function sendTouchEvent(windowUtils, aType, aEvent, aModifiers) { 75 var ids = [], xs=[], ys=[], rxs = [], rys = [], 76 rotations = [], forces = [], tiltXs = [], tiltYs = [], twists = []; 77 78 for (var touchType of ["touches", "changedTouches", "targetTouches"]) { 79 for (var i = 0; i < aEvent[touchType].length; i++) { 80 if (!ids.includes(aEvent[touchType][i].identifier)) { 81 ids.push(aEvent[touchType][i].identifier); 82 xs.push(aEvent[touchType][i].page.x); 83 ys.push(aEvent[touchType][i].page.y); 84 rxs.push(aEvent[touchType][i].radius.x); 85 rys.push(aEvent[touchType][i].radius.y); 86 rotations.push(aEvent[touchType][i].rotationAngle); 87 forces.push(aEvent[touchType][i].force); 88 tiltXs.push(0); 89 tiltYs.push(0); 90 twists.push(0); 91 } 92 } 93 } 94 return windowUtils.sendTouchEvent(aType, 95 ids, xs, ys, rxs, rys, 96 rotations, forces, tiltXs, tiltYs, twists, 97 aModifiers); 98 } 99 100 function touchEvent(aOptions) { 101 if (!aOptions) { 102 aOptions = {}; 103 } 104 this.ctrlKey = aOptions.ctrlKey || false; 105 this.altKey = aOptions.altKey || false; 106 this.shiftKey = aOptions.shiftKey || false; 107 this.metaKey = aOptions.metaKey || false; 108 this.touches = aOptions.touches || []; 109 this.targetTouches = aOptions.targetTouches || []; 110 this.changedTouches = aOptions.changedTouches || []; 111 } 112 113 function testtouch(aOptions) { 114 if (!aOptions) 115 aOptions = {}; 116 this.identifier = aOptions.identifier || 0; 117 this.target = aOptions.target || 0; 118 this.page = aOptions.page || {x: 0, y: 0}; 119 this.radius = aOptions.radius || {x: 0, y: 0}; 120 this.rotationAngle = aOptions.rotationAngle || 0; 121 this.force = aOptions.force || 1; 122 } 123 124 function testPreventDefault(name) { 125 let cwu = SpecialPowers.getDOMWindowUtils(window); 126 let target = document.getElementById("testTarget"); 127 let bcr = target.getBoundingClientRect(); 128 129 let touch1 = new testtouch({ 130 page: {x: Math.round(bcr.left + bcr.width/2), 131 y: Math.round(bcr.top + bcr.height/2)}, 132 target 133 }); 134 135 let event = new touchEvent({ 136 touches: [touch1], 137 targetTouches: [touch1], 138 changedTouches: [touch1] 139 }); 140 141 // test touchstart event fires correctly 142 var checkTouchesEvent = checkEvent(event, "touches"); 143 var checkTargetTouches = checkEvent(event, "targetTouches"); 144 145 /* This is the heart of the test. Verify that the touch event 146 looks correct both in and outside of a setTimeout */ 147 window.addEventListener("touchstart", function(firedEvent) { 148 checkTouchesEvent(firedEvent, true); 149 setTimeout(function() { 150 checkTouchesEvent(firedEvent, true); 151 checkTargetTouches(firedEvent, true); 152 153 event.touches = []; 154 event.targetTouches = []; 155 sendTouchEvent(cwu, "touchend", event, 0); 156 157 nextTest(); 158 }, 0); 159 }); 160 sendTouchEvent(cwu, "touchstart", event, 0); 161 } 162 163 async function doTest() { 164 await promiseApzFlushedRepaints(); 165 await waitUntilApzStable(); 166 167 tests.push(testPreventDefault); 168 169 tests.push(function() { 170 SimpleTest.finish(); 171 }); 172 173 nextTest(); 174 } 175 176 SimpleTest.waitForExplicitFinish(); 177 addLoadEvent(doTest); 178 179 </script> 180 </pre> 181 <div id="parent"> 182 <span id="testTarget" style="margin-left: 200px; padding: 5px; border: 1px solid black;">testTarget</span> 183 </div> 184 </body> 185 </html>