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