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.

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

mercurial