|
14 | 14 | */ |
15 | 15 | package org.htmlunit.javascript.host.html; |
16 | 16 |
|
17 | | -import java.net.URL; |
18 | | -import java.util.ArrayList; |
19 | | -import java.util.List; |
20 | | - |
21 | 17 | import org.htmlunit.MockWebConnection; |
22 | 18 | import org.htmlunit.SimpleWebTestCase; |
23 | 19 | import org.htmlunit.WebClient; |
24 | 20 | import org.htmlunit.junit.BrowserRunner; |
25 | | -import org.htmlunit.junit.BrowserRunner.Alerts; |
26 | 21 | import org.htmlunit.util.MimeType; |
27 | | -import org.htmlunit.util.NameValuePair; |
28 | 22 | import org.junit.Test; |
29 | 23 | import org.junit.runner.RunWith; |
30 | 24 |
|
|
38 | 32 | @RunWith(BrowserRunner.class) |
39 | 33 | public class HTMLImageElement2Test extends SimpleWebTestCase { |
40 | 34 |
|
41 | | - /** |
42 | | - * Verifies that if an image has an <tt>onload</tt> attribute, it gets downloaded |
43 | | - * and the <tt>onload</tt> handler gets invoked. |
44 | | - * @throws Exception if an error occurs |
45 | | - */ |
46 | | - @Test |
47 | | - @Alerts({"0", "1"}) |
48 | | - public void onLoad_calledWhenImageDownloaded_static() throws Exception { |
49 | | - final String html = "<html>\n" |
50 | | - + "<body>\n" |
51 | | - + " <img src='foo.png' onload='test()'>\n" |
52 | | - + " <script>\n" |
53 | | - // first script to be sure that img onload doesn't get executed after first JS execution |
54 | | - + " alert(0);\n" |
55 | | - + " </script>\n" |
56 | | - + " <script>\n" |
57 | | - + " function test() {\n" |
58 | | - + " alert(1);\n" |
59 | | - + " }\n" |
60 | | - + " </script>\n" |
61 | | - + "</body></html>"; |
62 | | - |
63 | | - final MockWebConnection conn = getMockWebConnection(); |
64 | | - final URL imageUrl = new URL(URL_FIRST, "foo.png"); |
65 | | - conn.setResponse(imageUrl, "foo", MimeType.IMAGE_PNG); |
66 | | - |
67 | | - loadPageWithAlerts(html); |
68 | | - assertEquals(imageUrl, conn.getLastWebRequest().getUrl()); |
69 | | - } |
70 | | - |
71 | | - /** |
72 | | - * Verifies that if an image has an <tt>onload</tt> attribute, it gets downloaded |
73 | | - * and the <tt>onload</tt> handler gets invoked. |
74 | | - * @throws Exception if an error occurs |
75 | | - */ |
76 | | - @Test |
77 | | - @Alerts("1") |
78 | | - public void onLoad_calledWhenImageDownloaded_dynamic() throws Exception { |
79 | | - final String html = "<html><body>\n" |
80 | | - + "<script>\n" |
81 | | - + " var i = document.createElement('img');\n" |
82 | | - + " i.src = '" + URL_SECOND + "';\n" |
83 | | - + " i.src = '" + URL_THIRD + "';\n" |
84 | | - + " i.onload = function() { alert(1); };\n" |
85 | | - + "</script></body></html>"; |
86 | | - |
87 | | - final MockWebConnection conn = getMockWebConnection(); |
88 | | - conn.setResponse(URL_SECOND, "foo", MimeType.IMAGE_PNG); |
89 | | - conn.setResponse(URL_THIRD, "foo", MimeType.IMAGE_PNG); |
90 | | - |
91 | | - loadPageWithAlerts(html); |
92 | | - assertEquals(URL_THIRD, conn.getLastWebRequest().getUrl()); |
93 | | - } |
94 | | - |
95 | | - /** |
96 | | - * Verifies that if an image has an <tt>onload</tt> attribute, it gets downloaded |
97 | | - * and the <tt>onload</tt> handler gets invoked. |
98 | | - * @throws Exception if an error occurs |
99 | | - */ |
100 | | - @Test |
101 | | - @Alerts("1") |
102 | | - public void onLoad_calledWhenImageDownloaded_dynamic_onLoad_already_set() throws Exception { |
103 | | - final String html = "<html><body>\n" |
104 | | - + "<script>\n" |
105 | | - + " var i = document.createElement('img');\n" |
106 | | - + " i.onload = function() { alert(1); };\n" |
107 | | - + " i.src = '" + URL_SECOND + "';\n" |
108 | | - + "</script></body></html>"; |
109 | | - |
110 | | - final MockWebConnection conn = getMockWebConnection(); |
111 | | - conn.setResponse(URL_SECOND, "foo", MimeType.IMAGE_PNG); |
112 | | - |
113 | | - loadPageWithAlerts(html); |
114 | | - assertEquals(URL_SECOND, conn.getLastWebRequest().getUrl()); |
115 | | - } |
116 | | - |
117 | | - /** |
118 | | - * Verifies that if an image is created if the page is already |
119 | | - * finished, the onload handler is called. |
120 | | - * @throws Exception if an error occurs |
121 | | - */ |
122 | | - @Test |
123 | | - @Alerts({"1", "2"}) |
124 | | - public void onLoad_calledWhenImageDownloaded_dynamic_twoSteps() throws Exception { |
125 | | - final String html = "<html><body>\n" |
126 | | - + "<script>\n" |
127 | | - + " var i = document.createElement('img');\n" |
128 | | - + " i.src = '" + URL_SECOND + "';\n" |
129 | | - + " i.onload = function() {\n" |
130 | | - + " alert(1);\n" |
131 | | - + " var i2 = document.createElement('img');\n" |
132 | | - + " i2.src = '" + URL_THIRD + "';\n" |
133 | | - + " i2.onload = function() {\n" |
134 | | - + " alert(2);\n" |
135 | | - + " };\n" |
136 | | - + " };\n" |
137 | | - + "</script></body></html>"; |
138 | | - |
139 | | - final MockWebConnection conn = getMockWebConnection(); |
140 | | - conn.setResponse(URL_SECOND, "foo", MimeType.IMAGE_PNG); |
141 | | - conn.setResponse(URL_THIRD, "foo", MimeType.IMAGE_PNG); |
142 | | - |
143 | | - loadPageWithAlerts(html); |
144 | | - assertEquals(URL_THIRD, conn.getLastWebRequest().getUrl()); |
145 | | - } |
146 | | - |
147 | | - /** |
148 | | - * Verifies that if an image has an <tt>onload</tt> attribute set from a script, it gets downloaded |
149 | | - * and the <tt>onload</tt> handler gets invoked. |
150 | | - * @throws Exception if an error occurs |
151 | | - */ |
152 | | - @Test |
153 | | - @Alerts({"image one", "image two"}) |
154 | | - public void onLoad_calledWhenImageDownloaded_mixed() throws Exception { |
155 | | - final String html |
156 | | - = "<html><body><img id='img' name='img'/><script>\n" |
157 | | - + " var i = document.getElementById('img');\n" |
158 | | - + " i.onload = function() {\n" |
159 | | - + " alert('image one');\n" |
160 | | - + " i.onload = function() {\n" |
161 | | - + " alert('image two');\n" |
162 | | - + " };\n" |
163 | | - + " i.src = '" + URL_THIRD + "';\n" |
164 | | - + " };\n" |
165 | | - + " i.setAttribute('src','" + URL_SECOND + "');\n" |
166 | | - + " var t = setTimeout(function() {clearTimeout(t);}, 500);\n" |
167 | | - + "</script></body></html>"; |
168 | | - |
169 | | - final MockWebConnection conn = getMockWebConnection(); |
170 | | - conn.setResponse(URL_SECOND, "foo", MimeType.IMAGE_PNG); |
171 | | - conn.setResponse(URL_THIRD, "foo", MimeType.IMAGE_PNG); |
172 | | - |
173 | | - loadPageWithAlerts(html); |
174 | | - |
175 | | - final List<String> requestedUrls = conn.getRequestedUrls(URL_FIRST); |
176 | | - assertEquals(requestedUrls.size(), 3); |
177 | | - assertEquals("", requestedUrls.get(0)); |
178 | | - assertEquals("second/", requestedUrls.get(1)); |
179 | | - assertEquals(URL_THIRD.toString(), requestedUrls.get(2)); |
180 | | - assertEquals(URL_THIRD, conn.getLastWebRequest().getUrl()); |
181 | | - } |
182 | | - |
183 | | - /** |
184 | | - * Verifies that if an image has an <tt>onload</tt> attribute, the <tt>onload</tt> handler |
185 | | - * does not get invoked if we can't download the image. |
186 | | - * @throws Exception if an error occurs |
187 | | - */ |
188 | | - @Test |
189 | | - public void onLoad_notCalledWhenImageNotDownloaded() throws Exception { |
190 | | - final String html = "<html><body><img src='" + URL_SECOND + "' onload='alert(1)'></body></html>"; |
191 | | - |
192 | | - final MockWebConnection conn = getMockWebConnection(); |
193 | | - conn.setResponse(URL_SECOND, "foo", 404, "Not Found", MimeType.TEXT_HTML, new ArrayList<NameValuePair>()); |
194 | | - |
195 | | - loadPageWithAlerts(html); |
196 | | - |
197 | | - assertEquals(URL_SECOND, conn.getLastWebRequest().getUrl()); |
198 | | - } |
199 | | - |
200 | 35 | /** |
201 | 36 | * Verifies that if an image has an <tt>onload</tt> attribute but javascript is disabled, |
202 | 37 | * the image is not downloaded. |
|
0 commit comments