<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<script>
description('Check stepUp() and stepDown() behavior for number type.');

var input = document.createElement('input');

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('');
debug('Invalid value');
shouldBe('stepUp("", null, null)', '"1"');
shouldBe('stepDown("", null, null)', '"-1"');

debug('');
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('');
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')
// Base/model/template tests
shouldBe('stepUp("0.1", 1, null)', '"1.1"');
shouldBe('stepUp("0.2", 1, null)', '"1.2"');
shouldBe('stepUp("1.0", 1, null)', '"2"');
shouldBe('stepUp("1.1", 1, null)', '"2.1"');
shouldBe('stepUp("1.2", 1, null)', '"2.2"');
shouldBe('stepUp("2.0", 1, null)', '"3"');

// Same as above, but with negative numbers.
debug('');
shouldBe('stepUp("-0.1", 1, null)', '"0.9"');
shouldBe('stepUp("-0.2", 1, null)', '"0.8"');
shouldBe('stepUp("-1.0", 1, null)', '"0"');
shouldBe('stepUp("-1.1", 1, null)', '"-0.1"');
shouldBe('stepUp("-1.2", 1, null)', '"-0.2"');
shouldBe('stepUp("-2.0", 1, null)', '"-1"');

// Same as above, but stepping down rather than up.
debug('');
shouldBe('stepDown("0.1", 1, null)', '"-0.9"');
shouldBe('stepDown("0.2", 1, null)', '"-0.8"');
shouldBe('stepDown("1.0", 1, null)', '"0"');
shouldBe('stepDown("1.1", 1, null)', '"0.1"');
shouldBe('stepDown("1.2", 1, null)', '"0.2"');
shouldBe('stepDown("2.0", 1, null)', '"1"');

debug('');
shouldBe('stepDown("-0.1", 1, null)', '"-1.1"');
shouldBe('stepDown("-0.2", 1, null)', '"-1.2"');
shouldBe('stepDown("-1.0", 1, null)', '"-2"');
shouldBe('stepDown("-1.1", 1, null)', '"-2.1"');
shouldBe('stepDown("-1.2", 1, null)', '"-2.2"');
shouldBe('stepDown("-2.0", 1, null)', '"-3"');

// Same as above, but with leading/trailing zeros removed.
debug('');
shouldBe('stepUp(".1", 1, null)', '"1.1"');
shouldBe('stepUp(".2", 1, null)', '"1.2"');
shouldBe('stepUp("1.", 1, null)', '"1"');
shouldBe('stepUp("2.", 1, null)', '"1"');

debug('');
shouldBe('stepUp("-.1", 1, null)', '"0.9"');
shouldBe('stepUp("-.2", 1, null)', '"0.8"');
shouldBe('stepUp("-1.", 1, null)', '"1"');
shouldBe('stepUp("-2.", 1, null)', '"1"');

debug('');
shouldBe('stepDown(".1", 1, null)', '"-0.9"');
shouldBe('stepDown(".2", 1, null)', '"-0.8"');
shouldBe('stepDown("1.", 1, null)', '"-1"');
shouldBe('stepDown("2.", 1, null)', '"-1"');

debug('');
shouldBe('stepDown("-.1", 1, null)', '"-1.1"');
shouldBe('stepDown("-.2", 1, null)', '"-1.2"');
shouldBe('stepDown("-1.", 1, null)', '"-1"');
shouldBe('stepDown("-2.", 1, null)', '"-1"');

// 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)', '"0.1"');
shouldBe('stepUp("2.", .1, null)', '"0.1"');

debug('');
shouldBe('stepUp("-.1", .1, null)', '"0"');
shouldBe('stepUp("-.2", .1, null)', '"-0.1"');
shouldBe('stepUp("-1.", .1, null)', '"0.1"');
shouldBe('stepUp("-2.", .1, null)', '"0.1"');

debug('');
shouldBe('stepDown(".1", .1, null)', '"0"');
shouldBe('stepDown(".2", .1, null)', '"0.1"');
shouldBe('stepDown("1.", .1, null)', '"-0.1"');
shouldBe('stepDown("2.", .1, null)', '"-0.1"');

debug('');
shouldBe('stepDown("-.1", .1, null)', '"-0.2"');
shouldBe('stepDown("-.2", .1, null)', '"-0.3"');
shouldBe('stepDown("-1.", .1, null)', '"-0.1"');
shouldBe('stepDown("-2.", .1, null)', '"-0.1"');

debug('');
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('');
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');
shouldThrowErrorName('stepUp("0", "any", null)', "InvalidStateError");
shouldThrowErrorName('stepDown("0", "any", null)', "InvalidStateError");

debug('');
debug('Step=any corner case');
shouldThrowErrorName('stepUpExplicitBounds("0", "100", "any", "1.5", "1")', "InvalidStateError");
shouldThrowErrorName('stepDownExplicitBounds("0", "100", "any", "1.5", "1")', "InvalidStateError");

debug('');
debug('Overflow/underflow');
shouldBe('stepDown("1", "1", "0")', '"0"');
shouldBe('stepDown("0", "1", "0")', '"0"');
shouldBe('stepDown("1", "1", "0", 2)', '"0"');
shouldBe('input.value', '"0"');
shouldBeEqualToString('stepDown("1", "1.797693134862315e+308", "", 2)', '-1.797693134862315e+308');
shouldBe('stepUp("-1", "1", "0")', '"0"');
shouldBe('stepUp("0", "1", "0")', '"0"');
shouldBe('stepUp("-1", "1", "0", 2)', '"0"');
shouldBe('input.value', '"0"');
shouldBeEqualToString('stepUp("1", "1.797693134862315e+308", "", 2)', '1.797693134862315e+308');

debug('');
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('');
debug('Huge value and small step');
shouldBe('input.min = ""; stepUp("1e+308", "1", "", 999999)', '"1e+308"');
shouldBe('input.max = ""; stepDown("1e+308", "1", "", 999999)', '"1e+308"');

debug('');
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('');
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"');

debug('');
</script>
</body>
</html>
