EventsSpec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. describe("Event Tests", function() {
  2. var testSlider, flag, mouse;
  3. var sliderElem;
  4. beforeEach(function() {
  5. flag = false;
  6. mouse = document.createEvent('MouseEvents');
  7. });
  8. describe("JQuery version", function() {
  9. beforeEach(function() {
  10. testSlider = $("#testSlider2").slider({
  11. value: 1
  12. });
  13. sliderElem = testSlider.slider('getElement');
  14. });
  15. afterEach(function() {
  16. if(testSlider) {
  17. testSlider.slider('destroy');
  18. testSlider = null;
  19. }
  20. sliderElem = null;
  21. });
  22. describe("Mouse Events", function() {
  23. var spy;
  24. beforeEach(function() {
  25. spy = jasmine.createSpy('spy');
  26. });
  27. it("'slideStart' event is triggered properly and can be binded to", function(done) {
  28. testSlider.on('slideStart', spy);
  29. testSlider.data('slider')._mousedown(mouse);
  30. setTimeout(function() {
  31. expect(spy).toHaveBeenCalled();
  32. done();
  33. });
  34. });
  35. it("'slide' event is triggered properly and can be binded to", function(done) {
  36. testSlider.on('slide', spy);
  37. testSlider.data('slider')._mousemove(mouse);
  38. setTimeout(function() {
  39. expect(spy).toHaveBeenCalled();
  40. done();
  41. });
  42. });
  43. it("'slide' event sets the right value on the input", function(done) {
  44. testSlider.on('slide', function() {
  45. expect(isNaN(testSlider.val())).not.toBeTruthy();
  46. done();
  47. });
  48. testSlider.data('slider')._mousemove(mouse);
  49. });
  50. it("'slide' event value and input value properties are synchronous", function() {
  51. testSlider.on('slide', function(e) {
  52. expect(e.value.toString()).toEqual(this.value);
  53. });
  54. testSlider.slider("setValue", 3, true, false);
  55. });
  56. it("'change' event value and input value properties are synchronous", function() {
  57. testSlider.on('change', function(e) {
  58. expect(e.value.newValue.toString()).toEqual(testSlider.val());
  59. });
  60. testSlider.slider("setValue", 3, false, true);
  61. });
  62. it("'slideStop' event is triggered properly and can be binded to", function(done) {
  63. testSlider.on('slideStop', spy);
  64. testSlider.data('slider')._mouseup(mouse);
  65. setTimeout(function() {
  66. expect(spy).toHaveBeenCalled();
  67. done();
  68. });
  69. });
  70. it("slider should not have duplicate events after calling 'refresh'", function(done) {
  71. var numTimesFired = 0;
  72. var obj = {
  73. addOne: function() {
  74. numTimesFired++;
  75. }
  76. };
  77. spyOn(obj, 'addOne').and.callThrough();
  78. testSlider.on('slideStop', obj.addOne);
  79. testSlider.slider('refresh');
  80. testSlider.data('slider')._mouseup(mouse);
  81. setTimeout(function() {
  82. expect(numTimesFired).toBe(1);
  83. expect(obj.addOne.calls.count()).toBe(1);
  84. done();
  85. });
  86. });
  87. describe("Disabled Slider Event Tests", function() {
  88. var spy;
  89. beforeEach(function() {
  90. testSlider.slider('disable');
  91. spy = jasmine.createSpy('spy');
  92. });
  93. it("should not trigger 'slideStart' event when disabled", function(done) {
  94. testSlider.on('slideStart', spy);
  95. testSlider.data('slider')._mousedown(mouse);
  96. window.setTimeout(function() {
  97. expect(spy).not.toHaveBeenCalled();
  98. done();
  99. });
  100. });
  101. it("should not trigger 'slide' event when disabled", function(done) {
  102. testSlider.on('slide', spy);
  103. testSlider.data('slider')._mousemove(mouse);
  104. window.setTimeout(function() {
  105. expect(spy).not.toHaveBeenCalled();
  106. done();
  107. });
  108. });
  109. it("should not trigger 'slideStop' event when disabled", function(done) {
  110. testSlider.on('slideStop', spy);
  111. testSlider.data('slider')._mouseup(mouse);
  112. window.setTimeout(function() {
  113. expect(spy).not.toHaveBeenCalled();
  114. done();
  115. });
  116. });
  117. });
  118. });
  119. describe("Touch Events", function() {
  120. var touch;
  121. var spy;
  122. var coords;
  123. var touchStart;
  124. var touchMove;
  125. var touchEnd;
  126. /*
  127. list can be either [[x, y], [x, y]] or [x, y]
  128. */
  129. function createTouchList(target, list) {
  130. if (Array.isArray(list) && list[0] && !Array.isArray(list[0])) {
  131. list = [list];
  132. }
  133. list = list.map(function (entry, index) {
  134. var x = entry[0], y = entry[1], id = entry[2] ? entry[2] : index + 1;
  135. return createTouch(x, y, target, id);
  136. });
  137. return document.createTouchList.apply(document, list);
  138. }
  139. function createTouch(x, y, target, id) {
  140. return document.createTouch(window, target,
  141. id || 1, //identifier
  142. x, //pageX / clientX
  143. y, //pageY / clientY
  144. x, //screenX
  145. y //screenY
  146. );
  147. }
  148. function initTouchEvent(touchEvent, type, touches) {
  149. var touch1 = touches[0];
  150. return touchEvent.initTouchEvent(
  151. touches, //touches
  152. touches, //targetTouches
  153. touches, //changedTouches
  154. type, //type
  155. window, //view
  156. touch1.screenX, //screenX
  157. touch1.screenY, //screenY
  158. touch1.clientX, //clientX
  159. touch1.clientY, //clientY
  160. false, //ctrlKey
  161. false, //altKey
  162. false, //shiftKey
  163. false //metaKey
  164. );
  165. }
  166. function createTouchEvent(elem, type, touches) {
  167. var touchEvent = document.createEvent('TouchEvent');
  168. if (Array.isArray(touches)) {
  169. touches = createTouchList(elem, touches);
  170. }
  171. initTouchEvent(touchEvent, type, touches);
  172. return touchEvent;
  173. }
  174. function calcTouchEventCoords(element) {
  175. var elementBB = element.getBoundingClientRect();
  176. return {
  177. clientX: elementBB.left,
  178. clientY: elementBB.top
  179. };
  180. }
  181. beforeEach(function() {
  182. touch = document.createEvent('Event');
  183. var dummyTouchEvent = document.createEvent('MouseEvents');
  184. touch.touches = [dummyTouchEvent];
  185. window.ontouchstart = true;
  186. spy = jasmine.createSpy('spy');
  187. coords = calcTouchEventCoords(sliderElem);
  188. touchStart = createTouchEvent(sliderElem, 'touchstart', [coords.clientX, coords.clientY]);
  189. touchMove = createTouchEvent(sliderElem, 'touchmove', [coords.clientX, coords.clientY]);
  190. touchEnd = createTouchEvent(sliderElem, 'touchend', [[0, 0]]);
  191. });
  192. afterEach(function() {
  193. window.ontouchstart = null;
  194. });
  195. it("'slideStart' event is triggered properly and can be binded to", function(done) {
  196. testSlider.on('slideStart', spy);
  197. window.setTimeout(function() {
  198. expect(spy).toHaveBeenCalled();
  199. done();
  200. });
  201. sliderElem.dispatchEvent(touchStart);
  202. sliderElem.dispatchEvent(touchEnd);
  203. });
  204. it("'slide' event is triggered properly and can be binded to", function(done) {
  205. testSlider.on('slide', spy);
  206. window.setTimeout(function() {
  207. expect(spy).toHaveBeenCalled();
  208. done();
  209. });
  210. // Expect to fail if ONLY dispatching 'touchmove' because `preventDefault()` will prevent
  211. // the browser from sending the corresponding 'mousemove'.
  212. // The 'mousedown' event must happen first via 'touchstart'.
  213. sliderElem.dispatchEvent(touchStart);
  214. sliderElem.dispatchEvent(touchMove);
  215. // Always call 'touchend' to remove any touch event listeners on the document.
  216. sliderElem.dispatchEvent(touchEnd);
  217. });
  218. it("'slide' event sets the right value on the input", function(done) {
  219. testSlider.on('slide', function() {
  220. expect(isNaN(testSlider.val())).not.toBeTruthy();
  221. done();
  222. });
  223. sliderElem.dispatchEvent(touchStart);
  224. sliderElem.dispatchEvent(touchMove);
  225. sliderElem.dispatchEvent(touchEnd);
  226. });
  227. it("'slide' event value and input value properties are synchronous", function() {
  228. testSlider.on('slide', function(e) {
  229. expect(e.value.toString()).toEqual(testSlider.val());
  230. });
  231. testSlider.slider("setValue", 3, true, false);
  232. });
  233. it("'change' event value and input value properties are synchronous", function() {
  234. testSlider.on('change', function(e) {
  235. expect(e.value.newValue.toString()).toEqual(testSlider.val());
  236. });
  237. testSlider.slider("setValue", 3, false, true);
  238. });
  239. it("'slideStop' event is triggered properly and can be binded to", function(done) {
  240. testSlider.on('slideStop', spy);
  241. window.setTimeout(function() {
  242. expect(spy).toHaveBeenCalled();
  243. done();
  244. });
  245. sliderElem.dispatchEvent(touchStart);
  246. sliderElem.dispatchEvent(touchMove);
  247. sliderElem.dispatchEvent(touchEnd);
  248. });
  249. it("slider should not have duplicate events after calling 'refresh'", function(done) {
  250. // FIXME: Should set `flag` to 0 and make sure spy is called only once
  251. testSlider.on('slideStop', spy);
  252. window.setTimeout(function() {
  253. expect(spy).toHaveBeenCalled();
  254. done();
  255. });
  256. sliderElem.dispatchEvent(touchStart);
  257. sliderElem.dispatchEvent(touchMove);
  258. sliderElem.dispatchEvent(touchEnd);
  259. testSlider.slider('refresh');
  260. });
  261. it("slider should not bind multiple touchstart events after calling 'refresh'", function(done) {
  262. flag = 0;
  263. var obj = {
  264. addOne: function() {
  265. flag++;
  266. }
  267. };
  268. spyOn(obj, 'addOne').and.callThrough();
  269. touch.initEvent("touchstart", true, true);
  270. testSlider.on('slideStart', obj.addOne);
  271. var handleElem = $('#testSlider2').prev('div.slider').find('.slider-handle:first').get(0);
  272. handleElem.dispatchEvent(touchStart);
  273. handleElem.dispatchEvent(touchMove);
  274. handleElem.dispatchEvent(touchEnd);
  275. testSlider.slider('refresh');
  276. window.setTimeout(function() {
  277. expect(flag).toBe(1);
  278. expect(obj.addOne.calls.count()).toBe(1);
  279. done();
  280. });
  281. });
  282. describe("Disabled Slider Event Tests", function() {
  283. var spy;
  284. beforeEach(function() {
  285. testSlider.slider('disable');
  286. spy = jasmine.createSpy('spy');
  287. });
  288. it("should not trigger 'slideStart' event when disabled", function(done) {
  289. touch.initEvent("touchstart");
  290. testSlider.on('slideStart', spy);
  291. testSlider.data('slider')._mousedown(touch);
  292. window.setTimeout(function() {
  293. expect(spy).not.toHaveBeenCalled();
  294. done();
  295. });
  296. });
  297. it("should not trigger 'slide' event when disabled", function(done) {
  298. touch.initEvent("touchmove");
  299. testSlider.on('slide', spy);
  300. testSlider.data('slider')._mousemove(touch);
  301. window.setTimeout(function() {
  302. expect(spy).not.toHaveBeenCalled();
  303. done();
  304. });
  305. });
  306. it("should not trigger 'slideStop' event when disabled", function(done) {
  307. touch.initEvent("touchend");
  308. testSlider.on('slideStop', spy);
  309. testSlider.data('slider')._mouseup(mouse);
  310. window.setTimeout(function() {
  311. expect(spy).not.toHaveBeenCalled();
  312. done();
  313. });
  314. });
  315. });
  316. });
  317. describe("Enabled/Disabled tests", function() {
  318. var spy;
  319. beforeEach(function() {
  320. spy = jasmine.createSpy('spy');
  321. });
  322. it("'slideDisabled' event is triggered properly and can be binded to", function(done) {
  323. testSlider.on('slideDisabled', spy);
  324. testSlider.slider('disable');
  325. window.setTimeout(function() {
  326. expect(spy).toHaveBeenCalled();
  327. done();
  328. });
  329. });
  330. it("'slideDisabled' event is triggered properly and can be binded to", function(done) {
  331. testSlider.on('slideEnabled', spy);
  332. testSlider.slider('disable');
  333. testSlider.slider('enable');
  334. window.setTimeout(function() {
  335. expect(spy).toHaveBeenCalled();
  336. done();
  337. });
  338. });
  339. it("'change' event is triggered properly and can be binded to", function(done) {
  340. testSlider.on('change', spy);
  341. testSlider.slider("setValue", 3, false, true);
  342. window.setTimeout(function() {
  343. expect(spy).toHaveBeenCalled();
  344. done();
  345. });
  346. });
  347. });
  348. }); // End of JQuery version tests
  349. describe("CommonJS version", function() {
  350. describe("Event repetition tests", function() {
  351. var testSlider, numTimesFired;
  352. var testObj;
  353. beforeEach(function() {
  354. testSlider = new Slider("#testSlider2");
  355. numTimesFired = 0;
  356. testObj = {
  357. addOne: function() {
  358. numTimesFired++;
  359. }
  360. };
  361. });
  362. afterEach(function() {
  363. testSlider.destroy();
  364. });
  365. it("'slide' event is triggered only once per slide action", function(done) {
  366. spyOn(testObj, 'addOne').and.callThrough();
  367. testSlider.on('slide', testObj.addOne);
  368. testSlider._mousemove(mouse);
  369. setTimeout(function() {
  370. expect(numTimesFired).toBe(1);
  371. expect(testObj.addOne.calls.count()).toBe(1);
  372. done();
  373. });
  374. });
  375. it("'slideStart' event is triggered only once per slide action", function(done) {
  376. spyOn(testObj, 'addOne').and.callThrough();
  377. testSlider.on('slideStart', testObj.addOne);
  378. testSlider._mousedown(mouse);
  379. setTimeout(function() {
  380. expect(numTimesFired).toBe(1);
  381. expect(testObj.addOne.calls.count()).toBe(1);
  382. done();
  383. });
  384. });
  385. it("'slideStop' event is triggered only once per slide action", function(done) {
  386. spyOn(testObj, 'addOne').and.callThrough();
  387. testSlider.on('slideStop', testObj.addOne);
  388. testSlider._mouseup(mouse);
  389. setTimeout(function() {
  390. expect(numTimesFired).toBe(1);
  391. expect(testObj.addOne.calls.count()).toBe(1);
  392. done();
  393. });
  394. });
  395. it("'change' event is triggered only once per value change action", function(done) {
  396. spyOn(testObj, 'addOne').and.callThrough();
  397. testSlider.on('change', testObj.addOne);
  398. testSlider.setValue(3, false, true);
  399. setTimeout(function() {
  400. expect(numTimesFired).toBe(1);
  401. expect(testObj.addOne.calls.count()).toBe(1);
  402. done();
  403. });
  404. });
  405. });
  406. }); // End of common JS tests
  407. }); // End of spec