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