TooltipMouseOverOptionSpec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. describe("'ticks_tooltip' Option tests", function() {
  2. var testSlider;
  3. var mouseEventArguments;
  4. beforeEach(function() {
  5. // Set up default set of mouse event arguments
  6. mouseEventArguments = [
  7. 'mousemove', // type
  8. true, // canBubble
  9. true, // cancelable
  10. document, // view,
  11. 0, // detail
  12. 0, // screenX
  13. 0, // screenY
  14. undefined, // clientX
  15. undefined, // clientY,
  16. false, // ctrlKey
  17. false, // altKey
  18. false, // shiftKey
  19. false, // metaKey,
  20. 0, // button
  21. null // relatedTarget
  22. ];
  23. });
  24. describe("ticks_tooltip states", function() {
  25. it("should have the tooltip above the last hovered over element", function() {
  26. testSlider = new Slider(document.getElementById("testSlider1"), {
  27. ticks: [0, 1, 2, 3, 4, 5, 6],
  28. ticks_positions: [0, 19, 29, 39, 49, 95, 100],
  29. step: 1,
  30. value: 4,
  31. ticks_tooltip: true,
  32. orientation: 'horizontal'
  33. });
  34. mouseEventArguments[8] = testSlider.sliderElem.offsetTop; // clientY
  35. var mouse49 = document.createEvent('MouseEvents');
  36. mouseEventArguments[7] = testSlider.ticks[4].offsetLeft + testSlider.sliderElem.offsetLeft; // clientX
  37. mouse49.initMouseEvent.apply(mouse49, mouseEventArguments);
  38. var mouse95 = document.createEvent('MouseEvents');
  39. mouseEventArguments[7] = testSlider.ticks[5].offsetLeft + testSlider.sliderElem.offsetLeft; // clientX
  40. mouse95.initMouseEvent.apply(mouse95, mouseEventArguments);
  41. var mouse100 = document.createEvent('MouseEvents');
  42. mouseEventArguments[7] = testSlider.ticks[6].offsetLeft + testSlider.sliderElem.offsetLeft; // clientX
  43. mouse100.initMouseEvent.apply(mouse100, mouseEventArguments);
  44. var mouseStart = document.createEvent('MouseEvents');
  45. mouseEventArguments[7] = testSlider.ticks[0].offsetLeft + testSlider.sliderElem.offsetLeft; // clientX
  46. mouseStart.initMouseEvent.apply(mouseStart, mouseEventArguments);
  47. //Simulate random movements
  48. testSlider.mousedown(mouse49);
  49. testSlider.mousemove(mouse95);
  50. // FIXME: Use 'mouseup' event type
  51. testSlider.mouseup(mouse95);
  52. testSlider.mousedown(mouse49);
  53. testSlider.mousemove(mouse100);
  54. testSlider.mousemove(mouse95);
  55. testSlider.mousemove(mouse95);
  56. testSlider.mousemove(mouseStart);
  57. expect(testSlider.tooltip.style.left).toBe("0%");
  58. });
  59. });
  60. describe("Always show the tooltip", function() {
  61. it("Should always display the tooltip after hovering over a tick", function(done) {
  62. testSlider = new Slider(document.getElementById("testSlider1"), {
  63. id: 'mySlider',
  64. min: 0,
  65. max: 10,
  66. step: 1,
  67. value: 1,
  68. ticks: [0, 5, 10],
  69. tooltip: 'always',
  70. ticks_tooltip: true,
  71. orientation: 'horizontal'
  72. });
  73. var bigOffset = 100000;
  74. var isTooltipVisible = $('#mySlider').find('.tooltip.tooltip-main').hasClass('in');
  75. expect(isTooltipVisible).toBe(true);
  76. var mouseenter = document.createEvent('MouseEvent');
  77. mouseEventArguments[0] = 'mouseenter';
  78. mouseEventArguments[7] =
  79. testSlider.ticks[1].offsetLeft + testSlider.sliderElem.offsetLeft; // clientX
  80. mouseenter.initMouseEvent.apply(mouseenter, mouseEventArguments);
  81. var mouseleave = document.createEvent('MouseEvent');
  82. mouseEventArguments[0] = 'mouseleave';
  83. mouseEventArguments[7] = testSlider.sliderElem.offsetLeft + bigOffset;
  84. mouseleave.initMouseEvent.apply(mouseleave, mouseEventArguments);
  85. testSlider.ticks[1].addEventListener('mouseleave', function() {
  86. isTooltipVisible = $('#mySlider').find('.tooltip.tooltip-main').hasClass('in');
  87. expect(isTooltipVisible).toBe(true);
  88. done();
  89. });
  90. testSlider.ticks[1].dispatchEvent(mouseenter);
  91. testSlider.ticks[1].dispatchEvent(mouseleave);
  92. });
  93. });
  94. afterEach(function() {
  95. if(testSlider) {
  96. if(testSlider instanceof Slider) { testSlider.destroy(); }
  97. testSlider = null;
  98. }
  99. });
  100. });
  101. /**
  102. * The mouse navigation tests are based on the following slider properties:
  103. *
  104. * initial value: 3 or [3, 7]
  105. * ticks: [0, 3, 5, 7, 10]
  106. * step: 1
  107. *
  108. * When the `ticks_tooltip` option is set to `true`, hovering over the ticks or handles
  109. * should show the tooltip above it with the value of the tick/handle.
  110. *
  111. * The test logic for sliders:
  112. * 1. Hover over the 1st tick
  113. * 2. Check if the tooltip is positioned correctly (left, top, right)
  114. * 3. Check if the tooltip should be showing
  115. * 4. Check if the tooltip contains the correct value
  116. * 5. Check if the slider value(s) haven't changed
  117. *
  118. */
  119. describe("`ticks_tooltip: true` mouse navigation test cases", function() {
  120. var initialValue = 3;
  121. var initialRangeValues = [3, 7];
  122. var tickValues = [0, 3, 5, 7, 10];
  123. var stepValue = 1;
  124. var orientations = ['horizontal', 'vertical'];
  125. var reversed = [false, true];
  126. var sliderTypes = ['single', 'range'];
  127. var rtl = [false, true];
  128. var testCases = [];
  129. var mouseEventArguments;
  130. function calcMouseEventCoords(element) {
  131. var elementBB = element.getBoundingClientRect();
  132. return {
  133. clientX: elementBB.left,
  134. clientY: elementBB.top
  135. };
  136. }
  137. function createMouseEvent(type, clientX, clientY) {
  138. var mouseEvent = document.createEvent('MouseEvent');
  139. mouseEventArguments[0] = type;
  140. mouseEventArguments[7] = clientX;
  141. mouseEventArguments[8] = clientY;
  142. mouseEvent.initMouseEvent.apply(mouseEvent, mouseEventArguments);
  143. return mouseEvent;
  144. }
  145. beforeEach(function() {
  146. // Set up default set of mouse event arguments
  147. mouseEventArguments = [
  148. 'mousemove', // type
  149. true, // canBubble
  150. true, // cancelable
  151. document, // view,
  152. 0, // detail
  153. 0, // screenX
  154. 0, // screenY
  155. undefined, // clientX
  156. undefined, // clientY,
  157. false, // ctrlKey
  158. false, // altKey
  159. false, // shiftKey
  160. false, // metaKey,
  161. 0, // button
  162. null // relatedTarget
  163. ];
  164. });
  165. sliderTypes.forEach(function(sliderType) {
  166. orientations.forEach(function(orientation) {
  167. rtl.forEach(function(isRTL) {
  168. reversed.forEach(function(isReversed) {
  169. var isHorizontal = orientation === 'horizontal';
  170. var isVertical = orientation === 'vertical';
  171. var isRange = sliderType === 'range';
  172. var whichStyle;
  173. if (isHorizontal) {
  174. if (isRTL) {
  175. whichStyle = 'right';
  176. }
  177. else {
  178. whichStyle = 'left';
  179. }
  180. }
  181. else if (isVertical) {
  182. whichStyle = 'top';
  183. }
  184. testCases.push({
  185. value: isRange ? initialRangeValues : initialValue,
  186. step: stepValue,
  187. range: isRange,
  188. orientation: orientation,
  189. reversed: isReversed,
  190. rtl: 'auto',
  191. isRTL: isRTL,
  192. inputId: isRTL ? 'rtlSlider' : 'testSlider1',
  193. expectedValue: isRange ? initialRangeValues : initialValue,
  194. stylePos: whichStyle
  195. });
  196. });
  197. });
  198. });
  199. });
  200. testCases.forEach(function(testCase) {
  201. describe("range=" + testCase.range + ", orientation=" + testCase.orientation +
  202. ", rtl=" + testCase.isRTL + ", reversed=" + testCase.reversed, function() {
  203. var $testSlider;
  204. var sliderElem;
  205. var $handle1;
  206. var $handle2;
  207. var $ticks;
  208. var sliderId;
  209. var sliderOptions;
  210. var $tooltip;
  211. var $tooltipInner;
  212. var lastTickIndex;
  213. var mouseEventType = 'mouseenter';
  214. beforeEach(function() {
  215. sliderId = testCase.range ? 'myRangeSlider' : 'mySlider';
  216. sliderOptions = {
  217. id: sliderId,
  218. step: testCase.step,
  219. orientation: testCase.orientation,
  220. value: testCase.value,
  221. range: testCase.range,
  222. reversed: testCase.reversed,
  223. rtl: 'auto',
  224. ticks: tickValues,
  225. ticks_tooltip: true
  226. };
  227. $testSlider = $('#'+testCase.inputId).slider(sliderOptions);
  228. sliderElem = $('#'+sliderId)[0];
  229. $ticks = $(sliderElem).find('.slider-tick');
  230. $handle1 = $(sliderElem).find('.slider-handle:first');
  231. $handle2 = $(sliderElem).find('.slider-handle:last');
  232. $tooltip = $(sliderElem).find('.tooltip.tooltip-main');
  233. $tooltipInner = $tooltip.find('.tooltip-inner');
  234. lastTickIndex = sliderOptions.ticks.length - 1;
  235. });
  236. afterEach(function() {
  237. // Clean up any associated event listeners
  238. $ticks = null;
  239. $handle1 = null;
  240. $handle2 = null;
  241. if ($testSlider) {
  242. $testSlider.slider('destroy');
  243. $testSlider = null;
  244. }
  245. });
  246. it("Should position the tooltip correctly", function(done) {
  247. $ticks.each(function(index, tickElem) {
  248. var coords = calcMouseEventCoords(tickElem);
  249. var tickCallback = function() {
  250. // Check position
  251. expect($tooltip.css(testCase.stylePos)).toBe($(this).css(testCase.stylePos));
  252. if (index === lastTickIndex) {
  253. done();
  254. }
  255. };
  256. // Set up listener and dispatch event
  257. this.addEventListener(mouseEventType, tickCallback, false);
  258. this.dispatchEvent(createMouseEvent(mouseEventType, coords.clientX, coords.clientY));
  259. });
  260. });
  261. it("Should show the tooltip", function(done) {
  262. $ticks.each(function(index, tickElem) {
  263. var coords = calcMouseEventCoords(tickElem);
  264. var tickCallback = function() {
  265. // Check that tooltip shows
  266. expect($tooltip.hasClass('in')).toBe(true);
  267. if (index === lastTickIndex) {
  268. done();
  269. }
  270. };
  271. this.addEventListener(mouseEventType, tickCallback, false);
  272. this.dispatchEvent(createMouseEvent(mouseEventType, coords.clientX, coords.clientY));
  273. });
  274. });
  275. it("Should not show the tooltip", function(done) {
  276. var bigOffset = 100000;
  277. $ticks.each(function(index, tickElem) {
  278. var coords = calcMouseEventCoords(tickElem);
  279. var tickCallback = function() {
  280. // Check that tooltip shows
  281. expect($tooltip.hasClass('in')).toBe(false);
  282. if (index === lastTickIndex) {
  283. done();
  284. }
  285. };
  286. this.addEventListener('mouseleave', tickCallback, false);
  287. this.dispatchEvent(createMouseEvent('mouseleave', coords.clientX + bigOffset, coords.clientY));
  288. });
  289. });
  290. it("Should contain the correct value for the tooltip", function(done) {
  291. $ticks.each(function(index, tickElem) {
  292. var coords = calcMouseEventCoords(tickElem);
  293. var tickCallback = function() {
  294. // Check value of tooltip
  295. expect($tooltipInner.text()).toBe(''+sliderOptions.ticks[index]);
  296. if (index === lastTickIndex) {
  297. done();
  298. }
  299. };
  300. this.addEventListener(mouseEventType, tickCallback, false);
  301. this.dispatchEvent(createMouseEvent(mouseEventType, coords.clientX, coords.clientY));
  302. });
  303. });
  304. it("Should not modify the value(s) of the slider when displaying the tooltip", function(done) {
  305. $ticks.each(function(index, tickElem) {
  306. var coords = calcMouseEventCoords(tickElem);
  307. var tickCallback = function() {
  308. var value = $testSlider.slider('getValue');
  309. // Check value of slider
  310. expect(value).toEqual(testCase.expectedValue);
  311. if (index === lastTickIndex) {
  312. done();
  313. }
  314. };
  315. this.addEventListener(mouseEventType, tickCallback, false);
  316. this.dispatchEvent(createMouseEvent(mouseEventType, coords.clientX, coords.clientY));
  317. });
  318. });
  319. describe("Test position and values of the tooltip when hovering over the handle(s)", function() {
  320. if (testCase.range) {
  321. it("Should position for the tooltip correctly (range)", function(done) {
  322. var handleElems = [$handle1[0], $handle2[0]];
  323. $.each(handleElems, function(index, handleElem) {
  324. var coords = calcMouseEventCoords(handleElem, testCase.orientation);
  325. var handleCallback = function() {
  326. // Check position
  327. expect($tooltip.css(testCase.stylePos)).toBe($(handleElem).css(testCase.stylePos));
  328. if (index === 1) {
  329. done();
  330. }
  331. };
  332. handleElem.addEventListener(mouseEventType, handleCallback, false);
  333. handleElem.dispatchEvent(createMouseEvent(mouseEventType, coords.clientX, coords.clientY));
  334. });
  335. });
  336. it("Should contain the correct values for the tooltip (range)", function(done) {
  337. var handleElems = [$handle1[0], $handle2[0]];
  338. $.each(handleElems, function(index, handleElem) {
  339. var coords = calcMouseEventCoords(handleElem, testCase.orientation);
  340. var handleCallback = function() {
  341. // Check value of tooltip
  342. expect($tooltipInner.text()).toBe(''+testCase.expectedValue[index]);
  343. if (index === 1) {
  344. done();
  345. }
  346. };
  347. handleElem.addEventListener(mouseEventType, handleCallback, false);
  348. handleElem.dispatchEvent(createMouseEvent(mouseEventType, coords.clientX, coords.clientY));
  349. });
  350. });
  351. }
  352. else {
  353. it("Should position for the tooltip correctly (single)", function(done) {
  354. var handleElem = $handle1[0];
  355. var coords = calcMouseEventCoords(handleElem, testCase.orientation);
  356. var handleCallback = function() {
  357. // Check position
  358. expect($tooltip.css(testCase.stylePos)).toBe($(handleElem).css(testCase.stylePos));
  359. done();
  360. };
  361. handleElem.addEventListener(mouseEventType, handleCallback, false);
  362. handleElem.dispatchEvent(createMouseEvent(mouseEventType, coords.clientX, coords.clientY));
  363. });
  364. it("Should contain the correct value for the tooltip (single)", function(done) {
  365. var handleElem = $handle1[0];
  366. var coords = calcMouseEventCoords(handleElem, testCase.orientation);
  367. var handleCallback = function() {
  368. // Check value of tooltip
  369. expect($tooltipInner.text()).toBe(''+testCase.expectedValue);
  370. done();
  371. };
  372. handleElem.addEventListener(mouseEventType, handleCallback, false);
  373. handleElem.dispatchEvent(createMouseEvent(mouseEventType, coords.clientX, coords.clientY));
  374. });
  375. }
  376. });
  377. });
  378. });
  379. });