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