Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <!-- |
michael@0 | 4 | https://bugzilla.mozilla.org/show_bug.cgi?id=508906 |
michael@0 | 5 | --> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Test for Bug 603008</title> |
michael@0 | 8 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 9 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 10 | </head> |
michael@0 | 11 | <body> |
michael@0 | 12 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=508906">Mozilla Bug 603008</a> |
michael@0 | 13 | <p id="display"></p> |
michael@0 | 14 | <div id="content" style="display: none"> |
michael@0 | 15 | |
michael@0 | 16 | </div> |
michael@0 | 17 | <pre id="test"> |
michael@0 | 18 | <script class="testbody" type="application/javascript;version=1.8"> |
michael@0 | 19 | |
michael@0 | 20 | /** Test for Bug 306008 - Touch* Events **/ |
michael@0 | 21 | |
michael@0 | 22 | let tests = [], testTarget, parent; |
michael@0 | 23 | |
michael@0 | 24 | let touch = { |
michael@0 | 25 | id: 0, |
michael@0 | 26 | point: {x: 0, y: 0}, |
michael@0 | 27 | radius: {x: 0, y: 0}, |
michael@0 | 28 | rotation: 0, |
michael@0 | 29 | force: 0.5, |
michael@0 | 30 | target: null |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | function nextTest() { |
michael@0 | 34 | if (tests.length) |
michael@0 | 35 | SimpleTest.executeSoon(tests.shift()); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | function random() { |
michael@0 | 39 | return Math.floor(Math.random() * 100); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function checkEvent(aFakeEvent) { |
michael@0 | 43 | return function(aEvent) { |
michael@0 | 44 | is(aFakeEvent.ctrlKey, aEvent.ctrlKey, "Correct ctrlKey"); |
michael@0 | 45 | is(aFakeEvent.altKey, aEvent.altKey, "Correct altKey"); |
michael@0 | 46 | is(aFakeEvent.shiftKey, aEvent.shiftKey, "Correct shiftKey"); |
michael@0 | 47 | is(aFakeEvent.metaKey, aEvent.metaKey, "Correct metaKey"); |
michael@0 | 48 | checkTouches(aFakeEvent.touches, aEvent.touches); |
michael@0 | 49 | checkTouches(aFakeEvent.targetTouches, aEvent.targetTouches); |
michael@0 | 50 | checkTouches(aFakeEvent.changedTouches, aEvent.changedTouches); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | function checkTouches(aTouches1, aTouches2) { |
michael@0 | 55 | is(aTouches1.length, aTouches2.length, "Correct touches length"); |
michael@0 | 56 | for (var i = 0; i < aTouches1.length; i++) { |
michael@0 | 57 | checkTouch(aTouches1[i], aTouches2[i]); |
michael@0 | 58 | } |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | function checkTouch(aFakeTouch, aTouch) { |
michael@0 | 62 | is(aFakeTouch.identifier, aTouch.identifier, "Touch has correct identifier"); |
michael@0 | 63 | is(aFakeTouch.target, aTouch.target, "Touch has correct target"); |
michael@0 | 64 | is(aFakeTouch.page.x, aTouch.pageX, "Touch has correct pageX"); |
michael@0 | 65 | is(aFakeTouch.page.y, aTouch.pageY, "Touch has correct pageY"); |
michael@0 | 66 | is(aFakeTouch.page.x + Math.round(window.mozInnerScreenX), aTouch.screenX, "Touch has correct screenX"); |
michael@0 | 67 | is(aFakeTouch.page.y + Math.round(window.mozInnerScreenY), aTouch.screenY, "Touch has correct screenY"); |
michael@0 | 68 | is(aFakeTouch.page.x, aTouch.clientX, "Touch has correct clientX"); |
michael@0 | 69 | is(aFakeTouch.page.y, aTouch.clientY, "Touch has correct clientY"); |
michael@0 | 70 | is(aFakeTouch.radius.x, aTouch.radiusX, "Touch has correct radiusX"); |
michael@0 | 71 | is(aFakeTouch.radius.y, aTouch.radiusY, "Touch has correct radiusY"); |
michael@0 | 72 | is(aFakeTouch.rotationAngle, aTouch.rotationAngle, "Touch has correct rotationAngle"); |
michael@0 | 73 | is(aFakeTouch.force, aTouch.force, "Touch has correct force"); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | function sendTouchEvent(windowUtils, aType, aEvent, aModifiers) { |
michael@0 | 77 | var ids = [], xs=[], ys=[], rxs = [], rys = [], |
michael@0 | 78 | rotations = [], forces = []; |
michael@0 | 79 | |
michael@0 | 80 | for (var touchType of ["touches", "changedTouches", "targetTouches"]) { |
michael@0 | 81 | for (var i = 0; i < aEvent[touchType].length; i++) { |
michael@0 | 82 | if (ids.indexOf(aEvent[touchType][i].identifier) == -1) { |
michael@0 | 83 | ids.push(aEvent[touchType][i].identifier); |
michael@0 | 84 | xs.push(aEvent[touchType][i].page.x); |
michael@0 | 85 | ys.push(aEvent[touchType][i].page.y); |
michael@0 | 86 | rxs.push(aEvent[touchType][i].radius.x); |
michael@0 | 87 | rys.push(aEvent[touchType][i].radius.y); |
michael@0 | 88 | rotations.push(aEvent[touchType][i].rotationAngle); |
michael@0 | 89 | forces.push(aEvent[touchType][i].force); |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | return windowUtils.sendTouchEvent(aType, |
michael@0 | 94 | ids, xs, ys, rxs, rys, |
michael@0 | 95 | rotations, forces, |
michael@0 | 96 | ids.length, aModifiers, 0); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | function touchEvent(aOptions) { |
michael@0 | 100 | if (!aOptions) { |
michael@0 | 101 | aOptions = {}; |
michael@0 | 102 | } |
michael@0 | 103 | this.ctrlKey = aOptions.ctrlKey || false; |
michael@0 | 104 | this.altKey = aOptions.altKey || false; |
michael@0 | 105 | this.shiftKey = aOptions.shiftKey || false; |
michael@0 | 106 | this.metaKey = aOptions.metaKey || false; |
michael@0 | 107 | this.touches = aOptions.touches || []; |
michael@0 | 108 | this.targetTouches = aOptions.targetTouches || []; |
michael@0 | 109 | this.changedTouches = aOptions.changedTouches || []; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | function testtouch(aOptions) { |
michael@0 | 113 | if (!aOptions) |
michael@0 | 114 | aOptions = {}; |
michael@0 | 115 | this.identifier = aOptions.identifier || 0; |
michael@0 | 116 | this.target = aOptions.target || 0; |
michael@0 | 117 | this.page = aOptions.page || {x: 0, y: 0}; |
michael@0 | 118 | this.radius = aOptions.radius || {x: 0, y: 0}; |
michael@0 | 119 | this.rotationAngle = aOptions.rotationAngle || 0; |
michael@0 | 120 | this.force = aOptions.force || 1; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | function testSingleTouch(name) { |
michael@0 | 124 | let cwu = SpecialPowers.getDOMWindowUtils(window); |
michael@0 | 125 | let target = document.getElementById("testTarget"); |
michael@0 | 126 | let target2 = document.getElementById("testTarget2"); |
michael@0 | 127 | let bcr = target.getBoundingClientRect(); |
michael@0 | 128 | let bcr2 = target2.getBoundingClientRect(); |
michael@0 | 129 | |
michael@0 | 130 | let touch1 = new testtouch({ |
michael@0 | 131 | page: {x: Math.round(bcr.left + bcr.width/2), |
michael@0 | 132 | y: Math.round(bcr.top + bcr.height/2)}, |
michael@0 | 133 | target: target |
michael@0 | 134 | }); |
michael@0 | 135 | let event = new touchEvent({ |
michael@0 | 136 | touches: [touch1], |
michael@0 | 137 | targetTouches: [touch1], |
michael@0 | 138 | changedTouches: [touch1] |
michael@0 | 139 | }); |
michael@0 | 140 | |
michael@0 | 141 | // test touchstart event fires correctly |
michael@0 | 142 | var checkFunction = checkEvent(event); |
michael@0 | 143 | window.addEventListener("touchstart", checkFunction, false); |
michael@0 | 144 | sendTouchEvent(cwu, "touchstart", event, 0); |
michael@0 | 145 | window.removeEventListener("touchstart", checkFunction, false); |
michael@0 | 146 | |
michael@0 | 147 | // test touchmove event fires correctly |
michael@0 | 148 | event.touches[0].page.x -= 1; |
michael@0 | 149 | event.targetTouches[0].page.x -= 1; |
michael@0 | 150 | event.changedTouches[0].page.x -= 1; |
michael@0 | 151 | checkFunction = checkEvent(event); |
michael@0 | 152 | window.addEventListener("touchmove", checkFunction, false); |
michael@0 | 153 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 154 | window.removeEventListener("touchmove", checkFunction, false); |
michael@0 | 155 | |
michael@0 | 156 | // test touchend event fires correctly |
michael@0 | 157 | event.touches = []; |
michael@0 | 158 | event.targetTouches = []; |
michael@0 | 159 | checkFunction = checkEvent(event); |
michael@0 | 160 | window.addEventListener("touchend", checkFunction, false); |
michael@0 | 161 | sendTouchEvent(cwu, "touchend", event, 0); |
michael@0 | 162 | window.removeEventListener("touchend", checkFunction, false); |
michael@0 | 163 | |
michael@0 | 164 | nextTest(); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | function testSingleTouch2(name) { |
michael@0 | 168 | // firing a touchstart that includes only one touch will evict any touches in the queue with touchend messages |
michael@0 | 169 | let cwu = SpecialPowers.getDOMWindowUtils(window); |
michael@0 | 170 | let target = document.getElementById("testTarget"); |
michael@0 | 171 | let target2 = document.getElementById("testTarget2"); |
michael@0 | 172 | let bcr = target.getBoundingClientRect(); |
michael@0 | 173 | let bcr2 = target2.getBoundingClientRect(); |
michael@0 | 174 | |
michael@0 | 175 | let touch1 = new testtouch({ |
michael@0 | 176 | identifier: 0, |
michael@0 | 177 | page: {x: Math.round(bcr.left + bcr.width/2), |
michael@0 | 178 | y: Math.round(bcr.top + bcr.height/2)}, |
michael@0 | 179 | target: target |
michael@0 | 180 | }); |
michael@0 | 181 | let event1 = new touchEvent({ |
michael@0 | 182 | touches: [touch1], |
michael@0 | 183 | targetTouches: [touch1], |
michael@0 | 184 | changedTouches: [touch1] |
michael@0 | 185 | }); |
michael@0 | 186 | let touch2 = new testtouch({ |
michael@0 | 187 | identifier: 1, |
michael@0 | 188 | page: {x: Math.round(bcr2.left + bcr2.width/2), |
michael@0 | 189 | y: Math.round(bcr2.top + bcr2.height/2)}, |
michael@0 | 190 | target: target2 |
michael@0 | 191 | }); |
michael@0 | 192 | let event2 = new touchEvent({ |
michael@0 | 193 | touches: [touch2], |
michael@0 | 194 | targetTouches: [touch2], |
michael@0 | 195 | changedTouches: [touch2] |
michael@0 | 196 | }); |
michael@0 | 197 | |
michael@0 | 198 | // test touchstart event fires correctly |
michael@0 | 199 | var checkFunction1 = checkEvent(event1); |
michael@0 | 200 | window.addEventListener("touchstart", checkFunction1, false); |
michael@0 | 201 | sendTouchEvent(cwu, "touchstart", event1, 0); |
michael@0 | 202 | window.removeEventListener("touchstart", checkFunction1, false); |
michael@0 | 203 | |
michael@0 | 204 | event1.touches = []; |
michael@0 | 205 | event1.targetTouches = []; |
michael@0 | 206 | checkFunction1 = checkEvent(event1); |
michael@0 | 207 | var checkFunction2 = checkEvent(event2); |
michael@0 | 208 | |
michael@0 | 209 | window.addEventListener("touchend", checkFunction1, false); |
michael@0 | 210 | window.addEventListener("touchstart", checkFunction2, false); |
michael@0 | 211 | sendTouchEvent(cwu, "touchstart", event2, 0); |
michael@0 | 212 | window.removeEventListener("touchend", checkFunction1, false); |
michael@0 | 213 | window.removeEventListener("touchstart", checkFunction2, false); |
michael@0 | 214 | |
michael@0 | 215 | sendTouchEvent(cwu, "touchstart", event1, 0); |
michael@0 | 216 | |
michael@0 | 217 | nextTest(); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | |
michael@0 | 221 | function testMultiTouch(name) { |
michael@0 | 222 | let cwu = SpecialPowers.getDOMWindowUtils(window); |
michael@0 | 223 | let target1 = document.getElementById("testTarget"); |
michael@0 | 224 | let target2 = document.getElementById("testTarget2"); |
michael@0 | 225 | let bcr = target1.getBoundingClientRect(); |
michael@0 | 226 | let bcr2 = target2.getBoundingClientRect(); |
michael@0 | 227 | |
michael@0 | 228 | let touch1 = new testtouch({ |
michael@0 | 229 | identifier: 0, |
michael@0 | 230 | page: {x: Math.round(bcr.left + bcr.width/2), |
michael@0 | 231 | y: Math.round(bcr.top + bcr.height/2)}, |
michael@0 | 232 | target: target1 |
michael@0 | 233 | }); |
michael@0 | 234 | let touch2 = new testtouch({ |
michael@0 | 235 | identifier: 1, |
michael@0 | 236 | page: {x: Math.round(bcr2.left + bcr2.width/2), |
michael@0 | 237 | y: Math.round(bcr2.top + bcr2.height/2)}, |
michael@0 | 238 | target: target2 |
michael@0 | 239 | }); |
michael@0 | 240 | let event = new touchEvent({ |
michael@0 | 241 | touches: [touch1], |
michael@0 | 242 | targetTouches: [touch1], |
michael@0 | 243 | changedTouches: [touch1] |
michael@0 | 244 | }); |
michael@0 | 245 | |
michael@0 | 246 | // test touchstart event fires correctly |
michael@0 | 247 | var checkFunction = checkEvent(event); |
michael@0 | 248 | window.addEventListener("touchstart", checkFunction, false); |
michael@0 | 249 | sendTouchEvent(cwu, "touchstart", event, 0); |
michael@0 | 250 | window.removeEventListener("touchstart", checkFunction, false); |
michael@0 | 251 | |
michael@0 | 252 | event.touches.push(touch2); |
michael@0 | 253 | event.targetTouches = [touch2]; |
michael@0 | 254 | event.changedTouches = [touch2]; |
michael@0 | 255 | window.addEventListener("touchstart", checkFunction, false); |
michael@0 | 256 | sendTouchEvent(cwu, "touchstart", event, 0); |
michael@0 | 257 | window.removeEventListener("touchstart", checkFunction, false); |
michael@0 | 258 | |
michael@0 | 259 | // test moving one touch point |
michael@0 | 260 | event.touches[0].page.x -= 1; |
michael@0 | 261 | event.targetTouches = [event.touches[0]]; |
michael@0 | 262 | event.changedTouches = [event.touches[0]]; |
michael@0 | 263 | window.addEventListener("touchmove", checkFunction, false); |
michael@0 | 264 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 265 | window.removeEventListener("touchmove", checkFunction, false); |
michael@0 | 266 | |
michael@0 | 267 | // test moving both touch points -- two touchmove events should fire, one on each target |
michael@0 | 268 | event.touches[0].page.x -= 1; |
michael@0 | 269 | event.touches[1].page.x -= 1; |
michael@0 | 270 | event.targetTouches = event.touches; |
michael@0 | 271 | event.changedTouches = event.touches; |
michael@0 | 272 | var touchMoveEvents = 0; |
michael@0 | 273 | var checkFunction2 = function(aEvent) { |
michael@0 | 274 | is(event.ctrlKey, aEvent.ctrlKey, "Correct ctrlKey"); |
michael@0 | 275 | is(event.altKey, aEvent.altKey, "Correct altKey"); |
michael@0 | 276 | is(event.shiftKey, aEvent.shiftKey, "Correct shiftKey"); |
michael@0 | 277 | is(event.metaKey, aEvent.metaKey, "Correct metaKey"); |
michael@0 | 278 | checkTouches(event.touches, aEvent.touches); |
michael@0 | 279 | checkTouches(event.changedTouches, aEvent.changedTouches); |
michael@0 | 280 | if (aEvent.targetTouches[0].target == target1) { |
michael@0 | 281 | checkTouches([event.touches[0]], aEvent.targetTouches); |
michael@0 | 282 | } else if (aEvent.targetTouches[0].target == target2) { |
michael@0 | 283 | checkTouches([event.touches[1]], aEvent.targetTouches); |
michael@0 | 284 | } else |
michael@0 | 285 | ok(false, "Event target is incorrect: " + event.targetTouches[0].target.nodeName + "#" + event.targetTouches[0].target.id); |
michael@0 | 286 | touchMoveEvents++; |
michael@0 | 287 | }; |
michael@0 | 288 | window.addEventListener("touchmove", checkFunction2, false); |
michael@0 | 289 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 290 | ok(touchMoveEvents, 2, "Correct number of touchmove events fired"); |
michael@0 | 291 | window.removeEventListener("touchmove", checkFunction2, false); |
michael@0 | 292 | |
michael@0 | 293 | // test removing just one finger |
michael@0 | 294 | var expected = new touchEvent({ |
michael@0 | 295 | touches: [touch2], |
michael@0 | 296 | targetTouches: [], |
michael@0 | 297 | changedTouches: [touch1] |
michael@0 | 298 | }); |
michael@0 | 299 | checkFunction = checkEvent(expected); |
michael@0 | 300 | |
michael@0 | 301 | event.touches = []; |
michael@0 | 302 | event.targetTouches = []; |
michael@0 | 303 | event.changedTouches = [touch1]; |
michael@0 | 304 | |
michael@0 | 305 | // test removing the other finger |
michael@0 | 306 | window.addEventListener("touchend", checkFunction, false); |
michael@0 | 307 | sendTouchEvent(cwu, "touchend", event, 0); |
michael@0 | 308 | window.removeEventListener("touchend", checkFunction, false); |
michael@0 | 309 | |
michael@0 | 310 | event.touches = []; |
michael@0 | 311 | event.targetTouches = []; |
michael@0 | 312 | event.changedTouches = [touch2]; |
michael@0 | 313 | checkFunction = checkEvent(event); |
michael@0 | 314 | window.addEventListener("touchend", checkFunction, false); |
michael@0 | 315 | sendTouchEvent(cwu, "touchend", event, 0); |
michael@0 | 316 | window.removeEventListener("touchend", checkFunction, false); |
michael@0 | 317 | |
michael@0 | 318 | nextTest(); |
michael@0 | 319 | } |
michael@0 | 320 | |
michael@0 | 321 | function testTouchChanged() { |
michael@0 | 322 | let cwu = SpecialPowers.getDOMWindowUtils(window); |
michael@0 | 323 | let target1 = document.getElementById("testTarget"); |
michael@0 | 324 | let bcr = target1.getBoundingClientRect(); |
michael@0 | 325 | |
michael@0 | 326 | let touch1 = new testtouch({ |
michael@0 | 327 | identifier: 0, |
michael@0 | 328 | page: {x: Math.round(bcr.left + bcr.width/2), |
michael@0 | 329 | y: Math.round(bcr.top + bcr.height/2)}, |
michael@0 | 330 | target: target1 |
michael@0 | 331 | }); |
michael@0 | 332 | let event = new touchEvent({ |
michael@0 | 333 | touches: [touch1], |
michael@0 | 334 | targetTouches: [touch1], |
michael@0 | 335 | changedTouches: [touch1] |
michael@0 | 336 | }); |
michael@0 | 337 | |
michael@0 | 338 | var checkFunction = checkEvent(event); |
michael@0 | 339 | sendTouchEvent(cwu, "touchstart", event, 0); |
michael@0 | 340 | |
michael@0 | 341 | var moveEvents = 0; |
michael@0 | 342 | function onMove(aEvent) { |
michael@0 | 343 | moveEvents++; |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | window.addEventListener("touchmove", onMove, false); |
michael@0 | 347 | |
michael@0 | 348 | // changing nothing should not fire a touchmove event |
michael@0 | 349 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 350 | |
michael@0 | 351 | // test moving x |
michael@0 | 352 | event.touches[0].page.x -= 1; |
michael@0 | 353 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 354 | |
michael@0 | 355 | // test moving y |
michael@0 | 356 | event.touches[0].page.y -= 1; |
michael@0 | 357 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 358 | |
michael@0 | 359 | // test changing y radius |
michael@0 | 360 | event.touches[0].radius.y += 1; |
michael@0 | 361 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 362 | |
michael@0 | 363 | // test changing x radius |
michael@0 | 364 | event.touches[0].radius.x += 1; |
michael@0 | 365 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 366 | |
michael@0 | 367 | // test changing rotationAngle |
michael@0 | 368 | event.touches[0].rotationAngle += 1; |
michael@0 | 369 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 370 | |
michael@0 | 371 | // test changing force |
michael@0 | 372 | event.touches[0].force += 1; |
michael@0 | 373 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 374 | |
michael@0 | 375 | // changing nothing again |
michael@0 | 376 | sendTouchEvent(cwu, "touchmove", event, 0); |
michael@0 | 377 | |
michael@0 | 378 | is(moveEvents, 6, "Six move events fired"); |
michael@0 | 379 | |
michael@0 | 380 | window.removeEventListener("touchmove", onMove, false); |
michael@0 | 381 | sendTouchEvent(cwu, "touchend", event, 0); |
michael@0 | 382 | nextTest(); |
michael@0 | 383 | } |
michael@0 | 384 | |
michael@0 | 385 | function testPreventDefault() { |
michael@0 | 386 | let cwu = SpecialPowers.getDOMWindowUtils(window); |
michael@0 | 387 | let target = document.getElementById("testTarget"); |
michael@0 | 388 | let target2 = document.getElementById("testTarget2"); |
michael@0 | 389 | let bcr = target.getBoundingClientRect(); |
michael@0 | 390 | let bcr2 = target2.getBoundingClientRect(); |
michael@0 | 391 | |
michael@0 | 392 | let touch1 = new testtouch({ |
michael@0 | 393 | page: {x: bcr.left + bcr.width/2, |
michael@0 | 394 | y: bcr.top + bcr.height/2}, |
michael@0 | 395 | target: target |
michael@0 | 396 | }); |
michael@0 | 397 | let event = new touchEvent({ |
michael@0 | 398 | touches: [touch1], |
michael@0 | 399 | targetTouches: [touch1], |
michael@0 | 400 | changedTouches: [touch1] |
michael@0 | 401 | }); |
michael@0 | 402 | |
michael@0 | 403 | let preventFunction = function(aEvent) { |
michael@0 | 404 | aEvent.preventDefault(); |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | let tests = [ |
michael@0 | 408 | [{ name: "touchstart", prevent: false }, |
michael@0 | 409 | { name: "touchmove", prevent: false }, |
michael@0 | 410 | { name: "touchmove", prevent: false }, |
michael@0 | 411 | { name: "touchend", prevent: false }], |
michael@0 | 412 | [{ name: "touchstart", prevent: true, doPrevent: true }, |
michael@0 | 413 | { name: "touchmove", prevent: true }, |
michael@0 | 414 | { name: "touchmove", prevent: true }, |
michael@0 | 415 | { name: "touchend", prevent: true }], |
michael@0 | 416 | [{ name: "touchstart", prevent: false }, |
michael@0 | 417 | { name: "touchmove", prevent: true, doPrevent: true }, |
michael@0 | 418 | { name: "touchmove", prevent: true }, |
michael@0 | 419 | { name: "touchend", prevent: true }], |
michael@0 | 420 | [{ name: "touchstart", prevent: false }, |
michael@0 | 421 | { name: "touchmove", prevent: false }, |
michael@0 | 422 | { name: "touchmove", prevent: false, doPrevent: true }, |
michael@0 | 423 | { name: "touchend", prevent: false }], |
michael@0 | 424 | [{ name: "touchstart", prevent: false }, |
michael@0 | 425 | { name: "touchmove", prevent: false }, |
michael@0 | 426 | { name: "touchmove", prevent: false }, |
michael@0 | 427 | { name: "touchend", prevent: false, doPrevent: true }] |
michael@0 | 428 | ]; |
michael@0 | 429 | |
michael@0 | 430 | var dotest = function(aTest) { |
michael@0 | 431 | if (aTest.doPrevent) { |
michael@0 | 432 | target.addEventListener(aTest.name, preventFunction, false); |
michael@0 | 433 | } |
michael@0 | 434 | |
michael@0 | 435 | if (aTest.name == "touchmove") { |
michael@0 | 436 | touch1.page.x++; |
michael@0 | 437 | event.touches[0] = touch1; |
michael@0 | 438 | } |
michael@0 | 439 | |
michael@0 | 440 | is(sendTouchEvent(cwu, aTest.name, event, 0), aTest.prevent, "Got correct status"); |
michael@0 | 441 | |
michael@0 | 442 | if (aTest.doPrevent) |
michael@0 | 443 | target.removeEventListener(aTest.name, preventFunction, false); |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | for (var i = 0; i < tests.length; i++) { |
michael@0 | 447 | for (var j = 0; j < tests[i].length; j++) { |
michael@0 | 448 | dotest(tests[i][j]); |
michael@0 | 449 | } |
michael@0 | 450 | } |
michael@0 | 451 | |
michael@0 | 452 | nextTest(); |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | function testRemovingElement() { |
michael@0 | 456 | let cwu = SpecialPowers.getDOMWindowUtils(window); |
michael@0 | 457 | let target = document.getElementById("testTarget"); |
michael@0 | 458 | let bcr = document.getElementById("testTarget").getBoundingClientRect(); |
michael@0 | 459 | |
michael@0 | 460 | let touch1 = new testtouch({ |
michael@0 | 461 | page: {x: bcr.left + bcr.width/2, |
michael@0 | 462 | y: bcr.top + bcr.height/2}, |
michael@0 | 463 | }); |
michael@0 | 464 | let e = new touchEvent({ |
michael@0 | 465 | touches: [touch1], |
michael@0 | 466 | targetTouches: [touch1], |
michael@0 | 467 | changedTouches: [touch1] |
michael@0 | 468 | }); |
michael@0 | 469 | |
michael@0 | 470 | var touchEvents = 0; |
michael@0 | 471 | var removeTarget = function(aEvent) { |
michael@0 | 472 | aEvent.target.parentNode.removeChild(aEvent.target); |
michael@0 | 473 | }; |
michael@0 | 474 | |
michael@0 | 475 | var checkTarget = function(aEvent) { |
michael@0 | 476 | is(aEvent.target, target, "Event has correct target"); |
michael@0 | 477 | touchEvents++; |
michael@0 | 478 | }; |
michael@0 | 479 | |
michael@0 | 480 | target.addEventListener("touchstart", removeTarget, false); |
michael@0 | 481 | target.addEventListener("touchmove", checkTarget, false); |
michael@0 | 482 | target.addEventListener("touchend", checkTarget, false); |
michael@0 | 483 | |
michael@0 | 484 | sendTouchEvent(cwu, "touchstart", e, 0); |
michael@0 | 485 | |
michael@0 | 486 | e.touches[0].page.x++; |
michael@0 | 487 | sendTouchEvent(cwu, "touchmove", e, 0); |
michael@0 | 488 | sendTouchEvent(cwu, "touchend", e, 0); |
michael@0 | 489 | |
michael@0 | 490 | target.removeEventListener("touchstart", removeTarget, false); |
michael@0 | 491 | target.removeEventListener("touchmove", checkTarget, false); |
michael@0 | 492 | target.removeEventListener("touchend", checkTarget, false); |
michael@0 | 493 | |
michael@0 | 494 | is(touchEvents, 2, "Check target was called twice"); |
michael@0 | 495 | |
michael@0 | 496 | nextTest(); |
michael@0 | 497 | } |
michael@0 | 498 | |
michael@0 | 499 | function testNAC() { |
michael@0 | 500 | let cwu = SpecialPowers.getDOMWindowUtils(window); |
michael@0 | 501 | let target = document.getElementById("testTarget3"); |
michael@0 | 502 | let bcr = target.getBoundingClientRect(); |
michael@0 | 503 | |
michael@0 | 504 | let touch1 = new testtouch({ |
michael@0 | 505 | page: {x: Math.round(bcr.left + bcr.width/2), |
michael@0 | 506 | y: Math.round(bcr.top + bcr.height/2)}, |
michael@0 | 507 | target: target |
michael@0 | 508 | }); |
michael@0 | 509 | let event = new touchEvent({ |
michael@0 | 510 | touches: [touch1], |
michael@0 | 511 | targetTouches: [touch1], |
michael@0 | 512 | changedTouches: [touch1] |
michael@0 | 513 | }); |
michael@0 | 514 | |
michael@0 | 515 | // test touchstart event fires correctly |
michael@0 | 516 | var checkFunction = checkEvent(event); |
michael@0 | 517 | window.addEventListener("touchstart", checkFunction, false); |
michael@0 | 518 | sendTouchEvent(cwu, "touchstart", event, 0); |
michael@0 | 519 | window.removeEventListener("touchstart", checkFunction, false); |
michael@0 | 520 | |
michael@0 | 521 | sendTouchEvent(cwu, "touchend", event, 0); |
michael@0 | 522 | |
michael@0 | 523 | nextTest(); |
michael@0 | 524 | } |
michael@0 | 525 | |
michael@0 | 526 | function doTest() { |
michael@0 | 527 | tests.push(testSingleTouch); |
michael@0 | 528 | tests.push(testSingleTouch2); |
michael@0 | 529 | tests.push(testMultiTouch); |
michael@0 | 530 | tests.push(testPreventDefault); |
michael@0 | 531 | tests.push(testTouchChanged); |
michael@0 | 532 | tests.push(testRemovingElement); |
michael@0 | 533 | tests.push(testNAC); |
michael@0 | 534 | |
michael@0 | 535 | tests.push(function() { |
michael@0 | 536 | SimpleTest.finish(); |
michael@0 | 537 | }); |
michael@0 | 538 | |
michael@0 | 539 | nextTest(); |
michael@0 | 540 | } |
michael@0 | 541 | |
michael@0 | 542 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 543 | addLoadEvent(doTest); |
michael@0 | 544 | |
michael@0 | 545 | </script> |
michael@0 | 546 | </pre> |
michael@0 | 547 | <div id="parent"> |
michael@0 | 548 | <span id="testTarget" style="padding: 5px; border: 1px solid black;">testTarget</span> |
michael@0 | 549 | <span id="testTarget2" style="padding: 5px; border: 1px solid blue;">testTarget</span> |
michael@0 | 550 | <input type="text" id="testTarget3"> |
michael@0 | 551 | </div> |
michael@0 | 552 | </body> |
michael@0 | 553 | </html> |