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 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  import org.openqa.selenium.WebDriver;
23  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
24  
25  /**
26   * Tests for {@link HtmlSvg}.
27   *
28   * @author Ahmed Ashour
29   * @author Frank Danek
30   */
31  @RunWith(BrowserRunner.class)
32  public class HtmlSvgTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts("[object SVGSVGElement]")
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' id='myId' version='1.1'>\n"
50              + "  </svg>\n"
51              + "</body></html>";
52  
53          final WebDriver driver = loadPageVerifyTitle2(html);
54          if (driver instanceof HtmlUnitDriver) {
55              final HtmlPage page = (HtmlPage) getEnclosedPage();
56              assertTrue(HtmlSvg.class.isInstance(page.getElementById("myId")));
57          }
58      }
59  
60      /**
61       * @throws Exception if the test fails
62       */
63      @Test
64      @Alerts("false")
65      public void style() throws Exception {
66          final String html = DOCTYPE_HTML
67              + "<html><body>\n"
68              + "  <svg xmlns='http://www.w3.org/2000/svg' id='myId' version='1.1'>\n"
69              + "  </svg>\n"
70              + "<script>\n"
71              + LOG_TITLE_FUNCTION
72              + "    log(document.getElementById('myId').style == undefined);\n"
73              + "</script>\n"
74              + "</body></html>";
75  
76          loadPageVerifyTitle2(html);
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts({"function", "function"})
84      public void functions() throws Exception {
85          final String html = DOCTYPE_HTML
86              + "<html><body>\n"
87              + "  <svg xmlns='http://www.w3.org/2000/svg' id='myId' version='1.1'>\n"
88              + "  </svg>\n"
89              + "<script>\n"
90              + LOG_TITLE_FUNCTION
91              + "  var svg =  document.getElementById('myId');\n"
92              + "  log(typeof svg.getScreenCTM);\n"
93              + "  log(typeof svg.createSVGMatrix);\n"
94              + "</script>\n"
95              + "</body></html>";
96  
97          loadPageVerifyTitle2(html);
98      }
99  
100     /**
101      * @throws Exception if the test fails
102      */
103     @Test
104     @Alerts("[object SVGMatrix]")
105     public void getScreenCTM() throws Exception {
106         final String html = DOCTYPE_HTML
107             + "<html><body>\n"
108             + "  <svg xmlns='http://www.w3.org/2000/svg' id='myId' version='1.1'>\n"
109             + "  </svg>\n"
110             + "<script>\n"
111             + LOG_TITLE_FUNCTION
112             + "  var svg =  document.getElementById('myId');\n"
113             + "  try {\n"
114             + "    log(svg.getScreenCTM());\n"
115             + "  } catch(e) { logEx(e); }\n"
116             + "</script>\n"
117             + "</body></html>";
118 
119         loadPageVerifyTitle2(html);
120     }
121 }