OLD | NEW |
---|---|
(Empty) | |
1 var pinchtest = (function() { | |
2 'use strict'; | |
3 | |
4 function assertTrue(condition, message) { | |
tdresser
2015/03/25 13:45:36
Also, this method is pretty much equivalent to con
tdresser
2015/03/25 13:45:36
It's a shame to have to implement all these trivia
wychen
2015/04/02 02:05:59
Thanks for the suggestion! I've looked at some JS
wychen
2015/04/02 02:05:59
Almost, since I need to throw an exception as well
tdresser
2015/04/07 13:56:15
Acknowledged.
tdresser
2015/04/07 13:56:15
Acknowledged.
| |
5 if (!condition) { | |
6 message = message || "Assertion failed"; | |
7 throw new Error(message); | |
8 } | |
9 } | |
10 | |
11 function assertClose(a, b, message) { | |
12 if (Math.abs(a-b) > 1e-5) { | |
13 message = message || "Assertion failed"; | |
14 console.log(a); | |
15 console.log(b); | |
16 console.log('"' + a + '" and "' + b + '" are not close.'); | |
17 throw new Error(message); | |
18 } | |
19 } | |
20 | |
21 function isEquivalent(a, b) { | |
22 // Create arrays of property names | |
23 var aProps = Object.getOwnPropertyNames(a); | |
24 var bProps = Object.getOwnPropertyNames(b); | |
25 | |
26 // If number of properties is different, | |
27 // objects are not equivalent | |
28 if (aProps.length != bProps.length) { | |
29 return false; | |
30 } | |
31 | |
32 for (var i = 0; i < aProps.length; i++) { | |
33 var propName = aProps[i]; | |
34 | |
35 // If values of same property are not equal, | |
36 // objects are not equivalent | |
37 if (a[propName] !== b[propName]) { | |
38 return false; | |
39 } | |
40 } | |
41 | |
42 // If we made it this far, objects | |
43 // are considered equivalent | |
44 return true; | |
45 } | |
46 | |
47 function assertEqual(a, b, message) { | |
48 if (!isEquivalent(a, b)) { | |
49 message = message || "Assertion failed"; | |
50 console.log(a); | |
51 console.log(b); | |
52 console.log('"' + a.toString() + '" and "' + b + '" are not equal.'); | |
53 throw new Error(message); | |
54 } | |
55 } | |
56 | |
57 function makeTouch(e) { | |
tdresser
2015/03/25 13:45:36
You should be able to get access to window.eventSe
wychen
2015/04/02 02:05:59
The reason I mock touch events in JS is exactly to
tdresser
2015/04/07 13:56:15
See https://code.google.com/p/chromium/codesearch#
| |
58 for (var i=0; i < e.length; i++) { | |
59 if (typeof(e[i]) === 'number') { | |
60 e[i] = {clientX: e[i]}; | |
61 } | |
62 if (e[i].clientY === undefined) { | |
63 e[i].clientY = e[i].clientX; | |
64 } | |
65 if (e[i].pageX === undefined) { | |
66 e[i].pageX = e[i].clientX; | |
67 } | |
68 if (e[i].pageY === undefined) { | |
69 e[i].pageY = e[i].clientY; | |
70 } | |
71 } | |
72 return { | |
73 touches: e, | |
74 preventDefault: function(){} | |
75 }; | |
76 } | |
77 | |
78 var eNull = makeTouch([]); | |
79 var e1 = makeTouch([100]); | |
80 var e2 = makeTouch([100, 300]); | |
81 var e3 = makeTouch([150, 250]); | |
82 var e4 = makeTouch([250]); | |
83 | |
84 var e3p = makeTouch([ | |
85 {clientX: 160, clientY: 145, pageX: 150, pageY: 150}, | |
86 {clientX: 260, clientY: 245, pageX: 250, pageY: 250} | |
87 ]); | |
88 var e4p = makeTouch([ | |
89 {clientX: 260, clientY: 250, pageX: 250} | |
90 ]); | |
91 | |
92 function testZoomOut() { | |
93 pincher.reset(); | |
94 | |
95 // Make sure start event doesn't change state | |
96 var oldState = pincher.status(); | |
97 pincher.handleTouchStart(e1); | |
98 assertEqual(oldState, pincher.status()); | |
99 pincher.handleTouchStart(e2); | |
100 assertEqual(oldState, pincher.status()); | |
101 | |
102 // Make sure extra move event doesn't change state | |
103 pincher.handleTouchMove(e2); | |
104 assertEqual(oldState, pincher.status()); | |
105 | |
106 pincher.handleTouchMove(e3); | |
107 | |
108 // Make sure end event doesn't change state | |
109 oldState = pincher.status(); | |
110 pincher.handleTouchEnd(e4); | |
111 assertEqual(oldState, pincher.status()); | |
112 pincher.handleTouchEnd(eNull); | |
113 assertEqual(oldState, pincher.status()); | |
114 | |
115 assertClose(pincher.status().scale, 0.5); | |
116 assertTrue(pincher.status().clampedScale < 0.9); | |
117 } | |
118 | |
119 function testZoomIn() { | |
120 pincher.reset(); | |
121 | |
122 var oldState = pincher.status(); | |
123 pincher.handleTouchStart(e4); | |
124 assertEqual(oldState, pincher.status()); | |
125 pincher.handleTouchStart(e3); | |
126 assertEqual(oldState, pincher.status()); | |
127 | |
128 pincher.handleTouchMove(e3); | |
129 assertEqual(oldState, pincher.status()); | |
130 | |
131 pincher.handleTouchMove(e2); | |
132 | |
133 oldState = pincher.status(); | |
134 pincher.handleTouchEnd(e1); | |
135 assertEqual(oldState, pincher.status()); | |
136 pincher.handleTouchEnd(eNull); | |
137 assertEqual(oldState, pincher.status()); | |
138 | |
139 assertClose(pincher.status().scale, 2); | |
140 assertTrue(pincher.status().clampedScale > 1.1); | |
141 } | |
142 | |
143 function testZoomOutAndPan() { | |
144 pincher.reset(); | |
145 pincher.handleTouchStart(e1); | |
tdresser
2015/03/25 13:45:36
I would find this much easier to read if the posit
wychen
2015/04/02 02:05:59
Indeed. They are inlined now.
| |
146 pincher.handleTouchStart(e2); | |
147 pincher.handleTouchMove(e3); | |
148 pincher.handleTouchMove(e3p); | |
149 pincher.handleTouchEnd(e4p); | |
150 pincher.handleTouchEnd(eNull); | |
151 assertClose(pincher.status().scale, 0.5); | |
152 assertClose(pincher.status().shiftX, 10); | |
153 assertClose(pincher.status().shiftY, -5); | |
154 } | |
155 | |
156 function testReversible() { | |
157 pincher.reset(); | |
158 pincher.handleTouchStart(e1); | |
159 pincher.handleTouchStart(e2); | |
160 pincher.handleTouchMove(e3); | |
161 pincher.handleTouchEnd(e4); | |
162 pincher.handleTouchEnd(eNull); | |
163 pincher.handleTouchStart(e4); | |
164 pincher.handleTouchStart(e3); | |
165 pincher.handleTouchMove(e2); | |
166 pincher.handleTouchEnd(e1); | |
167 pincher.handleTouchEnd(eNull); | |
168 assertClose(pincher.status().clampedScale, 1); | |
169 } | |
170 | |
171 var et3 = makeTouch([ | |
172 {clientX: 100, clientY: 100}, | |
173 {clientX: 300, clientY: 300}, | |
174 {clientX: 100, clientY: 300} | |
175 ]); | |
176 var et4 = makeTouch([ | |
177 {clientX: 100, clientY: 100}, | |
178 {clientX: 300, clientY: 300}, | |
179 {clientX: 100, clientY: 300}, | |
180 {clientX: 300, clientY: 100} | |
181 ]); | |
182 var et5 = makeTouch([ | |
183 {clientX: 150, clientY: 150}, | |
184 {clientX: 250, clientY: 250}, | |
185 {clientX: 150, clientY: 250}, | |
186 {clientX: 250, clientY: 150} | |
187 ]); | |
188 var et6 = makeTouch([ | |
189 {clientX: 150, clientY: 150}, | |
190 {clientX: 150, clientY: 250}, | |
191 {clientX: 250, clientY: 150} | |
192 ]); | |
193 var et7 = makeTouch([ | |
194 {clientX: 150, clientY: 150}, | |
195 {clientX: 250, clientY: 150} | |
196 ]); | |
197 var et8 = makeTouch([ | |
198 {clientX: 250, clientY: 150} | |
199 ]); | |
200 | |
201 var et3z = makeTouch([ | |
202 {clientX: 150, clientY: 150}, | |
203 {clientX: 250, clientY: 250}, | |
204 {clientX: 150, clientY: 250}, | |
205 ]); | |
206 | |
207 function testMultitouch() { | |
208 pincher.reset(); | |
209 | |
210 var oldState = pincher.status(); | |
211 pincher.handleTouchStart(e1); | |
212 assertEqual(oldState, pincher.status()); | |
213 pincher.handleTouchStart(e2); | |
214 assertEqual(oldState, pincher.status()); | |
215 pincher.handleTouchStart(et3); | |
216 assertEqual(oldState, pincher.status()); | |
217 pincher.handleTouchStart(et4); | |
218 assertEqual(oldState, pincher.status()); | |
219 | |
220 pincher.handleTouchMove(et5); | |
221 | |
222 oldState = pincher.status(); | |
223 pincher.handleTouchEnd(et6); | |
224 assertEqual(oldState, pincher.status()); | |
225 pincher.handleTouchEnd(et7); | |
226 assertEqual(oldState, pincher.status()); | |
227 pincher.handleTouchEnd(et8); | |
228 assertEqual(oldState, pincher.status()); | |
229 pincher.handleTouchEnd(eNull); | |
230 assertEqual(oldState, pincher.status()); | |
231 | |
232 assertClose(pincher.status().scale, 0.5); | |
233 assertTrue(pincher.status().clampedScale < 0.9); | |
234 } | |
235 | |
236 function testZoomOutThenMulti() { | |
237 pincher.reset(); | |
238 | |
239 var oldState = pincher.status(); | |
240 pincher.handleTouchStart(e1); | |
241 assertEqual(oldState, pincher.status()); | |
242 pincher.handleTouchStart(e2); | |
243 assertEqual(oldState, pincher.status()); | |
244 | |
245 pincher.handleTouchMove(e3); | |
246 assertClose(pincher.status().scale, 0.5); | |
247 assertTrue(pincher.status().clampedScale < 0.9); | |
248 | |
249 // Make sure adding and removing more point doesn't change state | |
250 oldState = pincher.status(); | |
251 pincher.handleTouchStart(et3z); | |
252 assertEqual(oldState, pincher.status()); | |
253 pincher.handleTouchEnd(e3); | |
254 assertEqual(oldState, pincher.status()); | |
255 | |
256 pincher.handleTouchStart(et3z); | |
257 pincher.handleTouchStart(et5); | |
258 assertEqual(oldState, pincher.status()); | |
259 | |
260 pincher.handleTouchMove(et4); | |
261 assertClose(pincher.status().scale, 1); | |
262 | |
263 oldState = pincher.status(); | |
264 pincher.handleTouchEnd(et3); | |
265 assertEqual(oldState, pincher.status()); | |
266 pincher.handleTouchEnd(e2); | |
267 assertEqual(oldState, pincher.status()); | |
268 pincher.handleTouchEnd(e1); | |
269 assertEqual(oldState, pincher.status()); | |
270 } | |
271 | |
272 function testCancel() { | |
273 pincher.reset(); | |
274 | |
275 pincher.handleTouchStart(e1); | |
276 pincher.handleTouchStart(e2); | |
277 pincher.handleTouchMove(e3); | |
278 pincher.handleTouchCancel(eNull); | |
279 | |
280 assertClose(pincher.status().scale, 0.5); | |
281 assertTrue(pincher.status().clampedScale < 0.9); | |
282 } | |
283 | |
284 return { | |
285 run: function(){ | |
286 testZoomOut(); | |
287 testZoomIn(); | |
288 testZoomOutAndPan(); | |
289 testReversible(); | |
290 testMultitouch(); | |
291 testZoomOutThenMulti(); | |
292 testCancel(); | |
293 pincher.reset(); | |
294 | |
295 return {success: true}; | |
296 } | |
297 }; | |
298 }()); | |
OLD | NEW |