tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_progress_element.html (8249B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=514437
      5 https://bugzilla.mozilla.org/show_bug.cgi?id=633913
      6 -->
      7 <head>
      8  <title>Test for progress element content and layout</title>
      9  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     10  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     11 </head>
     12 <body>
     13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=514437">Mozilla Bug 514437</a>
     14 and
     15 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=633913">Mozilla Bug 633913</a>
     16 <p id="display"></p>
     17 <iframe name="submit_frame" style="visibility: hidden;"></iframe>
     18 <div id="content" style="visibility: hidden;">
     19  <form id='f' method='get' target='submit_frame' action='foo'>
     20    <progress id='p'></progress>
     21  </form>
     22 </div>
     23 <pre id="test">
     24 <script type="application/javascript">
     25 
     26 SimpleTest.expectAssertions(0, 1);
     27 
     28 /** Test for progress element content and layout */
     29 
     30 function checkFormIDLAttribute(aElement)
     31 {
     32  is("form" in aElement, false, "<progress> shouldn't have a form attribute");
     33 }
     34 
     35 function checkAttribute(aElement, aAttribute, aNewValue, aExpectedValueForIDL)
     36 {
     37  var expectedValueForIDL = aNewValue;
     38  var expectedValueForContent = String(aNewValue);
     39 
     40  if (aExpectedValueForIDL !== undefined) {
     41    expectedValueForIDL = aExpectedValueForIDL;
     42  }
     43 
     44  if (aNewValue != null) {
     45    aElement.setAttribute(aAttribute, aNewValue);
     46    is(aElement.getAttribute(aAttribute), expectedValueForContent,
     47       aAttribute + " content attribute should be " + expectedValueForContent);
     48    is(aElement[aAttribute], expectedValueForIDL,
     49       aAttribute + " IDL attribute should be " + expectedValueForIDL);
     50 
     51    if (parseFloat(aNewValue) == aNewValue) {
     52      aElement[aAttribute] = aNewValue;
     53      is(aElement.getAttribute(aAttribute), expectedValueForContent,
     54         aAttribute + " content attribute should be " + expectedValueForContent);
     55      is(aElement[aAttribute], parseFloat(expectedValueForIDL),
     56         aAttribute + " IDL attribute should be " + parseFloat(expectedValueForIDL));
     57    }
     58  } else {
     59    aElement.removeAttribute(aAttribute);
     60    is(aElement.getAttribute(aAttribute), null,
     61       aAttribute + " content attribute should be null");
     62    is(aElement[aAttribute], expectedValueForIDL,
     63       aAttribute + " IDL attribute should be " + expectedValueForIDL);
     64  }
     65 }
     66 
     67 function checkValueAttribute()
     68 {
     69  var tests = [
     70    // value has to be a valid float, its default value is 0.0 otherwise.
     71    [ null, 0.0 ],
     72    [ 'fo', 0.0 ],
     73    // If value < 0.0, 0.0 is used instead.
     74    [ -1.0, 0.0 ],
     75    // If value >= max, max is used instead (max default value is 1.0).
     76    [  2.0, 1.0 ],
     77    [  1.0, 0.5, 0.5 ],
     78    [  10.0, 5.0, 5.0 ],
     79    [ 13.37, 13.37, 42.0 ],
     80    // Regular reflection.
     81    [  0.0 ],
     82    [  0.5 ],
     83    [  1.0 ],
     84    // Check double-precision value.
     85    [  0.234567898765432 ],
     86  ];
     87 
     88  var element = document.createElement('progress');
     89 
     90  for (var test of tests) {
     91    if (test[2]) {
     92      element.setAttribute('max', test[2]);
     93    }
     94 
     95    checkAttribute(element, 'value', test[0], test[1]);
     96 
     97    element.removeAttribute('max');
     98  }
     99 }
    100 
    101 function checkMaxAttribute()
    102 {
    103  var tests = [
    104    // max default value is 1.0.
    105    [ null, 1.0 ],
    106    // If value <= 0.0, 1.0 is used instead.
    107    [  0.0, 1.0 ],
    108    [ -1.0, 1.0 ],
    109    // Regular reflection.
    110    [  0.5 ],
    111    [  1.0 ],
    112    [  2.0 ],
    113    // Check double-precision value.
    114    [  0.234567898765432 ],
    115  ];
    116 
    117  var element = document.createElement('progress');
    118 
    119  for (var test of tests) {
    120    checkAttribute(element, 'max', test[0], test[1]);
    121  }
    122 }
    123 
    124 function checkPositionAttribute()
    125 {
    126  function checkPositionValue(aElement, aValue, aMax, aExpected) {
    127    if (aValue != null) {
    128      aElement.setAttribute('value', aValue);
    129    } else {
    130      aElement.removeAttribute('value');
    131    }
    132 
    133    if (aMax != null) {
    134      aElement.setAttribute('max', aMax);
    135    } else {
    136      aElement.removeAttribute('max');
    137    }
    138 
    139    is(aElement.position, aExpected, "position IDL attribute should be " + aExpected);
    140  }
    141 
    142  var tests = [
    143    // value has to be defined (indeterminate state).
    144    [ null, null, -1.0 ],
    145    [ null,  1.0, -1.0 ],
    146    // value has to be defined to a valid float (indeterminate state).
    147    [ 'foo', 1.0, -1.0 ],
    148    // If value < 0.0, 0.0 is used instead.
    149    [ -1.0,  1.0,  0.0 ],
    150    // If value >= max, max is used instead.
    151    [  2.0,  1.0,  1.0 ],
    152    // If max isn't present, max is set to 1.0.
    153    [ 1.0,  null,  1.0 ],
    154    // If max isn't a valid float, max is set to 1.0.
    155    [ 1.0, 'foo',  1.0 ],
    156    // If max isn't > 0, max is set to 1.0.
    157    [ 1.0,  -1.0,  1.0 ],
    158    // A few simple and valid values.
    159    [ 0.0,   1.0,  0.0 ],
    160    [ 0.1,   1.0,  0.1/1.0  ],
    161    [ 0.1,   2.0,  0.1/2.0  ],
    162    [  10,    50,  10/50 ],
    163    // Values implying .position is a double.
    164    [ 1.0,   3.0,  1.0/3.0 ],
    165    [ 0.1,   0.7,  0.1/0.7  ],
    166  ];
    167 
    168  var element = document.createElement('progress');
    169 
    170  for (var test of tests) {
    171    checkPositionValue(element, test[0], test[1], test[2], test[3]);
    172  }
    173 }
    174 
    175 function checkIndeterminatePseudoClass()
    176 {
    177  function checkIndeterminate(aElement, aValue, aMax, aIndeterminate) {
    178    if (aValue != null) {
    179      aElement.setAttribute('value', aValue);
    180    } else {
    181      aElement.removeAttribute('value');
    182    }
    183 
    184    if (aMax != null) {
    185      aElement.setAttribute('max', aMax);
    186    } else {
    187      aElement.removeAttribute('max');
    188    }
    189 
    190    is(aElement.matches("progress:indeterminate"), aIndeterminate,
    191       "<progress> indeterminate state should be " + aIndeterminate);
    192  }
    193 
    194  var tests = [
    195    // Indeterminate state: (value is undefined, or not a float)
    196    // value has to be defined (indeterminate state).
    197    [ null, null, true ],
    198    [ null,  1.0, true ],
    199    [ 'foo', 1.0, true ],
    200    // Determined state:
    201    [ -1.0,  1.0, false ],
    202    [  2.0,  1.0, false ],
    203    [ 1.0,  null, false ],
    204    [ 1.0, 'foo', false ],
    205    [ 1.0,  -1.0, false ],
    206    [ 0.0,   1.0, false ],
    207  ];
    208 
    209  var element = document.createElement('progress');
    210 
    211  for (var test of tests) {
    212    checkIndeterminate(element, test[0], test[1], test[2]);
    213  }
    214 }
    215 
    216 function checkFormListedElement(aElement)
    217 {
    218  is(document.forms[0].elements.length, 0, "the form should have no element");
    219 }
    220 
    221 function checkLabelable(aElement)
    222 {
    223  var content = document.getElementById('content');
    224  var label = document.createElement('label');
    225 
    226  content.appendChild(label);
    227  label.appendChild(aElement);
    228  is(label.control, aElement, "progress should be labelable");
    229 
    230  // Cleaning-up.
    231  content.removeChild(label);
    232  content.appendChild(aElement);
    233 }
    234 
    235 function checkNotResetableAndFormSubmission(aElement)
    236 {
    237  // Creating an input element to check the submission worked.
    238  var form = document.forms[0];
    239  var input = document.createElement('input');
    240 
    241  input.name = 'a';
    242  input.value = 'tulip';
    243  form.appendChild(input);
    244 
    245  // Setting values.
    246  aElement.value = 42.0;
    247  aElement.max = 100.0;
    248 
    249  document.getElementsByName('submit_frame')[0].addEventListener("load", function() {
    250    is(frames.submit_frame.location.href,
    251      `${location.origin}/tests/dom/html/test/forms/foo?a=tulip`,
    252       "The progress element value should not be submitted");
    253 
    254    checkNotResetable();
    255  }, {once: true});
    256 
    257  form.submit();
    258 }
    259 
    260 function checkNotResetable()
    261 {
    262  // Try to reset the form.
    263  var form = document.forms[0];
    264  var element = document.getElementById('p');
    265 
    266  element.value = 3.0;
    267  element.max = 42.0;
    268 
    269  form.reset();
    270 
    271  SimpleTest.executeSoon(function() {
    272    is(element.value, 3.0, "progress.value should not have changed");
    273    is(element.max, 42.0, "progress.max should not have changed");
    274 
    275    SimpleTest.finish();
    276  });
    277 }
    278 
    279 SimpleTest.waitForExplicitFinish();
    280 
    281 var p = document.getElementById('p');
    282 
    283 ok(p instanceof HTMLProgressElement,
    284   "The progress element should be instance of HTMLProgressElement");
    285 is(p.constructor, HTMLProgressElement,
    286   "The progress element constructor should be HTMLProgressElement");
    287 
    288 checkFormIDLAttribute(p);
    289 
    290 checkValueAttribute();
    291 
    292 checkMaxAttribute();
    293 
    294 checkPositionAttribute();
    295 
    296 checkIndeterminatePseudoClass();
    297 
    298 checkFormListedElement(p);
    299 
    300 checkLabelable(p);
    301 
    302 checkNotResetableAndFormSubmission(p);
    303 
    304 </script>
    305 </pre>
    306 </body>
    307 </html>