Skip to content
Snippets Groups Projects
KeyboardSupportSpec.js 15 KiB
Newer Older
  • Learn to ignore specific revisions
  • Laura Cappelli's avatar
    Laura Cappelli committed
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    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");
            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);
          });
        });
    
        describe("when handle2 tries to overtake handle1 from the right", function() {
          beforeEach(function() {
            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);
          });
        });
      });
    
      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);
            });
          });
        });
      });
    });