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