1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.dom;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.BrowserRunner;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.htmlunit.util.MimeType;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.openqa.selenium.WebDriver;
24
25
26
27
28
29
30
31
32
33
34 @RunWith(BrowserRunner.class)
35 public class DOMImplementationTest extends WebDriverTestCase {
36
37
38
39
40 @Test
41 @Alerts({"Core 1.0: true", "Core 2.0: true", "Core 3.0: true"})
42 public void hasFeature_Core() throws Exception {
43 hasFeature("Core", "['1.0', '2.0', '3.0']");
44 }
45
46
47
48
49 @Test
50 @Alerts({"HTML 1.0: true", "HTML 2.0: true", "HTML 3.0: true"})
51 public void hasFeature_HTML() throws Exception {
52 hasFeature("HTML", "['1.0', '2.0', '3.0']");
53 }
54
55
56
57
58 @Test
59 @Alerts({"XML 1.0: true", "XML 2.0: true", "XML 3.0: true"})
60 public void hasFeature_XML() throws Exception {
61 hasFeature("XML", "['1.0', '2.0', '3.0']");
62 }
63
64
65
66
67 @Test
68 @Alerts({"XHTML 1.0: true", "XHTML 2.0: true", "XHTML 3.0: true"})
69 public void hasFeature_XHTML() throws Exception {
70 hasFeature("XHTML", "['1.0', '2.0', '3.0']");
71 }
72
73
74
75
76 @Test
77 @Alerts({"Views 1.0: true", "Views 2.0: true", "Views 3.0: true"})
78 public void hasFeature_Views() throws Exception {
79 hasFeature("Views", "['1.0', '2.0', '3.0']");
80 }
81
82
83
84
85 @Test
86 @Alerts({"StyleSheets 1.0: true", "StyleSheets 2.0: true", "StyleSheets 3.0: true"})
87 public void hasFeature_StyleSheets() throws Exception {
88 hasFeature("StyleSheets", "['1.0', '2.0', '3.0']");
89 }
90
91
92
93
94 @Test
95 @Alerts({"CSS 1.0: true", "CSS 2.0: true", "CSS 3.0: true"})
96 public void hasFeature_CSS() throws Exception {
97 hasFeature("CSS", "['1.0', '2.0', '3.0']");
98 }
99
100
101
102
103 @Test
104 @Alerts({"CSS2 1.0: true", "CSS2 2.0: true", "CSS2 3.0: true"})
105 public void hasFeature_CSS2() throws Exception {
106 hasFeature("CSS2", "['1.0', '2.0', '3.0']");
107 }
108
109
110
111
112 @Test
113 @Alerts({"CSS3 1.0: true", "CSS3 2.0: true", "CSS3 3.0: true"})
114 public void hasFeature_CSS3() throws Exception {
115 hasFeature("CSS3", "['1.0', '2.0', '3.0']");
116 }
117
118
119
120
121 @Test
122 @Alerts({"Events 1.0: true", "Events 2.0: true", "Events 3.0: true"})
123 public void hasFeature_Events() throws Exception {
124 hasFeature("Events", "['1.0', '2.0', '3.0']");
125 }
126
127
128
129
130 @Test
131 @Alerts({"UIEvents 1.0: true", "UIEvents 2.0: true", "UIEvents 3.0: true"})
132 public void hasFeature_UIEvents() throws Exception {
133 hasFeature("UIEvents", "['1.0', '2.0', '3.0']");
134 }
135
136
137
138
139 @Test
140 @Alerts({"MouseEvents 1.0: true", "MouseEvents 2.0: true", "MouseEvents 3.0: true"})
141 public void hasFeature_MouseEvents() throws Exception {
142 hasFeature("MouseEvents", "['1.0', '2.0', '3.0']");
143 }
144
145
146
147
148 @Test
149 @Alerts({"TextEvents 1.0: true", "TextEvents 2.0: true", "TextEvents 3.0: true"})
150 public void hasFeature_TextEvents() throws Exception {
151 hasFeature("TextEvents", "['1.0', '2.0', '3.0']");
152 }
153
154
155
156
157 @Test
158 @Alerts({"KeyboardEvents 1.0: true", "KeyboardEvents 2.0: true", "KeyboardEvents 3.0: true"})
159 public void hasFeature_KeyboardEvents() throws Exception {
160 hasFeature("KeyboardEvents", "['1.0', '2.0', '3.0']");
161 }
162
163
164
165
166 @Test
167 @Alerts({"MutationEvents 1.0: true", "MutationEvents 2.0: true", "MutationEvents 3.0: true"})
168 public void hasFeature_MutationEvents() throws Exception {
169 hasFeature("MutationEvents", "['1.0', '2.0', '3.0']");
170 }
171
172
173
174
175 @Test
176 @Alerts({"MutationNameEvents 1.0: true", "MutationNameEvents 2.0: true", "MutationNameEvents 3.0: true"})
177 public void hasFeature_MutationNameEvents() throws Exception {
178 hasFeature("MutationNameEvents", "['1.0', '2.0', '3.0']");
179 }
180
181
182
183
184 @Test
185 @Alerts({"HTMLEvents 1.0: true", "HTMLEvents 2.0: true", "HTMLEvents 3.0: true"})
186 public void hasFeature_HTMLEvents() throws Exception {
187 hasFeature("HTMLEvents", "['1.0', '2.0', '3.0']");
188 }
189
190
191
192
193 @Test
194 @Alerts({"Range 1.0: true", "Range 2.0: true", "Range 3.0: true"})
195 public void hasFeature_Range() throws Exception {
196 hasFeature("Range", "['1.0', '2.0', '3.0']");
197 }
198
199
200
201
202 @Test
203 @Alerts({"Traversal 1.0: true", "Traversal 2.0: true", "Traversal 3.0: true"})
204 public void hasFeature_Traversal() throws Exception {
205 hasFeature("Traversal", "['1.0', '2.0', '3.0']");
206 }
207
208
209
210
211 @Test
212 @Alerts({"LS 1.0: true", "LS 2.0: true", "LS 3.0: true"})
213 public void hasFeature_LS() throws Exception {
214 hasFeature("LS", "['1.0', '2.0', '3.0']");
215 }
216
217
218
219
220 @Test
221 @Alerts({"LS-Async 1.0: true", "LS-Async 2.0: true", "LS-Async 3.0: true"})
222 public void hasFeature_LSAsync() throws Exception {
223 hasFeature("LS-Async", "['1.0', '2.0', '3.0']");
224 }
225
226
227
228
229 @Test
230 @Alerts({"Validation 1.0: true", "Validation 2.0: true", "Validation 3.0: true"})
231 public void hasFeature_Validation() throws Exception {
232 hasFeature("Validation", "['1.0', '2.0', '3.0']");
233 }
234
235
236
237
238 @Test
239 @Alerts({"XPath 1.0: true", "XPath 2.0: true", "XPath 3.0: true"})
240 public void hasFeature_XPath() throws Exception {
241 hasFeature("XPath", "['1.0', '2.0', '3.0']");
242 }
243
244
245
246
247 @Test
248 @Alerts({"http://www.w3.org/TR/SVG11/feature#BasicStructure 1.0: true",
249 "http://www.w3.org/TR/SVG11/feature#BasicStructure 1.1: true",
250 "http://www.w3.org/TR/SVG11/feature#BasicStructure 1.2: true"})
251 public void hasFeature_SVG_BasicStructure() throws Exception {
252 hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "['1.0', '1.1', '1.2']");
253 }
254
255
256
257
258 @Test
259 @Alerts({"http://www.w3.org/TR/SVG11/feature#Shape 1.0: true",
260 "http://www.w3.org/TR/SVG11/feature#Shape 1.1: true",
261 "http://www.w3.org/TR/SVG11/feature#Shape 1.2: true"})
262 public void hasFeature_SVG_Shape() throws Exception {
263 hasFeature("http://www.w3.org/TR/SVG11/feature#Shape", "['1.0', '1.1', '1.2']");
264 }
265
266 private void hasFeature(final String feature, final String versions) throws Exception {
267 final String html = DOCTYPE_HTML
268 + "<html><head>\n"
269 + "<script>\n"
270 + LOG_TITLE_FUNCTION
271 + " function test() {\n"
272 + " var feature = '" + feature + "';\n"
273 + " var versions = " + versions + ";\n"
274 + " for (var j = 0; j < versions.length; j++) {\n"
275 + " var version = versions[j];\n"
276 + " log(feature + ' ' + version + ': ' + document.implementation.hasFeature(feature, version));\n"
277 + " }\n"
278 + " }\n"
279 + "</script>\n"
280 + "</head>\n"
281 + "<body onload='test()'></body></html>";
282
283 loadPageVerifyTitle2(html);
284 }
285
286
287
288
289 @Test
290 @Alerts("[object XMLDocument]")
291 public void createDocument() throws Exception {
292 final String html = DOCTYPE_HTML
293 + "<html><head><script>\n"
294 + LOG_TITLE_FUNCTION
295 + " function test() {\n"
296 + " log(document.implementation.createDocument('', '', null));\n"
297 + " }\n"
298 + "</script></head><body onload='test()'>\n"
299 + "</body></html>";
300
301 loadPageVerifyTitle2(html);
302 }
303
304
305
306
307 @Test
308 @Alerts({"mydoc", "null", "mydoc", "null"})
309 public void createDocument_qualifiedName() throws Exception {
310 final String html = DOCTYPE_HTML
311 + "<html><head><script>\n"
312 + LOG_TITLE_FUNCTION
313 + " function test() {\n"
314 + " var doc = document.implementation.createDocument('', 'mydoc', null);\n"
315 + " log(doc.documentElement.tagName);\n"
316 + " log(doc.documentElement.prefix);\n"
317 + " log(doc.documentElement.localName);\n"
318 + " log(doc.documentElement.namespaceURI);\n"
319 + " }\n"
320 + "</script></head><body onload='test()'>\n"
321 + "</body></html>";
322
323 loadPageVerifyTitle2(html);
324 }
325
326
327
328
329 @Test
330 @Alerts({"mydoc", "null", "mydoc", "http://mynamespace"})
331 public void createDocument_namespaceAndQualifiedName() throws Exception {
332 final String html = DOCTYPE_HTML
333 + "<html><head><script>\n"
334 + LOG_TITLE_FUNCTION
335 + " function test() {\n"
336 + " var doc = document.implementation.createDocument('http://mynamespace', 'mydoc', null);\n"
337 + " log(doc.documentElement.tagName);\n"
338 + " log(doc.documentElement.prefix);\n"
339 + " log(doc.documentElement.localName);\n"
340 + " log(doc.documentElement.namespaceURI);\n"
341 + " }\n"
342 + "</script></head><body onload='test()'>\n"
343 + "</body></html>";
344
345 loadPageVerifyTitle2(html);
346 }
347
348
349
350
351 @Test
352 @Alerts({"m:mydoc", "m", "mydoc", "http://mynamespace"})
353 public void createDocument_namespaceAndQualifiedNameWithPrefix() throws Exception {
354 final String html = DOCTYPE_HTML
355 + "<html><head><script>\n"
356 + LOG_TITLE_FUNCTION
357 + " function test() {\n"
358 + " var doc = document.implementation.createDocument('http://mynamespace', 'm:mydoc', null);\n"
359 + " log(doc.documentElement.tagName);\n"
360 + " log(doc.documentElement.prefix);\n"
361 + " log(doc.documentElement.localName);\n"
362 + " log(doc.documentElement.namespaceURI);\n"
363 + " }\n"
364 + "</script></head><body onload='test()'>\n"
365 + "</body></html>";
366
367 loadPageVerifyTitle2(html);
368 }
369
370
371
372
373 @Test
374 @Alerts({"[object HTMLDocument]", "undefined"})
375 public void createHTMLDocument() throws Exception {
376 final String html = DOCTYPE_HTML
377 + "<html>\n"
378 + "<head>\n"
379 + " <script>\n"
380 + LOG_TITLE_FUNCTION
381 + " function test() {\n"
382 + " try {\n"
383 + " var doc = document.implementation.createHTMLDocument();\n"
384 + " log(doc);\n"
385 + " log(doc.window);\n"
386 + " } catch(e) { logEx(e); }\n"
387 + " }\n"
388 + "</script>\n"
389 + "</head>\n"
390 + "<body onload='test()'>\n"
391 + "</body></html>";
392
393 loadPageVerifyTitle2(html);
394 }
395
396
397
398
399 @Test
400 @Alerts({"[object HTMLDocument]", "newdoctitle"})
401 public void createHTMLDocument_title() throws Exception {
402 final String html = DOCTYPE_HTML
403 + "<html>\n"
404 + "<head>\n"
405 + " <script>\n"
406 + LOG_TITLE_FUNCTION
407 + " function test() {\n"
408 + " try {\n"
409 + " var doc = document.implementation.createHTMLDocument('newdoctitle');\n"
410 + " log(doc);\n"
411 + " log(doc.title);\n"
412 + " } catch(e) { logEx(e); }\n"
413 + " }\n"
414 + "</script>\n"
415 + "</head>\n"
416 + "<body onload='test()'>\n"
417 + "</body></html>";
418
419 loadPageVerifyTitle2(html);
420 }
421
422
423
424
425 @Test
426 @Alerts({"[object HTMLDocument]", ""})
427 public void createHTMLDocument_titleEmpty() throws Exception {
428 final String html = DOCTYPE_HTML
429 + "<html>\n"
430 + "<head>\n"
431 + " <script>\n"
432 + LOG_TITLE_FUNCTION
433 + " function test() {\n"
434 + " try {\n"
435 + " var doc = document.implementation.createHTMLDocument('');\n"
436 + " log(doc);\n"
437 + " log(doc.title);\n"
438 + " } catch(e) { logEx(e); }\n"
439 + " }\n"
440 + "</script>\n"
441 + "</head>\n"
442 + "<body onload='test()'>\n"
443 + "</body></html>";
444
445 loadPageVerifyTitle2(html);
446 }
447
448
449
450
451 @Test
452 @Alerts("2")
453 public void createHTMLDocument_jQuery() throws Exception {
454 final String html = DOCTYPE_HTML
455 + "<html>\n"
456 + "<head>\n"
457 + " <script>\n"
458 + LOG_TITLE_FUNCTION
459 + " function test() {\n"
460 + " try {\n"
461 + " var doc = document.implementation.createHTMLDocument('');\n"
462 + " doc.body.innerHTML = '<form></form><form></form>';\n"
463 + " log(doc.body.childNodes.length);\n"
464 + " } catch(e) { logEx(e); }\n"
465 + " }\n"
466 + "</script>\n"
467 + "</head>\n"
468 + "<body onload='test()'>\n"
469 + "</body></html>";
470
471 loadPageVerifyTitle2(html);
472 }
473
474
475
476
477 @Test
478 @Alerts("createdElement")
479 public void createHTMLDocument_createElement() throws Exception {
480 final String html = DOCTYPE_HTML
481 + "<html>\n"
482 + "<head>\n"
483 + " <script>\n"
484 + LOG_TITLE_FUNCTION
485 + " function test() {\n"
486 + " try {\n"
487 + " var doc = document.implementation.createHTMLDocument('');\n"
488 + " var p = doc.createElement('p');\n"
489 + " p.innertHTML = 'createdElement';\n"
490 + " log(p.innertHTML);\n"
491 + " } catch(e) { logEx(e); }\n"
492 + " }\n"
493 + "</script>\n"
494 + "</head>\n"
495 + "<body onload='test()'>\n"
496 + "</body></html>";
497
498 loadPageVerifyTitle2(html);
499 }
500
501
502
503
504 @Test
505 @Alerts({"<html><head></head><body></body></html>",
506 "<html><head><title></title></head><body></body></html>",
507 "<html><head><title>abc</title></head><body></body></html>"})
508 public void createHTMLDocument_htmlCode() throws Exception {
509 final String html = DOCTYPE_HTML
510 + "<html>\n"
511 + "<head>\n"
512 + " <script>\n"
513 + LOG_TITLE_FUNCTION
514 + " function test() {\n"
515 + " try {\n"
516 + " var doc = document.implementation.createHTMLDocument();\n"
517 + " log(doc.documentElement.outerHTML);\n"
518 + " } catch(e) { logEx(e); }\n"
519
520 + " try {\n"
521 + " var doc = document.implementation.createHTMLDocument('');\n"
522 + " log(doc.documentElement.outerHTML);\n"
523 + " } catch(e) { logEx(e); }\n"
524
525 + " try {\n"
526 + " var doc = document.implementation.createHTMLDocument('abc');\n"
527 + " log(doc.documentElement.outerHTML);\n"
528 + " } catch(e) { logEx(e); }\n"
529 + " }\n"
530 + "</script>\n"
531 + "</head>\n"
532 + "<body onload='test()'>\n"
533 + "</body></html>";
534
535 loadPageVerifyTitle2(html);
536 }
537
538
539
540
541 @Test
542 @Alerts("<html><head><title>test</title></head>"
543 + "<body><p>This is a new paragraph.</p></body></html>")
544 public void createHTMLDocumentAddParagraph() throws Exception {
545 final String html = DOCTYPE_HTML
546 + "<html>\n"
547 + "<head>\n"
548 + " <script>\n"
549 + LOG_TITLE_FUNCTION
550 + " function test() {\n"
551 + " try {\n"
552 + " var doc = document.implementation.createHTMLDocument('test');\n"
553 + " var p = doc.createElement('p');\n"
554 + " p.innerHTML = 'This is a new paragraph.';\n"
555 + " doc.body.appendChild(p);"
556 + " log(doc.documentElement.outerHTML);\n"
557 + " } catch(e) { logEx(e); }\n"
558 + " }\n"
559 + "</script>\n"
560 + "</head>\n"
561 + "<body onload='test()'>\n"
562 + "</body></html>";
563
564 loadPageVerifyTitle2(html);
565 }
566
567
568
569
570 @Test
571 @Alerts("<html><head><title>test</title></head><body><p>Hello</p></body></html>")
572 public void createHTMLDocumentInnerAddParagraph() throws Exception {
573 final String html = DOCTYPE_HTML
574 + "<html>\n"
575 + "<head>\n"
576 + " <script>\n"
577 + LOG_TITLE_FUNCTION
578 + " function test() {\n"
579 + " try {\n"
580 + " var doc = document.implementation.createHTMLDocument('test');\n"
581 + " doc.body.innerHTML = '<p>Hello</p>';\n"
582 + " log(doc.documentElement.outerHTML);\n"
583 + " } catch(e) { logEx(e); }\n"
584 + " }\n"
585 + "</script>\n"
586 + "</head>\n"
587 + "<body onload='test()'>\n"
588 + "</body></html>";
589
590 loadPageVerifyTitle2(html);
591 }
592
593
594
595
596 @Test
597 @Alerts("<html><head><title>test</title></head><body><img src=\"x\" onerror=\"log(1)\"></body></html>")
598 public void createHTMLDocumentInnerAddImg() throws Exception {
599 final String html = DOCTYPE_HTML
600 + "<html>\n"
601 + "<head>\n"
602 + " <script>\n"
603 + LOG_TITLE_FUNCTION
604 + " function test() {\n"
605 + " try {\n"
606 + " var doc = document.implementation.createHTMLDocument('test');\n"
607 + " doc.body.innerHTML = '<img src=\"x\" onerror=\"log(1)\">';\n"
608 + " log(doc.documentElement.outerHTML);\n"
609 + " } catch(e) { logEx(e); }\n"
610 + " }\n"
611 + "</script>\n"
612 + "</head>\n"
613 + "<body onload='test()'>\n"
614 + "</body></html>";
615
616 loadPageVerifyTitle2(html);
617 }
618
619
620
621
622 @Test
623 @Alerts("before1")
624 public void createHTMLDocumentInnerAddImgAddDocToIframe() throws Exception {
625 final String html = DOCTYPE_HTML
626 + "<html>\n"
627 + "<head>\n"
628 + " <script>\n"
629 + " function test() {\n"
630 + " try {\n"
631 + " var frame = document.getElementById('theFrame');\n"
632
633 + " var doc = document.implementation.createHTMLDocument('test');\n"
634 + " doc.body.innerHTML = '<img src=\"x\" onerror=\"window.parent.document.title += 1\">';\n"
635
636
637 + " var destDocument = frame.contentDocument;\n"
638 + " var srcNode = doc.documentElement;\n"
639 + " var newNode = destDocument.importNode(srcNode, true);\n"
640 + " destDocument.replaceChild(newNode, destDocument.documentElement);\n"
641 + " window.parent.document.title += 'before';"
642
643 + " } catch(e) { window.parent.document.title += 'exception'; }\n"
644 + " }\n"
645 + "</script>\n"
646 + "</head>\n"
647 + "<body onload='test()'>\n"
648 + " <iframe id='theFrame' src='about:blank' />"
649 + "</body></html>";
650
651 getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
652
653 final WebDriver driver = loadPage2(html);
654 assertTitle(driver, getExpectedAlerts()[0]);
655 }
656
657
658
659
660
661 @Test
662 @Alerts("before import;after import;1")
663 public void createHTMLDocumentInnerAddImgAddDocToIframe1() throws Exception {
664 final String html = DOCTYPE_HTML
665 + "<html>\n"
666 + "<head>\n"
667 + " <script>\n"
668 + " function test() {\n"
669 + " try {\n"
670 + " var frame = document.getElementById('theFrame');\n"
671
672 + " var doc = document.implementation.createHTMLDocument('test');\n"
673 + " doc.body.innerHTML = '<img src=\"x\" onerror=\"window.parent.document.title += 1\">';\n"
674
675
676 + " var destDocument = frame.contentDocument;\n"
677 + " var srcNode = doc.documentElement;\n"
678 + " document.title += 'before import;';\n"
679 + " var newNode = destDocument.importNode(srcNode, true);\n"
680 + " document.title += 'after import;';\n"
681 + " destDocument.replaceChild(newNode, destDocument.documentElement);\n"
682
683 + " } catch(e) { document.title += 'exception'; }\n"
684 + " }\n"
685 + "</script>\n"
686 + "</head>\n"
687 + "<body onload='test()'>\n"
688 + " <iframe id='theFrame' src='about:blank' />"
689 + "</body></html>";
690 getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
691
692 final WebDriver driver = loadPage2(html);
693 assertTitle(driver, getExpectedAlerts()[0]);
694 }
695 }