Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / forms / mutation-event-recalc.html
blob2d605d4133c21a0b1974e545e3c4b1fe6da3a4f7
1 <body>
2 <p>Test that HTMLSelectElement DOM is in a consistent state when handling mutation events.</p>
3 <form>
4 <select multiple size=5><option>1</option><option>2</option></select>
5 </form>
6 <div id=res></div>
7 <script>
8 if (window.testRunner)
9 testRunner.dumpAsText();
11 var select = document.forms[0].elements[0];
12 var res = document.getElementById("res");
13 var hadFailure = false;
15 function logError(message)
17 res.innerHTML += message + "<br>";
18 hadFailure = true;
21 // { test number, expected length, item to check, expected result }
22 var testInfo;
24 function checkSelect(evt)
26 // Force layout to verify that DOM is in a consistent state.
27 document.body.offsetTop;
29 if (!testInfo[0])
30 return;
32 if (select.length != testInfo[1])
33 logError("Test " + testInfo[0] + ". " + evt.type + ": Incorrect length. Was " + select.length + ", expected " + testInfo[1] + ".");
35 if (testInfo[3]) {
36 if (select.item(testInfo[2]).value != testInfo[3])
37 logError("Test " + testInfo[0] + ". " + evt.type + ": Incorrect item " + testInfo[2] + " value. Was " + select.item(testInfo[2]).value + ", expected " + testInfo[3] + ".");
38 } else {
39 if (select.item(testInfo[2]))
40 logError("Test " + testInfo[0] + ". " + evt.type + ": Item " + testInfo[2] + " exists when it should not.");
44 function onNodeRemoved()
46 // Force layout to verify that DOM is in a consistent state.
47 document.body.offsetTop;
49 if (select.length != 2) {
50 alert("FAIL: Incorrect length after removing an option.");
51 hadFailure = true;
54 if (select.item(2)) {
55 alert("FAIL: A removed option is still present when handling a DOMNodeRemoved event.");
56 hadFailure = true;
60 select.addEventListener("DOMNodeInserted", checkSelect, true);
61 select.addEventListener("DOMNodeInsertedIntoDocument", checkSelect, true);
62 select.addEventListener("DOMSubtreeModified", checkSelect, true);
63 select.addEventListener("DOMSubtreeModified", checkSelect, true);
65 testInfo = [1, 3, 2, "3"];
66 select.appendChild(new Option("3", "3", false, false));
67 checkSelect();
69 testInfo = [2, 2, 2, undefined];
70 select.removeChild(select.lastChild);
71 checkSelect();
73 testInfo = []; // A DOMSubtreeModified event may be dispatched between removal and addition, skip checks.
74 select.replaceChild(new Option("new", "new", false, false), select.lastChild);
75 testInfo = [3, 2, 1, "new"];
76 checkSelect();
78 testInfo = [4, 3, 0, "0"];
79 select.add(new Option("0", "0", false, false), select.firstChild);
80 testInfo = [4, 3, 0, "0"];
81 checkSelect();
83 testInfo = [5, 2, 0, "1"];
84 select.remove(0);
85 testInfo = [5, 2, 0, "1"];
86 checkSelect();
88 testInfo = [6, 3, 0, "-1"];
89 select.insertBefore(new Option("-1", "-1", false, false), select.firstChild);
90 testInfo = [6, 3, 0, "-1"];
91 checkSelect();
93 testInfo = [7, 3, 0, "-"];
94 select.firstChild.value = "-";
95 select.firstChild.text = "-";
96 testInfo = [7, 3, 0, "-"];
97 checkSelect();
99 testInfo = []; // Multiple DOMSubtreeModified event may be dispatched, skip check.
100 select.innerHTML = "<optgroup><option>1</option><option>2</option></optgroup>";
101 testInfo = [8, 2, 0, 1];
102 checkSelect();
104 var optgroup = select.firstChild;
106 testInfo = [9, 3, 2, "3"];
107 optgroup.appendChild(new Option("3", "3", false, false));
108 checkSelect();
110 testInfo = [10, 2, 2, undefined];
111 optgroup.removeChild(optgroup.lastChild);
112 checkSelect();
114 testInfo = []; // A DOMSubtreeModified event may be dispatched between removal and addition, skip checks.
115 optgroup.replaceChild(new Option("new", "new", false, false), optgroup.lastChild);
116 testInfo = [11, 2, 1, "new"];
117 checkSelect();
119 /* WebKit cannot add options right into a group via HTMLSelectElement.add().
120 See <https://bugs.webkit.org/show_bug.cgi?id=24606>.
121 testInfo = [12, 3, 0, "0"];
122 select.add(new Option("0", "0", false, false), optgroup.firstChild);
123 testInfo = [12, 3, 0, "0"];
124 checkSelect();
126 testInfo = [13, 2, 0, "1"];
127 select.remove(0);
128 checkSelect();
131 testInfo = [14, 3, 0, "-1"];
132 optgroup.insertBefore(new Option("-1", "-1", false, false), optgroup.firstChild);
133 checkSelect();
135 testInfo = [15, 3, 0, "-"];
136 optgroup.firstChild.value = "-";
137 optgroup.firstChild.text = "-";
138 checkSelect();
140 testInfo = [16, 0, 0, undefined];
141 optgroup.innerHTML = "";
142 checkSelect();
144 if (!hadFailure)
145 res.innerHTML = "SUCCESS";
146 </script>