test_hammer.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. var el, el2,
  2. hammer, hammer2;
  3. module('Tests', {
  4. setup: function() {
  5. el = utils.createHitArea();
  6. el2 = utils.createHitArea();
  7. },
  8. teardown: function() {
  9. if (hammer) {
  10. hammer.destroy();
  11. hammer = null;
  12. }
  13. if (hammer2) {
  14. hammer2.destroy();
  15. hammer2 = null;
  16. }
  17. }
  18. });
  19. test('hammer shortcut', function() {
  20. expect(2);
  21. Hammer.defaults.touchAction = 'pan-y';
  22. hammer = Hammer(el);
  23. ok(hammer instanceof Hammer.Manager, 'returns an instance of Manager');
  24. ok(hammer.touchAction.actions == Hammer.defaults.touchAction, 'set the default touchAction');
  25. });
  26. test('hammer shortcut with options', function() {
  27. expect(2);
  28. hammer = Hammer(el, {
  29. touchAction: 'none'
  30. });
  31. ok(hammer instanceof Hammer.Manager, 'returns an instance of Manager');
  32. ok(hammer.touchAction.actions == 'none', 'set the default touchAction');
  33. });
  34. /* Creating a hammer instance does not work on the same way
  35. * when using Hammer or Hammer.Manager.
  36. *
  37. * This can confuse developers who read tests to use the library when doc is missing.
  38. */
  39. test('Hammer and Hammer.Manager constructors work exactly on the same way.', function() {
  40. expect(2);
  41. hammer = new Hammer(el, {});
  42. equal(Hammer.defaults.preset.length, hammer.recognizers.length);
  43. hammer2 = new Hammer.Manager(el, {});
  44. equal(0, hammer2.recognizers.length);
  45. });
  46. /* DOC to disable default recognizers should be added.
  47. *
  48. * - Hammer(el). IMO: Currently, well done.
  49. * - Hammer(el, {}) . IMO: should disable default recognizers
  50. * - Hammer(el, {recognizers: null}). IMO: now, it fails.
  51. * - Hammer(el, {recognizers: []}). It works, but it is likely not intuitive.
  52. */
  53. test('A Hammer instance can be setup to not having default recognizers.', function() {
  54. expect(1);
  55. hammer = new Hammer(el, { recognizers: false });
  56. equal(0, hammer.recognizers.length);
  57. });
  58. /* The case was when I added a custom tap event which was added to the default
  59. * recognizers, and my custom tap gesture wasn't working (I do not know exactly the reason),
  60. * but removing the default recognizers solved the issue.
  61. */
  62. test('Adding the same recognizer type should remove the old recognizer', function() {
  63. expect(4);
  64. hammer = new Hammer(el);
  65. ok(!!hammer.get('tap'));
  66. equal(7, hammer.recognizers.length);
  67. var newTap = new Hammer.Tap({time: 1337});
  68. hammer.add(newTap);
  69. equal(7, hammer.recognizers.length);
  70. equal(1337, hammer.get('tap').options.time);
  71. });
  72. /*
  73. * Swipe gesture:
  74. * - in this tests, it does not update input.velocity ( always 0)
  75. * - does not fire swipeleft or swiperight events
  76. */
  77. asyncTest('Swiping to the left should fire swipeleft event', function() {
  78. expect(2);
  79. hammer = new Hammer(el, {recognizers: []});
  80. hammer.add(new Hammer.Swipe());
  81. hammer.on('swipe swipeleft', function() {
  82. ok(true);
  83. });
  84. Simulator.gestures.swipe(el, {pos: [300, 300], deltaY: 0, deltaX: -200}, function() {
  85. start();
  86. });
  87. });
  88. /*
  89. * Input target change
  90. */
  91. asyncTest('Should detect input while on other element', function() {
  92. expect(1);
  93. hammer = new Hammer(el, { inputTarget: document.body });
  94. hammer.on('tap', function() {
  95. ok(true);
  96. });
  97. Simulator.gestures.tap(document.body, null, function() {
  98. start();
  99. });
  100. });
  101. /* Hammer.Manager constructor accepts a "recognizers" option in which each
  102. * element is an array representation of a Recognizer.
  103. */
  104. test('Hammer.Manager accepts recognizers as arrays.', function() {
  105. expect(4);
  106. hammer = new Hammer.Manager(el, {
  107. recognizers: [
  108. [Hammer.Swipe],
  109. [Hammer.Pinch],
  110. [Hammer.Rotate],
  111. [Hammer.Pan, { direction: Hammer.DIRECTION_UP }, ['swipe', 'pinch'], ['rotate']]
  112. ]
  113. });
  114. equal(4, hammer.recognizers.length);
  115. var recognizerActual = hammer.recognizers[3];
  116. equal(recognizerActual.options.direction, Hammer.DIRECTION_UP);
  117. equal(2, Object.keys(recognizerActual.simultaneous).length);
  118. equal(1, recognizerActual.requireFail.length);
  119. });
  120. /*
  121. * Removing a recognizer which cannot be found would errantly remove the last recognizer in the
  122. * manager's list.
  123. */
  124. test('Remove non-existent recognizer.', function() {
  125. expect(1);
  126. hammer = new Hammer(el, {recognizers: []});
  127. hammer.add(new Hammer.Swipe());
  128. hammer.remove('tap');
  129. equal(1, hammer.recognizers.length);
  130. });
  131. test('check whether Hammer.defaults.cssProps is restored', function() {
  132. var beforeCssProps = {
  133. userSelect: 'text',
  134. touchSelect: 'grippers',
  135. touchCallout: 'default',
  136. contentZooming: 'chained',
  137. userDrag: 'element',
  138. tapHighlightColor: 'rgba(0, 1, 0, 0)'
  139. };
  140. var prop;
  141. Hammer.each(Hammer.defaults.cssProps, function(value, name) {
  142. prop = Hammer.prefixed(el.style, name);
  143. if (prop) {
  144. el.style[prop] = beforeCssProps[name];
  145. }
  146. });
  147. hammer = Hammer(el);
  148. hammer.destroy();
  149. hammer = null;
  150. Hammer.each(Hammer.defaults.cssProps, function(value, name) {
  151. prop = Hammer.prefixed(el.style, name);
  152. if (prop) {
  153. equal(el.style[prop], beforeCssProps[name], "check if " + name + " is restored");
  154. }
  155. });
  156. });