2010-12-23 07:18:14 +00:00
|
|
|
/*
|
|
|
|
|
* mobile slider unit tests
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
(function($){
|
2011-07-13 06:25:54 +00:00
|
|
|
var onChangeCnt = 0;
|
|
|
|
|
window.onChangeCounter = function() {
|
|
|
|
|
onChangeCnt++;
|
|
|
|
|
}
|
2010-12-23 07:18:14 +00:00
|
|
|
module('jquery.mobile.slider.js');
|
|
|
|
|
|
|
|
|
|
var keypressTest = function(opts){
|
|
|
|
|
var slider = $(opts.selector),
|
|
|
|
|
val = window.parseFloat(slider.val()),
|
|
|
|
|
handle = slider.siblings('.ui-slider').find('.ui-slider-handle');
|
|
|
|
|
|
|
|
|
|
expect( opts.keyCodes.length );
|
|
|
|
|
|
|
|
|
|
$.each(opts.keyCodes, function(i, elem){
|
2010-12-27 07:08:46 +00:00
|
|
|
|
|
|
|
|
// stub the keycode value and trigger the keypress
|
2010-12-23 07:18:14 +00:00
|
|
|
$.Event.prototype.keyCode = $.mobile.keyCode[elem];
|
|
|
|
|
handle.trigger('keydown');
|
2010-12-27 07:08:46 +00:00
|
|
|
|
|
|
|
|
val += opts.increment;
|
2011-02-03 07:46:20 +00:00
|
|
|
same(val, window.parseFloat(slider.val(), 10), "new value is " + opts.increment + " different");
|
2010-12-23 07:18:14 +00:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
test( "slider should move right with up, right, and page up keypress", function(){
|
|
|
|
|
keypressTest({
|
|
|
|
|
selector: '#range-slider-up',
|
|
|
|
|
keyCodes: ['UP', 'RIGHT', 'PAGE_UP'],
|
|
|
|
|
increment: 1
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2010-12-27 07:08:46 +00:00
|
|
|
test( "slider should move left with down, left, and page down keypress", function(){
|
2010-12-23 07:18:14 +00:00
|
|
|
keypressTest({
|
|
|
|
|
selector: '#range-slider-down',
|
|
|
|
|
keyCodes: ['DOWN', 'LEFT', 'PAGE_DOWN'],
|
|
|
|
|
increment: -1
|
|
|
|
|
});
|
|
|
|
|
});
|
2010-12-23 07:31:53 +00:00
|
|
|
|
2010-12-23 07:53:44 +00:00
|
|
|
test( "slider should move to range minimum on end keypress", function(){
|
2010-12-23 07:31:53 +00:00
|
|
|
var selector = "#range-slider-end",
|
|
|
|
|
initialVal = window.parseFloat($(selector).val(), 10),
|
|
|
|
|
max = window.parseFloat($(selector).attr('max'), 10);
|
|
|
|
|
|
|
|
|
|
keypressTest({
|
|
|
|
|
selector: selector,
|
|
|
|
|
keyCodes: ['END'],
|
|
|
|
|
increment: max - initialVal
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2010-12-23 07:53:44 +00:00
|
|
|
test( "slider should move to range minimum on end keypress", function(){
|
2010-12-23 07:31:53 +00:00
|
|
|
var selector = "#range-slider-home",
|
|
|
|
|
initialVal = window.parseFloat($(selector).val(), 10);
|
|
|
|
|
|
|
|
|
|
keypressTest({
|
|
|
|
|
selector: selector,
|
|
|
|
|
keyCodes: ['HOME'],
|
|
|
|
|
increment: 0 - initialVal
|
|
|
|
|
});
|
|
|
|
|
});
|
2010-12-27 07:08:46 +00:00
|
|
|
|
2011-02-03 07:46:20 +00:00
|
|
|
test( "slider should move positive by steps on keypress", function(){
|
|
|
|
|
keypressTest({
|
|
|
|
|
selector: "#stepped",
|
|
|
|
|
keyCodes: ['RIGHT'],
|
|
|
|
|
increment: 10
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test( "slider should move negative by steps on keypress", function(){
|
|
|
|
|
keypressTest({
|
|
|
|
|
selector: "#stepped",
|
|
|
|
|
keyCodes: ['LEFT'],
|
|
|
|
|
increment: -10
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2011-01-25 07:38:50 +00:00
|
|
|
test( "slider should validate input value on blur", function(){
|
|
|
|
|
var slider = $("#range-slider-up");
|
|
|
|
|
slider.focus();
|
|
|
|
|
slider.val(200);
|
|
|
|
|
same(slider.val(), "200");
|
|
|
|
|
slider.blur();
|
|
|
|
|
same(slider.val(), slider.attr('max'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test( "slider should not validate input on keyup", function(){
|
|
|
|
|
var slider = $("#range-slider-up");
|
|
|
|
|
slider.focus();
|
|
|
|
|
slider.val(200);
|
|
|
|
|
same(slider.val(), "200");
|
|
|
|
|
slider.keyup();
|
|
|
|
|
same(slider.val(), "200");
|
|
|
|
|
});
|
This commit decouples all widgets from the page plugin so that they can be used ad-hoc.
- Internally, each plugin self-initializes by binding to the pagecreate event.
- Unit tests have been added and adjusted to support some internal changes involved in this commit.
- In the process, the portions of the page plugin that were used to enhance the header,content,and footer sections of a native-app style page layout are now located in jquery.mobile.page.sections.js.
- No public API options have changed, except that the page plugin no longer has options for keepNative, and degradeInputs, as plugins now handle these internally (keepNative was never documented, and degradeInputs only affected slider, so it lives there now. Page options related to the page sections are now located in the page.sections script, but they are still configurable via the page plugin's options api.
- Make, Ant, and index files are updated with a new load order for all JS files.
2011-07-19 23:05:35 +00:00
|
|
|
|
|
|
|
|
test( "input type should degrade to number when slider is created", function(){
|
|
|
|
|
same($("#range-slider-up").attr( "type" ), "number");
|
|
|
|
|
});
|
2010-12-27 07:08:46 +00:00
|
|
|
|
|
|
|
|
// generic switch test function
|
|
|
|
|
var sliderSwitchTest = function(opts){
|
|
|
|
|
var slider = $("#slider-switch"),
|
|
|
|
|
handle = slider.siblings('.ui-slider').find('a'),
|
|
|
|
|
switchValues = {
|
|
|
|
|
'off' : 0,
|
|
|
|
|
'on' : 1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// One for the select and one for the aria-valuenow
|
|
|
|
|
expect( opts.keyCodes.length * 2 );
|
|
|
|
|
|
|
|
|
|
$.each(opts.keyCodes, function(i, elem){
|
|
|
|
|
// reset the values
|
|
|
|
|
slider[0].selectedIndex = switchValues[opts.start];
|
|
|
|
|
handle.attr({'aria-valuenow' : opts.start });
|
|
|
|
|
|
|
|
|
|
// stub the keycode and trigger the event
|
|
|
|
|
$.Event.prototype.keyCode = $.mobile.keyCode[elem];
|
|
|
|
|
handle.trigger('keydown');
|
|
|
|
|
|
|
|
|
|
same(handle.attr('aria-valuenow'), opts.finish, "handle value is " + opts.finish);
|
|
|
|
|
same(slider[0].selectedIndex, switchValues[opts.finish], "select input has correct index");
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
test( "switch should select on with up, right, page up and end", function(){
|
|
|
|
|
sliderSwitchTest({
|
|
|
|
|
start: 'off',
|
|
|
|
|
finish: 'on',
|
|
|
|
|
keyCodes: ['UP', 'RIGHT', 'PAGE_UP', 'END']
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test( "switch should select off with down, left, page down and home", function(){
|
|
|
|
|
sliderSwitchTest({
|
|
|
|
|
start: 'on',
|
|
|
|
|
finish: 'off',
|
|
|
|
|
keyCodes: ['DOWN', 'LEFT', 'PAGE_DOWN', 'HOME']
|
|
|
|
|
});
|
|
|
|
|
});
|
2011-07-13 06:25:54 +00:00
|
|
|
|
|
|
|
|
test( "onchange should not be called on create", function(){
|
|
|
|
|
equals(onChangeCnt, 0, "onChange should not have been called");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test( "onchange should be called onchange", function(){
|
|
|
|
|
onChangeCnt = 0;
|
|
|
|
|
$( "#onchange" ).slider( "refresh", 50 );
|
|
|
|
|
equals(onChangeCnt, 1, "onChange should have been called once");
|
|
|
|
|
});
|
2011-09-22 23:24:56 +00:00
|
|
|
|
2011-07-22 13:05:55 +00:00
|
|
|
test( "slider controls will create when inside a container that receives a 'create' event", function(){
|
2011-07-20 13:35:48 +00:00
|
|
|
ok( !$("#enhancetest").appendTo(".ui-page-active").find(".ui-slider").length, "did not have enhancements applied" );
|
2011-07-22 13:05:55 +00:00
|
|
|
ok( $("#enhancetest").trigger("create").find(".ui-slider").length, "enhancements applied" );
|
2011-07-20 13:35:48 +00:00
|
|
|
});
|
2011-09-22 23:24:56 +00:00
|
|
|
|
|
|
|
|
test( "toggle switch should fire one change event when clicked", function(){
|
|
|
|
|
var control = $( "#slider-switch" ),
|
|
|
|
|
widget = control.data( "slider" ),
|
|
|
|
|
slider = widget.slider,
|
|
|
|
|
handle = widget.handle,
|
|
|
|
|
changeCount = 0,
|
|
|
|
|
changeFunc = function( e ) {
|
|
|
|
|
ok( control[0].selectedIndex !== currentValue, "change event should only be triggered if the value changes");
|
|
|
|
|
++changeCount;
|
|
|
|
|
},
|
|
|
|
|
event = null,
|
|
|
|
|
offset = handle.offset(),
|
|
|
|
|
currentValue = control[0].selectedIndex;
|
|
|
|
|
|
|
|
|
|
function createEvent( name, target, x, y )
|
|
|
|
|
{
|
|
|
|
|
var event = $.Event( name );
|
|
|
|
|
event.target = target;
|
|
|
|
|
event.pageX = x;
|
|
|
|
|
event.pageY = y;
|
|
|
|
|
return event;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
control.bind( "change", changeFunc );
|
|
|
|
|
|
|
|
|
|
// The toggle switch actually updates on mousedown and mouseup events, so we go through
|
|
|
|
|
// the motions of generating all the events that happen during a click to make sure that
|
|
|
|
|
// during all of those events, the value only changes once.
|
|
|
|
|
|
|
|
|
|
slider.trigger( createEvent( "mousedown", handle[ 0 ], offset.left + 10, offset.top + 10 ) );
|
|
|
|
|
slider.trigger( createEvent( "mouseup", handle[ 0 ], offset.left + 10, offset.top + 10 ) );
|
|
|
|
|
slider.trigger( createEvent( "click", handle[ 0 ], offset.left + 10, offset.top + 10 ) );
|
|
|
|
|
|
|
|
|
|
control.unbind( "change", changeFunc );
|
|
|
|
|
|
|
|
|
|
ok( control[0].selectedIndex !== currentValue, "value did change");
|
|
|
|
|
same( changeCount, 1, "change event should be fired once during a click" );
|
|
|
|
|
});
|
2010-12-23 07:18:14 +00:00
|
|
|
})(jQuery);
|