test_gestures.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // TODO: this tests fails because tapRecognizer changes
  2. // it could be that tapRecognizer setup its BEGAN state and
  3. // disable the other gesture recognition
  4. var el, hammer, events;
  5. var allGestureEvents = [
  6. 'tap doubletap press',
  7. 'pinch pinchin pinchout pinchstart pinchmove pinchend pinchcancel',
  8. 'rotate rotatestart rotatemove rotateend rotatecancel',
  9. 'pan panstart panmove panup pandown panleft panright panend pancancel',
  10. 'swipe swipeleft swiperight swipeup swipedown'
  11. ].join(' ');
  12. module('Gesture recognition', {
  13. setup: function() {
  14. el = utils.createHitArea();
  15. hammer = new Hammer(el);
  16. hammer.get('pinch')
  17. .set({ // some threshold, since the simulator doesnt stays at scale:1 when rotating
  18. enable: true,
  19. threshold: .1
  20. });
  21. hammer.get('rotate')
  22. .set({ enable: true });
  23. hammer.on(allGestureEvents, function(ev) {
  24. events[ev.type] = true;
  25. });
  26. events = {};
  27. },
  28. teardown: function() {
  29. hammer && hammer.destroy();
  30. events = null;
  31. }
  32. });
  33. asyncTest('recognize pan', function() {
  34. expect(1);
  35. Simulator.gestures.pan(el, { duration: 500, deltaX: 100, deltaY: 0 }, function() {
  36. start();
  37. deepEqual(events, {
  38. pan: true,
  39. panstart: true,
  40. panmove: true,
  41. panright: true,
  42. panend: true
  43. });
  44. });
  45. });
  46. asyncTest('recognize press', function() {
  47. expect(1);
  48. Simulator.gestures.press(el, null, function() {
  49. start();
  50. deepEqual(events, {
  51. press: true
  52. });
  53. });
  54. });
  55. asyncTest('recognize swipe', function() {
  56. expect(1);
  57. Simulator.gestures.swipe(el, { duration: 300, deltaX: 400, deltaY: 0 }, function() {
  58. start();
  59. deepEqual(events, {
  60. pan: true,
  61. panstart: true,
  62. panmove: true,
  63. panright: true,
  64. panend: true,
  65. swipe: true,
  66. swiperight: true
  67. });
  68. });
  69. });
  70. asyncTest('recognize pinch', function() {
  71. expect(1);
  72. Simulator.gestures.pinch(el, { duration: 500, scale: .5 }, function() {
  73. start();
  74. deepEqual(events, {
  75. pinch: true,
  76. pinchstart: true,
  77. pinchmove: true,
  78. pinchend: true,
  79. pinchin: true
  80. });
  81. });
  82. });
  83. asyncTest('recognize children multitouch pinch', function() {
  84. expect(1);
  85. var el1 = utils.createHitArea(el),
  86. el2 = utils.createHitArea(el);
  87. Simulator.gestures.pinch([el1, el2], { duration: 500, scale: .5 }, function() {
  88. start();
  89. deepEqual(events, {
  90. pinch: true,
  91. pinchstart: true,
  92. pinchmove: true,
  93. pinchend: true,
  94. pinchin: true
  95. });
  96. });
  97. });
  98. asyncTest('recognize parent-child multitouch pinch', function() {
  99. expect(1);
  100. var el1 = utils.createHitArea(el);
  101. Simulator.gestures.pinch([el, el1], { duration: 100, scale: .5 }, function() {
  102. start();
  103. deepEqual(events, {
  104. pinch: true,
  105. pinchstart: true,
  106. pinchmove: true,
  107. pinchend: true,
  108. pinchin: true
  109. });
  110. });
  111. });
  112. asyncTest('recognize rotate', function() {
  113. expect(1);
  114. Simulator.gestures.rotate(el, { duration: 500, scale: 1 }, function() {
  115. start();
  116. deepEqual(events, {
  117. rotate: true,
  118. rotatestart: true,
  119. rotatemove: true,
  120. rotateend: true
  121. });
  122. });
  123. });
  124. asyncTest('recognize multitouch rotate', function() {
  125. expect(1);
  126. var el1 = utils.createHitArea(el);
  127. Simulator.gestures.rotate([el, el1], { duration: 500, scale: 1 }, function() {
  128. start();
  129. deepEqual(events, {
  130. rotate: true,
  131. rotatestart: true,
  132. rotatemove: true,
  133. rotateend: true
  134. });
  135. });
  136. });
  137. asyncTest('recognize rotate and pinch simultaneous', function() {
  138. expect(1);
  139. Simulator.gestures.pinchRotate(el, { duration: 500, scale: 2 }, function() {
  140. start();
  141. deepEqual(events, {
  142. rotate: true,
  143. rotatestart: true,
  144. rotatemove: true,
  145. rotateend: true,
  146. pinch: true,
  147. pinchstart: true,
  148. pinchmove: true,
  149. pinchend: true,
  150. pinchout: true
  151. });
  152. });
  153. });
  154. asyncTest('don\'t recognize pan and swipe when moving down, when only horizontal is allowed', function() {
  155. expect(1);
  156. Simulator.gestures.swipe(el, { duration: 250, deltaX: 0, deltaZ: 200 }, function() {
  157. start();
  158. deepEqual(events, { });
  159. });
  160. });
  161. asyncTest('don\'t recognize press if duration is too short.', function() {
  162. expect(1);
  163. Simulator.gestures.press(el, { duration: 240 });
  164. setTimeout(function() {
  165. start();
  166. deepEqual(events, { tap: true }, 'Tap gesture has been recognized.');
  167. }, 275);
  168. });
  169. asyncTest('don\'t recognize tap if duration is too long.', function() {
  170. expect(1);
  171. Simulator.gestures.tap(el, { duration: 255 });
  172. setTimeout(function() {
  173. start();
  174. deepEqual(events, { press: true }, 'Press gesture has been recognized.');
  175. }, 275);
  176. });