test_bug395107.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=395107
  5. -->
  6. <head>
  7. <title>Test for Bug 395107</title>
  8. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  10. </head>
  11. <body>
  12. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=395107">Mozilla Bug 395107</a>
  13. <p id="display"></p>
  14. <div id="content" style="display: none">
  15. </div>
  16. <pre id="test">
  17. <script class="testbody" type="text/javascript">
  18. /** Test for Bug 395107 **/
  19. var testNumber = 0;
  20. function assertSelected(aOption, aExpectDefaultSelected, aExpectSelected) {
  21. ++testNumber;
  22. is(aOption.defaultSelected, aExpectDefaultSelected,
  23. "Asserting default-selected state for option " + testNumber);
  24. is(aOption.selected, aExpectSelected,
  25. "Asserting selected state for option " + testNumber);
  26. }
  27. function assertSame(aSel1, aSel2Str, aTestNumber) {
  28. var div = document.createElement("div");
  29. div.innerHTML = aSel2Str;
  30. sel2 = div.firstChild;
  31. is(aSel1.options.length, sel2.options.length,
  32. "Length should be same in select test " + aTestNumber);
  33. is(aSel1.selectedIndex, sel2.selectedIndex,
  34. "Selected index should be same in select test " + aTestNumber);
  35. for (var i = 0; i < aSel1.options.length; ++i) {
  36. is(aSel1.options[i].selected, sel2.options[i].selected,
  37. "Options[" + i + "].selected should be the same in select test " +
  38. aTestNumber);
  39. is(aSel1.options[i].defaultSelected, sel2.options[i].defaultSelected,
  40. "Options[" + i +
  41. "].defaultSelected should be the same in select test " +
  42. aTestNumber);
  43. }
  44. }
  45. // In a single-select, setting an option selected should deselect an
  46. // existing selected option.
  47. var sel = document.createElement("select");
  48. sel.appendChild(new Option());
  49. is(sel.selectedIndex, 0, "First option should be selected");
  50. assertSelected(sel.firstChild, false, true);
  51. sel.appendChild(new Option());
  52. is(sel.selectedIndex, 0, "First option should still be selected");
  53. assertSelected(sel.firstChild, false, true);
  54. assertSelected(sel.firstChild.nextSibling, false, false);
  55. opt = new Option();
  56. sel.appendChild(opt);
  57. opt.defaultSelected = true;
  58. assertSelected(sel.firstChild, false, false);
  59. assertSelected(sel.firstChild.nextSibling, false, false);
  60. assertSelected(opt, true, true);
  61. is(opt, sel.firstChild.nextSibling.nextSibling, "What happened here?");
  62. is(sel.options[0], sel.firstChild, "Unexpected option 0");
  63. is(sel.options[1], sel.firstChild.nextSibling, "Unexpected option 1");
  64. is(sel.options[2], opt, "Unexpected option 2");
  65. is(sel.selectedIndex, 2, "Unexpected selectedIndex in select test 1");
  66. assertSame(sel, "<select><option><option><option selected></select>", 1);
  67. // Same, but with the option that gets set selected earlier in the select
  68. sel = document.createElement("select");
  69. sel.appendChild(new Option());
  70. sel.appendChild(new Option());
  71. opt = new Option();
  72. opt.defaultSelected = true;
  73. sel.appendChild(opt);
  74. opt = new Option();
  75. sel.options[0] = opt;
  76. opt.defaultSelected = true;
  77. assertSelected(sel.options[0], true, true);
  78. assertSelected(sel.options[1], false, false);
  79. assertSelected(sel.options[2], true, false);
  80. is(sel.selectedIndex, 0, "Unexpected selectedIndex in select test 2");
  81. // And now try unselecting options
  82. sel = document.createElement("select");
  83. sel.appendChild(new Option());
  84. opt = new Option();
  85. opt.defaultSelected = true;
  86. sel.appendChild(opt);
  87. sel.appendChild(new Option());
  88. opt.defaultSelected = false;
  89. assertSelected(sel.options[0], false, true);
  90. assertSelected(sel.options[1], false, false);
  91. assertSelected(sel.options[2], false, false);
  92. is(sel.selectedIndex, 0, "Unexpected selectedIndex in select test 2");
  93. </script>
  94. </pre>
  95. </body>
  96. </html>