123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768 |
- describe("Keyboard Support Tests", function() {
- var testSlider,
- handle1,
- handle2,
- keyboardEvent,
- initialMinVal = 0,
- initialMaxVal = 10,
- initialStepVal = 1,
- initialSliderVal = 5;
- /*
- Before/After setup
- */
- beforeEach(function() {
- // Create keyboard event
- keyboardEvent = document.createEvent("Events");
- keyboardEvent.initEvent("keydown", true, true);
- });
- afterEach(function() {
- if(testSlider) { testSlider.slider('destroy'); }
- keyboardEvent = null;
- keyboardEvent = null;
- });
- /*
- Begin Tests
- */
- describe("Clicking on slider handle automatically gives it focus", function() {
- beforeEach(function() {
- testSlider = $("#testSlider1").slider({
- id: 'testSlider'
- });
- handle1 = $("#testSlider").find(".slider-handle:first");
- });
- it("clicking on handle1 gives focus to handle1", function(done) {
- handle1.focus(function() {
- expect(true).toBeTruthy();
- done();
- });
- handle1.focus();
- });
- });
- describe("When slider handle has TAB focus", function() {
- it("should display it's tooltip if 'tooltip' option is set to 'show'", function() {
- testSlider = $("#testSlider1").slider({
- id: 'testSlider',
- tooltip: 'show'
- });
- handle1 = $("#testSlider").find(".slider-handle:first");
- // Check for no tooltip before focus
- var tooltipIsShown = $("#testSlider").find("div.tooltip").hasClass("in");
- expect(tooltipIsShown).toBeFalsy();
- handle1.focus();
- // Tooltip should be present after focus
- tooltipIsShown = $("#testSlider").find("div.tooltip").hasClass("in");
- expect(tooltipIsShown).toBeTruthy();
- });
- it("should not display it's tooltip if 'tooltip' option is set to 'hide'", function() {
- testSlider = $("#testSlider1").slider({
- id: 'testSlider',
- tooltip: 'hide'
- });
- handle1 = $("#testSlider").find(".slider-handle:first");
- // Check for hidden tooltip before focus
- var tooltipIsHidden = $("#testSlider").children("div.tooltip").hasClass("hide");
- expect(tooltipIsHidden).toBeTruthy();
- handle1.focus();
- // Tooltip should remain hidden after focus
- tooltipIsHidden = $("#testSlider").children("div.tooltip").hasClass("hide");
- expect(tooltipIsHidden).toBeTruthy();
- });
- it("should not affect the tooltip display if 'tooltip' option is set to 'always'", function() {
- testSlider = $("#testSlider1").slider({
- id: 'testSlider',
- tooltip: 'always'
- });
- handle1 = $("#testSlider").find(".slider-handle:first");
- var $tooltip = $("#testSlider").children("div.tooltip");
- // Check for shown tooltip before focus
- var tooltipIsShown = $tooltip.hasClass("in");
- expect(tooltipIsShown).toBeTruthy();
- handle1.focus();
- // Tooltip should remain present after focus
- tooltipIsShown = $tooltip.hasClass("in");
- expect(tooltipIsShown).toBeTruthy();
- });
- });
- describe("For horizontal sliders where its handle has focus", function() {
- beforeEach(function() {
- // Initialize the slider
- testSlider = $("#testSlider1").slider({
- id: 'testSlider',
- orientation: 'horizontal',
- min: initialMinVal,
- max: initialMaxVal,
- step: initialStepVal,
- value: initialSliderVal
- });
- // Focus on handle1
- handle1 = $("#testSlider .min-slider-handle");
- handle1.focus();
- });
- it("moves to the left by the 'step' value when the LEFT arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = $("#testSlider1").slider('getValue');
- var expectedSliderValue = initialSliderVal - initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 37;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("moves to the right by the 'step' value when the RIGHT arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = $("#testSlider1").slider('getValue');
- var expectedSliderValue = initialSliderVal + initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 39;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("moves to the left by the 'step' value when the DOWN arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal - initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 40;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("moves to the right by the 'step' value when the UP arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal + initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 38;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- });
- describe("For vertical sliders where its handle has focus", function() {
- beforeEach(function() {
- // Initialize the slider
- testSlider = $("#testSlider1").slider({
- id: 'testSlider',
- orientation: 'vertical',
- min: initialMinVal,
- max: initialMaxVal,
- step: initialStepVal,
- value: initialSliderVal
- });
- // Focus on handle1
- handle1 = $("#testSlider").find(".slider-handle:first");
- handle1.focus();
- });
- it("moves down by the 'step' value when the LEFT arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal - initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 37;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("moves up by the 'step' value when the RIGHT arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal + initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 39;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("moves down by the 'step' value when the DOWN arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal - initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 40;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("moves up by the 'step' value when the UP arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal + initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 38;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- });
- describe("For a reversed slider (regardless of 'orientation')", function() {
- beforeEach(function() {
- // Initialize the slider
- testSlider = $("#testSlider1").slider({
- id: 'testSlider',
- reversed: true,
- min: initialMinVal,
- max: initialMaxVal,
- step: initialStepVal,
- value: initialSliderVal
- });
- // Focus on handle1
- handle1 = $("#testSlider").find(".slider-handle:first");
- handle1.focus();
- });
- it("moves to the left by the 'step' value when the LEFT arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal - initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 37;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("moves to the right by the 'step' value when the RIGHT arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal + initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 39;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("moves to the left by the 'step' value when the DOWN arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal - initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 40;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("moves to the right by the 'step' value when the UP arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal + initialStepVal;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 38;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- });
- describe("For a range slider (regardless of 'orientation')", function() {
- beforeEach(function() {
- // Initialize the slider
- testSlider = $("#testSlider1").slider({
- id: 'testSlider',
- min: initialMinVal,
- max: initialMaxVal,
- step: initialStepVal,
- value: [initialSliderVal, initialSliderVal]
- });
- });
- describe("when handle1 tries to overtake handle2 from the left", function() {
- beforeEach(function() {
- handle1 = $("#testSlider").find(".slider-handle:first");
- handle2 = $("#testSlider").find(".slider-handle:last");
- handle1.focus();
- });
- it("handle2 moves to the right by the step value", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal + initialStepVal;
- expect(sliderValue[1]).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 39;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("handle1's value remains unchanged", function(done) {
- var sliderValue = testSlider.slider('getValue');
- handle1.on("keydown", function() {
- expect(sliderValue[0]).toBe(initialSliderVal);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 39;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- it("Should give focus to handle2 after overtaking from the left", function(done) {
- handle2.on("focus", function() {
- expect(true).toBe(true);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 39;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- });
- describe("when handle2 tries to overtake handle1 from the right", function() {
- beforeEach(function() {
- handle1 = $("#testSlider").find(".slider-handle:first");
- handle2 = $("#testSlider").find(".slider-handle:last");
- handle2.focus();
- });
- it("handle1 moves to the left by the step value", function(done) {
- handle2.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = initialSliderVal - initialStepVal;
- expect(sliderValue[0]).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 37;
- handle2[0].dispatchEvent(keyboardEvent);
- });
- it("handle2's value remains unchanged", function(done) {
- var sliderValue = testSlider.slider('getValue');
- handle2.on("keydown", function() {
- expect(sliderValue[1]).toBe(initialSliderVal);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 37;
- handle2[0].dispatchEvent(keyboardEvent);
- });
- it("Should give focus to handle1 after overtaking from the right", function(done) {
- handle1.on("focus", function() {
- expect(true).toBe(true);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = 37;
- handle2[0].dispatchEvent(keyboardEvent);
- });
- });
- });
- describe("For the natural arrow keys", function() {
- var testCases = [{
- reversed: false,
- keyEvent: 37,
- expectedSliderValue: initialSliderVal - initialStepVal,
- orientation: 'horizontal',
- key: 'left'
- }, {
- reversed: true,
- keyEvent: 37,
- expectedSliderValue: initialSliderVal + initialStepVal,
- orientation: 'horizontal',
- key: 'left'
- }, {
- reversed: false,
- keyEvent: 39,
- expectedSliderValue: initialSliderVal + initialStepVal,
- orientation: 'horizontal',
- key: 'right'
- }, {
- reversed: true,
- keyEvent: 39,
- expectedSliderValue: initialSliderVal - initialStepVal,
- orientation: 'horizontal',
- key: 'right'
- }, {
- reversed: false,
- keyEvent: 38,
- expectedSliderValue: initialSliderVal - initialStepVal,
- orientation: 'vertical',
- key: 'up'
- }, {
- reversed: true,
- keyEvent: 38,
- expectedSliderValue: initialSliderVal + initialStepVal,
- orientation: 'vertical',
- key: 'up'
- }, {
- reversed: false,
- keyEvent: 40,
- expectedSliderValue: initialSliderVal + initialStepVal,
- orientation: 'vertical',
- key: 'down'
- }, {
- reversed: true,
- keyEvent: 40,
- expectedSliderValue: initialSliderVal - initialStepVal,
- orientation: 'vertical',
- key: 'down'
- }];
- testCases.forEach(function(testCase) {
- describe("A"+((testCase.reversed)? " reversed" : "")+testCase.orientation+" slider is used for the arrow keys", function() {
- beforeEach(function() {
- // Initialize the slider
- testSlider = $("#testSlider1").slider({
- id: 'testSlider',
- min: initialMinVal,
- max: initialMaxVal,
- step: initialStepVal,
- value: initialSliderVal,
- natural_arrow_keys: true,
- reversed: testCase.reversed,
- orientation: testCase.orientation
- });
- handle1 = $("#testSlider").find(".slider-handle:first");
- handle1.focus();
- });
- it("moves to the "+testCase.key+" by the 'step' value when the "+testCase.key+" arrow key is pressed", function(done) {
- handle1.on("keydown", function() {
- var sliderValue = testSlider.slider('getValue');
- var expectedSliderValue = testCase.expectedSliderValue;
- expect(sliderValue).toBe(expectedSliderValue);
- done();
- });
- keyboardEvent.keyCode = keyboardEvent.which = testCase.keyEvent;
- handle1[0].dispatchEvent(keyboardEvent);
- });
- });
- });
- });
- });
- describe("Navigating slider with the keyboard", function() {
- var mySlider;
- var keyboardEvent;
- var options;
- var $handle1;
- var $handle2;
- describe("Does not trigger 'change' event when values do not change", function() {
- var initialValues = [0, 1];
- beforeEach(function() {
- options = {
- id: 'mySlider',
- min: -100,
- max: 100,
- step: 1,
- value: initialValues,
- range: true
- };
- // Create keyboard event
- keyboardEvent = document.createEvent('Event');
- keyboardEvent.initEvent('keydown', true, true);
- });
- afterEach(function() {
- if (mySlider) {
- if (mySlider instanceof Slider) { mySlider.destroy(); }
- mySlider = null;
- }
- });
- it("Should not trigger 'change' event", function(done) {
- var hasSlideStarted = false;
- var hasChanged = false;
- options.value = [-100, 0];
- mySlider = new Slider($('#testSlider1')[0], options);
- $handle1 = $('#mySlider').find('.slider-handle:first');
- mySlider.on('slideStart', function() {
- hasSlideStarted = true;
- });
- mySlider.on('change', function() {
- hasChanged = true;
- });
- mySlider.on('slideStop', function() {
- var value = mySlider.getValue();
- expect(hasSlideStarted).toBe(true);
- expect(hasChanged).toBe(false);
- expect(value).toEqual([-100, 0]);
- done();
- });
- // Move the handle to the left
- keyboardEvent.keyCode = keyboardEvent.which = 37;
- $handle1[0].dispatchEvent(keyboardEvent);
- });
- it("Should not trigger 'change' event via `setValue()`", function(done) {
- var isSliding = false;
- var hasChanged = false;
- mySlider = new Slider($('#testSlider1')[0], options);
- $handle1 = $('#mySlider').find('.slider-handle:first');
- mySlider.on('slide', function() {
- isSliding = true;
- });
- mySlider.on('change', function() {
- hasChanged = true;
- });
- // Change both values of the range slider
- mySlider.setValue(initialValues, true, true);
- window.setTimeout(function() {
- var value = mySlider.getValue();
- expect(isSliding).toBe(true);
- expect(hasChanged).toBe(false);
- expect(value).toEqual(initialValues);
- done();
- });
- });
- });
- describe("Does trigger 'change' event when either value changes for range sliders", function() {
- beforeEach(function() {
- options = {
- id: 'mySlider',
- min: -100,
- max: 100,
- step: 1,
- value: [-10, 10],
- range: true
- };
- // Create keyboard event
- keyboardEvent = document.createEvent('Event');
- keyboardEvent.initEvent('keydown', true, true);
- });
- afterEach(function() {
- if (mySlider) {
- if (mySlider instanceof Slider) { mySlider.destroy(); }
- mySlider = null;
- }
- });
- it("Should trigger 'change' event when the moving the lower handle", function(done) {
- var hasSlideStarted = false;
- var hasChanged = false;
- mySlider = new Slider($('#testSlider1')[0], options);
- $handle1 = $('#mySlider').find('.slider-handle:first');
- mySlider.on('slideStart', function() {
- hasSlideStarted = true;
- });
- mySlider.on('change', function() {
- hasChanged = true;
- });
- mySlider.on('slideStop', function() {
- var value = mySlider.getValue();
- expect(hasSlideStarted).toBe(true);
- expect(hasChanged).toBe(true);
- expect(value).toEqual([-11, 10]);
- done();
- });
- // Move the handle to the left
- keyboardEvent.keyCode = keyboardEvent.which = 37;
- $handle1[0].dispatchEvent(keyboardEvent);
- });
- it("Should trigger 'change' event when moving the upper handle", function(done) {
- var hasSlideStarted = false;
- var hasChanged = false;
- mySlider = new Slider($('#testSlider1')[0], options);
- $handle2 = $('#mySlider').find('.slider-handle:last');
- mySlider.on('slideStart', function() {
- hasSlideStarted = true;
- });
- mySlider.on('change', function() {
- hasChanged = true;
- });
- mySlider.on('slideStop', function() {
- var value = mySlider.getValue();
- expect(hasSlideStarted).toBe(true);
- expect(hasChanged).toBe(true);
- expect(value).toEqual([-10, 11]);
- done();
- });
- // Move the handle to the right
- keyboardEvent.keyCode = keyboardEvent.which = 39;
- $handle2[0].dispatchEvent(keyboardEvent);
- });
- it("Should trigger 'change' event when both values change via `setValue()`", function(done) {
- var isSliding = false;
- mySlider = new Slider($('#testSlider1')[0], options);
- $handle2 = $('#mySlider').find('.slider-handle:last');
- mySlider.on('slide', function() {
- isSliding = true;
- });
- mySlider.on('change', function() {
- var value = mySlider.getValue();
- expect(isSliding).toBe(true);
- expect(value).toEqual([-50, 50]);
- done();
- });
- // Change both values of the range slider
- mySlider.setValue([-50, 50], true, true);
- });
- });
- });
- describe("Navigating range sliders with the keyboard", function() {
- var mySlider;
- var keyboardEvent;
- var options;
- var $handle1;
- var $handle2;
- describe("Range slider values", function() {
- beforeEach(function() {
- options = {
- id: 'mySlider',
- min: -100,
- max: 100,
- step: 1,
- value: [0, 1],
- range: true
- };
- // Create keyboard event
- keyboardEvent = document.createEvent('Event');
- keyboardEvent.initEvent('keydown', true, true);
- });
- afterEach(function() {
- if (mySlider) {
- if (mySlider instanceof Slider) { mySlider.destroy(); }
- mySlider = null;
- }
- });
- it("Should not go below minimum at 'slideStart'", function(done) {
- options.value = [-100, 0];
- mySlider = new Slider($('#testSlider1')[0], options);
- $handle1 = $('#mySlider').find('.slider-handle:first');
- mySlider.on('slideStart', function(newVal) {
- expect(newVal).toEqual([-100, 0]);
- done();
- });
- // Move the handle to the left
- keyboardEvent.keyCode = keyboardEvent.which = 37;
- $handle1[0].dispatchEvent(keyboardEvent);
- });
- it("Should not go above maximum at 'slideStart'", function(done) {
- options.value = [0, 100];
- mySlider = new Slider($('#testSlider1')[0], options);
- $handle2 = $('#mySlider').find('.slider-handle:last');
- mySlider.on('slideStart', function(newVal) {
- expect(newVal).toEqual([0, 100]);
- done();
- });
- // Move the handle to the right
- keyboardEvent.keyCode = keyboardEvent.which = 39;
- $handle2[0].dispatchEvent(keyboardEvent);
- });
- });
- });
|