dom/events/test/test_bug741666.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/events/test/test_bug741666.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,176 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=741666
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 741666</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body>
    1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=741666">Mozilla Bug 741666</a>
    1.16 +<p id="display"></p>
    1.17 +<div id="content" style="display: none">
    1.18 +  
    1.19 +</div>
    1.20 +<pre id="test">
    1.21 +<script class="testbody" type="application/javascript;version=1.8">
    1.22 +
    1.23 +/** Test for Bug 306008 - Touch events with a reference held should retain their touch lists **/
    1.24 +
    1.25 +let tests = [], testTarget, parent;
    1.26 +
    1.27 +let touch = {
    1.28 +  id: 0,
    1.29 +  point: {x: 0, y: 0},
    1.30 +  radius: {x: 0, y: 0},
    1.31 +  rotation: 0,
    1.32 +  force: 0.5,
    1.33 +  target: null
    1.34 +}
    1.35 +
    1.36 +function nextTest() {
    1.37 +  if (tests.length)
    1.38 +    SimpleTest.executeSoon(tests.shift());
    1.39 +}
    1.40 +
    1.41 +function checkEvent(aFakeEvent, aTouches) {
    1.42 +  return function(aEvent, aTrusted) {
    1.43 +    is(aFakeEvent.ctrlKey, aEvent.ctrlKey, "Correct ctrlKey");
    1.44 +    is(aFakeEvent.altKey, aEvent.altKey, "Correct altKey");
    1.45 +    is(aFakeEvent.shiftKey, aEvent.shiftKey, "Correct shiftKey");
    1.46 +    is(aFakeEvent.metaKey, aEvent.metaKey, "Correct metaKey");
    1.47 +    is(aEvent.isTrusted, aTrusted, "Event is trusted");
    1.48 +    checkTouches(aFakeEvent[aTouches], aEvent[aTouches]);
    1.49 +  }
    1.50 +}
    1.51 +
    1.52 +function checkTouches(aTouches1, aTouches2) {
    1.53 +  is(aTouches1.length, aTouches2.length, "Correct touches length");
    1.54 +  for (var i = 0; i < aTouches1.length; i++) {
    1.55 +    checkTouch(aTouches1[i], aTouches2[i]);
    1.56 +  }
    1.57 +}
    1.58 +
    1.59 +function checkTouch(aFakeTouch, aTouch) {
    1.60 +  is(aFakeTouch.identifier, aTouch.identifier, "Touch has correct identifier");
    1.61 +  is(aFakeTouch.target, aTouch.target, "Touch has correct target");
    1.62 +  is(aFakeTouch.page.x, aTouch.pageX, "Touch has correct pageX");
    1.63 +  is(aFakeTouch.page.y, aTouch.pageY, "Touch has correct pageY");
    1.64 +  is(aFakeTouch.page.x + Math.round(window.mozInnerScreenX), aTouch.screenX, "Touch has correct screenX");
    1.65 +  is(aFakeTouch.page.y + Math.round(window.mozInnerScreenY), aTouch.screenY, "Touch has correct screenY");
    1.66 +  is(aFakeTouch.page.x, aTouch.clientX, "Touch has correct clientX");
    1.67 +  is(aFakeTouch.page.y, aTouch.clientY, "Touch has correct clientY");
    1.68 +  is(aFakeTouch.radius.x, aTouch.radiusX, "Touch has correct radiusX");
    1.69 +  is(aFakeTouch.radius.y, aTouch.radiusY, "Touch has correct radiusY");
    1.70 +  is(aFakeTouch.rotationAngle, aTouch.rotationAngle, "Touch has correct rotationAngle");
    1.71 +  is(aFakeTouch.force, aTouch.force, "Touch has correct force");
    1.72 +}
    1.73 +
    1.74 +function sendTouchEvent(windowUtils, aType, aEvent, aModifiers) {
    1.75 +  var ids = [], xs=[], ys=[], rxs = [], rys = [],
    1.76 +      rotations = [], forces = [];
    1.77 +
    1.78 +  for (var touchType of ["touches", "changedTouches", "targetTouches"]) {
    1.79 +    for (var i = 0; i < aEvent[touchType].length; i++) {
    1.80 +      if (ids.indexOf(aEvent[touchType][i].identifier) == -1) {
    1.81 +        ids.push(aEvent[touchType][i].identifier);
    1.82 +        xs.push(aEvent[touchType][i].page.x);
    1.83 +        ys.push(aEvent[touchType][i].page.y);
    1.84 +        rxs.push(aEvent[touchType][i].radius.x);
    1.85 +        rys.push(aEvent[touchType][i].radius.y);
    1.86 +        rotations.push(aEvent[touchType][i].rotationAngle);
    1.87 +        forces.push(aEvent[touchType][i].force);
    1.88 +      }
    1.89 +    }
    1.90 +  }
    1.91 +  return windowUtils.sendTouchEvent(aType,
    1.92 +                                    ids, xs, ys, rxs, rys,
    1.93 +                                    rotations, forces,
    1.94 +                                    ids.length, aModifiers, 0);
    1.95 +}
    1.96 +
    1.97 +function touchEvent(aOptions) {
    1.98 +  if (!aOptions) {
    1.99 +    aOptions = {};
   1.100 +  }
   1.101 +  this.ctrlKey = aOptions.ctrlKey || false;
   1.102 +  this.altKey = aOptions.altKey || false;
   1.103 +  this.shiftKey = aOptions.shiftKey || false;
   1.104 +  this.metaKey = aOptions.metaKey || false;
   1.105 +  this.touches = aOptions.touches || [];
   1.106 +  this.targetTouches = aOptions.targetTouches || [];
   1.107 +  this.changedTouches = aOptions.changedTouches || [];
   1.108 +}
   1.109 +
   1.110 +function testtouch(aOptions) {
   1.111 +  if (!aOptions)
   1.112 +    aOptions = {};
   1.113 +  this.identifier = aOptions.identifier || 0;
   1.114 +  this.target = aOptions.target || 0;
   1.115 +  this.page = aOptions.page || {x: 0, y: 0};
   1.116 +  this.radius = aOptions.radius || {x: 0, y: 0};
   1.117 +  this.rotationAngle = aOptions.rotationAngle || 0;
   1.118 +  this.force = aOptions.force || 1;
   1.119 +}
   1.120 +
   1.121 +function testPreventDefault(name) {
   1.122 +  let cwu = SpecialPowers.getDOMWindowUtils(window);
   1.123 +  let target = document.getElementById("testTarget");
   1.124 +  let bcr = target.getBoundingClientRect();
   1.125 +
   1.126 +  let touch1 = new testtouch({
   1.127 +    page: {x: Math.round(bcr.left + bcr.width/2),
   1.128 +           y: Math.round(bcr.top  + bcr.height/2)},
   1.129 +    target: target
   1.130 +  });
   1.131 +
   1.132 +  let event = new touchEvent({
   1.133 +    touches: [touch1],
   1.134 +    targetTouches: [touch1],
   1.135 +    changedTouches: [touch1]
   1.136 +  });
   1.137 +
   1.138 +  // test touchstart event fires correctly
   1.139 +  var checkTouches = checkEvent(event, "touches");
   1.140 +  var checkTargetTouches = checkEvent(event, "targetTouches");
   1.141 +
   1.142 +  /* This is the heart of the test. Verify that the touch event
   1.143 +     looks correct both in and outside of a setTimeout */
   1.144 +  window.addEventListener("touchstart", function(firedEvent) {
   1.145 +    checkTouches(firedEvent, true);
   1.146 +    setTimeout(function() {
   1.147 +      checkTouches(firedEvent, true);
   1.148 +      checkTargetTouches(firedEvent, true);
   1.149 +
   1.150 +      event.touches = [];
   1.151 +      event.targetTouches = [];
   1.152 +      sendTouchEvent(cwu, "touchend", event, 0);
   1.153 +
   1.154 +      nextTest();
   1.155 +    }, 0);
   1.156 +  }, false);
   1.157 +  sendTouchEvent(cwu, "touchstart", event, 0);
   1.158 +}
   1.159 +
   1.160 +function doTest() {
   1.161 +  tests.push(testPreventDefault);
   1.162 +
   1.163 +  tests.push(function() {
   1.164 +    SimpleTest.finish();
   1.165 +  });
   1.166 +
   1.167 +  nextTest();
   1.168 +}
   1.169 +
   1.170 +SimpleTest.waitForExplicitFinish();
   1.171 +addLoadEvent(doTest);
   1.172 +
   1.173 +</script>
   1.174 +</pre>
   1.175 +<div id="parent">
   1.176 +  <span id="testTarget" style="padding: 5px; border: 1px solid black;">testTarget</span>
   1.177 +</div>
   1.178 +</body>
   1.179 +</html>

mercurial