forked from madrobby/zepto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch.html
338 lines (279 loc) · 8.28 KB
/
touch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link rel="stylesheet" href="test.css">
<title>Zepto Touch unit tests</title>
<script src="../vendor/evidence.js"></script>
<script src="evidence_runner.js"></script>
<script>
// avoid caching
(function(){
function load(scripts){
scripts.split(' ').forEach(function(script){
document.write('<script src="../src/'+script+'.js?'+(+new Date)+'"></scr'+'ipt>')
})
}
load('zepto event ie touch')
})()
</script>
</head>
<body>
<h1>Zepto Touch unit tests</h1>
<p id="results">
Running… see browser console for results
</p>
<style>
#test {
position: absolute;
left: 0;
top: 0;
width: 100px;
height: 50px;
}
</style>
<script>
(function(){
var emitEvent, tests
// Fire a simulated touch event.
// While it is possible to fire real touch events,
// there are cross-browser issues, and this way we
// can test touch events in browsers that don't
// actually support touch input (like desktop Safari).
//
// Zepto's touch module only uses the `pageX/Y` and `target`
// properties of the first touch in the `touches` TouchList
function emitTouchEvent(type, element, x, y) {
var event = document.createEvent('Event'),
touch = { pageX: x, pageY: y, target: element }
event.initEvent('touch'+type, true, true)
event.touches = [touch]
element.dispatchEvent(event)
}
// IE 10, Windows Phone 8
// MSPointer events, same idea, different implementation.
// For compatibility accepts the same arguments as `touchEvent`
// (touch event names will be mapped to MSPointer event names)
function emitMSPointerEvent(type, element, x, y) {
var event = document.createEvent('Event'),
eventMap = { start: 'Down', move: 'Move', end: 'Up'}
event.initEvent('MSPointer'+eventMap[type], true, true)
event.pageX = x
event.pageY = y
event.pointerType = 666
event.MSPOINTER_TYPE_TOUCH = 666
event.isPrimary = true
element.dispatchEvent(event)
}
// IE 11
// Pointer events, same idea again, yet different implementation.
// For compatibility accepts the same arguments as `touchEvent`
// (touch event names will be mapped to pointer event names)
function emitPointerEvent(type, element, x, y) {
var event = document.createEvent('Event'),
eventMap = { start: 'down', move: 'move', end: 'up'}
event.initEvent('pointer'+eventMap[type], true, true)
event.pageX = x
event.pageY = y
event.pointerType = 'touch'
event.isPrimary = true
element.dispatchEvent(event)
}
function down(element, x, y) {
emitEvent('start', element, x, y)
}
function move(element, x,y) {
emitEvent('move', element, x, y)
}
function up(element) {
emitEvent('end', element)
}
tests = {
setUp: function(){
emitEvent = this.emitEvent
$('<div id=test>TEST ELEMENT</div>').appendTo('body')
},
tearDown: function(){
$('#test').off()
$('#test').remove()
},
testTap: function(t){
var count = 0, element = $('#test').get(0)
$('#test').on('tap', function(){
count++
})
down(element, 10, 10)
up(element)
t.pause()
setTimeout(function(){
t.resume(function(){
t.assertEqual(1, count)
})
}, 50)
},
testScrollDoesNotFireTap: function(t){
var element = $('#test').get(0), count = 0
$('#test').on('tap', function () {
count++
})
down(element, 0, 0)
move(element, 0, 20)
move(element, 0, 20)
up(element)
t.pause()
setTimeout(function(){
t.resume(function(){
t.assertEqual(0, count)
})
}, 50)
},
testSingleTapDoesNotInterfereWithTappingTwice: function(t){
var count = 0, element = $('#test').get(0)
$('#test').on('tap', function(){
count++
})
down(element, 10, 10)
up(element)
t.pause()
setTimeout(function(){
down(element, 10, 10)
up(element)
t.resume(function(){
t.pause()
setTimeout(function(){
t.resume(function(){
t.assertEqual(2, count)
})
}, 200)
})
}, 200)
},
// should be fired if there is one tap within 250ms
testSingleTap: function(t){
var singleCount = 0, doubleCount = 0, element = $('#test').get(0)
$('#test').on('singleTap', function(){
singleCount++
}).on('doubleTap', function(){
doubleCount++
})
down(element, 10, 10)
up(element)
t.pause()
setTimeout(function(){
t.resume(function(){
t.assertEqual(1, singleCount)
t.assertEqual(0, doubleCount)
})
}, 300)
},
// should be fired if there are two taps within 250ms
testDoubleTap: function(t){
var singleCount = 0, doubleCount = 0, element = $('#test').get(0)
$('#test').on('singleTap', function(){
singleCount++
}).on('doubleTap', function(){
doubleCount++
})
down(element, 10, 10)
up(element)
t.pause()
setTimeout(function(){
down(element, 12, 12)
up(element)
t.resume(function(){
t.pause()
setTimeout(function(){
t.resume(function(){
t.assertEqual(0, singleCount)
t.assertEqual(1, doubleCount)
})
}, 100)
})
}, 100)
},
// should be fired if the finger is down in the same location for >750ms
testLongTap: function(t){
var count = 0, element = $('#test').get(0)
$('#test').on('longTap', function(){
count++
})
down(element, 10, 10)
t.pause()
setTimeout(function(){
up(element)
t.resume(function(){
t.assertEqual(1, count)
})
}, 900)
},
testLongTapDoesNotFireIfFingerIsMoved: function(t){
var count = 0, element = $('#test').get(0)
$('#test').on('longTap', function(){
count++
})
down(element, 10, 10)
t.pause()
setTimeout(function(){
move(element, 50, 10)
t.resume(function(){
t.pause()
setTimeout(function(){
up(element)
t.resume(function(){
t.assertEqual(0, count)
})
}, 450)
})
}, 450)
},
testSwipe: function(t){
var swipeCount = 0, element = $('#test').get(0)
$('#test').on('swipe', function(){
swipeCount++
})
down(element, 10, 10)
t.pause()
setTimeout(function(){
move(element, 70, 10)
up(element)
t.resume(function(){
t.pause()
setTimeout(function(){
t.resume(function(){
t.assertEqual(1, swipeCount)
})
}, 50)
})
}, 50)
},
testTapDoNotFireWhenMoveToPointAndBack: function (t) {
var tapCount = 0, element = $('#test').get(0)
$('#test').on('tap', function(){
tapCount++
})
down(element, 10, 10)
move(element, 60, 10)
t.pause()
setTimeout(function(){
t.resume(function(){
move(element, 10, 10)
up(element)
t.pause()
setTimeout(function(){
t.resume( function () {
t.assertEqual(0, tapCount)
})
}, 100)
})
}, 100)
}
// TODO: test swipes in specific directions
}
Evidence('TouchTest', $.extend({ emitEvent: emitTouchEvent }, tests))
Evidence('MSPointerTest', $.extend({ emitEvent: emitMSPointerEvent }, tests))
Evidence('PointerTest', $.extend({ emitEvent: emitPointerEvent }, tests))
})()
</script>
</body>
</html>