1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.html;
16
17 import java.io.InputStream;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21
22 import org.apache.commons.io.IOUtils;
23 import org.htmlunit.CollectingAlertHandler;
24 import org.htmlunit.MockWebConnection;
25 import org.htmlunit.SimpleWebTestCase;
26 import org.htmlunit.WebClient;
27 import org.htmlunit.html.HtmlPage;
28 import org.htmlunit.util.MimeType;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34
35
36
37
38 public class HTMLImageElement2Test extends SimpleWebTestCase {
39
40
41
42
43
44
45
46 @Test
47 public void onLoad_notDownloadedWhenJavascriptDisabled() throws Exception {
48 final String html = DOCTYPE_HTML
49 + "<html><body><img src='" + URL_SECOND + "' onload='alert(1)'></body></html>";
50
51 final WebClient client = getWebClientWithMockWebConnection();
52 client.getOptions().setJavaScriptEnabled(false);
53
54 final MockWebConnection conn = getMockWebConnection();
55 conn.setResponse(URL_SECOND, "foo", MimeType.IMAGE_PNG);
56
57 loadPageWithAlerts(html);
58 assertEquals(URL_FIRST, conn.getLastWebRequest().getUrl());
59 }
60
61
62
63
64
65
66 @Test
67 public void onload_complex_JavascriptDisabled() throws Exception {
68 try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
69 final byte[] directBytes = IOUtils.toByteArray(is);
70
71 getMockWebConnection().setResponse(URL_SECOND, directBytes, 200, "ok",
72 MimeType.IMAGE_JPEG, Collections.emptyList());
73 }
74
75 final String html = DOCTYPE_HTML
76 + "<html>\n"
77 + "<head>\n"
78 + "<script>\n"
79 + " function test(i) {\n"
80 + " alert('in');\n"
81 + " var image = new Image();\n"
82 + " image.onload = function () { alert(\"Image.onload(\" + i + \")\") };\n"
83 + " image.src = '" + URL_SECOND + "';\n"
84 + " alert('out');\n"
85 + " }\n"
86 + "</script>\n"
87 + "</head>\n"
88 + "<body onload='test(0)'>\n"
89 + "<button id='myId'"
90 + "onmousedown=\"alert('mousedown'); test(1)\" "
91 + "onmouseup=\"alert('mouseup'); test(2)\" "
92 + "onclick=\"alert('click'); test(3)\"></button>\n"
93 + "</body>\n"
94 + "</html>\n";
95
96 final WebClient client = getWebClientWithMockWebConnection();
97 client.getOptions().setJavaScriptEnabled(false);
98
99 final List<String> collectedAlerts = new ArrayList<>();
100 client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
101
102 final HtmlPage page = loadPage(html);
103 page.getElementById("myId").click();
104 assertEquals(getExpectedAlerts(), collectedAlerts);
105 assertEquals(1, getMockWebConnection().getRequestCount());
106 assertEquals(URL_FIRST, getMockWebConnection().getLastWebRequest().getUrl());
107 }
108
109
110
111
112
113
114 @Test
115 public void onload_complex_JavascriptEngineDisabled() throws Exception {
116 try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
117 final byte[] directBytes = IOUtils.toByteArray(is);
118
119 getMockWebConnection().setResponse(URL_SECOND, directBytes, 200, "ok",
120 MimeType.IMAGE_JPEG, Collections.emptyList());
121 }
122
123 final String html = DOCTYPE_HTML
124 + "<html>\n"
125 + "<head>\n"
126 + "<script>\n"
127 + " function test(i) {\n"
128 + " alert('in');\n"
129 + " var image = new Image();\n"
130 + " image.onload = function () { alert(\"Image.onload(\" + i + \")\") };\n"
131 + " image.src = '" + URL_SECOND + "';\n"
132 + " alert('out');\n"
133 + " }\n"
134 + "</script>\n"
135 + "</head>\n"
136 + "<body onload='test(0)'>\n"
137 + "<button id='myId'"
138 + "onmousedown=\"alert('mousedown'); test(1)\" "
139 + "onmouseup=\"alert('mouseup'); test(2)\" "
140 + "onclick=\"alert('click'); test(3)\"></button>\n"
141 + "</body>\n"
142 + "</html>\n";
143
144 try (WebClient webClient = new WebClient(getBrowserVersion(), false, null, -1)) {
145 final List<String> collectedAlerts = new ArrayList<>();
146
147 final HtmlPage page = loadPage(webClient, html, collectedAlerts);
148 assertFalse(page.getWebClient().isJavaScriptEngineEnabled());
149
150 page.getElementById("myId").click();
151 assertEquals(getExpectedAlerts(), collectedAlerts);
152 assertEquals(1, getMockWebConnection().getRequestCount());
153 assertEquals(URL_FIRST, getMockWebConnection().getLastWebRequest().getUrl());
154 }
155 }
156
157 }