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