touch.html 8.3 KB

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