Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <!-- |
michael@0 | 4 | https://bugzilla.mozilla.org/show_bug.cgi?id=970964 |
michael@0 | 5 | --> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Test for Bug 970964</title> |
michael@0 | 8 | <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 9 | <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script> |
michael@0 | 10 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
michael@0 | 11 | </head> |
michael@0 | 12 | <body> |
michael@0 | 13 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=970964">Mozilla Bug 970964</a> |
michael@0 | 14 | <p id="display"></p> |
michael@0 | 15 | <div id="content" style="display: none"> |
michael@0 | 16 | |
michael@0 | 17 | </div> |
michael@0 | 18 | <pre id="test"> |
michael@0 | 19 | <script type="application/javascript"> |
michael@0 | 20 | |
michael@0 | 21 | /** Test for Bug 970964 **/ |
michael@0 | 22 | |
michael@0 | 23 | function ok(condition, msg) { |
michael@0 | 24 | parent.ok(condition, msg); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | function is(a, b, msg) { |
michael@0 | 28 | parent.is(a, b, msg); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | function testtouch(aOptions) { |
michael@0 | 32 | if (!aOptions) |
michael@0 | 33 | aOptions = {}; |
michael@0 | 34 | this.identifier = aOptions.identifier || 0; |
michael@0 | 35 | this.target = aOptions.target || 0; |
michael@0 | 36 | this.page = aOptions.page || {x: 0, y: 0}; |
michael@0 | 37 | this.radius = aOptions.radius || {x: 0, y: 0}; |
michael@0 | 38 | this.rotationAngle = aOptions.rotationAngle || 0; |
michael@0 | 39 | this.force = aOptions.force || 1; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function touchEvent(aOptions) { |
michael@0 | 43 | if (!aOptions) { |
michael@0 | 44 | aOptions = {}; |
michael@0 | 45 | } |
michael@0 | 46 | this.ctrlKey = aOptions.ctrlKey || false; |
michael@0 | 47 | this.altKey = aOptions.altKey || false; |
michael@0 | 48 | this.shiftKey = aOptions.shiftKey || false; |
michael@0 | 49 | this.metaKey = aOptions.metaKey || false; |
michael@0 | 50 | this.touches = aOptions.touches || []; |
michael@0 | 51 | this.targetTouches = aOptions.targetTouches || []; |
michael@0 | 52 | this.changedTouches = aOptions.changedTouches || []; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | function sendTouchEvent(windowUtils, aType, aEvent, aModifiers) { |
michael@0 | 56 | var ids = [], xs=[], ys=[], rxs = [], rys = [], |
michael@0 | 57 | rotations = [], forces = []; |
michael@0 | 58 | |
michael@0 | 59 | for (var touchType of ["touches", "changedTouches", "targetTouches"]) { |
michael@0 | 60 | for (var i = 0; i < aEvent[touchType].length; i++) { |
michael@0 | 61 | if (ids.indexOf(aEvent[touchType][i].identifier) == -1) { |
michael@0 | 62 | ids.push(aEvent[touchType][i].identifier); |
michael@0 | 63 | xs.push(aEvent[touchType][i].page.x); |
michael@0 | 64 | ys.push(aEvent[touchType][i].page.y); |
michael@0 | 65 | rxs.push(aEvent[touchType][i].radius.x); |
michael@0 | 66 | rys.push(aEvent[touchType][i].radius.y); |
michael@0 | 67 | rotations.push(aEvent[touchType][i].rotationAngle); |
michael@0 | 68 | forces.push(aEvent[touchType][i].force); |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | return windowUtils.sendTouchEvent(aType, |
michael@0 | 73 | ids, xs, ys, rxs, rys, |
michael@0 | 74 | rotations, forces, |
michael@0 | 75 | ids.length, aModifiers, 0); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | function getDefaultArgEvent(eventname) { |
michael@0 | 79 | return new PointerEvent(eventname, { |
michael@0 | 80 | bubbles: true, cancelable: true, view: window, |
michael@0 | 81 | detail: 0, screenX: 0, screenY: 0, clientX: 0, clientY: 0, |
michael@0 | 82 | ctrlKey: false, altKey: false, shiftKey: false, metaKey: false, |
michael@0 | 83 | button: 0, relatedTarget: null, pointerId: 0 |
michael@0 | 84 | }); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | function getTouchEventForTarget(target, cwu, id) { |
michael@0 | 88 | var bcr = target.getBoundingClientRect(); |
michael@0 | 89 | var touch = new testtouch({ |
michael@0 | 90 | page: {x: Math.round(bcr.left + bcr.width/2), |
michael@0 | 91 | y: Math.round(bcr.top + bcr.height/2)}, |
michael@0 | 92 | target: target, |
michael@0 | 93 | identifier: id, |
michael@0 | 94 | }); |
michael@0 | 95 | var event = new touchEvent({ |
michael@0 | 96 | touches: [touch], |
michael@0 | 97 | targetTouches: [touch], |
michael@0 | 98 | changedTouches: [touch] |
michael@0 | 99 | }); |
michael@0 | 100 | return event; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | function runTests() { |
michael@0 | 104 | var d0 = document.getElementById("d0"); |
michael@0 | 105 | var d1 = document.getElementById("d1"); |
michael@0 | 106 | var d2 = document.getElementById("d2"); |
michael@0 | 107 | var d3 = document.getElementById("d3"); |
michael@0 | 108 | |
michael@0 | 109 | // Test Pointer firing before any mouse/touch original source |
michael@0 | 110 | |
michael@0 | 111 | var mouseDownTriggered = 0; |
michael@0 | 112 | var pointerDownTriggered = 0; |
michael@0 | 113 | var touchDownTriggered = 0; |
michael@0 | 114 | var touchCancelTriggered = 0; |
michael@0 | 115 | var pointerCancelTriggered = 0; |
michael@0 | 116 | |
michael@0 | 117 | d0.onmousedown = function(e) { |
michael@0 | 118 | mouseDownTriggered = 1; |
michael@0 | 119 | is(pointerDownTriggered , 1, "Mouse event must be triggered after pointer event!"); |
michael@0 | 120 | }; |
michael@0 | 121 | d0.ontouchstart = function(e) { |
michael@0 | 122 | touchDownTriggered = 1; |
michael@0 | 123 | is(touchDownTriggered , 1, "Touch event must be triggered after pointer event!"); |
michael@0 | 124 | } |
michael@0 | 125 | d0.ontouchcancel = function(e) { |
michael@0 | 126 | touchCancelTriggered = 1; |
michael@0 | 127 | is(pointerCancelTriggered, 1, "Touch cancel event must be triggered after pointer event!"); |
michael@0 | 128 | } |
michael@0 | 129 | d0.onpointerdown = function(e) { |
michael@0 | 130 | pointerDownTriggered = 1; |
michael@0 | 131 | is(mouseDownTriggered, 0, "Pointer event must be triggered before mouse event!"); |
michael@0 | 132 | is(touchDownTriggered, 0, "Pointer event must be triggered before touch event!"); |
michael@0 | 133 | }; |
michael@0 | 134 | d0.addEventListener("pointercancel", function(ev) { |
michael@0 | 135 | d0.removeEventListener("pointercancel", arguments.callee, false); |
michael@0 | 136 | is(ev.pointerId, 0, "Correct default pointerId"); |
michael@0 | 137 | is(ev.bubbles, true, "bubbles should be true"); |
michael@0 | 138 | is(ev.cancelable, false, "pointercancel cancelable should be false "); |
michael@0 | 139 | pointerCancelTriggered = 1; |
michael@0 | 140 | is(touchCancelTriggered, 0, "Pointer event must be triggered before touch event!"); |
michael@0 | 141 | }, false); |
michael@0 | 142 | |
michael@0 | 143 | // Test pointer event generated from mouse event |
michael@0 | 144 | synthesizeMouse(d1, 3, 3, { type: "mousemove"}); |
michael@0 | 145 | synthesizeMouse(d1, 3, 3, { type: "mousedown"}); |
michael@0 | 146 | synthesizeMouse(d1, 3, 3, { type: "mouseup"}); |
michael@0 | 147 | |
michael@0 | 148 | // Test pointer event generated from touch event |
michael@0 | 149 | pointerDownTriggered = 0; |
michael@0 | 150 | mouseDownTriggered = 0; |
michael@0 | 151 | |
michael@0 | 152 | var cwu = SpecialPowers.getDOMWindowUtils(window); |
michael@0 | 153 | var event1 = getTouchEventForTarget(d1, cwu, 0); |
michael@0 | 154 | sendTouchEvent(cwu, "touchmove", event1, 0); |
michael@0 | 155 | sendTouchEvent(cwu, "touchstart", event1, 0); |
michael@0 | 156 | // Test Touch to Pointer Cancel |
michael@0 | 157 | sendTouchEvent(cwu, "touchcancel", event1, 0); |
michael@0 | 158 | |
michael@0 | 159 | // Check Pointer enter/leave from mouse generated event |
michael@0 | 160 | var mouseEnterTriggered = 0; |
michael@0 | 161 | var pointerEnterTriggered = 0; |
michael@0 | 162 | d2.onpointerenter = function(e) { |
michael@0 | 163 | pointerEnterTriggered = 1; |
michael@0 | 164 | is(e.bubbles, false, "bubbles should be false"); |
michael@0 | 165 | is(e.cancelable, false, "cancelable should be false"); |
michael@0 | 166 | is(mouseEnterTriggered, 0, "Pointer event must be triggered before mouse event!"); |
michael@0 | 167 | }; |
michael@0 | 168 | d2.onmouseenter = function(e) { |
michael@0 | 169 | mouseEnterTriggered = 1; |
michael@0 | 170 | is(pointerEnterTriggered , 1, "Mouse event must be triggered after pointer event!"); |
michael@0 | 171 | }; |
michael@0 | 172 | synthesizeMouse(d2, 3, 3, { type: "mousemove"}); |
michael@0 | 173 | d2.onmouseenter = function(e) {} |
michael@0 | 174 | |
michael@0 | 175 | // Test Multi Pointer enter/leave for pointers generated from Mouse and Touch at the same time |
michael@0 | 176 | // Enter mouse and touch generated pointers to different elements |
michael@0 | 177 | var d1enterCount = 0; |
michael@0 | 178 | var d2enterCount = 0; |
michael@0 | 179 | var d3enterCount = 0; |
michael@0 | 180 | var d1leaveCount = 0; |
michael@0 | 181 | var d2leaveCount = 0; |
michael@0 | 182 | var d3leaveCount = 0; |
michael@0 | 183 | var mousePointerEnterLeaveCount = 0; |
michael@0 | 184 | var touchPointerEnterLeaveCount = 0; |
michael@0 | 185 | |
michael@0 | 186 | var checkPointerType = function(pointerType) { |
michael@0 | 187 | if (pointerType == "mouse") { |
michael@0 | 188 | ++mousePointerEnterLeaveCount; |
michael@0 | 189 | } else if (pointerType == "touch") { |
michael@0 | 190 | ++touchPointerEnterLeaveCount; |
michael@0 | 191 | } |
michael@0 | 192 | }; |
michael@0 | 193 | |
michael@0 | 194 | d1.onpointerenter = function(e) { |
michael@0 | 195 | ++d1enterCount; |
michael@0 | 196 | is(e.bubbles, false, "bubbles should be false"); |
michael@0 | 197 | is(e.cancelable, false, "cancelable should be false"); |
michael@0 | 198 | checkPointerType(e.pointerType); |
michael@0 | 199 | }; |
michael@0 | 200 | d2.onpointerenter = function(e) { |
michael@0 | 201 | ++d2enterCount; |
michael@0 | 202 | is(e.bubbles, false, "bubbles should be false"); |
michael@0 | 203 | is(e.cancelable, false, "cancelable should be false"); |
michael@0 | 204 | checkPointerType(e.pointerType); |
michael@0 | 205 | }; |
michael@0 | 206 | d3.onpointerenter = function(e) { |
michael@0 | 207 | ++d3enterCount; |
michael@0 | 208 | is(e.bubbles, false, "bubbles should be false"); |
michael@0 | 209 | is(e.cancelable, false, "cancelable should be false"); |
michael@0 | 210 | checkPointerType(e.pointerType); |
michael@0 | 211 | }; |
michael@0 | 212 | d1.onpointerleave = function(e) { |
michael@0 | 213 | ++d1leaveCount; |
michael@0 | 214 | is(e.bubbles, false, "bubbles should be false"); |
michael@0 | 215 | is(e.cancelable, false, "cancelable should be false"); |
michael@0 | 216 | checkPointerType(e.pointerType); |
michael@0 | 217 | }; |
michael@0 | 218 | d2.onpointerleave = function(e) { |
michael@0 | 219 | ++d2leaveCount; |
michael@0 | 220 | is(e.bubbles, false, "bubbles should be false"); |
michael@0 | 221 | is(e.cancelable, false, "cancelable should be false"); |
michael@0 | 222 | checkPointerType(e.pointerType); |
michael@0 | 223 | }; |
michael@0 | 224 | d3.onpointerleave = function(e) { |
michael@0 | 225 | ++d3leaveCount; |
michael@0 | 226 | is(e.bubbles, false, "bubbles should be false"); |
michael@0 | 227 | is(e.cancelable, false, "cancelable should be false"); |
michael@0 | 228 | checkPointerType(e.pointerType); |
michael@0 | 229 | }; |
michael@0 | 230 | |
michael@0 | 231 | synthesizeMouse(d1, 3, 3, { type: "mousemove"}); |
michael@0 | 232 | sendTouchEvent(cwu, "touchmove", getTouchEventForTarget(d3, cwu, 3), 0); |
michael@0 | 233 | is(touchPointerEnterLeaveCount, 1, "Wrong touch enterLeave count for!"); |
michael@0 | 234 | is(mousePointerEnterLeaveCount, 2, "Wrong mouse enterLeave count for!"); |
michael@0 | 235 | |
michael@0 | 236 | is(d1enterCount, 1, "Wrong enter count for! d1"); |
michael@0 | 237 | is(d2leaveCount, 1, "Wrong leave count for! d2"); |
michael@0 | 238 | is(d3enterCount, 1, "Wrong enter count for! d3"); |
michael@0 | 239 | |
michael@0 | 240 | sendTouchEvent(cwu, "touchmove", getTouchEventForTarget(d1, cwu, 3), 0); |
michael@0 | 241 | synthesizeMouse(d3, 3, 3, { type: "mousemove"}); |
michael@0 | 242 | is(touchPointerEnterLeaveCount, 3, "Wrong touch enterLeave count for!"); |
michael@0 | 243 | is(mousePointerEnterLeaveCount, 4, "Wrong mouse enterLeave count for!"); |
michael@0 | 244 | |
michael@0 | 245 | is(d3leaveCount, 1, "Wrong leave count for! d3"); |
michael@0 | 246 | is(d1leaveCount, 1, "Wrong leave count for! d1"); |
michael@0 | 247 | is(d1enterCount, 2, "Wrong enter count for! d1"); |
michael@0 | 248 | is(d3enterCount, 2, "Wrong enter count for! d3"); |
michael@0 | 249 | |
michael@0 | 250 | sendTouchEvent(cwu, "touchmove", getTouchEventForTarget(d2, cwu, 3), 0); |
michael@0 | 251 | synthesizeMouse(d2, 3, 3, { type: "mousemove"}); |
michael@0 | 252 | is(touchPointerEnterLeaveCount, 5, "Wrong touch enterLeave count for!"); |
michael@0 | 253 | is(mousePointerEnterLeaveCount, 6, "Wrong mouse enterLeave count for!"); |
michael@0 | 254 | |
michael@0 | 255 | is(d1leaveCount, 2, "Wrong leave count for! d1"); |
michael@0 | 256 | is(d2enterCount, 2, "Wrong enter count for! d2"); |
michael@0 | 257 | is(d3leaveCount, 2, "Wrong leave count for! d3"); |
michael@0 | 258 | |
michael@0 | 259 | sendTouchEvent(cwu, "touchmove", getTouchEventForTarget(d1, cwu, 3), 0); |
michael@0 | 260 | synthesizeMouse(d1, 3, 3, { type: "mousemove"}); |
michael@0 | 261 | is(touchPointerEnterLeaveCount, 7, "Wrong touch enterLeave count for!"); |
michael@0 | 262 | is(mousePointerEnterLeaveCount, 8, "Wrong mouse enterLeave count for!"); |
michael@0 | 263 | |
michael@0 | 264 | is(d2leaveCount, 3, "Wrong leave count for! d2"); |
michael@0 | 265 | is(d1enterCount, 4, "Wrong enter count for! d1"); |
michael@0 | 266 | |
michael@0 | 267 | // Test for pointer buttons when it generated from mousemove event |
michael@0 | 268 | d1.onpointermove = function(e) { |
michael@0 | 269 | is(e.buttons, 0, "Buttons must be 0 on pointer generated from mousemove"); |
michael@0 | 270 | is(e.button, -1, "Button must be -1 on pointer generated from mousemove when no buttons pressed"); |
michael@0 | 271 | is(e.pointerType, "mouse", "Pointer type must be mouse"); |
michael@0 | 272 | }; |
michael@0 | 273 | cwu.sendMouseEvent("mousemove", 4, 4, 0, 0, 0, false, 0, 0); |
michael@0 | 274 | |
michael@0 | 275 | d1.onpointermove = function(e) { |
michael@0 | 276 | is(e.buttons, 1, "Buttons must be 1 on pointer generated from touch event"); |
michael@0 | 277 | is(e.button, 0, "Button must be 0 on pointer generated from touch eventd"); |
michael@0 | 278 | is(e.pointerType, "touch", "Pointer type must be touch"); |
michael@0 | 279 | }; |
michael@0 | 280 | sendTouchEvent(cwu, "touchmove", getTouchEventForTarget(d1, cwu, 2), 0); |
michael@0 | 281 | |
michael@0 | 282 | // Test for cancel trigger pointerOut (Touch Pointer must be at d1 now) |
michael@0 | 283 | pointerCancelTriggered = 0; |
michael@0 | 284 | var pointerOutTriggeredForCancelEvent = 0; |
michael@0 | 285 | var pointerLeaveTriggeredForCancelEvent = 0; |
michael@0 | 286 | d1.onpointerout = function(e) { |
michael@0 | 287 | if (pointerOutTriggeredForCancelEvent == 0) { |
michael@0 | 288 | is(e.pointerId, 3, "Wrong Pointer type, should be id from Touch event"); |
michael@0 | 289 | is(e.pointerType, "touch", "Wrong Pointer type, should be touch type"); |
michael@0 | 290 | } else { |
michael@0 | 291 | is(e.pointerId, 0, "Wrong Pointer type, should be id from mouse event"); |
michael@0 | 292 | is(e.pointerType, "mouse", "Wrong Pointer type, should be mouse type"); |
michael@0 | 293 | } |
michael@0 | 294 | pointerOutTriggeredForCancelEvent = 1; |
michael@0 | 295 | }; |
michael@0 | 296 | d1.onpointerleave = function(e) { |
michael@0 | 297 | is(pointerOutTriggeredForCancelEvent, 1, "Pointer Out must be dispatched bedore Pointer leave"); |
michael@0 | 298 | if (pointerLeaveTriggeredForCancelEvent == 0) { |
michael@0 | 299 | is(e.pointerId, 3, "Wrong Pointer type, should be id from Touch event"); |
michael@0 | 300 | is(e.pointerType, "touch", "Wrong Pointer type, should be touch type"); |
michael@0 | 301 | } else { |
michael@0 | 302 | is(e.pointerId, 0, "Wrong Pointer type, should be id from mouse event"); |
michael@0 | 303 | is(e.pointerType, "mouse", "Wrong Pointer type, should be mouse type"); |
michael@0 | 304 | } |
michael@0 | 305 | pointerLeaveTriggeredForCancelEvent = 1; |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | sendTouchEvent(cwu, "touchcancel", getTouchEventForTarget(d1, cwu, 3), 0); |
michael@0 | 309 | is(pointerOutTriggeredForCancelEvent, 1, "Pointer Out not dispatched on PointerCancel"); |
michael@0 | 310 | is(pointerLeaveTriggeredForCancelEvent, 1, "Pointer Leave not dispatched on PointerCancel"); |
michael@0 | 311 | |
michael@0 | 312 | finishTest(); |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | function finishTest() { |
michael@0 | 316 | // Let window.onerror have a chance to fire |
michael@0 | 317 | setTimeout(function() { |
michael@0 | 318 | setTimeout(function() { |
michael@0 | 319 | window.parent.postMessage("SimpleTest.finish();", "*"); |
michael@0 | 320 | }, 0); |
michael@0 | 321 | }, 0); |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | window.onload = function () { |
michael@0 | 325 | SpecialPowers.pushPrefEnv({ |
michael@0 | 326 | "set": [ |
michael@0 | 327 | ["dom.w3c_pointer_events.enabled", true], |
michael@0 | 328 | ] |
michael@0 | 329 | }, runTests); |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 333 | |
michael@0 | 334 | </script> |
michael@0 | 335 | </pre> |
michael@0 | 336 | <div id="d0"> |
michael@0 | 337 | Test divs -- |
michael@0 | 338 | <div id="d1">t</div><div id="d2">t</div><div id="d3">t</div> |
michael@0 | 339 | -- |
michael@0 | 340 | </div> |
michael@0 | 341 | </body> |
michael@0 | 342 | </html> |