Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / script-tests / select-options-add.js
blob0703f1215b2ffa7e5fce4428938dae2171819e3b
1 description(
2 "This test checks the behavior of the add() method on the select.options object.<br>" +
3 "It covers both the the one-argument (1.x) and two-argument (2.x) signatures of the add() method."
4 );
6 div = document.createElement("div");
7 sel = document.createElement("select");
8 sel.setAttribute("id", "select1");
9 div.appendChild(sel);
10 sel = document.createElement("select");
11 sel.setAttribute("id", "select2");
12 div.appendChild(sel);
13 document.body.insertBefore(div, document.getElementById("console").nextSibling);
15 debug("1.1 Add Option to empty Options");
16 var select1 = document.getElementById("select1");
17 var option1 = document.createElement("OPTION");
18 select1.options.add(option1);
19 option1.value = "1";
20 option1.textContent = "A";
21 shouldBe("select1.options.length", "1");
22 shouldBe("select1.selectedIndex", "0");
23 shouldBe("select1.options[0].value", "'1'");
24 shouldBe("select1.options[0].textContent", "'A'");
25 debug("");
27 debug("1.2 Add Option to non-empty Options");
28 option1 = document.createElement("OPTION");
29 select1.options.add(option1);
30 option1.value = "2";
31 option1.textContent = "B";
32 shouldBe("select1.options.length", "2");
33 shouldBe("select1.selectedIndex", "0");
34 shouldBe("select1.options[0].value", "'1'");
35 shouldBe("select1.options[0].textContent", "'A'");
36 shouldBe("select1.options[1].value", "'2'");
37 shouldBe("select1.options[1].textContent", "'B'");
38 debug("");
40 debug("1.3 Add Option after setting parameters");
41 option1 = document.createElement("OPTION");
42 option1.value = "3";
43 option1.textContent = "C";
44 select1.options.add(option1);
45 shouldBe("select1.options.length", "3");
46 shouldBe("select1.selectedIndex", "0");
47 shouldBe("select1.options[0].value", "'1'");
48 shouldBe("select1.options[0].textContent", "'A'");
49 shouldBe("select1.options[1].value", "'2'");
50 shouldBe("select1.options[1].textContent", "'B'");
51 shouldBe("select1.options[2].value", "'3'");
52 shouldBe("select1.options[2].textContent", "'C'");
53 debug("");
55 debug("1.4 Add a non-Option element");
56 option1 = document.createElement("DIV");
57 shouldThrow("select1.options.add(option1)");
58 shouldBe("select1.options.length", "3");
59 shouldBe("select1.selectedIndex", "0");
60 debug("");
62 debug("1.5 Add a non-element (string)");
63 option1 = "o";
64 shouldThrow("select1.options.add(option1)");
65 shouldBe("select1.options.length", "3");
66 shouldBe("select1.selectedIndex", "0");
67 debug("");
69 debug("1.6 Add a non-element (number)");
70 option1 = 3.14;
71 shouldThrow("select1.options.add(option1)");
72 shouldBe("select1.options.length", "3");
73 shouldBe("select1.selectedIndex", "0");
74 debug("");
76 debug("1.7 Add a non-element (boolean)");
77 option1 = true;
78 shouldThrow("select1.options.add(option1)");
79 shouldBe("select1.options.length", "3");
80 shouldBe("select1.selectedIndex", "0");
81 debug("");
83 debug("1.8 Add undefined");
84 option1 = undefined;
85 shouldThrow("select1.options.add(option1)");
86 shouldBe("select1.options.length", "3");
87 shouldBe("select1.selectedIndex", "0");
88 debug("");
90 debug("1.9 Add null");
91 option1 = null;
92 shouldThrow("select1.options.add(option1)");
93 shouldBe("select1.options.length", "3");
94 shouldBe("select1.selectedIndex", "0");
95 debug("");
97 debug("1.10 Add negative infinity");
98 option1 = -1/0;
99 shouldThrow("select1.options.add(option1)");
100 shouldBe("select1.options.length", "3");
101 shouldBe("select1.selectedIndex", "0");
102 debug("");
104 debug("1.11 Add NaN");
105 option1 = 0/0;
106 shouldThrow("select1.options.add(option1)");
107 shouldBe("select1.options.length", "3");
108 shouldBe("select1.selectedIndex", "0");
109 debug("");
111 debug("1.12 Add positive infinity");
112 option1 = 1/0;
113 shouldThrow("select1.options.add(option1)");
114 shouldBe("select1.options.length", "3");
115 shouldBe("select1.selectedIndex", "0");
116 debug("");
118 debug("2.1 Add Option to empty Options");
119 var select2 = document.getElementById("select2");
120 var option2 = document.createElement("OPTION");
121 select2.options.add(option2, 0);
122 option2.value = "1";
123 option2.textContent = "A";
124 shouldBe("select2.options.length", "1");
125 shouldBe("select2.selectedIndex", "0");
126 shouldBe("select2.options[0].value", "'1'");
127 shouldBe("select2.options[0].textContent", "'A'");
128 debug("");
130 debug("2.2 Add Option after setting parameters");
131 option2 = document.createElement("OPTION");
132 option2.value = "2";
133 option2.textContent = "B";
134 select2.options.add(option2, 1);
135 shouldBe("select2.options.length", "2");
136 shouldBe("select2.selectedIndex", "0");
137 shouldBe("select2.options[0].value", "'1'");
138 shouldBe("select2.options[0].textContent", "'A'");
139 shouldBe("select2.options[1].value", "'2'");
140 shouldBe("select2.options[1].textContent", "'B'");
141 debug("");
143 debug("2.3 Insert Option at beginning of Options");
144 option2 = document.createElement("OPTION");
145 select2.options.add(option2, 0);
146 option2.value = "0";
147 option2.textContent = "Z";
148 shouldBe("select2.options.length", "3");
149 shouldBe("select2.selectedIndex", "1");
150 shouldBe("select2.options[0].value", "'0'");
151 shouldBe("select2.options[0].textContent", "'Z'");
152 shouldBe("select2.options[1].value", "'1'");
153 shouldBe("select2.options[1].textContent", "'A'");
154 shouldBe("select2.options[2].value", "'2'");
155 shouldBe("select2.options[2].textContent", "'B'");
156 debug("");
158 debug("2.4 Insert Option in middle of Options");
159 option2 = document.createElement("OPTION");
160 select2.options.add(option2, 2);
161 option2.value = "1.5";
162 option2.textContent = "A.5";
163 shouldBe("select2.options.length", "4");
164 shouldBe("select2.selectedIndex", "1");
165 shouldBe("select2.options[0].value", "'0'");
166 shouldBe("select2.options[0].textContent", "'Z'");
167 shouldBe("select2.options[1].value", "'1'");
168 shouldBe("select2.options[1].textContent", "'A'");
169 shouldBe("select2.options[2].value", "'1.5'");
170 shouldBe("select2.options[2].textContent", "'A.5'");
171 shouldBe("select2.options[3].value", "'2'");
172 shouldBe("select2.options[3].textContent", "'B'");
173 debug("");
175 debug("2.5 Insert Option at end of Options");
176 option2 = document.createElement("OPTION");
177 select2.options.add(option2, 4);
178 option2.value = "3";
179 option2.textContent = "C";
180 shouldBe("select2.options.length", "5");
181 shouldBe("select2.selectedIndex", "1");
182 shouldBe("select2.options[0].value", "'0'");
183 shouldBe("select2.options[0].textContent", "'Z'");
184 shouldBe("select2.options[1].value", "'1'");
185 shouldBe("select2.options[1].textContent", "'A'");
186 shouldBe("select2.options[2].value", "'1.5'");
187 shouldBe("select2.options[2].textContent", "'A.5'");
188 shouldBe("select2.options[3].value", "'2'");
189 shouldBe("select2.options[3].textContent", "'B'");
190 shouldBe("select2.options[4].value", "'3'");
191 shouldBe("select2.options[4].textContent", "'C'");
192 debug("");
194 debug("2.6 Insert Option beyond the end of Options");
195 option2 = document.createElement("OPTION");
196 select2.options.add(option2, 6);
197 option2.value = "4";
198 option2.textContent = "D";
199 shouldBe("select2.options.length", "6");
200 shouldBe("select2.selectedIndex", "1");
201 shouldBe("select2.options[0].value", "'0'");
202 shouldBe("select2.options[0].textContent", "'Z'");
203 shouldBe("select2.options[1].value", "'1'");
204 shouldBe("select2.options[1].textContent", "'A'");
205 shouldBe("select2.options[2].value", "'1.5'");
206 shouldBe("select2.options[2].textContent", "'A.5'");
207 shouldBe("select2.options[3].value", "'2'");
208 shouldBe("select2.options[3].textContent", "'B'");
209 shouldBe("select2.options[4].value", "'3'");
210 shouldBe("select2.options[4].textContent", "'C'");
211 shouldBe("select2.options[5].value", "'4'");
212 shouldBe("select2.options[5].textContent", "'D'");
213 debug("");
215 debug("2.7 Add an Option at index -1");
216 option2 = document.createElement("OPTION");
217 select2.options.add(option2, -1);
218 option2.value = "5";
219 option2.textContent = "E";
220 shouldBe("select2.options.length", "7");
221 shouldBe("select2.selectedIndex", "1");
222 shouldBe("select2.options[0].value", "'0'");
223 shouldBe("select2.options[0].textContent", "'Z'");
224 shouldBe("select2.options[1].value", "'1'");
225 shouldBe("select2.options[1].textContent", "'A'");
226 shouldBe("select2.options[2].value", "'1.5'");
227 shouldBe("select2.options[2].textContent", "'A.5'");
228 shouldBe("select2.options[3].value", "'2'");
229 shouldBe("select2.options[3].textContent", "'B'");
230 shouldBe("select2.options[4].value", "'3'");
231 shouldBe("select2.options[4].textContent", "'C'");
232 shouldBe("select2.options[5].value", "'4'");
233 shouldBe("select2.options[5].textContent", "'D'");
234 shouldBe("select2.options[6].value", "'5'");
235 shouldBe("select2.options[6].textContent", "'E'");
236 debug("");
238 debug("2.8 Add an Option at index -2");
239 option2 = document.createElement("OPTION");
240 shouldNotThrow("select2.options.add(option2, -2)");
241 shouldBe("select2.options.length", "8");
242 shouldBe("select2.selectedIndex", "1");
243 debug("");
245 debug("2.9 Add an Option at index -Infinity");
246 option2 = document.createElement("OPTION");
247 shouldNotThrow("select2.options.add(option2, -1/0)");
248 shouldBe("select2.options.length", "9");
249 shouldBe("select2.selectedIndex", "2");
250 debug("");
252 debug("2.10 Add an Option at index NaN");
253 option2 = document.createElement("OPTION");
254 shouldNotThrow("select2.options.add(option2, 0/0)");
255 shouldBe("select2.options.length", "10");
256 shouldBe("select2.selectedIndex", "3");
257 debug("");
259 debug("2.11 Add an Option at index Infinity");
260 option2 = document.createElement("OPTION");
261 shouldNotThrow("select2.options.add(option2, 1/0)");
262 shouldBe("select2.options.length", "11");
263 shouldBe("select2.selectedIndex", "4");
264 debug("");
266 debug("2.12 Add a non-Option element");
267 option2 = document.createElement("DIV");
268 shouldThrow("select2.options.add(option2, 1)");
269 shouldBe("select2.options.length", "11");
270 shouldBe("select2.selectedIndex", "4");
271 debug("");
273 debug("2.13 Add a non-element (string)");
274 option2 = "o";
275 shouldThrow("select2.options.add(option2, 1)");
276 shouldBe("select2.options.length", "11");
277 shouldBe("select2.selectedIndex", "4");
278 debug("");
280 debug("2.14 Add a non-element (number)");
281 option2 = 3.14;
282 shouldThrow("select2.options.add(option2, 1)");
283 shouldBe("select2.options.length", "11");
284 shouldBe("select2.selectedIndex", "4");
285 debug("");
287 debug("2.15 Add a non-element (boolean)");
288 option2 = true;
289 shouldThrow("select2.options.add(option2, 1)");
290 shouldBe("select2.options.length", "11");
291 shouldBe("select2.selectedIndex", "4");
292 debug("");
294 debug("2.16 Add undefined");
295 option2 = undefined;
296 shouldThrow("select2.options.add(option2, 1)");
297 shouldBe("select2.options.length", "11");
298 shouldBe("select2.selectedIndex", "4");
299 debug("");
301 debug("2.17 Add null");
302 option2 = null;
303 shouldThrow("select2.options.add(option2, 1)");
304 shouldBe("select2.options.length", "11");
305 shouldBe("select2.selectedIndex", "4");
306 debug("");
308 debug("2.18 Add negative infinity");
309 option2 = -1/0;
310 shouldThrow("select2.options.add(option2, 1)");
311 shouldBe("select2.options.length", "11");
312 shouldBe("select2.selectedIndex", "4");
313 debug("");
315 debug("2.19 Add NaN");
316 option2 = 0/0;
317 shouldThrow("select2.options.add(option2, 1)");
318 shouldBe("select2.options.length", "11");
319 shouldBe("select2.selectedIndex", "4");
320 debug("");
322 debug("2.20 Add positive infinity");
323 option2 = 1/0;
324 shouldThrow("select2.options.add(option2, 1)");
325 shouldBe("select2.options.length", "11");
326 shouldBe("select2.selectedIndex", "4");
327 debug("");