1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.net.URL;
18
19 import org.htmlunit.SimpleWebTestCase;
20 import org.htmlunit.junit.BrowserRunner;
21 import org.htmlunit.junit.annotation.Alerts;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24
25
26
27
28
29
30
31 @RunWith(BrowserRunner.class)
32 public class HtmlLinkTest extends SimpleWebTestCase {
33
34
35
36
37 @Test
38 @Alerts("body onLoad")
39 public void onLoad() throws Exception {
40 getWebClientWithMockWebConnection().getOptions().setCssEnabled(false);
41 getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.css"), "");
42
43 final String html = DOCTYPE_HTML
44 + "<html>\n"
45 + "<head>\n"
46 + " <link rel='stylesheet' href='simple.css' "
47 + "onload='alert(\"onLoad\")' onerror='alert(\"onError\")'>\n"
48 + "</head>\n"
49 + "<body onload='alert(\"body onLoad\")'>\n"
50 + "</body>\n"
51 + "</html>";
52
53 loadPageWithAlerts(html);
54 assertEquals(1, getMockWebConnection().getRequestCount());
55 }
56
57
58
59
60 @Test
61 public void onLoadDynamic() throws Exception {
62 getWebClientWithMockWebConnection().getOptions().setCssEnabled(false);
63 getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.css"), "");
64
65 final String html = DOCTYPE_HTML
66 + "<html>\n"
67 + "<head>\n"
68 + " <script>\n"
69 + " function test() {\n"
70 + " var dynLink = document.createElement('link');\n"
71 + " dynLink.rel = 'stylesheet';\n"
72 + " dynLink.type = 'text/css';\n"
73 + " dynLink.href = 'simple.css';"
74 + " dynLink.onload = function (e) { log(\"onLoad \" + e) };\n"
75 + " dynLink.onerror = function (e) { log(\"onError \" + e) };\n"
76 + " document.head.appendChild(dynLink);\n"
77
78 + " var dynLink = document.createElement('link');\n"
79 + " dynLink.rel = 'stylesheet';\n"
80 + " dynLink.type = 'text/css';\n"
81 + " dynLink.href = 'unknown.css';"
82 + " dynLink.onload = function (e) { log(\"onLoad \" + e) };\n"
83 + " dynLink.onerror = function (e) { log(\"onError \" + e) };\n"
84 + " document.head.appendChild(dynLink);\n"
85 + " }\n"
86
87 + " function log(x) {\n"
88 + " document.getElementById('log').value += x + '\\n';\n"
89 + " }\n"
90 + " </script>\n"
91 + "</head>\n"
92 + "<body onload='test()'></body>\n"
93 + " <textarea id='log' cols='80' rows='40'></textarea>\n"
94 + "</body>\n"
95 + "</html>";
96
97 final HtmlPage page = loadPageWithAlerts(html);
98 final String text = page.getElementById("log").getAttribute("value").trim().replaceAll("\r", "");
99 assertEquals(String.join("\n", getExpectedAlerts()), text);
100
101 assertEquals(1, getMockWebConnection().getRequestCount());
102 }
103 }