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.svg;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Very simple test for SVG "support".
23   *
24   * @author Marc Guillemot
25   * @author Frank Danek
26   * @author Ronald Brill
27   */
28  public class SVGTest extends WebDriverTestCase {
29  
30      /**
31       * Test for issue 3313921.
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts("svgElem")
36      public void getAttribute() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html><body>\n"
39              + "<script>\n"
40              + LOG_TITLE_FUNCTION
41              + "try {\n"
42              + "  var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');\n"
43              + "  svg.setAttribute('id', 'svgElem');\n"
44              + "  document.body.appendChild(svg);\n"
45              + "  log(document.getElementById('svgElem').getAttribute('id'));\n"
46              + "} catch(e) { logEx(e); }\n"
47              + "</script></body></html>";
48  
49          loadPageVerifyTitle2(html);
50      }
51  
52      /**
53       * Test for issue 3313921.
54       * @throws Exception if the test fails
55       */
56      @Test
57      @Alerts("clicked")
58      public void triggerEvent() throws Exception {
59          final String html = DOCTYPE_HTML
60                  + "<html>\n"
61                  + "<head>\n"
62                  + "<script>\n"
63                  + LOG_TITLE_FUNCTION
64                  + "  function init() {\n"
65                  + "    try {\n"
66                  + "      var rect = document.getElementById('rect');\n"
67                  + "      rect.addEventListener('click', function() { log('clicked') });\n"
68  
69                  + "      var e = document.createEvent('MouseEvents');\n"
70                  + "      e.initEvent('click', true, false);\n"
71                  + "      document.getElementById('rect').dispatchEvent(e);\n"
72                  + "    } catch(e) { logEx(e); }\n"
73                  + "  }\n"
74                  + "</script>\n"
75                  + "</head>\n"
76                  + "<body onload='init()'>\n"
77                  + "<svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
78                  + "  <rect id='rect'  width='300' height='100' />\n"
79                  + "</svg>\n"
80                  + "</body>\n"
81                  + "</html>";
82  
83          loadPageVerifyTitle2(html);
84      }
85  }