gecko/toolkit/content/tests/chrome/test_textbox_number.xul

354 lines
12 KiB
XML

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
<!--
XUL Widget Test for textbox type="number"
-->
<window title="Textbox type='number' test" width="500" height="600"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<hbox>
<textbox id="n1" type="number" size="4"/>
<textbox id="n2" type="number" value="10" min="5" max="15" wraparound="true"/>
</hbox>
<hbox>
<textbox id="n3" type="number" size="4" value="25" min="1" max="12" increment="3"/>
</hbox>
<hbox>
<textbox id="n4" type="number" size="4" value="-2" min="-8" max="18"/>
<textbox id="n5" type="number" value="-17" min="-10" max="-3"/>
</hbox>
<hbox>
<textbox id="n6" type="number" size="4" value="9" min="12" max="8"/>
</hbox>
<hbox>
<textbox id="n7" type="number" size="4" value="4.678" min="2" max="10.5" decimalplaces="2"/>
<textbox id="n8" type="number" hidespinbuttons="true"/>
</hbox>
<hbox>
<textbox id="n9" type="number" size="4" oninput="updateInputEventCount();"/>
</hbox>
<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/>
<!-- test code goes here -->
<script type="application/javascript"><![CDATA[
SimpleTest.waitForExplicitFinish();
// ---- NOTE: the numbers used in these tests are carefully chosen to avoid
// ---- floating point rounding issues
function doTests() {
var n1 = $("n1");
var n2 = $("n2");
var n3 = $("n3");
var n4 = $("n4");
var n5 = $("n5");
var n6 = $("n6");
var n7 = $("n7");
testValsMinMax(n1, "initial n1", 0, 0, Infinity);
testValsMinMax(n2, "initial n2", 10, 5, 15);
testValsMinMax(n3, "initial n3", 12, 1, 12);
testValsMinMax(n4, "initial n4", -2, -8, 18);
testValsMinMax(n5, "initial n5", -10, -10, -3);
testValsMinMax(n6, "initial n6", 12, 12, 12);
testValsMinMax(n7, "initial n7", 4.68, 2, 10.5); // value should be rounded
ok(n1.spinButtons != null && n1.spinButtons.localName == "spinbuttons", "spinButtons set");
isnot(n1.decimalSymbol, "", "n1.decimalSymbol is set to something");
n1.decimalSymbol = ".";
SimpleTest.ise(n1.decimalSymbol, ".", "n1.decimalSymbol set to '.'");
SimpleTest.ise(n1.wrapAround, false, "wrapAround defaults to false");
SimpleTest.ise(n1.increment, 1, "increment defaults to 1");
SimpleTest.ise(n1.decimalPlaces, 0, "decimalPlaces defaults to 0");
SimpleTest.ise(n2.wrapAround, true, "wrapAround when set to true");
SimpleTest.ise(n3.increment, 3, "increment when set to 1");
SimpleTest.ise(n7.decimalPlaces, 2, "decimalPlaces when set to 2");
// test changing the value
n1.value = "1700";
testVals(n1, "set value,", 1700);
n1.value = 1600;
testVals(n1, "set value int,", 1600);
n2.value = "2";
testVals(n2, "set value below min,", 5);
n2.value = 2;
testVals(n2, "set value below min int,", 5);
n2.value = 18;
testVals(n2, "set value above max,", 15);
n2.value = -6;
testVals(n2, "set value below min negative,", 5);
n5.value = -2;
testVals(n5, "set value above max positive,", -3);
n7.value = 5.999;
testVals(n7, "set value to decimal,", 6, "6.00");
n7.value = "1.42";
testVals(n7, "set value to decimal below min,", 2.00, "2.00");
n7.value = 24.1;
testVals(n7, "set value to decimal above max,", 10.5, "10.50");
n1.value = 4.75;
testVals(n1, "set value to decimal round,", 5);
// test changing the valueNumber
n1.valueNumber = 27;
testVals(n1, "set valueNumber,", 27);
n2.valueNumber = 1;
testVals(n2, "set valueNumber below min,", 5);
n2.valueNumber = 77;
testVals(n2, "set valueNumber above max,", 15);
n2.valueNumber = -5;
testVals(n2, "set valueNumber below min negative,", 5);
n5.valueNumber = -8;
n5.valueNumber = -1;
testVals(n5, "set valueNumber above max positive,", -3);
n7.valueNumber = 8.23;
testVals(n7, "set valueNumber to decimal,", 8.23);
n7.valueNumber = 0.77;
testVals(n7, "set valueNumber to decimal below min,", 2.00, "2.00");
n7.valueNumber = 29.157;
testVals(n7, "set valueNumber to decimal above max,", 10.5, "10.50");
n1.value = 8.9;
testVals(n1, "set valueNumber to decimal round,", 9);
// test changing the min
n1.value = 6;
n1.min = 8;
testValsMinMax(n1, "set integer min,", 8, 8, Infinity);
n7.value = 5.5;
n7.min = 6.7;
testValsMinMax(n7, "set decimal min,", 6.7, 6.7, 10.5, "6.70");
// test changing the max
n1.value = 25;
n1.max = 22;
testValsMinMax(n1, "set integer max,", 22, 8, 22);
n7.value = 10.2;
n7.max = 10.1;
testValsMinMax(n7, "set decimal max,", 10.1, 6.7, 10.1, "10.10");
// test decrease() and increase() methods
testIncreaseDecrease(n1, "integer", 1, 0, 8, 22);
testIncreaseDecrease(n7, "decimal", 1, 2, 6.7, 10.1);
testIncreaseDecrease(n3, "integer with increment", 3, 0, 1, 12);
n7.min = 2.7;
n7.value = 10.1;
n7.increment = 4.3;
SimpleTest.ise(n7.increment, 4.3, "increment changed");
testIncreaseDecrease(n7, "integer with increment", 4.3, 2, 2.7, 10.1);
n2.value = n2.min;
n2.decrease();
testVals(n2, "integer wraparound decrease method", n2.max);
n2.increase();
testVals(n2, "integer wraparound decrease method", n2.min);
n7.wrapAround = true;
SimpleTest.ise(n7.wrapAround, true, "change wrapAround");
n7.value = n7.min + 0.01;
n7.decrease();
testVals(n7, "decimal wraparound decrease method", n7.max, n7.max.toFixed(2));
n7.increase();
testVals(n7, "decimal wraparound decrease method", n7.min, n7.min.toFixed(2));
n1.value = 22;
n1.decimalPlaces = 3;
testVals(n1, "set decimalPlaces 3", 22, "22.000");
n1.value = 10.624;
testVals(n1, "set decimalPlaces 3 set value,", 10.624);
n1.decimalPlaces = 0;
testVals(n1, "set decimalPlaces 0 set value,", 11);
n1.decimalPlaces = Infinity;
n1.value = 10.678123;
testVals(n1, "set decimalPlaces Infinity set value,", 10.678123);
n1.decimalSymbol = ",";
SimpleTest.ise(n1.decimalSymbol, ",", "n1.decimalSymbol set to ','");
n1.value = "9.67";
testVals(n1, "set decimalPlaces set value,", 9.67);
n1.decimalSymbol = ".";
SimpleTest.ise(n1.decimalSymbol, ".", "n1.decimalSymbol set back to '.'");
n1.decimalPlaces = 0;
// UI tests
n1.min = 5;
n1.max = 15;
n1.value = 5;
n1.focus();
var sb = n1.spinButtons;
var sbbottom = sb.getBoundingClientRect().bottom - sb.getBoundingClientRect().top - 2;
synthesizeKey("VK_UP", {});
testVals(n1, "key up", 6);
synthesizeKey("VK_DOWN", {});
testVals(n1, "key down", 5);
synthesizeMouse(sb, 2, 2, {});
testVals(n1, "spinbuttons up", 6);
synthesizeMouse(sb, 2, sbbottom, {});
testVals(n1, "spinbuttons down", 5);
n1.value = 15;
synthesizeKey("VK_UP", {});
testVals(n1, "key up at max", 15);
synthesizeMouse(sb, 2, 2, {});
testVals(n1, "spinbuttons up at max", 15);
n1.value = 5;
synthesizeKey("VK_DOWN", {});
testVals(n1, "key down at min", 5);
synthesizeMouse(sb, 2, sbbottom, {});
testVals(n1, "spinbuttons down at min", 5);
n1.wrapAround = true;
n1.value = 15;
synthesizeKey("VK_UP", {});
testVals(n1, "key up wraparound at max", 5);
n1.value = 5;
synthesizeKey("VK_DOWN", {});
testVals(n1, "key down wraparound at min", 15);
n1.value = 15;
synthesizeMouse(sb, 2, 2, {});
testVals(n1, "spinbuttons up wraparound at max", 5);
n1.value = 5;
synthesizeMouse(sb, 2, sbbottom, {});
testVals(n1, "spinbuttons down wraparound at min", 15);
// check read only state
n1.readOnly = true;
n1.min = -10;
n1.max = 15;
n1.value = 12;
// no events should fire and no changes should occur when the field is read only
synthesizeKeyExpectEvent("VK_UP", { }, n1, "!change", "key up read only");
is(n1.value, 12, "key up read only value");
synthesizeKeyExpectEvent("VK_DOWN", { }, n1, "!change", "key down read only");
is(n1.value, 12, "key down read only value");
synthesizeMouseExpectEvent(sb, 2, 2, { }, n1, "!change", "mouse up read only");
is(n1.value, 12, "mouse up read only value");
synthesizeMouseExpectEvent(sb, 2, sbbottom, { }, n1, "!change", "mouse down read only");
is(n1.value, 12, "mouse down read only value");
n1.readOnly = false;
n1.disabled = true;
synthesizeMouseExpectEvent(sb, 2, 2, { }, n1, "!change", "mouse up disabled");
is(n1.value, 12, "mouse up disabled value");
synthesizeMouseExpectEvent(sb, 2, sbbottom, { }, n1, "!change", "mouse down disabled");
is(n1.value, 12, "mouse down disabled value");
var nsbrect = $("n8").spinButtons.getBoundingClientRect();
ok(nsbrect.left == 0 && nsbrect.top == 0 && nsbrect.right == 0, nsbrect.bottom == 0,
"hidespinbuttons");
var n9 = $("n9");
is(n9.value, 0, "initial value");
n9.select();
synthesizeKey("4", {});
is(inputEventCount, 1, "input event count");
is(inputEventValue, "4", "input value");
is(n9.value, "4", "updated value");
synthesizeKey("2", {});
is(inputEventCount, 2, "input event count");
is(inputEventValue, "42", "input value");
is(n9.value, "42", "updated value");
synthesizeKey("VK_BACK_SPACE", {});
is(inputEventCount, 3, "input event count");
is(inputEventValue, "4", "input value");
is(n9.value, "4", "updated value");
synthesizeKey("A", {accelKey: true});
synthesizeKey("VK_DELETE", {});
is(inputEventCount, 4, "input event count");
is(inputEventValue, "0", "input value");
is(n9.value, "0", "updated value");
SimpleTest.finish();
}
var inputEventCount = 0;
var inputEventValue = null;
function updateInputEventCount() {
inputEventValue = $("n9").value;
inputEventCount++;
};
function testVals(nb, name, valueNumber, valueFieldNumber) {
if (valueFieldNumber === undefined)
valueFieldNumber = "" + valueNumber;
SimpleTest.ise(nb.value, "" + valueNumber, name + " value is '" + valueNumber + "'");
SimpleTest.ise(nb.valueNumber, valueNumber, name + " valueNumber is " + valueNumber);
// This value format depends on the localized decimal symbol.
var localizedValue = valueFieldNumber.replace(/\./, nb.decimalSymbol);
SimpleTest.ise(nb.inputField.value, localizedValue,
name + " inputField value is '" + localizedValue + "'");
}
function testValsMinMax(nb, name, valueNumber, min, max, valueFieldNumber) {
testVals(nb, name, valueNumber, valueFieldNumber);
SimpleTest.ise(nb.min, min, name + " min is " + min);
SimpleTest.ise(nb.max, max, name + " max is " + max);
}
function testIncreaseDecrease(nb, testid, increment, fixedCount, min, max)
{
testid += " ";
nb.value = max;
nb.decrease();
testVals(nb, testid + "decrease method", max - increment,
(max - increment).toFixed(fixedCount));
nb.increase();
testVals(nb, testid + "increase method", max, max.toFixed(fixedCount));
nb.value = min;
nb.decrease();
testVals(nb, testid + "decrease method at min", min, min.toFixed(fixedCount));
nb.value = max;
nb.increase();
testVals(nb, testid + "increase method at max", max, max.toFixed(fixedCount));
nb.focus();
nb.value = min;
// pressing the cursor up and down keys should adjust the value
synthesizeKeyExpectEvent("VK_UP", { }, nb, "change", testid + "key up");
is(nb.value, min + increment, testid + "key up");
nb.value = max;
synthesizeKeyExpectEvent("VK_UP", { }, nb, "!change", testid + "key up at max");
is(nb.value, max, testid + "key up at max");
synthesizeKeyExpectEvent("VK_DOWN", { }, nb, "change", testid + "key down");
is(nb.value, max - increment, testid + "key down");
nb.value = min;
synthesizeKeyExpectEvent("VK_DOWN", { }, nb, "!change", testid + "key down at min");
is(nb.value, min, testid + "key down at min");
// check pressing the spinbutton arrows
var sb = nb.spinButtons;
var sbbottom = sb.getBoundingClientRect().bottom - sb.getBoundingClientRect().top - 2;
nb.value = min;
synthesizeMouseExpectEvent(sb, 2, 2, { }, nb, "change", testid + "mouse up");
is(nb.value, min + increment, testid + "mouse up");
nb.value = max;
synthesizeMouseExpectEvent(sb, 2, 2, { }, nb, "!change", testid + "mouse up at max");
synthesizeMouseExpectEvent(sb, 2, sbbottom, { }, nb, "change", testid + "mouse down");
is(nb.value, max - increment, testid + "mouse down");
nb.value = min;
synthesizeMouseExpectEvent(sb, 2, sbbottom, { }, nb, "!change", testid + "mouse down at min");
}
SimpleTest.waitForFocus(doTests);
]]></script>
</window>