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.svg;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.html.DomElement;
19  import org.htmlunit.html.HtmlPage;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.jupiter.api.Test;
22  import org.openqa.selenium.WebDriver;
23  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
24  
25  /**
26   * Tests for {@link SvgElement}.
27   *
28   * @author Ahmed Ashour
29   * @author Frank Danek
30   * @author Ronald Brill
31   */
32  public class SvgElementTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts("[object SVGElement]")
39      public void simpleScriptable() throws Exception {
40          final String html = DOCTYPE_HTML
41              + "<html><head>\n"
42              + "<script>\n"
43              + LOG_TITLE_FUNCTION
44              + "  function test() {\n"
45              + "    log(document.getElementById('myId'));\n"
46              + "  }\n"
47              + "</script>\n"
48              + "</head><body onload='test()'>\n"
49              + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
50              + "    <invalid id='myId'/>\n"
51              + "  </svg>\n"
52              + "</body></html>";
53  
54          final WebDriver driver = loadPageVerifyTitle2(html);
55          if (driver instanceof HtmlUnitDriver) {
56              final HtmlPage page = (HtmlPage) getEnclosedPage();
57              if ("[object SVGElement]".equals(getExpectedAlerts()[0])) {
58                  assertTrue(SvgElement.class.getName().equals(page.getElementById("myId").getClass().getName()));
59              }
60              else {
61                  assertTrue(DomElement.class.getName().equals(page.getElementById("myId").getClass().getName()));
62              }
63          }
64      }
65  
66      /**
67       * @throws Exception if the test fails
68       */
69      @Test
70      @Alerts("true")
71      public void oninput() throws Exception {
72          final String html = DOCTYPE_HTML
73              + "<html>\n"
74              + "<head>\n"
75              + "  <script>\n"
76              + LOG_TITLE_FUNCTION
77              + "    function test() {\n"
78              + "      var testNode = document.getElementById('myId');\n"
79              + "      log('oninput' in document);\n"
80              + "    }\n"
81              + "  </script>\n"
82              + "</head>\n"
83              + "<body onload='test()'>\n"
84              + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
85              + "    <invalid id='myId'/>\n"
86              + "  </svg>\n"
87              + "</body>\n"
88              + "</html>";
89  
90          loadPageVerifyTitle2(html);
91      }
92  
93      /**
94       * @throws Exception if the test fails
95       */
96      @Test
97      @Alerts({"1", "myLine"})
98      public void querySelectorAll() throws Exception {
99          final String html = DOCTYPE_HTML
100             + "<html>\n"
101             + "<head>\n"
102             + "<style>\n"
103             + "  .red   {color:#FF0000;}\n"
104             + "</style>\n"
105             + "<script>\n"
106             + LOG_TITLE_FUNCTION
107             + "function test() {\n"
108             + "  var testNode = document.getElementById('myId');\n"
109             + "  var redTags = testNode.querySelectorAll('.red');\n"
110             + "  log(redTags.length);\n"
111             + "  log(redTags.item(0).id);\n"
112             + "}\n"
113             + "</script>\n"
114             + "</head>\n"
115             + "<body onload='test()'>\n"
116             + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
117             + "    <g id='myId'>\n"
118             + "      <line id='myLine' x1='0' y1='0' x2='2' y2='4' class='red' />\n"
119             + "    </g>\n"
120             + "  </svg>\n"
121             + "</body></html>";
122 
123         loadPageVerifyTitle2(html);
124     }
125 
126     /**
127      * @throws Exception if the test fails
128      */
129     @Test
130     @Alerts("[object SVGLineElement]")
131     public void querySelector() throws Exception {
132         final String html = DOCTYPE_HTML
133             + "<html>\n"
134             + "<head>\n"
135             + "<style>\n"
136             + "  .red   {color:#FF0000;}\n"
137             + "</style>\n"
138             + "<script>\n"
139             + LOG_TITLE_FUNCTION
140             + "function test() {\n"
141             + "  var testNode = document.getElementById('myId');\n"
142             + "  log(testNode.querySelector('.red'));\n"
143             + "}\n"
144             + "</script>\n"
145             + "</head>\n"
146             + "<body onload='test()'>\n"
147             + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
148             + "    <g id='myId'>\n"
149             + "      <line id='myLine' x1='0' y1='0' x2='2' y2='4' class='red' />\n"
150             + "    </g>\n"
151             + "  </svg>\n"
152             + "</body></html>";
153 
154         loadPageVerifyTitle2(html);
155     }
156 
157     /**
158      * @throws Exception if the test fails
159      */
160     @Test
161     @Alerts("[object SVGRect]")
162     public void getBBox() throws Exception {
163         final String html = DOCTYPE_HTML
164             + "<html>\n"
165             + "<head>\n"
166             + "<script>\n"
167             + LOG_TITLE_FUNCTION
168             + "function test() {\n"
169             + "  var testNode = document.getElementById('myId');\n"
170             + "  log(testNode.getBBox());\n"
171             + "}\n"
172             + "</script>\n"
173             + "</head>\n"
174             + "<body onload='test()'>\n"
175             + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
176             + "    <g id='myId'>\n"
177             + "      <line id='myLine' x1='0' y1='0' x2='2' y2='4' class='red' />\n"
178             + "    </g>\n"
179             + "  </svg>\n"
180             + "</body></html>";
181 
182         loadPageVerifyTitle2(html);
183     }
184 }