toolkit/content/tests/chrome/test_textbox_number.xul

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 <?xml version="1.0"?>
michael@0 2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
michael@0 3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
michael@0 4 <!--
michael@0 5 XUL Widget Test for textbox type="number"
michael@0 6 -->
michael@0 7 <window title="Textbox type='number' test" width="500" height="600"
michael@0 8 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
michael@0 9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 10 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
michael@0 11
michael@0 12 <hbox>
michael@0 13 <textbox id="n1" type="number" size="4"/>
michael@0 14 <textbox id="n2" type="number" value="10" min="5" max="15" wraparound="true"/>
michael@0 15 </hbox>
michael@0 16 <hbox>
michael@0 17 <textbox id="n3" type="number" size="4" value="25" min="1" max="12" increment="3"/>
michael@0 18 </hbox>
michael@0 19 <hbox>
michael@0 20 <textbox id="n4" type="number" size="4" value="-2" min="-8" max="18"/>
michael@0 21 <textbox id="n5" type="number" value="-17" min="-10" max="-3"/>
michael@0 22 </hbox>
michael@0 23 <hbox>
michael@0 24 <textbox id="n6" type="number" size="4" value="9" min="12" max="8"/>
michael@0 25 </hbox>
michael@0 26 <hbox>
michael@0 27 <textbox id="n7" type="number" size="4" value="4.678" min="2" max="10.5" decimalplaces="2"/>
michael@0 28 <textbox id="n8" type="number" hidespinbuttons="true"/>
michael@0 29 </hbox>
michael@0 30 <hbox>
michael@0 31 <textbox id="n9" type="number" size="4" oninput="updateInputEventCount();"/>
michael@0 32 </hbox>
michael@0 33
michael@0 34 <!-- test results are displayed in the html:body -->
michael@0 35 <body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/>
michael@0 36
michael@0 37 <!-- test code goes here -->
michael@0 38 <script type="application/javascript"><![CDATA[
michael@0 39 SimpleTest.waitForExplicitFinish();
michael@0 40
michael@0 41 // ---- NOTE: the numbers used in these tests are carefully chosen to avoid
michael@0 42 // ---- floating point rounding issues
michael@0 43
michael@0 44 function doTests() {
michael@0 45 var n1 = $("n1");
michael@0 46 var n2 = $("n2");
michael@0 47 var n3 = $("n3");
michael@0 48 var n4 = $("n4");
michael@0 49 var n5 = $("n5");
michael@0 50 var n6 = $("n6");
michael@0 51 var n7 = $("n7");
michael@0 52
michael@0 53 testValsMinMax(n1, "initial n1", 0, 0, Infinity);
michael@0 54 testValsMinMax(n2, "initial n2", 10, 5, 15);
michael@0 55 testValsMinMax(n3, "initial n3", 12, 1, 12);
michael@0 56 testValsMinMax(n4, "initial n4", -2, -8, 18);
michael@0 57 testValsMinMax(n5, "initial n5", -10, -10, -3);
michael@0 58 testValsMinMax(n6, "initial n6", 12, 12, 12);
michael@0 59 testValsMinMax(n7, "initial n7", 4.68, 2, 10.5); // value should be rounded
michael@0 60
michael@0 61 ok(n1.spinButtons != null && n1.spinButtons.localName == "spinbuttons", "spinButtons set");
michael@0 62 isnot(n1.decimalSymbol, "", "n1.decimalSymbol is set to something");
michael@0 63 n1.decimalSymbol = ".";
michael@0 64 SimpleTest.ise(n1.decimalSymbol, ".", "n1.decimalSymbol set to '.'");
michael@0 65 SimpleTest.ise(n1.wrapAround, false, "wrapAround defaults to false");
michael@0 66 SimpleTest.ise(n1.increment, 1, "increment defaults to 1");
michael@0 67 SimpleTest.ise(n1.decimalPlaces, 0, "decimalPlaces defaults to 0");
michael@0 68
michael@0 69 SimpleTest.ise(n2.wrapAround, true, "wrapAround when set to true");
michael@0 70 SimpleTest.ise(n3.increment, 3, "increment when set to 1");
michael@0 71 SimpleTest.ise(n7.decimalPlaces, 2, "decimalPlaces when set to 2");
michael@0 72
michael@0 73 // test changing the value
michael@0 74 n1.value = "1700";
michael@0 75 testVals(n1, "set value,", 1700);
michael@0 76 n1.value = 1600;
michael@0 77 testVals(n1, "set value int,", 1600);
michael@0 78 n2.value = "2";
michael@0 79 testVals(n2, "set value below min,", 5);
michael@0 80 n2.value = 2;
michael@0 81 testVals(n2, "set value below min int,", 5);
michael@0 82 n2.value = 18;
michael@0 83 testVals(n2, "set value above max,", 15);
michael@0 84 n2.value = -6;
michael@0 85 testVals(n2, "set value below min negative,", 5);
michael@0 86 n5.value = -2;
michael@0 87 testVals(n5, "set value above max positive,", -3);
michael@0 88 n7.value = 5.999;
michael@0 89 testVals(n7, "set value to decimal,", 6, "6.00");
michael@0 90 n7.value = "1.42";
michael@0 91 testVals(n7, "set value to decimal below min,", 2.00, "2.00");
michael@0 92 n7.value = 24.1;
michael@0 93 testVals(n7, "set value to decimal above max,", 10.5, "10.50");
michael@0 94 n1.value = 4.75;
michael@0 95 testVals(n1, "set value to decimal round,", 5);
michael@0 96
michael@0 97 // test changing the valueNumber
michael@0 98 n1.valueNumber = 27;
michael@0 99 testVals(n1, "set valueNumber,", 27);
michael@0 100 n2.valueNumber = 1;
michael@0 101 testVals(n2, "set valueNumber below min,", 5);
michael@0 102 n2.valueNumber = 77;
michael@0 103 testVals(n2, "set valueNumber above max,", 15);
michael@0 104 n2.valueNumber = -5;
michael@0 105 testVals(n2, "set valueNumber below min negative,", 5);
michael@0 106 n5.valueNumber = -8;
michael@0 107 n5.valueNumber = -1;
michael@0 108 testVals(n5, "set valueNumber above max positive,", -3);
michael@0 109 n7.valueNumber = 8.23;
michael@0 110 testVals(n7, "set valueNumber to decimal,", 8.23);
michael@0 111 n7.valueNumber = 0.77;
michael@0 112 testVals(n7, "set valueNumber to decimal below min,", 2.00, "2.00");
michael@0 113 n7.valueNumber = 29.157;
michael@0 114 testVals(n7, "set valueNumber to decimal above max,", 10.5, "10.50");
michael@0 115 n1.value = 8.9;
michael@0 116 testVals(n1, "set valueNumber to decimal round,", 9);
michael@0 117
michael@0 118 // test changing the min
michael@0 119 n1.value = 6;
michael@0 120 n1.min = 8;
michael@0 121 testValsMinMax(n1, "set integer min,", 8, 8, Infinity);
michael@0 122 n7.value = 5.5;
michael@0 123 n7.min = 6.7;
michael@0 124 testValsMinMax(n7, "set decimal min,", 6.7, 6.7, 10.5, "6.70");
michael@0 125
michael@0 126 // test changing the max
michael@0 127 n1.value = 25;
michael@0 128 n1.max = 22;
michael@0 129 testValsMinMax(n1, "set integer max,", 22, 8, 22);
michael@0 130 n7.value = 10.2;
michael@0 131 n7.max = 10.1;
michael@0 132 testValsMinMax(n7, "set decimal max,", 10.1, 6.7, 10.1, "10.10");
michael@0 133
michael@0 134 // test decrease() and increase() methods
michael@0 135 testIncreaseDecrease(n1, "integer", 1, 0, 8, 22);
michael@0 136 testIncreaseDecrease(n7, "decimal", 1, 2, 6.7, 10.1);
michael@0 137 testIncreaseDecrease(n3, "integer with increment", 3, 0, 1, 12);
michael@0 138
michael@0 139 n7.min = 2.7;
michael@0 140 n7.value = 10.1;
michael@0 141 n7.increment = 4.3;
michael@0 142 SimpleTest.ise(n7.increment, 4.3, "increment changed");
michael@0 143 testIncreaseDecrease(n7, "integer with increment", 4.3, 2, 2.7, 10.1);
michael@0 144
michael@0 145 n2.value = n2.min;
michael@0 146 n2.decrease();
michael@0 147 testVals(n2, "integer wraparound decrease method", n2.max);
michael@0 148 n2.increase();
michael@0 149 testVals(n2, "integer wraparound decrease method", n2.min);
michael@0 150
michael@0 151 n7.wrapAround = true;
michael@0 152 SimpleTest.ise(n7.wrapAround, true, "change wrapAround");
michael@0 153 n7.value = n7.min + 0.01;
michael@0 154 n7.decrease();
michael@0 155 testVals(n7, "decimal wraparound decrease method", n7.max, n7.max.toFixed(2));
michael@0 156 n7.increase();
michael@0 157 testVals(n7, "decimal wraparound decrease method", n7.min, n7.min.toFixed(2));
michael@0 158
michael@0 159 n1.value = 22;
michael@0 160 n1.decimalPlaces = 3;
michael@0 161 testVals(n1, "set decimalPlaces 3", 22, "22.000");
michael@0 162 n1.value = 10.624;
michael@0 163 testVals(n1, "set decimalPlaces 3 set value,", 10.624);
michael@0 164 n1.decimalPlaces = 0;
michael@0 165 testVals(n1, "set decimalPlaces 0 set value,", 11);
michael@0 166 n1.decimalPlaces = Infinity;
michael@0 167 n1.value = 10.678123;
michael@0 168 testVals(n1, "set decimalPlaces Infinity set value,", 10.678123);
michael@0 169
michael@0 170 n1.decimalSymbol = ",";
michael@0 171 SimpleTest.ise(n1.decimalSymbol, ",", "n1.decimalSymbol set to ','");
michael@0 172 n1.value = "9.67";
michael@0 173 testVals(n1, "set decimalPlaces set value,", 9.67);
michael@0 174
michael@0 175 n1.decimalSymbol = ".";
michael@0 176 SimpleTest.ise(n1.decimalSymbol, ".", "n1.decimalSymbol set back to '.'");
michael@0 177 n1.decimalPlaces = 0;
michael@0 178
michael@0 179 // UI tests
michael@0 180 n1.min = 5;
michael@0 181 n1.max = 15;
michael@0 182 n1.value = 5;
michael@0 183 n1.focus();
michael@0 184
michael@0 185 var sb = n1.spinButtons;
michael@0 186 var sbbottom = sb.getBoundingClientRect().bottom - sb.getBoundingClientRect().top - 2;
michael@0 187
michael@0 188 synthesizeKey("VK_UP", {});
michael@0 189 testVals(n1, "key up", 6);
michael@0 190
michael@0 191 synthesizeKey("VK_DOWN", {});
michael@0 192 testVals(n1, "key down", 5);
michael@0 193
michael@0 194 synthesizeMouse(sb, 2, 2, {});
michael@0 195 testVals(n1, "spinbuttons up", 6);
michael@0 196 synthesizeMouse(sb, 2, sbbottom, {});
michael@0 197 testVals(n1, "spinbuttons down", 5);
michael@0 198
michael@0 199 n1.value = 15;
michael@0 200 synthesizeKey("VK_UP", {});
michael@0 201 testVals(n1, "key up at max", 15);
michael@0 202 synthesizeMouse(sb, 2, 2, {});
michael@0 203 testVals(n1, "spinbuttons up at max", 15);
michael@0 204
michael@0 205 n1.value = 5;
michael@0 206 synthesizeKey("VK_DOWN", {});
michael@0 207 testVals(n1, "key down at min", 5);
michael@0 208 synthesizeMouse(sb, 2, sbbottom, {});
michael@0 209 testVals(n1, "spinbuttons down at min", 5);
michael@0 210
michael@0 211 n1.wrapAround = true;
michael@0 212 n1.value = 15;
michael@0 213 synthesizeKey("VK_UP", {});
michael@0 214 testVals(n1, "key up wraparound at max", 5);
michael@0 215 n1.value = 5;
michael@0 216 synthesizeKey("VK_DOWN", {});
michael@0 217 testVals(n1, "key down wraparound at min", 15);
michael@0 218
michael@0 219 n1.value = 15;
michael@0 220 synthesizeMouse(sb, 2, 2, {});
michael@0 221 testVals(n1, "spinbuttons up wraparound at max", 5);
michael@0 222 n1.value = 5;
michael@0 223 synthesizeMouse(sb, 2, sbbottom, {});
michael@0 224 testVals(n1, "spinbuttons down wraparound at min", 15);
michael@0 225
michael@0 226 // check read only state
michael@0 227 n1.readOnly = true;
michael@0 228 n1.min = -10;
michael@0 229 n1.max = 15;
michael@0 230 n1.value = 12;
michael@0 231 // no events should fire and no changes should occur when the field is read only
michael@0 232 synthesizeKeyExpectEvent("VK_UP", { }, n1, "!change", "key up read only");
michael@0 233 is(n1.value, 12, "key up read only value");
michael@0 234 synthesizeKeyExpectEvent("VK_DOWN", { }, n1, "!change", "key down read only");
michael@0 235 is(n1.value, 12, "key down read only value");
michael@0 236
michael@0 237 synthesizeMouseExpectEvent(sb, 2, 2, { }, n1, "!change", "mouse up read only");
michael@0 238 is(n1.value, 12, "mouse up read only value");
michael@0 239 synthesizeMouseExpectEvent(sb, 2, sbbottom, { }, n1, "!change", "mouse down read only");
michael@0 240 is(n1.value, 12, "mouse down read only value");
michael@0 241
michael@0 242 n1.readOnly = false;
michael@0 243 n1.disabled = true;
michael@0 244 synthesizeMouseExpectEvent(sb, 2, 2, { }, n1, "!change", "mouse up disabled");
michael@0 245 is(n1.value, 12, "mouse up disabled value");
michael@0 246 synthesizeMouseExpectEvent(sb, 2, sbbottom, { }, n1, "!change", "mouse down disabled");
michael@0 247 is(n1.value, 12, "mouse down disabled value");
michael@0 248
michael@0 249 var nsbrect = $("n8").spinButtons.getBoundingClientRect();
michael@0 250 ok(nsbrect.left == 0 && nsbrect.top == 0 && nsbrect.right == 0, nsbrect.bottom == 0,
michael@0 251 "hidespinbuttons");
michael@0 252
michael@0 253 var n9 = $("n9");
michael@0 254 is(n9.value, 0, "initial value");
michael@0 255 n9.select();
michael@0 256 synthesizeKey("4", {});
michael@0 257 is(inputEventCount, 1, "input event count");
michael@0 258 is(inputEventValue, "4", "input value");
michael@0 259 is(n9.value, "4", "updated value");
michael@0 260 synthesizeKey("2", {});
michael@0 261 is(inputEventCount, 2, "input event count");
michael@0 262 is(inputEventValue, "42", "input value");
michael@0 263 is(n9.value, "42", "updated value");
michael@0 264 synthesizeKey("VK_BACK_SPACE", {});
michael@0 265 is(inputEventCount, 3, "input event count");
michael@0 266 is(inputEventValue, "4", "input value");
michael@0 267 is(n9.value, "4", "updated value");
michael@0 268 synthesizeKey("A", {accelKey: true});
michael@0 269 synthesizeKey("VK_DELETE", {});
michael@0 270 is(inputEventCount, 4, "input event count");
michael@0 271 is(inputEventValue, "0", "input value");
michael@0 272 is(n9.value, "0", "updated value");
michael@0 273
michael@0 274 SimpleTest.finish();
michael@0 275 }
michael@0 276
michael@0 277 var inputEventCount = 0;
michael@0 278 var inputEventValue = null;
michael@0 279 function updateInputEventCount() {
michael@0 280 inputEventValue = $("n9").value;
michael@0 281 inputEventCount++;
michael@0 282 };
michael@0 283
michael@0 284 function testVals(nb, name, valueNumber, valueFieldNumber) {
michael@0 285 if (valueFieldNumber === undefined)
michael@0 286 valueFieldNumber = "" + valueNumber;
michael@0 287
michael@0 288 SimpleTest.ise(nb.value, "" + valueNumber, name + " value is '" + valueNumber + "'");
michael@0 289 SimpleTest.ise(nb.valueNumber, valueNumber, name + " valueNumber is " + valueNumber);
michael@0 290
michael@0 291 // This value format depends on the localized decimal symbol.
michael@0 292 var localizedValue = valueFieldNumber.replace(/\./, nb.decimalSymbol);
michael@0 293 SimpleTest.ise(nb.inputField.value, localizedValue,
michael@0 294 name + " inputField value is '" + localizedValue + "'");
michael@0 295 }
michael@0 296
michael@0 297 function testValsMinMax(nb, name, valueNumber, min, max, valueFieldNumber) {
michael@0 298 testVals(nb, name, valueNumber, valueFieldNumber);
michael@0 299 SimpleTest.ise(nb.min, min, name + " min is " + min);
michael@0 300 SimpleTest.ise(nb.max, max, name + " max is " + max);
michael@0 301 }
michael@0 302
michael@0 303 function testIncreaseDecrease(nb, testid, increment, fixedCount, min, max)
michael@0 304 {
michael@0 305 testid += " ";
michael@0 306
michael@0 307 nb.value = max;
michael@0 308 nb.decrease();
michael@0 309 testVals(nb, testid + "decrease method", max - increment,
michael@0 310 (max - increment).toFixed(fixedCount));
michael@0 311 nb.increase();
michael@0 312 testVals(nb, testid + "increase method", max, max.toFixed(fixedCount));
michael@0 313 nb.value = min;
michael@0 314 nb.decrease();
michael@0 315 testVals(nb, testid + "decrease method at min", min, min.toFixed(fixedCount));
michael@0 316 nb.value = max;
michael@0 317 nb.increase();
michael@0 318 testVals(nb, testid + "increase method at max", max, max.toFixed(fixedCount));
michael@0 319
michael@0 320 nb.focus();
michael@0 321 nb.value = min;
michael@0 322
michael@0 323 // pressing the cursor up and down keys should adjust the value
michael@0 324 synthesizeKeyExpectEvent("VK_UP", { }, nb, "change", testid + "key up");
michael@0 325 is(nb.value, min + increment, testid + "key up");
michael@0 326 nb.value = max;
michael@0 327 synthesizeKeyExpectEvent("VK_UP", { }, nb, "!change", testid + "key up at max");
michael@0 328 is(nb.value, max, testid + "key up at max");
michael@0 329 synthesizeKeyExpectEvent("VK_DOWN", { }, nb, "change", testid + "key down");
michael@0 330 is(nb.value, max - increment, testid + "key down");
michael@0 331 nb.value = min;
michael@0 332 synthesizeKeyExpectEvent("VK_DOWN", { }, nb, "!change", testid + "key down at min");
michael@0 333 is(nb.value, min, testid + "key down at min");
michael@0 334
michael@0 335 // check pressing the spinbutton arrows
michael@0 336 var sb = nb.spinButtons;
michael@0 337 var sbbottom = sb.getBoundingClientRect().bottom - sb.getBoundingClientRect().top - 2;
michael@0 338 nb.value = min;
michael@0 339 synthesizeMouseExpectEvent(sb, 2, 2, { }, nb, "change", testid + "mouse up");
michael@0 340 is(nb.value, min + increment, testid + "mouse up");
michael@0 341 nb.value = max;
michael@0 342 synthesizeMouseExpectEvent(sb, 2, 2, { }, nb, "!change", testid + "mouse up at max");
michael@0 343 synthesizeMouseExpectEvent(sb, 2, sbbottom, { }, nb, "change", testid + "mouse down");
michael@0 344 is(nb.value, max - increment, testid + "mouse down");
michael@0 345 nb.value = min;
michael@0 346 synthesizeMouseExpectEvent(sb, 2, sbbottom, { }, nb, "!change", testid + "mouse down at min");
michael@0 347 }
michael@0 348
michael@0 349 SimpleTest.waitForFocus(doTests);
michael@0 350
michael@0 351 ]]></script>
michael@0 352
michael@0 353 </window>

mercurial