View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import java.io.InputStream;
18  import java.net.URL;
19  import java.util.Collections;
20  
21  import org.apache.commons.io.IOUtils;
22  import org.htmlunit.WebDriverTestCase;
23  import org.htmlunit.http.HttpStatus;
24  import org.htmlunit.junit.annotation.Alerts;
25  import org.htmlunit.junit.annotation.HtmlUnitNYI;
26  import org.htmlunit.util.MimeType;
27  import org.junit.jupiter.api.Test;
28  import org.openqa.selenium.By;
29  import org.openqa.selenium.WebDriver;
30  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
31  import org.openqa.selenium.interactions.Actions;
32  
33  /**
34   * Tests for {@link HtmlImage}.
35   *
36   * @author Ronald Brill
37   */
38  public class HtmlImage2Test extends WebDriverTestCase {
39  
40      /**
41       * @throws Exception if the test fails
42       */
43      @Test
44      @Alerts("1")
45      public void loadImageWithoutSource() throws Exception {
46          loadImage("");
47          loadImageInnerHtml("");
48          loadImageImportNodeHtml("");
49      }
50  
51      /**
52       * @throws Exception if the test fails
53       */
54      @Test
55      @Alerts("1")
56      public void loadImageEmptySource() throws Exception {
57          loadImage("src=''");
58          loadImageInnerHtml("src=''");
59          loadImageImportNodeHtml("src=''");
60      }
61  
62      /**
63       * @throws Exception if the test fails
64       */
65      @Test
66      @Alerts(DEFAULT = "1",
67              FF = "2",
68              FF_ESR = "2")
69      public void loadImageBlankSource() throws Exception {
70          loadImage("src=' '");
71          loadImageInnerHtml("src=' '");
72          loadImageImportNodeHtml("src=' '");
73      }
74  
75      /**
76       * @throws Exception if the test fails
77       */
78      @Test
79      @Alerts("2")
80      public void loadImage() throws Exception {
81          loadImage("src='img.jpg'");
82          loadImageInnerHtml("src='img.jpg'");
83          loadImageImportNodeHtml("src='img.jpg'");
84      }
85  
86      /**
87       * @throws Exception if the test fails
88       */
89      @Test
90      @Alerts("2")
91      public void loadImageUnknown() throws Exception {
92          loadImage("src='unknown'");
93          loadImageInnerHtml("src='unknown'");
94      }
95  
96      /**
97       * @throws Exception if the test fails
98       */
99      @Test
100     @Alerts(DEFAULT = "2",
101             CHROME = "1",
102             EDGE = "1")
103     @HtmlUnitNYI(CHROME = "2",
104             EDGE = "2")
105     public void loadImageUnknown2() throws Exception {
106         loadImageImportNodeHtml("src='unknown'");
107     }
108 
109     /**
110      * @throws Exception if the test fails
111      */
112     @Test
113     @Alerts("1")
114     public void loadImageBrokenUrl() throws Exception {
115         loadImage("src='rbri://nowhere'");
116         loadImageInnerHtml("src='rbri://nowhere'");
117         loadImageImportNodeHtml("src='rbri://nowhere'");
118     }
119 
120     /**
121      * @throws Exception if the test fails
122      */
123     @Test
124     @Alerts("1")
125     public void loadImageAboutBlank() throws Exception {
126         loadImage("src='about:blank'");
127         loadImageInnerHtml("src='about:blank'");
128         loadImageImportNodeHtml("src='about:blank'");
129     }
130 
131     /**
132      * @throws Exception if the test fails
133      */
134     @Test
135     @Alerts("1")
136     public void loadImageAboutX() throws Exception {
137         loadImage("src='about:x'");
138         loadImageInnerHtml("src='about:x'");
139         loadImageImportNodeHtml("src='about:x'");
140     }
141 
142     /**
143      * @throws Exception if the test fails
144      */
145     @Test
146     @Alerts("2")
147     public void loadImageWrongType() throws Exception {
148         loadImage("src='" + URL_FIRST + "'");
149         loadImageInnerHtml("src='" + URL_FIRST + "'");
150     }
151 
152     /**
153      * @throws Exception if the test fails
154      */
155     @Test
156     @Alerts(DEFAULT = "2",
157             CHROME = "1",
158             EDGE = "1")
159     @HtmlUnitNYI(CHROME = "2",
160             EDGE = "2")
161     public void loadImageWrongType2() throws Exception {
162         loadImageImportNodeHtml("src='" + URL_FIRST + "'");
163     }
164 
165     private void loadImage(final String src) throws Exception {
166         getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
167 
168         try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
169             final byte[] directBytes = IOUtils.toByteArray(is);
170             final URL urlImage = new URL(URL_FIRST, "img.jpg");
171             getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", Collections.emptyList());
172         }
173 
174         final String html = DOCTYPE_HTML
175             + "<html><head>\n"
176             + "<script>\n"
177             + "  function test() {\n"
178             + "    var img = document.getElementById('myImage');\n"
179             + "  }\n"
180             + "</script>\n"
181             + "</head><body onload='test()'>\n"
182             + "  <img id='myImage' " + src + " >\n"
183             + "</body></html>";
184 
185         final int count = getMockWebConnection().getRequestCount();
186         final WebDriver driver = getWebDriver();
187         if (driver instanceof HtmlUnitDriver) {
188             ((HtmlUnitDriver) driver).setDownloadImages(true);
189         }
190         loadPage2(html);
191         assertEquals(Integer.parseInt(getExpectedAlerts()[0]), getMockWebConnection().getRequestCount() - count);
192     }
193 
194     private void loadImageInnerHtml(final String src) throws Exception {
195         try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
196             final byte[] directBytes = IOUtils.toByteArray(is);
197             final URL urlImage = new URL(URL_FIRST, "img.jpg");
198             getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", Collections.emptyList());
199         }
200 
201         final String html = DOCTYPE_HTML
202             + "<html><head>\n"
203             + "<script>\n"
204             + "  function test() {\n"
205             + "    var tester = document.getElementById('tester');\n"
206             + "    tester.innerHTML = \"<img id='myImage' " + src + " >\";\n"
207             + "  }\n"
208             + "</script>\n"
209             + "</head><body>\n"
210             + "  <button id='test' onclick='test()'>Test</button>\n"
211             + "  <div id='tester'></div>\n"
212             + "</body></html>";
213 
214         final int count = getMockWebConnection().getRequestCount();
215         final WebDriver driver = getWebDriver();
216         if (driver instanceof HtmlUnitDriver) {
217             ((HtmlUnitDriver) driver).setDownloadImages(true);
218         }
219         loadPage2(html);
220 
221         driver.findElement(By.id("test")).click();
222         assertEquals(Integer.parseInt(getExpectedAlerts()[0]), getMockWebConnection().getRequestCount() - count);
223     }
224 
225     private void loadImageImportNodeHtml(final String src) throws Exception {
226         getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
227 
228         try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
229             final byte[] directBytes = IOUtils.toByteArray(is);
230             final URL urlImage = new URL(URL_FIRST, "img.jpg");
231             getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", Collections.emptyList());
232         }
233 
234         final String html = DOCTYPE_HTML
235             + "<html><head>\n"
236             + "<script>\n"
237             + "  function test() {\n"
238             + "    var tester = document.getElementById('tester');\n"
239 
240             + "    var doc = document.implementation.createHTMLDocument('test');\n"
241             + "    doc.body.innerHTML = \"<img id='myImage' " + src + " >\";\n"
242 
243             + "    var srcNode = doc.getElementById('myImage');\n"
244             + "    var newNode = document.importNode(srcNode, true);\n"
245             + "    document.body.replaceChild(newNode, tester);\n"
246             + "    alert('before');\n"
247             + "  }\n"
248             + "</script>\n"
249             + "</head><body>\n"
250             + "  <button id='test' onclick='test()'>Test</button>\n"
251             + "  <div id='tester'></div>\n"
252             + "</body></html>";
253 
254         final int count = getMockWebConnection().getRequestCount();
255         final WebDriver driver = getWebDriver();
256         if (driver instanceof HtmlUnitDriver) {
257             ((HtmlUnitDriver) driver).setDownloadImages(true);
258         }
259         loadPage2(html);
260 
261         driver.findElement(By.id("test")).click();
262         verifyAlerts(driver, "before");
263 
264         assertEquals(Integer.parseInt(getExpectedAlerts()[0]), getMockWebConnection().getRequestCount() - count);
265     }
266 
267     /**
268      * @throws Exception if an error occurs
269      */
270     @Test
271     @Alerts("true")
272     public void isDisplayed() throws Exception {
273         isDisplayed("src='img.jpg'");
274     }
275 
276     /**
277      * @throws Exception if an error occurs
278      */
279     @Test
280     @Alerts("false")
281     public void isDisplayedNoSource() throws Exception {
282         isDisplayed("");
283     }
284 
285     /**
286      * @throws Exception if an error occurs
287      */
288     @Test
289     @Alerts("false")
290     public void isDisplayedEmptySource() throws Exception {
291         isDisplayed("src=''");
292     }
293 
294     /**
295      * @throws Exception if an error occurs
296      */
297     @Test
298     @Alerts(DEFAULT = "true",
299             CHROME = "false",
300             EDGE = "false")
301     public void isDisplayedBlankSource() throws Exception {
302         isDisplayed("src=' '");
303     }
304 
305     /**
306      * @throws Exception if an error occurs
307      */
308     @Test
309     @Alerts("true")
310     public void isDisplayedInvalidSource() throws Exception {
311         isDisplayed("src='unknown.gif'");
312     }
313 
314     /**
315      * @throws Exception if an error occurs
316      */
317     @Test
318     @Alerts("true")
319     public void isDisplayedWrongType() throws Exception {
320         isDisplayed("src='" + URL_FIRST + "'");
321     }
322 
323     /**
324      * @throws Exception if an error occurs
325      */
326     @Test
327     @Alerts("false")
328     public void isDisplayedDisplayNone() throws Exception {
329         isDisplayed("src='img.jpg' style='display: none'");
330     }
331 
332     private void isDisplayed(final String src) throws Exception {
333         try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
334             final byte[] directBytes = IOUtils.toByteArray(is);
335             final URL urlImage = new URL(URL_FIRST, "img.jpg");
336             getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", Collections.emptyList());
337 
338             getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
339         }
340 
341         final String html = DOCTYPE_HTML
342                 + "<html><head><title>Page A</title></head>\n"
343                 + "<body>\n"
344                 + "  <img id='myImg' " + src + " >\n"
345                 + "</body></html>";
346 
347         final WebDriver driver = loadPage2(html);
348 
349         final boolean displayed = driver.findElement(By.id("myImg")).isDisplayed();
350         assertEquals(Boolean.parseBoolean(getExpectedAlerts()[0]), displayed);
351     }
352 
353     /**
354      * @throws Exception if the test fails
355      */
356     @Test
357     @Alerts("§§URL§§img.gif")
358     public void src() throws Exception {
359         final String html = DOCTYPE_HTML
360             + "<html><head>\n"
361             + "<script>\n"
362             + LOG_TITLE_FUNCTION
363             + "  function test() {\n"
364             + "    var img = document.getElementById('myImg');\n"
365             + "    log(img.src);\n"
366             + "  }\n"
367             + "</script>\n"
368             + "</head>\n"
369             + "<body onload='test()'>\n"
370             + "  <img id='myImg' src='img.gif'>\n"
371             + "</body>\n"
372             + "</html>";
373 
374         expandExpectedAlertsVariables(URL_FIRST);
375         loadPageVerifyTitle2(html);
376     }
377 
378     /**
379      * @throws Exception if the test fails
380      */
381     @Test
382     @Alerts({"1", "§§URL§§abcd/img.gif"})
383     public void lineBreaksInUrl() throws Exception {
384         try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-gif.img")) {
385             final byte[] directBytes = IOUtils.toByteArray(is);
386             final URL urlImage = new URL(URL_SECOND, "abcd/img.gif");
387             getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok",
388                                                 MimeType.IMAGE_GIF, Collections.emptyList());
389         }
390 
391         final String html = DOCTYPE_HTML
392             + "<html><head>\n"
393             + "<script>\n"
394             + LOG_TITLE_FUNCTION
395             + "  function test() {\n"
396             + "    var img = document.getElementById('myImg');\n"
397             + "    img.width;\n" // this forces image loading in htmlunit
398             + "    log(img.width);\n"
399             + "    log(img.src);\n"
400             + "  }\n"
401             + "</script>\n"
402             + "</head>\n"
403             + "<body onload='test()'>\n"
404             + "  <img id='myImg' src='" + URL_SECOND + "a\rb\nc\r\nd/img.gif' onError='log(\"broken\");'>\n"
405             + "</body>\n"
406             + "</html>";
407 
408         expandExpectedAlertsVariables(URL_SECOND);
409         loadPageVerifyTitle2(html);
410     }
411 
412     /**
413      * @throws Exception if the test fails
414      */
415     @Test
416     @Alerts(CHROME = {"58", "29", "76", "178"},
417             EDGE = {"58", "29", "80", "165"},
418             FF = {"58", "29", "70", "119"},
419             FF_ESR = {"58", "29", "70", "119"})
420     @HtmlUnitNYI(CHROME = {"18", "18", "18", "18"},
421             EDGE = {"18", "18", "18", "18"},
422             FF = {"18", "18", "18", "18"},
423             FF_ESR = {"18", "18", "18", "18"})
424     public void clickWithCoordinates() throws Exception {
425         try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-gif.img")) {
426             final byte[] directBytes = IOUtils.toByteArray(is);
427             final URL urlImage = new URL(URL_SECOND, "img.gif");
428             getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok",
429                                         MimeType.IMAGE_GIF, Collections.emptyList());
430         }
431 
432         final String html = DOCTYPE_HTML
433             + "<html><head>\n"
434             + "<script>\n"
435             + LOG_TITLE_FUNCTION
436             + "  function clickImage(event) {\n"
437             + "    log(event.clientX);\n"
438             + "    log(event.clientY);\n"
439             + "    log(event.screenX);\n"
440             + "    log(event.screenY);\n"
441             + "  }\n"
442             + "</script>\n"
443             + "</head>\n"
444             + "<body>\n"
445             + "  <img id='myImg' src='" + URL_SECOND + "img.gif' "
446                     + "width='100px' height='42px' onclick='clickImage(event)'>\n"
447             + "</body>\n"
448             + "</html>";
449 
450         final WebDriver driver = loadPage2(html);
451 
452         final Actions actions = new Actions(driver);
453         // this requires a webdriver change
454         actions.moveToElement(driver.findElement(By.id("myImg")), 0, 0).click().build().perform();
455 
456         verifyTitle2(driver, getExpectedAlerts());
457     }
458 
459     /**
460      * @throws Exception on test failure
461      */
462     @Test
463     @Alerts("error [object HTMLImageElement]")
464     public void addEventListener_NoContent() throws Exception {
465         addEventListener(HttpStatus.NO_CONTENT_204);
466     }
467 
468     /**
469      * @throws Exception on test failure
470      */
471     @Test
472     @Alerts("error [object HTMLImageElement]")
473     public void addEventListener_BadRequest() throws Exception {
474         addEventListener(HttpStatus.BAD_REQUEST_400);
475     }
476 
477     /**
478      * @throws Exception on test failure
479      */
480     @Test
481     @Alerts("error [object HTMLImageElement]")
482     public void addEventListener_Forbidden() throws Exception {
483         addEventListener(HttpStatus.FORBIDDEN_403);
484     }
485 
486     /**
487      * @throws Exception on test failure
488      */
489     @Test
490     @Alerts("error [object HTMLImageElement]")
491     public void addEventListener_NotFound() throws Exception {
492         addEventListener(HttpStatus.NOT_FOUND_404);
493     }
494 
495     /**
496      * @throws Exception on test failure
497      */
498     @Test
499     @Alerts("error [object HTMLImageElement]")
500     public void addEventListener_MethodNotAllowed() throws Exception {
501         addEventListener(HttpStatus.METHOD_NOT_ALLOWED_405);
502     }
503 
504     /**
505      * @throws Exception on test failure
506      */
507     @Test
508     @Alerts("error [object HTMLImageElement]")
509     public void addEventListener_NotAcceptable() throws Exception {
510         addEventListener(HttpStatus.NOT_ACCEPTABLE_406);
511     }
512 
513     /**
514      * @throws Exception on test failure
515      */
516     @Test
517     @Alerts("error [object HTMLImageElement]")
518     public void addEventListener_ProxyAuthRequired() throws Exception {
519         addEventListener(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407);
520     }
521 
522     /**
523      * @throws Exception on test failure
524      */
525     @Test
526     @Alerts("error [object HTMLImageElement]")
527     public void addEventListener_RequestTimeout() throws Exception {
528         addEventListener(HttpStatus.REQUEST_TIMEOUT_408);
529     }
530 
531     /**
532      * @throws Exception on test failure
533      */
534     @Test
535     @Alerts("error [object HTMLImageElement]")
536     public void addEventListener_Conflict() throws Exception {
537         addEventListener(HttpStatus.CONFLICT_409);
538     }
539 
540     /**
541      * @throws Exception on test failure
542      */
543     @Test
544     @Alerts("error [object HTMLImageElement]")
545     public void addEventListener_Gone() throws Exception {
546         addEventListener(HttpStatus.GONE_410);
547     }
548 
549     /**
550      * @throws Exception on test failure
551      */
552     @Test
553     @Alerts("error [object HTMLImageElement]")
554     public void addEventListener_LengthRequired() throws Exception {
555         addEventListener(HttpStatus.LENGTH_REQUIRED_411);
556     }
557 
558     /**
559      * @throws Exception on test failure
560      */
561     @Test
562     @Alerts("error [object HTMLImageElement]")
563     public void addEventListener_PreconditionFailed() throws Exception {
564         addEventListener(HttpStatus.PRECONDITION_FAILED_412);
565     }
566 
567     /**
568      * @throws Exception on test failure
569      */
570     @Test
571     @Alerts("error [object HTMLImageElement]")
572     public void addEventListener_PayloadTooLarge() throws Exception {
573         addEventListener(HttpStatus.PAYLOAD_TOO_LARGE_413);
574     }
575 
576     /**
577      * @throws Exception on test failure
578      */
579     @Test
580     @Alerts("error [object HTMLImageElement]")
581     public void addEventListener_UriTooLong() throws Exception {
582         addEventListener(HttpStatus.URI_TOO_LONG_414);
583     }
584 
585     /**
586      * @throws Exception on test failure
587      */
588     @Test
589     @Alerts("error [object HTMLImageElement]")
590     public void addEventListener_UnsupportedMediaType() throws Exception {
591         addEventListener(HttpStatus.UNSUPPORTED_MEDIA_TYPE_415);
592     }
593 
594     /**
595      * @throws Exception on test failure
596      */
597     @Test
598     @Alerts("error [object HTMLImageElement]")
599     public void addEventListener_RangeNotSatisfiable() throws Exception {
600         addEventListener(HttpStatus.RANGE_NOT_SATISFIABLE_416);
601     }
602 
603     /**
604      * @throws Exception on test failure
605      */
606     @Test
607     @Alerts("error [object HTMLImageElement]")
608     public void addEventListener_ExpectationFailed() throws Exception {
609         addEventListener(HttpStatus.EXPECTATION_FAILED_417);
610     }
611 
612     /**
613      * @throws Exception on test failure
614      */
615     @Test
616     @Alerts("error [object HTMLImageElement]")
617     public void addEventListener_ImaTeapot() throws Exception {
618         addEventListener(HttpStatus.IM_A_TEAPOT_418);
619     }
620 
621     /**
622      * @throws Exception on test failure
623      */
624     @Test
625     @Alerts("error [object HTMLImageElement]")
626     public void addEventListener_EnhanceYourCalm() throws Exception {
627         addEventListener(HttpStatus.ENHANCE_YOUR_CALM_420);
628     }
629 
630     /**
631      * @throws Exception on test failure
632      */
633     @Test
634     @Alerts("error [object HTMLImageElement]")
635     public void addEventListener_MisdirectedRequest() throws Exception {
636         addEventListener(HttpStatus.MISDIRECTED_REQUEST_421);
637     }
638 
639     /**
640      * @throws Exception on test failure
641      */
642     @Test
643     @Alerts("error [object HTMLImageElement]")
644     public void addEventListener_UnprocessableEntity() throws Exception {
645         addEventListener(HttpStatus.UNPROCESSABLE_ENTITY_422);
646     }
647 
648     /**
649      * @throws Exception on test failure
650      */
651     @Test
652     @Alerts("error [object HTMLImageElement]")
653     public void addEventListener_Locked() throws Exception {
654         addEventListener(HttpStatus.LOCKED_423);
655     }
656 
657     /**
658      * @throws Exception on test failure
659      */
660     @Test
661     @Alerts("error [object HTMLImageElement]")
662     public void addEventListener_FailedDependency() throws Exception {
663         addEventListener(HttpStatus.FAILED_DEPENDENCY_424);
664     }
665 
666     /**
667      * @throws Exception on test failure
668      */
669     @Test
670     @Alerts("error [object HTMLImageElement]")
671     public void addEventListener_UpgradeRequired() throws Exception {
672         addEventListener(HttpStatus.UPGRADE_REQUIRED_426);
673     }
674 
675     /**
676      * @throws Exception on test failure
677      */
678     @Test
679     @Alerts("error [object HTMLImageElement]")
680     public void addEventListener_PreconditionRequired() throws Exception {
681         addEventListener(HttpStatus.PRECONDITION_REQUIRED_428);
682     }
683 
684     /**
685      * @throws Exception on test failure
686      */
687     @Test
688     @Alerts("error [object HTMLImageElement]")
689     public void addEventListener_TooManyRedirects() throws Exception {
690         addEventListener(HttpStatus.TOO_MANY_REQUESTS_429);
691     }
692 
693     /**
694      * @throws Exception on test failure
695      */
696     @Test
697     @Alerts("error [object HTMLImageElement]")
698     public void addEventListener_RequestHeaderFieldsTooLarge() throws Exception {
699         addEventListener(HttpStatus.REQUEST_HEADER_FIELDS_TOO_LARGE_431);
700     }
701 
702     /**
703      * @throws Exception on test failure
704      */
705     @Test
706     @Alerts("error [object HTMLImageElement]")
707     public void addEventListener_UnavailableForLegalReasons() throws Exception {
708         addEventListener(HttpStatus.UNAVAILABLE_FOR_LEGAL_REASONS_451);
709     }
710 
711     /**
712      * @throws Exception on test failure
713      */
714     @Test
715     @Alerts("error [object HTMLImageElement]")
716     public void addEventListener_InternalServerError() throws Exception {
717         addEventListener(HttpStatus.INTERNAL_SERVER_ERROR_500);
718     }
719 
720     /**
721      * @throws Exception on test failure
722      */
723     @Test
724     @Alerts("error [object HTMLImageElement]")
725     public void addEventListener_NotImplemented() throws Exception {
726         addEventListener(HttpStatus.NOT_IMPLEMENTED_501);
727     }
728 
729     /**
730      * @throws Exception on test failure
731      */
732     @Test
733     @Alerts("error [object HTMLImageElement]")
734     public void addEventListener_BadGateway() throws Exception {
735         addEventListener(HttpStatus.BAD_GATEWAY_502);
736     }
737 
738     /**
739      * @throws Exception on test failure
740      */
741     @Test
742     @Alerts("error [object HTMLImageElement]")
743     public void addEventListener_ServiceUnavailable() throws Exception {
744         addEventListener(HttpStatus.SERVICE_UNAVAILABLE_503);
745     }
746 
747     /**
748      * @throws Exception on test failure
749      */
750     @Test
751     @Alerts("error [object HTMLImageElement]")
752     public void addEventListener_GatewayTimeout() throws Exception {
753         addEventListener(HttpStatus.GATEWAY_TIMEOUT_504);
754     }
755 
756     /**
757      * @throws Exception on test failure
758      */
759     @Test
760     @Alerts("error [object HTMLImageElement]")
761     public void addEventListener_HttpVersionNotSupported() throws Exception {
762         addEventListener(HttpStatus.HTTP_VERSION_NOT_SUPPORTED_505);
763     }
764 
765     /**
766      * @throws Exception on test failure
767      */
768     @Test
769     @Alerts("error [object HTMLImageElement]")
770     public void addEventListener_InsufficientStrorage() throws Exception {
771         addEventListener(HttpStatus.INSUFFICIENT_STORAGE_507);
772     }
773 
774     /**
775      * @throws Exception on test failure
776      */
777     @Test
778     @Alerts("error [object HTMLImageElement]")
779     public void addEventListener_LoopDetected() throws Exception {
780         addEventListener(HttpStatus.LOOP_DETECTED_508);
781     }
782 
783     /**
784      * @throws Exception on test failure
785      */
786     @Test
787     @Alerts("error [object HTMLImageElement]")
788     public void addEventListener_NotExtended() throws Exception {
789         addEventListener(HttpStatus.NOT_EXTENDED_510);
790     }
791 
792     /**
793      * @throws Exception on test failure
794      */
795     @Test
796     @Alerts("error [object HTMLImageElement]")
797     public void addEventListener_NetworkAuthenticationRequired() throws Exception {
798         addEventListener(HttpStatus.NETWORK_AUTHENTICATION_REQUIRED_511);
799     }
800 
801     private void addEventListener(final int statusCode) throws Exception {
802         // use always a different url to avoid caching effects
803         final URL scriptUrl = new URL(URL_SECOND, "" + System.currentTimeMillis() + ".png");
804 
805         final String html = DOCTYPE_HTML
806             + "<html><head>\n"
807             + "<script>\n"
808             + LOG_TITLE_FUNCTION
809             + "  function test() {\n"
810             + "    var s1 = document.createElement('img');\n"
811             + "    s1.src = '" + scriptUrl + "';\n"
812             + "    s1.addEventListener('load', function() { log('load'); }, false);\n"
813             + "    s1.addEventListener('error', function(event) { log(event.type + ' ' + event.target); }, false);\n"
814             + "    document.body.insertBefore(s1, document.body.firstChild);\n"
815             + "  }\n"
816             + "</script>\n"
817             + "</head>\n"
818             + "<body onload='test()'></body>\n"
819             + "</html>";
820 
821         getMockWebConnection().setResponse(scriptUrl, (String) null,
822                 statusCode, "test", MimeType.TEXT_JAVASCRIPT, null);
823         loadPageVerifyTitle2(html);
824     }
825 }