|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 https://bugzilla.mozilla.org/show_bug.cgi?id=391994 |
|
5 --> |
|
6 <head> |
|
7 <title>Test for Bug 391994</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=391994">Mozilla Bug 391994</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 391994 **/ |
|
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 // Creation methods |
|
51 var opt = document.createElement("option"); |
|
52 assertSelected(opt, false, false); |
|
53 |
|
54 opt = new Option(); |
|
55 assertSelected(opt, false, false); |
|
56 |
|
57 // Setting of defaultSelected |
|
58 opt = new Option(); |
|
59 opt.setAttribute("selected", "selected"); |
|
60 assertSelected(opt, true, true); |
|
61 |
|
62 opt = new Option(); |
|
63 opt.defaultSelected = true; |
|
64 assertSelected(opt, true, true); |
|
65 is(opt.hasAttribute("selected"), true, "Attribute should be set"); |
|
66 is(opt.getAttribute("selected"), "", |
|
67 "Attribute should be set to empty string"); |
|
68 |
|
69 // Setting of selected |
|
70 opt = new Option(); |
|
71 opt.selected = false; |
|
72 assertSelected(opt, false, false); |
|
73 |
|
74 opt = new Option(); |
|
75 opt.selected = true; |
|
76 assertSelected(opt, false, true); |
|
77 |
|
78 // Interaction of selected and defaultSelected |
|
79 opt = new Option(); |
|
80 opt.selected; |
|
81 opt.setAttribute("selected", "selected"); |
|
82 assertSelected(opt, true, true); |
|
83 |
|
84 opt = new Option(); |
|
85 opt.selected = false; |
|
86 opt.setAttribute("selected", "selected"); |
|
87 assertSelected(opt, true, false); |
|
88 |
|
89 opt = new Option(); |
|
90 opt.setAttribute("selected", "selected"); |
|
91 opt.selected = true; |
|
92 opt.removeAttribute("selected"); |
|
93 assertSelected(opt, false, true); |
|
94 |
|
95 // First test of putting things in a <select>: Adding default-selected option |
|
96 // should select it. |
|
97 var sel = document.createElement("select"); |
|
98 sel.appendChild(new Option()); |
|
99 is(sel.selectedIndex, 0, "First option should be selected"); |
|
100 assertSelected(sel.firstChild, false, true); |
|
101 |
|
102 sel.appendChild(new Option()); |
|
103 is(sel.selectedIndex, 0, "First option should still be selected"); |
|
104 assertSelected(sel.firstChild, false, true); |
|
105 assertSelected(sel.firstChild.nextSibling, false, false); |
|
106 |
|
107 opt = new Option(); |
|
108 opt.defaultSelected = true; |
|
109 sel.appendChild(opt); |
|
110 assertSelected(sel.firstChild, false, false); |
|
111 assertSelected(sel.firstChild.nextSibling, false, false); |
|
112 assertSelected(opt, true, true); |
|
113 is(opt, sel.firstChild.nextSibling.nextSibling, "What happened here?"); |
|
114 is(sel.options[0], sel.firstChild, "Unexpected option 0"); |
|
115 is(sel.options[1], sel.firstChild.nextSibling, "Unexpected option 1"); |
|
116 is(sel.options[2], opt, "Unexpected option 2"); |
|
117 is(sel.selectedIndex, 2, "Unexpected selectedIndex in select test 1"); |
|
118 |
|
119 assertSame(sel, "<select><option><option><option selected></select>", 1); |
|
120 |
|
121 // Second test of putting things in a <select>: Adding two default-selected |
|
122 // options should select the second one. |
|
123 sel = document.createElement("select"); |
|
124 sel.appendChild(new Option()); |
|
125 sel.appendChild(new Option()); |
|
126 opt = new Option(); |
|
127 opt.defaultSelected = true; |
|
128 sel.appendChild(opt); |
|
129 opt = new Option(); |
|
130 opt.defaultSelected = true; |
|
131 sel.appendChild(opt); |
|
132 assertSelected(sel.options[0], false, false); |
|
133 assertSelected(sel.options[1], false, false); |
|
134 assertSelected(sel.options[2], true, false); |
|
135 assertSelected(sel.options[3], true, true); |
|
136 is(sel.selectedIndex, 3, "Unexpected selectedIndex in select test 2"); |
|
137 |
|
138 assertSame(sel, |
|
139 "<select><option><option><option selected><option selected></select>", 2); |
|
140 |
|
141 // Third test of putting things in a <select>: adding a selected option earlier |
|
142 // than another selected option should make the new option selected. |
|
143 sel = document.createElement("select"); |
|
144 sel.appendChild(new Option()); |
|
145 sel.appendChild(new Option()); |
|
146 opt = new Option(); |
|
147 opt.defaultSelected = true; |
|
148 sel.appendChild(opt); |
|
149 opt = new Option(); |
|
150 opt.defaultSelected = true; |
|
151 sel.options[0] = opt; |
|
152 assertSelected(sel.options[0], true, true); |
|
153 assertSelected(sel.options[1], false, false); |
|
154 assertSelected(sel.options[2], true, false); |
|
155 is(sel.selectedIndex, 0, "Unexpected selectedIndex in select test 3"); |
|
156 |
|
157 // Fourth test of putting things in a <select>: Just like second test, but with |
|
158 // a <select multiple> |
|
159 sel = document.createElement("select"); |
|
160 sel.multiple = true; |
|
161 sel.appendChild(new Option()); |
|
162 sel.appendChild(new Option()); |
|
163 opt = new Option(); |
|
164 opt.defaultSelected = true; |
|
165 sel.appendChild(opt); |
|
166 opt = new Option(); |
|
167 opt.defaultSelected = true; |
|
168 sel.appendChild(opt); |
|
169 assertSelected(sel.options[0], false, false); |
|
170 assertSelected(sel.options[1], false, false); |
|
171 assertSelected(sel.options[2], true, true); |
|
172 assertSelected(sel.options[3], true, true); |
|
173 is(sel.selectedIndex, 2, "Unexpected selectedIndex in select test 4"); |
|
174 |
|
175 assertSame(sel, |
|
176 "<select multiple><option><option>" + |
|
177 "<option selected><option selected></select>", |
|
178 4); |
|
179 |
|
180 </script> |
|
181 </pre> |
|
182 </body> |
|
183 </html> |
|
184 |