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.event;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  import org.openqa.selenium.WebDriver;
21  
22  /**
23   * Tests for {@link EventHandler}.
24   *
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   */
28  public class EventHandlerTest extends WebDriverTestCase {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      public void caller() 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(test.caller);\n"
41              + "  }\n"
42              + "</script></head>\n"
43              + "<body onload='test()'>\n"
44              + "</body></html>";
45  
46          final WebDriver driver = loadPage2(html);
47          assertTrue(driver.getTitle().contains("function onload(event)"));
48      }
49  
50      /**
51       * @throws Exception if the test fails
52       */
53      @Test
54      @Alerts({"function onload(event) { test() }",
55               "function onload(event) { test() }",
56               "function onload(event) { test() }"})
57      public void testToString() throws Exception {
58          final String html = DOCTYPE_HTML
59              + "<html><head>\n"
60              + "<script>\n"
61              + LOG_TITLE_FUNCTION
62              + "  function test() {\n"
63              + "    var e = test.caller;\n"
64              + "    log(e);\n"
65              + "    log('' + e);\n"
66              + "    log(e.toString());\n"
67              + "  }\n"
68              + "</script></head>\n"
69              + "<body onload='test()'>\n"
70              + "</body></html>";
71  
72          loadPageVerifyTitle2(html);
73      }
74  
75      /**
76       * @throws Exception if the test fails
77       */
78      @Test
79      @Alerts({"function onload(event) { test() }",
80               "function onload(event) { test() }",
81               "function onload(event) { test() }"})
82      public void testToStringWhitespace() throws Exception {
83          final String html = DOCTYPE_HTML
84              + "<html><head>\n"
85              + "<script>\n"
86              + LOG_TITLE_FUNCTION
87              + "  function test() {\n"
88              + "    var e = test.caller;\n"
89              + "    log(e);\n"
90              + "    log('' + e);\n"
91              + "    log(e.toString());\n"
92              + "  }\n"
93              + "</script></head>\n"
94              + "<body onload='  test() \t \n'>\n"
95              + "</body></html>";
96  
97          loadPageVerifyTitle2(html);
98      }
99  
100     /**
101      * @throws Exception if the test fails
102      */
103     @Test
104     @Alerts({"function onload(event) { test() // comment }",
105              "function onload(event) { test() // comment }",
106              "function onload(event) { test() // comment }"})
107     public void testToStringCommentAtEnd() throws Exception {
108         final String html = DOCTYPE_HTML
109             + "<html><head>\n"
110             + "<script>\n"
111             + LOG_TITLE_FUNCTION
112             + "  function test() {\n"
113             + "    var e = test.caller;\n"
114             + "    log(e);\n"
115             + "    log('' + e);\n"
116             + "    log(e.toString());\n"
117             + "  }\n"
118             + "</script></head>\n"
119             + "<body onload='test() // comment'>\n"
120             + "</body></html>";
121 
122         loadPageVerifyTitle2(html);
123     }
124 }