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.htmlunit.junit.annotation.HtmlUnitNYI;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  
24  /**
25   * Tests for {@link SVGTextElement}.
26   *
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class SVGTextElementTest extends WebDriverTestCase {
31  
32      /**
33       * @throws Exception if the test fails
34       */
35      @Test
36      @Alerts("function SVGTextElement() { [native code] }")
37      public void simpleScriptable() throws Exception {
38          final String html = DOCTYPE_HTML
39              + "<html><head>\n"
40              + "<script>\n"
41              + LOG_TITLE_FUNCTION
42              + "  function test() {\n"
43              + "    log(window.SVGTextElement);\n"
44              + "  }\n"
45              + "</script>\n"
46              + "</head><body onload='test()'>\n"
47              + "</body></html>";
48  
49          loadPageVerifyTitle2(html);
50      }
51  
52      /**
53       * @throws Exception if the test fails
54       */
55      @Test
56      @Alerts({"[object SVGTextElement]", "true"})
57      public void getComputedTextLengthAvailable() throws Exception {
58          final String html = DOCTYPE_HTML
59              + "<html><head>\n"
60              + "<script>\n"
61              + LOG_TITLE_FUNCTION
62              + "  function test() {\n"
63              + "    if (window.SVGPathElement) {\n"
64              + "      var text = document.getElementById('myId');\n"
65              + "      log(text);\n"
66              + "      log(text.getComputedTextLength() > 0);\n"
67              + "    }\n"
68              + "  }\n"
69              + "</script>\n"
70              + "</head><body onload='test()'>\n"
71              + "<svg width='100%' height='100%' viewBox='0 0 400 400' xmlns='http://www.w3.org/2000/svg'>\n"
72              + "  <text id='myId' x='0' y='4' fill='orange'>HtmlUnit is great!</text>\n"
73              + "</svg>\n"
74              + "</body></html>";
75  
76          loadPageVerifyTitle2(html);
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts({"[object SVGTextElement]", "117.3"})
84      @HtmlUnitNYI(CHROME = {"[object SVGTextElement]", "180.0"},
85              EDGE = {"[object SVGTextElement]", "180.0"},
86              FF = {"[object SVGTextElement]", "180.0"},
87              FF_ESR = {"[object SVGTextElement]", "180.0"})
88      public void getComputedTextLength() throws Exception {
89          final String html = DOCTYPE_HTML
90              + "<html><head>\n"
91              + "<script>\n"
92              + LOG_TITLE_FUNCTION
93              + "  function test() {\n"
94              + "    if (window.SVGTextElement) {\n"
95              + "      var text = document.getElementById('myId');\n"
96              + "      log(text);\n"
97              + "      var length = text.getComputedTextLength();\n"
98              + "      log(length.toFixed(1));\n"
99              + "    }\n"
100             + "  }\n"
101             + "</script>\n"
102             + "</head><body onload='test()'>\n"
103             + "<svg width='100%' height='100%' viewBox='0 0 400 400' xmlns='http://www.w3.org/2000/svg'>\n"
104             + "  <text id='myId' x='0' y='4' fill='orange'>HtmlUnit is great!</text>\n"
105             + "</svg>\n"
106             + "</body></html>";
107 
108         loadPageVerifyTitle2(html);
109     }
110 }