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 SVGTextPathElement}.
24   *
25   * @author Ronald Brill
26   */
27  public class SVGTextPathElementTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts("function SVGTextPathElement() { [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.SVGTextPathElement);\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 SVGTextPathElement]", "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              + "  <defs>\n"
70              + "    <path id='myTextPath' d='M 50,100 Q 150,50 250,100' />\n"
71              + "  </defs>\n"
72              + "  <text fill='steelblue' font-size='20'>\n"
73              + "    <textPath id='myId' xlink:href='#myTextPath'>Heho</textPath>\n"
74              + "  </text>\n"
75              + "</svg>\n"
76              + "</body></html>";
77  
78          loadPageVerifyTitle2(html);
79      }
80  
81      /**
82       * @throws Exception if the test fails
83       */
84      @Test
85      @Alerts({"[object SVGTextPathElement]", "43.3"})
86      @HtmlUnitNYI(CHROME = {"[object SVGTextPathElement]", "40.0"},
87              EDGE = {"[object SVGTextPathElement]", "40.0"},
88              FF = {"[object SVGTextPathElement]", "40.0"},
89              FF_ESR = {"[object SVGTextPathElement]", "40.0"})
90      public void getComputedTextLength() throws Exception {
91          final String html = DOCTYPE_HTML
92              + "<html><head>\n"
93              + "<script>\n"
94              + LOG_TITLE_FUNCTION
95              + "  function test() {\n"
96              + "    if (window.SVGTextElement) {\n"
97              + "      var text = document.getElementById('myId');\n"
98              + "      log(text);\n"
99              + "      var length = text.getComputedTextLength();\n"
100             + "      log(length.toFixed(1));\n"
101             + "    }\n"
102             + "  }\n"
103             + "</script>\n"
104             + "</head><body onload='test()'>\n"
105             + "<svg width='100%' height='100%' viewBox='0 0 400 400' xmlns='http://www.w3.org/2000/svg'>\n"
106             + "  <defs>\n"
107             + "    <path id='myTextPath' d='M 50,100 Q 150,50 250,100' />\n"
108             + "  </defs>\n"
109             + "  <text fill='steelblue' font-size='20'>\n"
110             + "    <textPath id='myId' xlink:href='#myTextPath'>Heho</textPath>\n"
111             + "  </text>\n"
112             + "</svg>\n"
113             + "</body></html>";
114 
115         loadPageVerifyTitle2(html);
116     }
117 }