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.svg;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.html.DomElement;
19  import org.htmlunit.html.HtmlPage;
20  import org.htmlunit.html.HtmlScript;
21  import org.htmlunit.junit.BrowserRunner;
22  import org.htmlunit.junit.annotation.Alerts;
23  import org.htmlunit.junit.annotation.HtmlUnitNYI;
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  import org.openqa.selenium.WebDriver;
27  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
28  
29  /**
30   * Tests for {@link SvgScript}.
31   *
32   * @author Ahmed Ashour
33   * @author Frank Danek
34   * @author Ronald Brill
35   */
36  @RunWith(BrowserRunner.class)
37  public class SvgScriptTest extends WebDriverTestCase {
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts("[object SVGScriptElement]")
44      public void simpleScriptable() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><head>\n"
47              + "<script>\n"
48              + LOG_TITLE_FUNCTION
49              + "  function test() {\n"
50              + "    log(document.getElementById('myId'));\n"
51              + "  }\n"
52              + "</script>\n"
53              + "</head><body onload='test()'>\n"
54              + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
55              + "    <script id='myId'></script>\n"
56              + "  </svg>\n"
57              + "</body></html>";
58  
59          final WebDriver driver = loadPageVerifyTitle2(html);
60          if (driver instanceof HtmlUnitDriver) {
61              final HtmlPage page = (HtmlPage) getEnclosedPage();
62              assertType(getExpectedAlerts()[0], page.getElementById("myId"));
63          }
64      }
65  
66      private void assertType(final String expectedAlert, final DomElement element) {
67          if ("[object SVGScriptElement]".equals(expectedAlert)) {
68              assertTrue(SvgScript.class.isInstance(element));
69          }
70          else {
71              assertTrue(HtmlScript.class.isInstance(element));
72          }
73      }
74  
75      /**
76       * @throws Exception if the test fails
77       */
78      @Test
79      @Alerts({"[object SVGScriptElement]", "[object HTMLScriptElement]"})
80      @HtmlUnitNYI(CHROME = {"[object SVGScriptElement]", "[object SVGScriptElement]"},
81              EDGE = {"[object SVGScriptElement]", "[object SVGScriptElement]"},
82              FF = {"[object SVGScriptElement]", "[object SVGScriptElement]"},
83              FF_ESR = {"[object SVGScriptElement]", "[object SVGScriptElement]"})
84      public void htmlOrSvg() throws Exception {
85          final String html = "<svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
86              + "<script id='id1'>\n"
87              + LOG_TITLE_FUNCTION
88              + "  function test() {\n"
89              + "    log(document.getElementById('id1'));\n"
90              + "    log(document.getElementById('id2'));\n"
91              + "  }\n"
92              + "</script>\n"
93              + "<body onload='test()'>\n"
94              + "  <script id='id2'></script>\n"
95              + "</body>\n"
96              + "</svg>";
97  
98          final WebDriver driver = loadPageVerifyTitle2(html);
99          if (driver instanceof HtmlUnitDriver) {
100             final HtmlPage page = (HtmlPage) getEnclosedPage();
101             assertType(getExpectedAlerts()[0], page.getElementById("id1"));
102             assertType(getExpectedAlerts()[1], page.getElementById("id2"));
103         }
104     }
105 }