2010-10-22 04:23:52 +00:00
|
|
|
/*
|
|
|
|
* spinner_core.js
|
|
|
|
*/
|
|
|
|
|
2010-10-26 10:14:45 +00:00
|
|
|
var simulateKeyDownUp = function(el, kCode, shift) {
|
|
|
|
el.simulate("keydown",{keyCode:kCode, shiftKey: shift || false })
|
|
|
|
.simulate("keyup",{keyCode:kCode, shiftKey: shift || false });
|
|
|
|
};
|
2010-10-22 04:23:52 +00:00
|
|
|
|
|
|
|
(function($) {
|
|
|
|
|
|
|
|
// Spinner Tests
|
|
|
|
module("spinner: core");
|
|
|
|
|
|
|
|
test("destroy", function() {
|
2010-10-26 10:21:22 +00:00
|
|
|
// cheat a bit to get IE6 to pass
|
|
|
|
$("#spin").val(0);
|
2010-10-26 10:14:45 +00:00
|
|
|
var beforeHtml = $("#spin").parent().html();
|
|
|
|
var afterHtml = $("#spin").spinner().spinner("destroy").parent().html();
|
|
|
|
equal( afterHtml, beforeHtml, "before/after html should be the same" );
|
2010-10-22 04:23:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("keydown UP on input, increases value not greater than max", function() {
|
2010-10-26 10:14:45 +00:00
|
|
|
var el = $("#spin").spinner({
|
2010-10-22 04:23:52 +00:00
|
|
|
max:100,
|
|
|
|
value:50,
|
|
|
|
step:10
|
2010-10-26 10:14:45 +00:00
|
|
|
});
|
2010-10-22 04:23:52 +00:00
|
|
|
|
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
|
|
|
equals(el.val(), 60);
|
|
|
|
|
|
|
|
for (i = 0; i<11; i++) {
|
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
|
|
|
}
|
|
|
|
equals(el.val(), 100);
|
|
|
|
|
2010-10-26 10:44:32 +00:00
|
|
|
el.spinner("value", 50);
|
2010-10-22 04:23:52 +00:00
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.UP);
|
|
|
|
equals(el.val(), 60);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("keydown DOWN on input, decreases value not less than min", function() {
|
2010-10-26 10:14:45 +00:00
|
|
|
var el = $("#spin").spinner({
|
2010-10-22 04:23:52 +00:00
|
|
|
min:-100,
|
|
|
|
value:50,
|
|
|
|
step:10
|
2010-10-26 10:14:45 +00:00
|
|
|
});
|
2010-10-22 04:23:52 +00:00
|
|
|
|
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
|
|
|
equals(el.val(), 40);
|
|
|
|
|
|
|
|
for (i = 0; i<21; i++) {
|
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
|
|
|
}
|
|
|
|
equals(el.val(), -100);
|
|
|
|
|
2010-10-26 10:44:32 +00:00
|
|
|
el.spinner("value", 50);
|
2010-10-22 04:23:52 +00:00
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.DOWN);
|
|
|
|
equals(el.val(), 40);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("keydown PGUP on input, increases value not greater than max", function() {
|
2010-10-26 10:14:45 +00:00
|
|
|
var el = $("#spin").spinner({
|
|
|
|
max: 500,
|
|
|
|
value: 0,
|
|
|
|
step: 10
|
|
|
|
});
|
2010-10-22 04:23:52 +00:00
|
|
|
|
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP);
|
2010-10-26 10:14:45 +00:00
|
|
|
equal(el.val(), 100);
|
2010-10-22 04:23:52 +00:00
|
|
|
|
|
|
|
for (i = 0; i<5; i++) {
|
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP);
|
|
|
|
}
|
2010-10-26 10:14:45 +00:00
|
|
|
equal(el.val(), 500);
|
2010-10-22 04:23:52 +00:00
|
|
|
|
2010-10-26 10:44:32 +00:00
|
|
|
el.spinner("value", 0);
|
2010-10-22 04:23:52 +00:00
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.PAGE_UP);
|
2010-10-26 10:14:45 +00:00
|
|
|
equals(el.val(), 100);
|
2010-10-22 04:23:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("keydown PGDN on input, decreases value not less than min", function() {
|
2010-10-26 10:14:45 +00:00
|
|
|
var el = $("#spin").spinner({
|
|
|
|
min:-500,
|
2010-10-22 04:23:52 +00:00
|
|
|
value:0,
|
|
|
|
step:10
|
2010-10-26 10:14:45 +00:00
|
|
|
});
|
2010-10-22 04:23:52 +00:00
|
|
|
|
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN);
|
2010-10-26 10:14:45 +00:00
|
|
|
equals(el.val(), -100);
|
2010-10-22 04:23:52 +00:00
|
|
|
|
|
|
|
for (i = 0; i<5; i++) {
|
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN);
|
|
|
|
}
|
2010-10-26 10:14:45 +00:00
|
|
|
equals(el.val(), -500);
|
2010-10-22 04:23:52 +00:00
|
|
|
|
2010-10-26 10:44:32 +00:00
|
|
|
el.spinner("value", 0);
|
2010-10-22 04:23:52 +00:00
|
|
|
simulateKeyDownUp(el, $.ui.keyCode.PAGE_DOWN);
|
|
|
|
equals(el.val(), -100);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("mouse click on buttons", function() {
|
2010-10-26 10:14:45 +00:00
|
|
|
var el = $("#spin").spinner(),
|
|
|
|
val = 0;
|
2010-10-22 04:23:52 +00:00
|
|
|
|
|
|
|
$(".ui-spinner-up").trigger("mousedown").trigger("mouseup");
|
|
|
|
equals(el.val(), ++val, "mouse click to up");
|
|
|
|
|
|
|
|
$(".ui-spinner-down").trigger("mousedown").trigger("mouseup");
|
|
|
|
equals(el.val(), --val, "mouse click to down");
|
|
|
|
|
2010-10-26 10:44:32 +00:00
|
|
|
el.spinner("value", 50);
|
2010-10-22 04:23:52 +00:00
|
|
|
$(".ui-spinner-up").trigger("mousedown").trigger("mouseup");
|
|
|
|
equals(el.val(), 51);
|
|
|
|
|
2010-10-26 10:44:32 +00:00
|
|
|
el.spinner("value", 50);
|
2010-10-22 04:23:52 +00:00
|
|
|
$(".ui-spinner-down").trigger("mousedown").trigger("mouseup");
|
|
|
|
equals(el.val(), 49);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("mouse wheel on input", function() {
|
2010-10-26 10:14:45 +00:00
|
|
|
expect(3);
|
|
|
|
|
|
|
|
var el = $("#spin").spinner();
|
|
|
|
el.trigger("mousewheel", 1);
|
|
|
|
equal(el.val(), 1);
|
|
|
|
|
|
|
|
// mousewheel handler uses a timeout, need to accomodate that
|
|
|
|
stop();
|
|
|
|
setTimeout(function() {
|
|
|
|
el.trigger("mousewheel", -1);
|
|
|
|
equal(el.val(), 0);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
el.trigger("mousewheel", -1);
|
|
|
|
equal(el.val(), -1);
|
|
|
|
start();
|
|
|
|
}, 100);
|
|
|
|
}, 100);
|
|
|
|
|
2010-10-22 04:23:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("reading HTML5 attributes", function() {
|
2010-10-26 10:14:45 +00:00
|
|
|
var el = $('<input id="spinner" type="number" min="-100" max="100" value="5" step="2">').spinner();
|
2010-10-22 04:23:52 +00:00
|
|
|
equals(el.spinner('option', 'value'), 5, 'value');
|
|
|
|
equals(el.spinner('option', 'max'), 100, 'max');
|
|
|
|
equals(el.spinner('option', 'min'), -100, 'min');
|
|
|
|
equals(el.spinner('option', 'step'), 2, 'step');
|
|
|
|
});
|
|
|
|
|
|
|
|
test("ARIA attributes", function() {
|
2011-01-15 13:28:13 +00:00
|
|
|
var el = $('#spin').spinner({ min: -5, max: 5, value: 2 });;
|
2010-10-22 04:23:52 +00:00
|
|
|
|
2011-01-15 13:28:13 +00:00
|
|
|
equals(el.attr('role'), 'spinbutton', 'role');
|
|
|
|
equals(el.attr('aria-valuemin'), -5, 'aria-valuemin');
|
|
|
|
equals(el.attr('aria-valuemax'), 5, 'aria-valuemax');
|
|
|
|
equals(el.attr('aria-valuenow'), 2, 'aria-valuenow');
|
2010-10-22 04:23:52 +00:00
|
|
|
|
|
|
|
el.spinner('stepUp');
|
|
|
|
|
2011-01-15 13:28:13 +00:00
|
|
|
equals(el.attr('aria-valuenow'), 3, 'stepUp 1 step changes aria-valuenow');
|
2010-10-22 04:23:52 +00:00
|
|
|
|
|
|
|
el.spinner('option', { min: -10, max: 10 });
|
|
|
|
|
2011-01-15 13:28:13 +00:00
|
|
|
equals(el.attr('aria-valuemin'), -10, 'min option changed aria-valuemin changes');
|
|
|
|
equals(el.attr('aria-valuemax'), 10, 'max option changed aria-valuemax changes');
|
2010-10-22 04:23:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
})(jQuery);
|