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.net.URL;
18  
19  import org.htmlunit.SimpleWebTestCase;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link HtmlLink}.
25   *
26   * @author Ahmed Ashour
27   * @author Marc Guillemot
28   */
29  public class HtmlLinkTest extends SimpleWebTestCase {
30  
31      /**
32       * @throws Exception if an error occurs
33       */
34      @Test
35      @Alerts("body onLoad")
36      public void onLoad() throws Exception {
37          getWebClientWithMockWebConnection().getOptions().setCssEnabled(false);
38          getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.css"), "");
39  
40          final String html = DOCTYPE_HTML
41                  + "<html>\n"
42                  + "<head>\n"
43                  + "  <link rel='stylesheet' href='simple.css' "
44                          + "onload='alert(\"onLoad\")' onerror='alert(\"onError\")'>\n"
45                  + "</head>\n"
46                  + "<body onload='alert(\"body onLoad\")'>\n"
47                  + "</body>\n"
48                  + "</html>";
49  
50          loadPageWithAlerts(html);
51          assertEquals(1, getMockWebConnection().getRequestCount());
52      }
53  
54      /**
55       * @throws Exception if an error occurs
56       */
57      @Test
58      public void onLoadDynamic() throws Exception {
59          getWebClientWithMockWebConnection().getOptions().setCssEnabled(false);
60          getMockWebConnection().setResponse(new URL(URL_FIRST, "simple.css"), "");
61  
62          final String html = DOCTYPE_HTML
63                  + "<html>\n"
64                  + "<head>\n"
65                  + "  <script>\n"
66                  + "    function test() {\n"
67                  + "      var dynLink = document.createElement('link');\n"
68                  + "      dynLink.rel = 'stylesheet';\n"
69                  + "      dynLink.type = 'text/css';\n"
70                  + "      dynLink.href = 'simple.css';"
71                  + "      dynLink.onload = function (e) { log(\"onLoad \" + e) };\n"
72                  + "      dynLink.onerror = function (e) { log(\"onError \" + e) };\n"
73                  + "      document.head.appendChild(dynLink);\n"
74  
75                  + "      var dynLink = document.createElement('link');\n"
76                  + "      dynLink.rel = 'stylesheet';\n"
77                  + "      dynLink.type = 'text/css';\n"
78                  + "      dynLink.href = 'unknown.css';"
79                  + "      dynLink.onload = function (e) { log(\"onLoad \" + e) };\n"
80                  + "      dynLink.onerror = function (e) { log(\"onError \" + e) };\n"
81                  + "      document.head.appendChild(dynLink);\n"
82                  + "    }\n"
83  
84                  + "    function log(x) {\n"
85                  + "      document.getElementById('log').value += x + '\\n';\n"
86                  + "    }\n"
87                  + "  </script>\n"
88                  + "</head>\n"
89                  + "<body onload='test()'></body>\n"
90                  + "  <textarea id='log' cols='80' rows='40'></textarea>\n"
91                  + "</body>\n"
92                  + "</html>";
93  
94          final HtmlPage page = loadPageWithAlerts(html);
95          final String text = page.getElementById("log").getAttribute("value").trim().replaceAll("\r", "");
96          assertEquals(String.join("\n", getExpectedAlerts()), text);
97  
98          assertEquals(1, getMockWebConnection().getRequestCount());
99      }
100 }