jquery.flot.touch.Test.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /* eslint-disable */
  2. /* global $, describe, it, xit, xdescribe, after, afterEach, expect*/
  3. describe("flot touch plugin", function () {
  4. var placeholder, plot, options;
  5. beforeEach(function () {
  6. placeholder = setFixtures('<div id="test-container" style="width: 600px;height: 400px">')
  7. .find('#test-container');
  8. options = {
  9. xaxes: [{ autoScale: 'exact' }],
  10. yaxes: [{ autoScale: 'exact' }],
  11. zoom: { interactive: true, amount: 10, active: true },
  12. pan: { interactive: true, frameRate: -1, enableTouch: true, active: true }
  13. };
  14. });
  15. it('shows that the eventHolder is cleared through shutdown when the plot is replaced', function() {
  16. plot = $.plot(placeholder, [[]], options);
  17. var eventPlaceholder = plot.getEventHolder();
  18. spy = spyOn(eventPlaceholder, 'removeEventListener').and.callThrough();
  19. plot = $.plot(placeholder, [[]], options);
  20. expect(spy).toHaveBeenCalledWith('touchstart', jasmine.any(Function))
  21. expect(spy).toHaveBeenCalledWith('touchmove', jasmine.any(Function));
  22. expect(spy).toHaveBeenCalledWith('touchend', jasmine.any(Function));
  23. });
  24. it('do not stop origin touch event propagation if it is allowed', () => {
  25. jasmine.clock().install().mockDate();
  26. var oldPropagateSupportedGesture = options.propagateSupportedGesture ;
  27. options.propagateSupportedGesture = true;
  28. plot = $.plot(placeholder, [[]], options);
  29. var eventHolder = plot.getEventHolder(),
  30. spy = jasmine.createSpy('origin touch event handler');
  31. eventHolder.parentNode.addEventListener('touchstart', spy, { once: true });
  32. eventHolder.parentNode.addEventListener('touchmove', spy, { once: true });
  33. eventHolder.parentNode.addEventListener('touchend', spy, { once: true });
  34. var bubbleTouchEvents = [
  35. new UIEvent('touchstart', { bubbles: true }),
  36. new UIEvent('touchmove', { bubbles: true }),
  37. new UIEvent('touchend', { bubbles: true }),
  38. ];
  39. bubbleTouchEvents.forEach((event) => {
  40. event.touches = [{ identifier: 0, target: eventHolder }];
  41. eventHolder.dispatchEvent(event);
  42. });
  43. expect(spy).toHaveBeenCalledTimes(3);
  44. options.propagateSupportedGesture = oldPropagateSupportedGesture;
  45. jasmine.clock().uninstall();
  46. });
  47. describe('long tap', function() {
  48. beforeEach(function() {
  49. jasmine.clock().install().mockDate();
  50. });
  51. afterEach(function() {
  52. jasmine.clock().uninstall();
  53. });
  54. it('should trigger the long tap event',function() {
  55. plot = $.plot(placeholder, [[]], options);
  56. var eventHolder = plot.getEventHolder(),
  57. spy = jasmine.createSpy('long tap handler'),
  58. coords = [{x: 10, y: 20}];
  59. eventHolder.addEventListener('longtap', spy);
  60. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  61. jasmine.clock().tick(1600);
  62. jasmine.clock().tick(1600);
  63. jasmine.clock().tick(1600);
  64. expect(spy).toHaveBeenCalled();
  65. expect(spy.calls.count()).toBe(1);
  66. });
  67. it('should trigger the long tap event even when there is a small move of the touch point',function() {
  68. plot = $.plot(placeholder, [[]], options);
  69. var eventHolder = plot.getEventHolder(),
  70. spy = jasmine.createSpy('long tap handler'),
  71. initialCoords = [{x: 10, y: 20}],
  72. finalCoords = [{x: 11, y: 21}];
  73. eventHolder.addEventListener('longtap', spy);
  74. simulate.sendTouchEvents(initialCoords, eventHolder, 'touchstart');
  75. simulate.sendTouchEvents(finalCoords, eventHolder, 'touchmove');
  76. jasmine.clock().tick(1600);
  77. expect(spy).toHaveBeenCalled();
  78. expect(spy.calls.count()).toBe(1);
  79. });
  80. it('should not trigger the long tap event when there is a large move of the touch point',function() {
  81. plot = $.plot(placeholder, [[]], options);
  82. var eventHolder = plot.getEventHolder(),
  83. spy = jasmine.createSpy('long tap handler'),
  84. initialCoords = [{x: 10, y: 20}],
  85. finalCoords = [{x: 100, y: 200}];
  86. eventHolder.addEventListener('longtap', spy);
  87. simulate.sendTouchEvents(initialCoords, eventHolder, 'touchstart');
  88. simulate.sendTouchEvents(finalCoords, eventHolder, 'touchmove');
  89. jasmine.clock().tick(1600);
  90. expect(spy).not.toHaveBeenCalled();
  91. });
  92. it('should not trigger the long tap event when the touch ends too soon',function() {
  93. plot = $.plot(placeholder, [[]], options);
  94. var eventHolder = plot.getEventHolder(),
  95. spy = jasmine.createSpy('long tap handler'),
  96. coords = [{x: 10, y: 20}];
  97. eventHolder.addEventListener('longtap', spy);
  98. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  99. jasmine.clock().tick(1400);
  100. simulate.sendTouchEvents(coords, eventHolder, 'touchend');
  101. jasmine.clock().tick(200);
  102. expect(spy).not.toHaveBeenCalled();
  103. });
  104. it('should not trigger the long tap event when the plot is replaced', function() {
  105. plot = $.plot(placeholder, [[]], options);
  106. var eventHolder = plot.getEventHolder(),
  107. spy = jasmine.createSpy('long tap handler'),
  108. coords = [{x: 10, y: 20}];
  109. eventHolder.addEventListener('longtap', spy);
  110. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  111. plot = $.plot(placeholder, [[]], options);
  112. jasmine.clock().tick(1600);
  113. expect(spy).not.toHaveBeenCalled();
  114. });
  115. it('should trigger multiple long tap events',function() {
  116. plot = $.plot(placeholder, [[]], options);
  117. var eventHolder = plot.getEventHolder(),
  118. spy = jasmine.createSpy('long tap handler'),
  119. coords = [{x: 10, y: 20}];
  120. eventHolder.addEventListener('longtap', spy);
  121. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  122. jasmine.clock().tick(1600);
  123. simulate.sendTouchEvents(coords, eventHolder, 'touchend');
  124. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  125. jasmine.clock().tick(1600);
  126. simulate.sendTouchEvents(coords, eventHolder, 'touchend');
  127. expect(spy).toHaveBeenCalled();
  128. expect(spy.calls.count()).toBe(2);
  129. });
  130. });
  131. describe('pinch', function() {
  132. beforeEach(function() {
  133. jasmine.clock().install().mockDate();
  134. });
  135. afterEach(function() {
  136. jasmine.clock().uninstall();
  137. });
  138. it('should be able to trigger pinchstart event',function() {
  139. plot = $.plot(placeholder, [[]], options);
  140. var eventHolder = plot.getEventHolder(),
  141. spy = jasmine.createSpy('pinch handler'),
  142. coords = [{x: 10, y: 20}, {x: 15, y: 20}];
  143. eventHolder.addEventListener('pinchstart', spy);
  144. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  145. expect(spy).toHaveBeenCalled();
  146. expect(spy.calls.count()).toBe(1);
  147. });
  148. it('should not trigger pinch event for plot not active',function() {
  149. plot = $.plot(placeholder, [
  150. [
  151. [0, 0],
  152. [10, 10]
  153. ]
  154. ], {
  155. xaxes: [{ autoScale: 'exact' }],
  156. yaxes: [{ autoScale: 'exact' }],
  157. zoom: { interactive: true, active: false, amount: 10 },
  158. pan: { interactive: true, active: false, frameRate: -1 }
  159. });
  160. var eventHolder = plot.getEventHolder(),
  161. spy = jasmine.createSpy('pinch handler'),
  162. coords = [{x: 10, y: 20}, {x: 15, y: 20}];
  163. eventHolder.addEventListener('pinchstart', spy);
  164. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  165. expect(spy).not.toHaveBeenCalled();
  166. expect(spy.calls.count()).toBe(0);
  167. });
  168. it('should not trigger pinch event for only one touch',function() {
  169. plot = $.plot(placeholder, [[]], options);
  170. var eventHolder = plot.getEventHolder(),
  171. spy = jasmine.createSpy('pinch handler'),
  172. coords = [{x: 10, y: 20}];
  173. eventHolder.addEventListener('pinchstart', spy);
  174. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  175. expect(spy).not.toHaveBeenCalled();
  176. expect(spy.calls.count()).toBe(0);
  177. });
  178. it('should not trigger pinch event for touch outside plot',function() {
  179. plot = $.plot(placeholder, [[]], options);
  180. var eventHolder = plot.getEventHolder(),
  181. mockEventHolder = {},
  182. spy = jasmine.createSpy('pinch handler'),
  183. coords = [{x: 10, y: 20}, {x: 15, y: 20}];
  184. eventHolder.addEventListener('pinchstart', spy);
  185. mockEventHolder.dispatchEvent = function() {};
  186. simulate.sendTouchEvents(coords, mockEventHolder, 'touchstart');
  187. expect(spy).not.toHaveBeenCalled();
  188. expect(spy.calls.count()).toBe(0);
  189. });
  190. it('allows default propagation for three touches',function() {
  191. plot = $.plot(placeholder, [], options);
  192. var eventHolder = plot.getEventHolder(),
  193. touchCoords = [{pageX: 10, pageY: 20}, {pageX: 15, pageY: 20}, {pageX: 20, pageY: 25}],
  194. touchStartEvent = new CustomEvent('touchstart'),
  195. touchMoveEvent = new CustomEvent('touchmove');
  196. touchStartEvent.touches = touchCoords;
  197. touchStartEvent.preventDefault = jasmine.createSpy();
  198. touchMoveEvent.touches = touchCoords;
  199. touchMoveEvent.preventDefault = jasmine.createSpy();
  200. eventHolder.dispatchEvent(touchStartEvent);
  201. eventHolder.dispatchEvent(touchMoveEvent);
  202. expect(touchStartEvent.preventDefault).not.toHaveBeenCalled();
  203. expect(touchMoveEvent.preventDefault).not.toHaveBeenCalled();
  204. });
  205. });
  206. describe('pan', function() {
  207. beforeEach(function() {
  208. jasmine.clock().install().mockDate();
  209. });
  210. afterEach(function() {
  211. jasmine.clock().uninstall();
  212. });
  213. it('should be able to trigger pan event',function() {
  214. plot = $.plot(placeholder, [[]], options);
  215. var eventHolder = plot.getEventHolder(),
  216. spy = jasmine.createSpy('pan handler'),
  217. coords = [{x: 10, y: 20}];
  218. eventHolder.addEventListener('pan', spy);
  219. simulate.sendTouchEvents(coords, eventHolder, 'pan');
  220. expect(spy).toHaveBeenCalled();
  221. expect(spy.calls.count()).toBe(1);
  222. });
  223. it('should not trigger pan event for plot not active',function() {
  224. plot = $.plot(placeholder, [
  225. [
  226. [0, 0],
  227. [10, 10]
  228. ]
  229. ], {
  230. xaxes: [{ autoScale: 'exact' }],
  231. yaxes: [{ autoScale: 'exact' }],
  232. zoom: { interactive: true, active: false, amount: 10 },
  233. pan: { interactive: true, active: false, frameRate: -1 }
  234. });
  235. var eventHolder = plot.getEventHolder(),
  236. spy = jasmine.createSpy('pan handler'),
  237. coords = [{x: 10, y: 20}];
  238. eventHolder.addEventListener('pan', spy);
  239. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  240. expect(spy).not.toHaveBeenCalled();
  241. expect(spy.calls.count()).toBe(0);
  242. });
  243. it('should not trigger pan event for touch outside plot',function() {
  244. plot = $.plot(placeholder, [[]], options);
  245. var eventHolder = plot.getEventHolder(),
  246. mockEventHolder = {},
  247. spy = jasmine.createSpy('pan handler'),
  248. coords = [{x: 10, y: 20}];
  249. eventHolder.addEventListener('panstart', spy);
  250. mockEventHolder.dispatchEvent = function() {};
  251. simulate.sendTouchEvents(coords, mockEventHolder, 'touchstart');
  252. expect(spy).not.toHaveBeenCalled();
  253. expect(spy.calls.count()).toBe(0);
  254. });
  255. });
  256. describe('doubletap', function() {
  257. beforeEach(function() {
  258. jasmine.clock().install().mockDate();
  259. });
  260. afterEach(function() {
  261. jasmine.clock().uninstall();
  262. });
  263. it('should trigger the double tap event', function() {
  264. plot = $.plot(placeholder, [[]], options);
  265. var eventHolder = plot.getEventHolder(),
  266. spy = jasmine.createSpy('double tap handler'),
  267. coords = [{x: 10, y: 20}];
  268. eventHolder.addEventListener('doubletap', spy);
  269. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  270. jasmine.clock().tick(200);
  271. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  272. expect(spy).toHaveBeenCalled();
  273. expect(spy.calls.count()).toBe(1);
  274. });
  275. it('should trigger the double tap event even when there is a different touch point', function() {
  276. plot = $.plot(placeholder, [[]], options);
  277. var eventHolder = plot.getEventHolder(),
  278. spy = jasmine.createSpy('double tap handler'),
  279. initialCoords = [{x: 10, y: 20}],
  280. finalCoords = [{x: 11, y: 21}];
  281. eventHolder.addEventListener('doubletap', spy);
  282. simulate.sendTouchEvents(initialCoords, eventHolder, 'touchstart');
  283. jasmine.clock().tick(300);
  284. simulate.sendTouchEvents(finalCoords, eventHolder, 'touchstart');
  285. expect(spy).toHaveBeenCalled();
  286. expect(spy.calls.count()).toBe(1);
  287. });
  288. it('should not trigger the double tap event for a big interval between taps', function() {
  289. plot = $.plot(placeholder, [[]], options);
  290. var eventHolder = plot.getEventHolder(),
  291. spy = jasmine.createSpy('double tap handler'),
  292. coords = [{x: 10, y: 20}];
  293. eventHolder.addEventListener('doubletap', spy);
  294. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  295. jasmine.clock().tick(1000);
  296. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  297. expect(spy).not.toHaveBeenCalled();
  298. expect(spy.calls.count()).toBe(0);
  299. });
  300. it('should not trigger the double tap event for one of the touches outside plot area', function() {
  301. plot = $.plot(placeholder, [[]], options);
  302. var eventHolder = plot.getEventHolder(),
  303. mockEventHolder = {},
  304. spy = jasmine.createSpy('double tap handler'),
  305. initialCoords = [{x: 10, y: 20}],
  306. finalCoords = [{x: 100, y: 200}];
  307. mockEventHolder.dispatchEvent = function() {};
  308. eventHolder.addEventListener('doubletap', spy);
  309. simulate.sendTouchEvents(initialCoords, eventHolder, 'touchstart');
  310. jasmine.clock().tick(100);
  311. simulate.sendTouchEvents(finalCoords, mockEventHolder, 'touchmove');
  312. expect(spy).not.toHaveBeenCalled();
  313. });
  314. });
  315. describe('tap', function() {
  316. beforeEach(function() {
  317. jasmine.clock().install().mockDate();
  318. });
  319. afterEach(function() {
  320. jasmine.clock().uninstall();
  321. });
  322. it('should trigger the tap event', function() {
  323. plot = $.plot(placeholder, [[]], options);
  324. var eventHolder = plot.getEventHolder(),
  325. spy = jasmine.createSpy('tap handler'),
  326. coords = [{x: 10, y: 20}];
  327. eventHolder.addEventListener('tap', spy);
  328. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  329. jasmine.clock().tick(50);
  330. simulate.sendTouchEvents(coords, eventHolder, 'touchend');
  331. expect(spy).toHaveBeenCalled();
  332. expect(spy.calls.count()).toBe(1);
  333. });
  334. it('should trigger the tap event even when there is a different touch point', function() {
  335. plot = $.plot(placeholder, [[]], options);
  336. var eventHolder = plot.getEventHolder(),
  337. spy = jasmine.createSpy('tap handler'),
  338. initialCoords = [{x: 10, y: 20}],
  339. finalCoords = [{x: 11, y: 21}];
  340. eventHolder.addEventListener('tap', spy);
  341. simulate.sendTouchEvents(initialCoords, eventHolder, 'touchstart');
  342. jasmine.clock().tick(50);
  343. simulate.sendTouchEvents(finalCoords, eventHolder, 'touchend');
  344. expect(spy).toHaveBeenCalled();
  345. expect(spy.calls.count()).toBe(1);
  346. });
  347. it('should not trigger the tap event for a big interval between taps', function() {
  348. plot = $.plot(placeholder, [[]], options);
  349. var eventHolder = plot.getEventHolder(),
  350. spy = jasmine.createSpy('tap handler'),
  351. coords = [{x: 10, y: 20}];
  352. eventHolder.addEventListener('tap', spy);
  353. simulate.sendTouchEvents(coords, eventHolder, 'touchstart');
  354. jasmine.clock().tick(200);
  355. simulate.sendTouchEvents(coords, eventHolder, 'touchend');
  356. expect(spy).not.toHaveBeenCalled();
  357. expect(spy.calls.count()).toBe(0);
  358. });
  359. it('should not trigger the tap event for a big interval between taps', function() {
  360. plot = $.plot(placeholder, [[]], options);
  361. var eventHolder = plot.getEventHolder(),
  362. spy = jasmine.createSpy('tap handler'),
  363. initalCoords = [{x: 10, y: 20}],
  364. moveCoords = [{x: 30, y: 60}];;
  365. eventHolder.addEventListener('tap', spy);
  366. simulate.sendTouchEvents(initalCoords, eventHolder, 'touchstart');
  367. simulate.sendTouchEvents(moveCoords, eventHolder, 'touchmove');
  368. jasmine.clock().tick(30);
  369. simulate.sendTouchEvents(moveCoords, eventHolder, 'touchend');
  370. expect(spy).not.toHaveBeenCalled();
  371. expect(spy.calls.count()).toBe(0);
  372. });
  373. });
  374. });