test_bug395107.html (3737B)
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 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 16 </div> 17 <pre id="test"> 18 <script class="testbody" type="text/javascript"> 19 20 /** Test for Bug 395107 */ 21 var testNumber = 0; 22 23 function assertSelected(aOption, aExpectDefaultSelected, aExpectSelected) { 24 ++testNumber; 25 is(aOption.defaultSelected, aExpectDefaultSelected, 26 "Asserting default-selected state for option " + testNumber); 27 is(aOption.selected, aExpectSelected, 28 "Asserting selected state for option " + testNumber); 29 } 30 31 function assertSame(aSel1, aSel2Str, aTestNumber) { 32 var div = document.createElement("div"); 33 div.innerHTML = aSel2Str; 34 sel2 = div.firstChild; 35 is(aSel1.options.length, sel2.options.length, 36 "Length should be same in select test " + aTestNumber); 37 is(aSel1.selectedIndex, sel2.selectedIndex, 38 "Selected index should be same in select test " + aTestNumber); 39 for (var i = 0; i < aSel1.options.length; ++i) { 40 is(aSel1.options[i].selected, sel2.options[i].selected, 41 "Options[" + i + "].selected should be the same in select test " + 42 aTestNumber); 43 is(aSel1.options[i].defaultSelected, sel2.options[i].defaultSelected, 44 "Options[" + i + 45 "].defaultSelected should be the same in select test " + 46 aTestNumber); 47 } 48 } 49 50 // In a single-select, setting an option selected should deselect an 51 // existing selected option. 52 var sel = document.createElement("select"); 53 sel.appendChild(new Option()); 54 is(sel.selectedIndex, 0, "First option should be selected"); 55 assertSelected(sel.firstChild, false, true); 56 sel.appendChild(new Option()); 57 is(sel.selectedIndex, 0, "First option should still be selected"); 58 assertSelected(sel.firstChild, false, true); 59 assertSelected(sel.firstChild.nextSibling, false, false); 60 61 opt = new Option(); 62 sel.appendChild(opt); 63 opt.defaultSelected = true; 64 assertSelected(sel.firstChild, false, false); 65 assertSelected(sel.firstChild.nextSibling, false, false); 66 assertSelected(opt, true, true); 67 is(opt, sel.firstChild.nextSibling.nextSibling, "What happened here?"); 68 is(sel.options[0], sel.firstChild, "Unexpected option 0"); 69 is(sel.options[1], sel.firstChild.nextSibling, "Unexpected option 1"); 70 is(sel.options[2], opt, "Unexpected option 2"); 71 is(sel.selectedIndex, 2, "Unexpected selectedIndex in select test 1"); 72 73 assertSame(sel, "<select><option><option><option selected></select>", 1); 74 75 // Same, but with the option that gets set selected earlier in the select 76 sel = document.createElement("select"); 77 sel.appendChild(new Option()); 78 sel.appendChild(new Option()); 79 opt = new Option(); 80 opt.defaultSelected = true; 81 sel.appendChild(opt); 82 opt = new Option(); 83 sel.options[0] = opt; 84 opt.defaultSelected = true; 85 assertSelected(sel.options[0], true, true); 86 assertSelected(sel.options[1], false, false); 87 assertSelected(sel.options[2], true, false); 88 is(sel.selectedIndex, 0, "Unexpected selectedIndex in select test 2"); 89 90 // And now try unselecting options 91 sel = document.createElement("select"); 92 sel.appendChild(new Option()); 93 opt = new Option(); 94 opt.defaultSelected = true; 95 sel.appendChild(opt); 96 sel.appendChild(new Option()); 97 opt.defaultSelected = false; 98 99 assertSelected(sel.options[0], false, true); 100 assertSelected(sel.options[1], false, false); 101 assertSelected(sel.options[2], false, false); 102 is(sel.selectedIndex, 0, "Unexpected selectedIndex in select test 2"); 103 104 </script> 105 </pre> 106 </body> 107 </html>