blob: 64cfd8f32b2946e09d00882f82e608cefa07a198 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test-pre.js"></script>
</head>
<body>
<script>
description('Check stepUp() and stepDown() bahevior for number type.');
var input = document.createElement('input');
var invalidStateErr = '"Error: InvalidStateError: DOM Exception 11"';
function setInputAttributes(min, max, step, value) {
input.min = min;
input.max = max;
input.step = step;
input.value = value;
}
function stepUp(value, step, max, optionalStepCount) {
setInputAttributes(null, max, step, value);
if (typeof optionalStepCount != "undefined")
input.stepUp(optionalStepCount);
else
input.stepUp();
return input.value;
}
function stepDown(value, step, min, optionalStepCount) {
setInputAttributes(min, null, step, value);
if (typeof optionalStepCount != "undefined")
input.stepDown(optionalStepCount);
else
input.stepDown();
return input.value;
}
function stepUpExplicitBounds(min, max, step, value, stepCount) {
setInputAttributes(min, max, step, value);
if (typeof stepCount !== 'undefined')
input.stepUp(stepCount);
else
input.stepUp();
return input.value;
}
function stepDownExplicitBounds(min, max, step, value, stepCount) {
setInputAttributes(min, max, step, value);
if (typeof stepCount !== 'undefined')
input.stepDown(stepCount);
else
input.stepDown();
return input.value;
}
debug('Number type');
input.type = 'number';
debug('Invalid value');
shouldThrow('stepUp("", null, null)', invalidStateErr);
shouldThrow('stepDown("", null, null)', invalidStateErr);
debug('Non-number arguments');
shouldBe('stepUp("0", null, null, "0")', '"0"');
shouldBe('stepDown("0", null, null, "0")', '"0"');
shouldBe('stepUp("0", null, null, "foo")', '"0"');
shouldBe('stepDown("0", null, null, "foo")', '"0"');
shouldBe('stepUp("0", null, null, null)', '"0"');
shouldBe('stepDown("0", null, null, null)', '"0"');
debug('Normal cases');
shouldBe('stepUp("0", null, null)', '"1"');
shouldBe('stepUp("1", null, null, 2)', '"3"');
shouldBe('stepUp("3", null, null, -1)', '"2"');
shouldBe('stepDown("2", null, null)', '"1"');
shouldBe('stepDown("1", null, null, 2)', '"-1"');
shouldBe('stepDown("-1", null, null, -1)', '"0"');
debug('Extra arguments');
shouldBe('input.value = "0"; input.min = null; input.step = null; input.stepUp(1, 2); input.value', '"1"');
shouldBe('input.value = "1"; input.stepDown(1, 3); input.value', '"0"');
debug('Invalid step value');
shouldBe('stepUp("0", "foo", null)', '"1"');
shouldBe('stepUp("1", "0", null)', '"2"');
shouldBe('stepUp("2", "-1", null)', '"3"');
debug('Step=any');
shouldThrow('stepUp("0", "any", null)', invalidStateErr);
shouldThrow('stepDown("0", "any", null)', invalidStateErr);
debug('Step=any corner case');
shouldThrow('stepUpExplicitBounds("0", "100", "any", "1.5", "1")', invalidStateErr);
shouldThrow('stepDownExplicitBounds("0", "100", "any", "1.5", "1")', invalidStateErr);
debug('Overflow/underflow');
shouldBe('stepDown("1", "1", "0")', '"0"');
shouldThrow('stepDown("0", "1", "0")', invalidStateErr);
shouldThrow('stepDown("1", "1", "0", 2)', invalidStateErr);
shouldBe('input.value', '"1"');
shouldThrow('stepDown("1", "3.40282346e+38", "", 2)', invalidStateErr);
shouldBe('stepUp("-1", "1", "0")', '"0"');
shouldThrow('stepUp("0", "1", "0")', invalidStateErr);
shouldThrow('stepUp("-1", "1", "0", 2)', invalidStateErr);
shouldBe('input.value', '"-1"');
shouldThrow('stepUp("1", "3.40282346e+38", "", 2)', invalidStateErr);
debug('stepDown()/stepUp() for stepMismatch values');
shouldBe('stepUp("1", "2", "")', '"3"');
shouldBe('input.stepDown(); input.value', '"1"');
shouldBe('input.min = "0"; stepUp("9", "10", "", 9)', '"99"');
shouldBe('stepDown("19", "10", "0")', '"9"');
shouldBe('stepUp("89", "10", "99")', '"99"');
debug('Huge value and small step');
shouldBe('input.min = ""; stepUp("1e+38", "1", "", 999999)', '"1e+38"');
shouldBe('input.max = ""; stepDown("1e+38", "1", "", 999999)', '"1e+38"');
debug('Fractional numbers');
shouldBe('input.min = ""; stepUp("0", "0.33333333333333333", "", 3)', '"1"');
shouldBe('stepUp("1", "0.1", "", 10)', '"2"');
shouldBe('input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.value', '"3"');
shouldBe('input.min = "0"; stepUp("0", "0.003921568627450980", "1", 255)', '"1"');
shouldBe('for (var i = 0; i < 255; i++) { input.stepDown(); }; input.value', '"0"');
debug('Rounding');
shouldBe('stepUp("5.005", "0.005", "", 2)', '"5.015"');
shouldBe('stepUp("5.005", "0.005", "", 11)', '"5.06"');
shouldBe('stepUp("5.005", "0.005", "", 12)', '"5.065"');
shouldBe('stepUpExplicitBounds("4", "9", "0.005", "5.005", 2)', '"5.015"');
shouldBe('stepUpExplicitBounds("4", "9", "0.005", "5.005", 11)', '"5.06"');
shouldBe('stepUpExplicitBounds("4", "9", "0.005", "5.005", 12)', '"5.065"');
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>