Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 var pinchtest = (function() { | |
| 2 'use strict'; | |
| 3 | |
| 4 function assertTrue(condition, message) { | |
| 5 if (!condition) { | |
| 6 message = message || "Assertion failed"; | |
| 7 console.trace(); | |
| 8 throw new Error(message); | |
| 9 } | |
| 10 } | |
| 11 | |
| 12 function assertClose(a, b, message) { | |
| 13 if (Math.abs(a-b) > 1e-5) { | |
| 14 message = message || "Assertion failed"; | |
| 15 console.log('"', a, '" and "', b, '" are not close.'); | |
| 16 console.trace(); | |
| 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, '" and "', b, '" are not equal'); | |
| 51 console.trace(); | |
| 52 throw new Error(message); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 function makeTouch(e, offset) { | |
|
tdresser
2015/04/07 13:56:15
I find this somewhat hard to read, with the type o
wychen
2015/04/20 22:46:47
The interface of eventSender looks nice, but unfor
| |
| 57 for (var i=0; i < e.length; i++) { | |
| 58 if (typeof(e[i]) === 'number') { | |
| 59 e[i] = [e[i], e[i]]; | |
| 60 } | |
| 61 if (Array.isArray(e[i])) { | |
| 62 e[i] = {clientX: e[i][0], clientY: e[i][1]}; | |
| 63 } | |
| 64 if (e[i].pageX === undefined) { | |
| 65 e[i].pageX = e[i].clientX; | |
| 66 } | |
| 67 if (e[i].pageY === undefined) { | |
| 68 e[i].pageY = e[i].clientY; | |
| 69 } | |
| 70 if (Array.isArray(offset)) { | |
| 71 e[i].clientX += offset[0]; | |
| 72 e[i].clientY += offset[1]; | |
| 73 } | |
| 74 } | |
| 75 return { | |
| 76 touches: e, | |
| 77 preventDefault: function(){} | |
| 78 }; | |
| 79 } | |
| 80 | |
| 81 var eNull = makeTouch([]); | |
| 82 | |
| 83 function testZoomOut() { | |
| 84 pincher.reset(); | |
| 85 | |
| 86 // Make sure start event doesn't change state | |
| 87 var oldState = pincher.status(); | |
| 88 pincher.handleTouchStart(makeTouch([100])); | |
| 89 assertEqual(oldState, pincher.status()); | |
| 90 pincher.handleTouchStart(makeTouch([100, 300])); | |
| 91 assertEqual(oldState, pincher.status()); | |
| 92 | |
| 93 // Make sure extra move event doesn't change state | |
| 94 pincher.handleTouchMove(makeTouch([100, 300])); | |
| 95 assertEqual(oldState, pincher.status()); | |
| 96 | |
| 97 pincher.handleTouchMove(makeTouch([150, 250])); | |
| 98 | |
| 99 // Make sure end event doesn't change state | |
| 100 oldState = pincher.status(); | |
| 101 pincher.handleTouchEnd(makeTouch([250])); | |
| 102 assertEqual(oldState, pincher.status()); | |
| 103 pincher.handleTouchEnd([]); | |
| 104 assertEqual(oldState, pincher.status()); | |
| 105 | |
| 106 assertTrue(pincher.status().clampedScale < 0.9); | |
|
tdresser
2015/04/07 13:56:15
I'd prefer this assertion be directly after the to
wychen
2015/04/20 22:46:47
Done.
| |
| 107 } | |
| 108 | |
| 109 function testZoomIn() { | |
| 110 pincher.reset(); | |
| 111 | |
| 112 var oldState = pincher.status(); | |
| 113 pincher.handleTouchStart(makeTouch([150])); | |
| 114 assertEqual(oldState, pincher.status()); | |
| 115 pincher.handleTouchStart(makeTouch([150, 250])); | |
| 116 assertEqual(oldState, pincher.status()); | |
| 117 | |
| 118 pincher.handleTouchMove(makeTouch([100, 300])); | |
| 119 | |
| 120 oldState = pincher.status(); | |
| 121 pincher.handleTouchEnd(makeTouch([100])); | |
| 122 assertEqual(oldState, pincher.status()); | |
| 123 pincher.handleTouchEnd([]); | |
| 124 assertEqual(oldState, pincher.status()); | |
| 125 | |
| 126 assertTrue(pincher.status().clampedScale > 1.1); | |
|
tdresser
2015/04/07 13:56:15
I'd prefer this assertion be directly after the to
wychen
2015/04/20 22:46:46
Done.
| |
| 127 } | |
| 128 | |
| 129 function testZoomOutAndPan() { | |
| 130 pincher.reset(); | |
| 131 pincher.handleTouchStart(makeTouch([100])); | |
| 132 pincher.handleTouchStart(makeTouch([100, 300])); | |
| 133 pincher.handleTouchMove(makeTouch([150, 250])); | |
| 134 pincher.handleTouchMove(makeTouch([150, 250], [10, -5])); | |
| 135 pincher.handleTouchEnd(makeTouch([150], [10, -5])); | |
| 136 pincher.handleTouchEnd([]); | |
| 137 | |
| 138 assertClose(pincher.status().shiftX, 10); | |
| 139 assertClose(pincher.status().shiftY, -5); | |
| 140 assertTrue(pincher.status().clampedScale < 0.9); | |
| 141 } | |
| 142 | |
| 143 function testReversible() { | |
| 144 pincher.reset(); | |
| 145 pincher.handleTouchStart(makeTouch([10])); | |
| 146 pincher.handleTouchStart(makeTouch([10, 30])); | |
| 147 pincher.handleTouchMove(makeTouch([0, 40])); | |
| 148 pincher.handleTouchEnd(makeTouch([40])); | |
| 149 pincher.handleTouchEnd([]); | |
| 150 pincher.handleTouchStart(makeTouch([40])); | |
| 151 pincher.handleTouchStart(makeTouch([40, 0])); | |
| 152 pincher.handleTouchMove(makeTouch([30, 10])); | |
| 153 pincher.handleTouchEnd(makeTouch([30])); | |
|
tdresser
2015/04/07 13:56:15
Where did this 30 come from? Shouldn't it be 10?
wychen
2015/04/20 22:46:46
Done.
| |
| 154 pincher.handleTouchEnd([]); | |
| 155 assertClose(pincher.status().clampedScale, 1); | |
| 156 } | |
| 157 | |
| 158 function testMultitouchZoomOut() { | |
| 159 pincher.reset(); | |
| 160 | |
| 161 var oldState = pincher.status(); | |
| 162 pincher.handleTouchStart(makeTouch([100])); | |
| 163 assertEqual(oldState, pincher.status()); | |
| 164 pincher.handleTouchStart(makeTouch([100, 300])); | |
| 165 assertEqual(oldState, pincher.status()); | |
| 166 pincher.handleTouchStart(makeTouch([0, 100, 300])); | |
| 167 assertEqual(oldState, pincher.status()); | |
| 168 pincher.handleTouchStart(makeTouch([0, 100, 300, 400])); | |
| 169 assertEqual(oldState, pincher.status()); | |
| 170 | |
| 171 // Multi-touch zoom out. | |
| 172 pincher.handleTouchMove(makeTouch([100, 150, 250, 300])); | |
| 173 | |
| 174 oldState = pincher.status(); | |
| 175 pincher.handleTouchEnd(makeTouch([100, 150, 300])); | |
| 176 assertEqual(oldState, pincher.status()); | |
| 177 pincher.handleTouchEnd(makeTouch([150, 300])); | |
| 178 assertEqual(oldState, pincher.status()); | |
| 179 pincher.handleTouchEnd(makeTouch([300])); | |
| 180 assertEqual(oldState, pincher.status()); | |
| 181 pincher.handleTouchEnd([]); | |
| 182 assertEqual(oldState, pincher.status()); | |
| 183 | |
| 184 assertTrue(pincher.status().clampedScale < 0.9); | |
| 185 } | |
| 186 | |
| 187 function testZoomOutThenMulti() { | |
| 188 pincher.reset(); | |
| 189 | |
| 190 var oldState = pincher.status(); | |
| 191 pincher.handleTouchStart(makeTouch([100])); | |
| 192 assertEqual(oldState, pincher.status()); | |
| 193 pincher.handleTouchStart(makeTouch([100, 300])); | |
| 194 assertEqual(oldState, pincher.status()); | |
| 195 | |
| 196 // Zoom out. | |
| 197 pincher.handleTouchMove(makeTouch([150, 250])); | |
| 198 assertTrue(pincher.status().clampedScale < 0.9); | |
| 199 | |
| 200 // Make sure adding and removing more point doesn't change state | |
| 201 oldState = pincher.status(); | |
| 202 pincher.handleTouchStart(makeTouch([150, 250, 600])); | |
| 203 assertEqual(oldState, pincher.status()); | |
| 204 pincher.handleTouchEnd(makeTouch([150, 250])); | |
| 205 assertEqual(oldState, pincher.status()); | |
| 206 | |
| 207 // More than two fingers. | |
| 208 pincher.handleTouchStart(makeTouch([150, 250, [150, 250]])); | |
| 209 pincher.handleTouchStart(makeTouch([150, 250, [150, 250], [250, 150]])); | |
| 210 assertEqual(oldState, pincher.status()); | |
| 211 | |
| 212 pincher.handleTouchMove(makeTouch([100, 300, [100, 300], [300, 100]])); | |
| 213 assertClose(pincher.status().scale, 1); | |
| 214 | |
| 215 oldState = pincher.status(); | |
| 216 pincher.handleTouchEnd(makeTouch([100, 300, [100, 300]])); | |
| 217 assertEqual(oldState, pincher.status()); | |
| 218 pincher.handleTouchEnd(makeTouch([100, 300])); | |
| 219 assertEqual(oldState, pincher.status()); | |
| 220 pincher.handleTouchEnd(makeTouch([300])); | |
| 221 assertEqual(oldState, pincher.status()); | |
| 222 } | |
| 223 | |
| 224 function testCancel() { | |
| 225 pincher.reset(); | |
| 226 | |
| 227 pincher.handleTouchStart(makeTouch([100])); | |
| 228 pincher.handleTouchStart(makeTouch([100, 300])); | |
| 229 pincher.handleTouchMove(makeTouch([150, 250])); | |
| 230 assertTrue(pincher.status().clampedScale < 0.9); | |
| 231 | |
| 232 var oldState = pincher.status(); | |
| 233 pincher.handleTouchCancel([]); | |
| 234 assertEqual(oldState, pincher.status()); | |
| 235 | |
| 236 pincher.handleTouchStart(makeTouch([150])); | |
| 237 pincher.handleTouchStart(makeTouch([150, 250])); | |
| 238 pincher.handleTouchMove(makeTouch([100, 300])); | |
| 239 assertClose(pincher.status().clampedScale, 1); | |
| 240 } | |
| 241 | |
| 242 function testSingularity() { | |
| 243 pincher.reset(); | |
| 244 | |
| 245 pincher.handleTouchStart(makeTouch([100])); | |
| 246 pincher.handleTouchStart(makeTouch([100, 100])); | |
| 247 pincher.handleTouchMove(makeTouch([50, 150])); | |
| 248 assertTrue(pincher.status().clampedScale > 1.1); | |
| 249 assertTrue(pincher.status().clampedScale < 100); | |
| 250 assertTrue(pincher.status().scale < 100); | |
| 251 | |
| 252 pincher.handleTouchCancel([]); | |
| 253 } | |
| 254 | |
| 255 function testMinSpan() { | |
| 256 pincher.reset(); | |
| 257 | |
| 258 pincher.handleTouchStart(makeTouch([50])); | |
| 259 pincher.handleTouchStart(makeTouch([50, 150])); | |
| 260 pincher.handleTouchMove(makeTouch([100, 100])); | |
| 261 assertTrue(pincher.status().clampedScale < 0.9); | |
| 262 assertTrue(pincher.status().clampedScale > 0); | |
| 263 assertTrue(pincher.status().scale > 0); | |
| 264 | |
| 265 pincher.handleTouchCancel([]); | |
| 266 } | |
| 267 | |
| 268 return { | |
| 269 run: function(){ | |
| 270 testZoomOut(); | |
| 271 testZoomIn(); | |
| 272 testZoomOutAndPan(); | |
| 273 testReversible(); | |
| 274 testMultitouchZoomOut(); | |
| 275 testZoomOutThenMulti(); | |
| 276 testCancel(); | |
| 277 testSingularity(); | |
| 278 testMinSpan(); | |
| 279 pincher.reset(); | |
| 280 | |
| 281 return {success: true}; | |
| 282 } | |
| 283 }; | |
| 284 }()); | |
| OLD | NEW |