1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import org.htmlunit.SimpleWebTestCase;
18 import org.htmlunit.corejs.javascript.ScriptableObject;
19 import org.htmlunit.javascript.host.html.HTMLElement;
20 import org.htmlunit.junit.annotation.Alerts;
21 import org.htmlunit.junit.annotation.HtmlUnitNYI;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24
25
26
27
28
29
30
31
32
33
34 public class HtmlTableRowTest extends SimpleWebTestCase {
35
36 private static final String HTML = DOCTYPE_HTML
37 + "<html><head><title>foo</title></head><body>\n"
38 + "<table id='table'><tr id='row'>\n"
39 + "<td id='cell' width='20'><input type='text' id='foo'/></td>\n"
40 + "</tr></table>\n"
41 + "</body></html>";
42
43 private HtmlPage page_;
44 private HtmlTable table_;
45 private HtmlTableBody tbody_;
46 private HtmlTableRow row_;
47 private HtmlTableCell cell_;
48 private HtmlTableRow rowClone_;
49 private HtmlTableCell cellClone_;
50
51
52
53
54
55 @BeforeEach
56 public void init() throws Exception {
57 page_ = loadPage(HTML);
58
59 table_ = page_.getHtmlElementById("table");
60 tbody_ = (HtmlTableBody) table_.getFirstChild();
61 row_ = table_.getRow(0);
62 cell_ = row_.getCell(0);
63
64 rowClone_ = (HtmlTableRow) row_.cloneNode(true);
65 cellClone_ = rowClone_.getCell(0);
66 }
67
68
69
70
71 @Test
72 public void clonePreservesOriginal() {
73 assertSame(tbody_, row_.getParentNode());
74 assertSame(row_, cell_.getParentNode());
75 assertSame(cell_, row_.getCell(0));
76 assertEquals("row", row_.getId());
77 assertEquals("cell", cell_.getId());
78 }
79
80
81
82
83
84 @Test
85 public void clonesAreDistinct() throws Exception {
86 assertNotSame(row_, rowClone_);
87 assertNotSame(cell_, cellClone_);
88 }
89
90
91
92
93 @Test
94 public void cloneHasSamePage() {
95 assertSame(cell_.getPage(), cellClone_.getPage());
96 assertSame(row_.getPage(), rowClone_.getPage());
97 }
98
99
100
101
102
103 @Test
104 public void clonedRowHasNullParent() throws Exception {
105 assertNull(rowClone_.getParentNode());
106 }
107
108
109
110
111
112 @Test
113 public void clonedRowHasDifferentChildren() throws Exception {
114 assertEquals(row_.getCells().size(), rowClone_.getCells().size());
115 assertNotSame(row_.getFirstChild(), rowClone_.getFirstChild());
116 }
117
118
119
120
121 @Test
122 public void clonedCellHasDifferentChildren() {
123 assertNotSame(cell_.getParentNode(), cellClone_.getParentNode());
124 assertNotNull(cell_.getFirstChild());
125 assertNotSame(cell_.getFirstChild(), cellClone_.getFirstChild());
126 }
127
128
129
130
131
132 @Test
133 public void clonedCellHasClonedRowAsParent() throws Exception {
134 assertSame(rowClone_, cellClone_.getParentNode());
135 }
136
137
138
139
140 @Test
141 public void cloneAttributesCopiedFromOriginal() {
142 assertEquals("20", cell_.getAttribute("width"));
143 assertEquals("20", cellClone_.getAttribute("width"));
144 }
145
146
147
148
149
150 @Test
151 public void cloneAttributeIsIndependentOfOriginal() {
152 cellClone_.setAttribute("a", "one");
153 assertFalse("one".equals(cell_.getAttribute("a")));
154 }
155
156
157
158
159
160 @Test
161 public void originalAttributeIsIndependentOfClone() {
162 cell_.setAttribute("a", "one");
163 assertFalse("one".equals(cellClone_.getAttribute("a")));
164 }
165
166
167
168
169
170 @Test
171 public void cloneValueIsIndependentOfOriginal() {
172 cellClone_.setNodeValue("one");
173 assertFalse("one".equals(cell_.getNodeValue()));
174 }
175
176
177
178
179 @Test
180 public void cloneIdIsIndependentOfOriginal() {
181 cellClone_.setNodeValue("one");
182 assertFalse("one".equals(cell_.getNodeValue()));
183 }
184
185
186
187
188
189
190
191 @Test
192 public void scriptCanGetOriginalCell() {
193 final String cmd = "document.getElementById('cell')";
194 final Object object = page_.executeJavaScript(cmd).getJavaScriptResult();
195
196 final HtmlElement cellElement = ((HTMLElement) object).getDomNodeOrDie();
197 assertSame(cell_, cellElement);
198 }
199
200
201
202
203
204 @Test
205 public void cellScriptObjectIsReturnedByScript() {
206 final String cmd = "document.getElementById('cell')";
207 final HTMLElement jselement = (HTMLElement) page_.executeJavaScript(cmd).getJavaScriptResult();
208
209 assertSame(jselement, cell_.getScriptableObject());
210 }
211
212
213
214
215
216 @Test
217 public void scriptCanSetJsPropertyOnCell() {
218 final String cmd = "document.getElementById('cell').a = 'original'; document.getElementById('cell')";
219 final Object object = page_.executeJavaScript(cmd).getJavaScriptResult();
220
221 final HTMLElement jselement = (HTMLElement) object;
222 assertEquals("original", ScriptableObject.getProperty(jselement, "a"));
223
224 assertSame(jselement, cell_.getScriptableObject());
225 }
226
227
228
229
230 @Test
231 @Alerts("disabled")
232 @HtmlUnitNYI(CHROME = "",
233 EDGE = "",
234 FF = "",
235 FF_ESR = "")
236 public void cloneScriptCanSetDisabledOnCell() {
237 final String cmd = "document.getElementById('cell').disabled = 'true'";
238 page_.executeJavaScript(cmd);
239
240 assertEquals(getExpectedAlerts()[0], cell_.getAttribute("disabled"));
241 }
242
243
244
245
246 @Test
247 public void cloneScriptCanSetAttributeOnCell() {
248 final String cmd = "document.getElementById('cell').setAttribute('a','original')";
249 page_.executeJavaScript(cmd);
250 assertEquals("original", cell_.getAttribute("a"));
251 }
252
253
254
255
256
257
258
259 @Test
260 public void cloneScriptSetAttributeIndependentOfOriginal() {
261 final String cmd = "document.getElementById('cell').setAttribute('a','original')";
262 page_.executeJavaScript(cmd);
263
264 assertEquals("original", cell_.getAttribute("a"));
265 assertFalse("original".equals(cellClone_.getAttribute("a")));
266 }
267
268
269
270
271
272 @Test
273 @Alerts({"disabled", ""})
274 @HtmlUnitNYI(CHROME = {"", ""},
275 EDGE = {"", ""},
276 FF = {"", ""},
277 FF_ESR = {"", ""})
278 public void cloneScriptSetDisabledIndependentOfOriginal() {
279 final String cmd = "document.getElementById('cell').disabled = 'true'";
280 page_.executeJavaScript(cmd);
281
282 assertEquals(getExpectedAlerts()[0], cell_.getAttribute("disabled"));
283 assertEquals(getExpectedAlerts()[1], cellClone_.getAttribute("disabled"));
284 }
285
286
287
288
289
290
291 @Test
292 public void cloneHasDifferentScriptableObject() {
293 final String cmd = "document.getElementById('cell')";
294
295 page_.executeJavaScript(cmd);
296
297 assertNotSame(cell_.getScriptableObject(), cellClone_.getScriptableObject());
298 }
299
300
301
302
303
304 @Test
305 public void scriptDomOperations() {
306 final String cmd = "document.getElementById('foo').value = 'Input!';document.getElementById('foo')";
307 page_.executeJavaScript(cmd);
308
309 final HtmlInput input = (HtmlInput) cell_.getFirstChild();
310 assertEquals("", input.getValueAttribute());
311 assertEquals("Input!", input.getValue());
312
313 final HtmlInput inputClone = (HtmlInput) cellClone_.getFirstChild();
314 assertEquals("", inputClone.getValueAttribute());
315 assertFalse("Input!".equals(inputClone.getValue()));
316 }
317 }