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