blob: 4900dccd1e26896e40242edf6539bce7c941d962 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test-pre.js"></script>
</head>
<body>
<script>
description('Check stepping-up and -down for number input from renderer.');
var input = document.createElement('input');
var invalidStateErr = '"InvalidStateError (DOM Exception 11): The object is in an invalid state."';
function sendKey(keyName) {
var event = document.createEvent('KeyboardEvent');
event.initKeyboardEvent('keydown', true, true, document.defaultView, keyName);
input.dispatchEvent(event);
}
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")
if (optionalStepCount < 0)
for (var i = 0; i < -optionalStepCount; i++)
sendKey('Down');
else
for (var i = 0; i < optionalStepCount; i++)
sendKey('Up');
else
sendKey('Up');
return input.value;
}
function stepDown(value, step, min, optionalStepCount) {
setInputAttributes(min, null, step, value);
if (typeof optionalStepCount != "undefined")
if (optionalStepCount < 0)
for (var i = 0; i < -optionalStepCount; i++)
sendKey('Up');
else
for (var i = 0; i < optionalStepCount; i++)
sendKey('Down');
else
sendKey('Down');
return input.value;
}
// Range value gets automatically shifted based on bounds,
// So always set the min and max first to get expected behavior
function stepUpExplicitBounds(min, max, step, value, stepCount) {
setInputAttributes(min, max, step, value);
if (typeof stepCount !== 'undefined')
if (stepCount < 0) {
for (var i = 0; i < -stepCount; i++)
sendKey('Down');
} else {
for (var i = 0; i < stepCount; i++)
sendKey('Up');
}
else
sendKey('Up');
return input.value;
}
function stepDownExplicitBounds(min, max, step, value, stepCount) {
setInputAttributes(min, max, step, value);
if (typeof stepCount !== 'undefined')
if (stepCount < 0) {
for (var i = 0; i < -stepCount; i++)
sendKey('Up');
} else {
for (var i = 0; i < stepCount; i++)
sendKey('Down');
}
else
sendKey('Down');
return input.value;
}
debug('Number type');
input.type = 'number';
debug('Function arguments are (value, step, {min or max}, [stepCount]).');
debug('');
debug('Invalid value');
shouldBe('stepUp("", null, null)', '"1"');
shouldBe('stepDown("", null, null)', '"-1"');
shouldBe('stepUp("", "any", null)', '"1"');
shouldBe('stepDown("", "any", null)', '"-1"');
shouldBe('stepUp("", "foo", null)', '"1"');
shouldBe('stepDown("", "foo", null)', '"-1"');
shouldBe('stepUp("foo", null, null)', '"1"');
shouldBe('stepDown("foo", null, null)', '"-1"');
shouldBe('stepUp("foo", "any", null)', '"1"');
shouldBe('stepDown("foo", "any", null)', '"-1"');
shouldBe('stepUp("foo", "foo", null)', '"1"');
shouldBe('stepDown("foo", "foo", null)', '"-1"');
debug('');
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('');
debug('Fractional cases')
// Note: these tests also appear in number-stepup-stepdown.html. However, the
// results here differ from the results there. According to:
//
// https://html.spec.whatwg.org/multipage/forms.html#number-state-(type=number)
//
// The browser is allowed to clamp resulting values to the nearest multiple of
// the [step] value:
//
// When the element is suffering from a step mismatch, the user agent may
// round the element's value to the nearest number for which the element
// would not suffer from a step mismatch. If there are two such numbers,
// user agents are encouraged to pick the one nearest positive infinity.
//
// WebKit seesm to perform this clamping when stepping from the UI, but not
// from the API.
//
// See also the function comment for InputType::stepUpFromRenderer.
// Base/model/template tests
shouldBe('stepUp("0.1", 1, null)', '"1"') // Is 1.1 in non-render test
shouldBe('stepUp("0.2", 1, null)', '"1"'); // Is 1.1 in non-render test
shouldBe('stepUp("1.0", 1, null)', '"2"');
shouldBe('stepUp("1.1", 1, null)', '"2"'); // Is 2.1 in non-render test
shouldBe('stepUp("1.2", 1, null)', '"2"'); // Is 2.2 in non-render test
shouldBe('stepUp("2.0", 1, null)', '"3"');
debug('');
// Same as above, but with negative numbers.
shouldBe('stepUp("-0.1", 1, null)', '"0"'); // Is 0.9 in non-render test
shouldBe('stepUp("-0.2", 1, null)', '"0"'); // Is 0.8 in non-render test
shouldBe('stepUp("-1.0", 1, null)', '"0"');
shouldBe('stepUp("-1.1", 1, null)', '"-1"'); // Is -0.1 in non-render test
shouldBe('stepUp("-1.2", 1, null)', '"-1"'); // Is -0.2 in non-render test
shouldBe('stepUp("-2.0", 1, null)', '"-1"');
debug('');
// Same as above, but stepping down rather than up.
shouldBe('stepDown("0.1", 1, null)', '"0"'); // Is -0.9 in non-render test
shouldBe('stepDown("0.2", 1, null)', '"0"'); // Is -0.8 in non-render test
shouldBe('stepDown("1.0", 1, null)', '"0"');
shouldBe('stepDown("1.1", 1, null)', '"1"'); // Is 0.1 in non-render test
shouldBe('stepDown("1.2", 1, null)', '"1"'); // Is 0.2 in non-render test
shouldBe('stepDown("2.0", 1, null)', '"1"');
debug('');
shouldBe('stepDown("-0.1", 1, null)', '"-1"'); // Is -1.1 in non-render test
shouldBe('stepDown("-0.2", 1, null)', '"-1"'); // Is -1.2 in non-render test
shouldBe('stepDown("-1.0", 1, null)', '"-2"');
shouldBe('stepDown("-1.1", 1, null)', '"-2"'); // Is -2.1 in non-render test
shouldBe('stepDown("-1.2", 1, null)', '"-2"'); // Is -2.2 in non-render test
shouldBe('stepDown("-2.0", 1, null)', '"-3"');
// Same as above, but with leading/trailing zeros removed.
debug('');
shouldBe('stepUp(".1", 1, null)', '"1"'); // Is 1.1 in non-render test
shouldBe('stepUp(".2", 1, null)', '"1"'); // Is 1.2 in non-render test
shouldBe('stepUp("1.", 1, null)', '"2"');
shouldBe('stepUp("2.", 1, null)', '"3"');
debug('');
shouldBe('stepUp("-.1", 1, null)', '"0"'); // Is 0.9 in non-render test
shouldBe('stepUp("-.2", 1, null)', '"0"'); // Is 0.8 in non-render test
shouldBe('stepUp("-1.", 1, null)', '"0"');
shouldBe('stepUp("-2.", 1, null)', '"-1"');
debug('');
shouldBe('stepDown(".1", 1, null)', '"0"'); // Is -0.9 in non-render test
shouldBe('stepDown(".2", 1, null)', '"0"'); // Is -0.8 in non-render test
shouldBe('stepDown("1.", 1, null)', '"0"');
shouldBe('stepDown("2.", 1, null)', '"1"');
debug('');
shouldBe('stepDown("-.1", 1, null)', '"-1"'); // Is -1.1 in non-render test
shouldBe('stepDown("-.2", 1, null)', '"-1"'); // Is -1.2 in non-render test
shouldBe('stepDown("-1.", 1, null)', '"-2"');
shouldBe('stepDown("-2.", 1, null)', '"-3"');
// Same as above, but stepping by .1 rather than 1.
debug('');
shouldBe('stepUp("0.1", .1, null)', '"0.2"');
shouldBe('stepUp("0.2", .1, null)', '"0.3"');
shouldBe('stepUp("1.0", .1, null)', '"1.1"');
shouldBe('stepUp("1.1", .1, null)', '"1.2"');
shouldBe('stepUp("1.2", .1, null)', '"1.3"');
shouldBe('stepUp("2.0", .1, null)', '"2.1"');
debug('');
shouldBe('stepUp("-0.1", .1, null)', '"0"');
shouldBe('stepUp("-0.2", .1, null)', '"-0.1"');
shouldBe('stepUp("-1.0", .1, null)', '"-0.9"');
shouldBe('stepUp("-1.1", .1, null)', '"-1"');
shouldBe('stepUp("-1.2", .1, null)', '"-1.1"');
shouldBe('stepUp("-2.0", .1, null)', '"-1.9"');
debug('');
shouldBe('stepDown("0.1", .1, null)', '"0"');
shouldBe('stepDown("0.2", .1, null)', '"0.1"');
shouldBe('stepDown("1.0", .1, null)', '"0.9"');
shouldBe('stepDown("1.1", .1, null)', '"1"');
shouldBe('stepDown("1.2", .1, null)', '"1.1"');
shouldBe('stepDown("2.0", .1, null)', '"1.9"');
debug('');
shouldBe('stepDown("-0.1", .1, null)', '"-0.2"');
shouldBe('stepDown("-0.2", .1, null)', '"-0.3"');
shouldBe('stepDown("-1.0", .1, null)', '"-1.1"');
shouldBe('stepDown("-1.1", .1, null)', '"-1.2"');
shouldBe('stepDown("-1.2", .1, null)', '"-1.3"');
shouldBe('stepDown("-2.0", .1, null)', '"-2.1"');
debug('');
shouldBe('stepUp(".1", .1, null)', '"0.2"');
shouldBe('stepUp(".2", .1, null)', '"0.3"');
shouldBe('stepUp("1.", .1, null)', '"1.1"');
shouldBe('stepUp("2.", .1, null)', '"2.1"');
debug('');
shouldBe('stepUp("-.1", .1, null)', '"0"');
shouldBe('stepUp("-.2", .1, null)', '"-0.1"');
shouldBe('stepUp("-1.", .1, null)', '"-0.9"');
shouldBe('stepUp("-2.", .1, null)', '"-1.9"');
shouldBe('stepDown(".1", .1, null)', '"0"');
shouldBe('stepDown(".2", .1, null)', '"0.1"');
shouldBe('stepDown("1.", .1, null)', '"0.9"');
shouldBe('stepDown("2.", .1, null)', '"1.9"');
debug('');
shouldBe('stepDown("-.1", .1, null)', '"-0.2"');
shouldBe('stepDown("-.2", .1, null)', '"-0.3"');
shouldBe('stepDown("-1.", .1, null)', '"-1.1"');
shouldBe('stepDown("-2.", .1, null)', '"-2.1"');
debug('');
debug('Invalid step value');
shouldBe('stepUp("0", "foo", null)', '"1"');
shouldBe('stepUp("1", "0", null)', '"2"');
shouldBe('stepUp("2", "-1", null)', '"3"');
debug('');
debug('Step=any');
shouldBe('stepUp("0", "any", null)', '"1"');
shouldBe('stepDown("0", "any", null)', '"-1"');
debug('');
debug('Step=any corner case');
shouldBe('stepUpExplicitBounds("0", "100", "any", "1.5", "1")', '"2.5"');
shouldBe('stepDownExplicitBounds("0", "100", "any", "1.5", "1")', '"0.5"');
debug('');
debug('Overflow/underflow');
shouldBe('stepDown("1", "1", "0")', '"0"');
shouldBe('stepDown("0", "1", "0")', '"0"');
shouldBe('stepDown("1", "1", "0", 2)', '"0"');
shouldBe('stepDown("1", "3.40282346e+38", "", 2)', '"-3.40282346e+38"');
shouldBe('stepUp("-1", "1", "0")', '"0"');
shouldBe('stepUp("0", "1", "0")', '"0"');
shouldBe('stepUp("-1", "1", "0", 2)', '"0"');
shouldBe('stepUp("1", "3.40282346e+38", "", 2)', '"3.40282346e+38"');
debug('');
debug('stepDown()/stepUp() for stepMismatch values');
shouldBe('stepUp("1", "2", "")', '"2"');
shouldBe('input.min = "0"; stepUp("9", "10", "")', '"10"');
shouldBe('stepDown("19", "10", "0")', '"10"');
shouldBe('stepUp("89", "10", "99")', '"90"');
shouldBe('stepUp("7", "300", "")', '"300"');
shouldBe('stepUp("-7", "300", "")', '"0"');
shouldBe('stepDown("7", "300", "")', '"0"');
shouldBe('stepDown("-7", "300", "")', '"-300"');
debug('');
debug('Huge value and small step');
shouldBe('input.min = ""; stepUp("1e+38", "1", "", 999)', '"1e+38"');
shouldBe('input.max = ""; stepDown("1e+38", "1", "", 999)', '"1e+38"');
debug('');
debug('Fractional numbers');
shouldBe('input.min = ""; stepUp("0", "0.33333333333333333", "", 3)', '"1"');
shouldBe('stepUp("1", "0.1", "", 10)', '"2"');
shouldBe('input.min = "0"; stepUp("0", "0.003921568627450980", "1", 255)', '"1"');
debug('');
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"');
shouldBe('stepUpExplicitBounds(-4, 4, 1, "")', '"1"');
shouldBe('stepDownExplicitBounds(-4, 4, 1, "")', '"-1"');
shouldBe('stepDownExplicitBounds(0, 4, 1, "")', '"0"');
shouldBe('stepUpExplicitBounds(-4, 0, 1, "")', '"0"');
shouldBe('stepDownExplicitBounds(1, 4, 1, "")', '"1"');
shouldBe('stepUpExplicitBounds(1, 4, 1, "")', '"1"');
shouldBe('stepDownExplicitBounds(-4, -1, 1, "")', '"-1"');
shouldBe('stepUpExplicitBounds(-4, -1, 1, "")', '"-1"');
shouldBe('stepUpExplicitBounds(-100, null, 3, "")', '"2"');
shouldBe('stepDownExplicitBounds(-100, null, 3, "")', '"-1"');
shouldBe('stepUpExplicitBounds(1, 4, 1, 0)', '"1"');
shouldBe('stepDownExplicitBounds(1, 4, 1, 0)', '"0"');
shouldBe('stepDownExplicitBounds(-4, -1, 1, 0)', '"-1"');
shouldBe('stepUpExplicitBounds(-4, -1, 1, 0)', '"0"');
shouldBe('stepUpExplicitBounds(-100, null, 3, 3)', '"5"');
shouldBe('stepDownExplicitBounds(-100, null, 3, 3)', '"2"');
debug('');
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>