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