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.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   * Tests for {@link HTMLImageElement}.
35   *
36   * @author <a href="mailto:george@murnock.com">George Murnock</a>
37   * @author Marc Guillemot
38   * @author Ronald Brill
39   */
40  @RunWith(BrowserRunner.class)
41  public class HTMLImageElement2Test extends SimpleWebTestCase {
42  
43      /**
44       * Verifies that if an image has an <tt>onload</tt> attribute but javascript is disabled,
45       * the image is not downloaded.
46       * Issue: 3123380
47       * @throws Exception if an error occurs
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       * Make sure this works without an exception.
66       *
67       * @throws Exception on test failure
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      * Make sure this works without an exception.
114      *
115      * @throws Exception on test failure
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 }