Skip to content

Commit 3a9652c

Browse files
committed
slider switch keyboard access tests Fixes jquery-archive#632
1 parent 06b83ad commit 3a9652c

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

js/jquery.mobile.forms.slider.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,16 @@ $.widget( "mobile.slider", $.mobile.widget, {
131131
var valuenow = $( this ).attr( "aria-valuenow" ),
132132
index;
133133

134+
if ( self.options.disabled ) {
135+
return;
136+
}
137+
134138
// convert switch values to slider
135139
if(valuenow.match(/off|on/)){
136-
index = switchValues[valuenow];
137-
} else {
138-
index = window.parseFloat(valuenow, 10);
140+
valuenow = switchValues[valuenow];
139141
}
140142

141-
if ( self.options.disabled ) {
142-
return;
143-
}
143+
index = window.parseFloat(valuenow, 10);
144144

145145
// In all cases prevent the default and mark the handle as active
146146
switch ( event.keyCode ) {

tests/unit/slider/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,27 @@ <h2 id="qunit-userAgent"></h2>
6363

6464
<div id="foo" data-role="page">
6565
<div data-role="fieldcontain">
66-
<label for="slider-1">Input slider:</label>
6766
<input type="range" name="slider-1" id="range-slider-up" value="0" min="0" max="100" data-theme="b" data-track-theme="a" />
6867
</div>
6968

7069
<div data-role="fieldcontain">
71-
<label for="slider-1">Input slider:</label>
7270
<input type="range" name="slider-1" id="range-slider-down" value="10" min="0" max="100" data-theme="b" data-track-theme="a" />
7371
</div>
7472

7573
<div data-role="fieldcontain">
76-
<label for="slider-1">Input slider:</label>
7774
<input type="range" name="slider-1" id="range-slider-home" value="75" min="0" max="100" data-theme="b" data-track-theme="a" />
7875
</div>
7976

8077
<div data-role="fieldcontain">
8178
<label for="slider-1">Input slider:</label>
8279
<input type="range" name="slider-1" id="range-slider-end" value="15" min="0" max="100" data-theme="b" data-track-theme="a" />
8380
</div>
81+
82+
<div data-role="fieldcontain">
83+
<select name="slider" id="slider-switch" data-role="slider">
84+
<option value="off">Off</option>
85+
<option value="on">On</option>
86+
</select>
87+
</div>
8488
</div>
8589
</html>

tests/unit/slider/slider_events.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
expect( opts.keyCodes.length );
1414

1515
$.each(opts.keyCodes, function(i, elem){
16+
17+
// stub the keycode value and trigger the keypress
1618
$.Event.prototype.keyCode = $.mobile.keyCode[elem];
1719
handle.trigger('keydown');
18-
val = val + opts.increment;
20+
21+
val += opts.increment;
1922
same(val, window.parseFloat(slider.val(), 10), "new value is one larger");
2023
});
2124
};
@@ -28,7 +31,7 @@
2831
});
2932
});
3033

31-
test( "slider should move right with down, left, and page down keypress", function(){
34+
test( "slider should move left with down, left, and page down keypress", function(){
3235
keypressTest({
3336
selector: '#range-slider-down',
3437
keyCodes: ['DOWN', 'LEFT', 'PAGE_DOWN'],
@@ -58,4 +61,47 @@
5861
increment: 0 - initialVal
5962
});
6063
});
64+
65+
66+
// generic switch test function
67+
var sliderSwitchTest = function(opts){
68+
var slider = $("#slider-switch"),
69+
handle = slider.siblings('.ui-slider').find('a'),
70+
switchValues = {
71+
'off' : 0,
72+
'on' : 1
73+
};
74+
75+
// One for the select and one for the aria-valuenow
76+
expect( opts.keyCodes.length * 2 );
77+
78+
$.each(opts.keyCodes, function(i, elem){
79+
// reset the values
80+
slider[0].selectedIndex = switchValues[opts.start];
81+
handle.attr({'aria-valuenow' : opts.start });
82+
83+
// stub the keycode and trigger the event
84+
$.Event.prototype.keyCode = $.mobile.keyCode[elem];
85+
handle.trigger('keydown');
86+
87+
same(handle.attr('aria-valuenow'), opts.finish, "handle value is " + opts.finish);
88+
same(slider[0].selectedIndex, switchValues[opts.finish], "select input has correct index");
89+
});
90+
};
91+
92+
test( "switch should select on with up, right, page up and end", function(){
93+
sliderSwitchTest({
94+
start: 'off',
95+
finish: 'on',
96+
keyCodes: ['UP', 'RIGHT', 'PAGE_UP', 'END']
97+
});
98+
});
99+
100+
test( "switch should select off with down, left, page down and home", function(){
101+
sliderSwitchTest({
102+
start: 'on',
103+
finish: 'off',
104+
keyCodes: ['DOWN', 'LEFT', 'PAGE_DOWN', 'HOME']
105+
});
106+
});
61107
})(jQuery);

0 commit comments

Comments
 (0)