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