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  
21  /**
22   * Tests for {@link EventListenersContainer}.
23   *
24   * @author Ahmed Ashour
25   * @author Ronald Brill
26   */
27  public class EventListenersContainerTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts({"someName", "myevent", "[object Window]"})
34      public void addEventListener() throws Exception {
35          final String html = DOCTYPE_HTML
36              + "<html><head>\n"
37              + "<script>\n"
38              + LOG_TITLE_FUNCTION
39              + "  function MyEventListener(name) {\n"
40              + "    this.name = name;\n"
41              + "  }\n"
42              + "\n"
43              + "  MyEventListener.prototype = {\n"
44              + "    handleEvent: function(evt) {\n"
45              + "      log(this.name);\n"
46              + "      log(evt.type);\n"
47              + "      log(evt.target);\n"
48              + "    }\n"
49              + "  }\n"
50              + "\n"
51              + "  function test() {\n"
52              + "    try {\n"
53              + "      var listener = new MyEventListener('someName');\n"
54              + "      window.addEventListener('myevent', listener, false);\n"
55              + "      window.dispatchEvent(new Event('myevent'));\n"
56              + "    } catch(e) {\n"
57              + "      logEx(e);\n"
58              + "    }\n"
59              + "  }\n"
60              + "</script>\n"
61              + "</head><body onload='test()'>\n"
62              + "</body></html>";
63  
64          loadPageVerifyTitle2(html);
65      }
66  
67      /**
68       * @throws Exception if the test fails
69       */
70      @Test
71      @Alerts({"someName", "myevent", "[object HTMLBodyElement]"})
72      public void addEventListener_node() throws Exception {
73          final String html = DOCTYPE_HTML
74              + "<html><head>\n"
75              + "<script>\n"
76              + LOG_TITLE_FUNCTION
77              + "  function MyEventListener(name) {\n"
78              + "    this.name = name;\n"
79              + "  }\n"
80              + "\n"
81              + "  MyEventListener.prototype = {\n"
82              + "    handleEvent: function(evt) {\n"
83              + "      log(this.name);\n"
84              + "      log(evt.type);\n"
85              + "      log(evt.target);\n"
86              + "    }\n"
87              + "  }\n"
88              + "\n"
89              + "  function test() {\n"
90              + "    try {\n"
91              + "      var listener = new MyEventListener('someName');\n"
92              + "      document.body.addEventListener('myevent', listener, false);\n"
93              + "      document.body.dispatchEvent(new Event('myevent'));\n"
94              + "    } catch(e) {\n"
95              + "      logEx(e);\n"
96              + "    }\n"
97              + "  }\n"
98              + "</script>\n"
99              + "</head><body onload='test()'>\n"
100             + "</body></html>";
101 
102         loadPageVerifyTitle2(html);
103     }
104 
105     /**
106      * @throws Exception if the test fails
107      */
108     @Test
109     @Alerts({})
110     public void addEventListener_no_handleEvent() throws Exception {
111         final String html = DOCTYPE_HTML
112             + "<html><head>\n"
113             + "<script>\n"
114             + LOG_TITLE_FUNCTION
115             + "  function MyEventListener(name) {\n"
116             + "    this.name = name;\n"
117             + "  }\n"
118             + "\n"
119             + "  function test() {\n"
120             + "    try {\n"
121             + "      var listener = new MyEventListener('someName');\n"
122             + "      window.addEventListener('myevent', listener, false);\n"
123             + "      window.dispatchEvent(new Event('myevent'));\n"
124             + "    } catch(e) {\n"
125             + "      logEx(e);\n"
126             + "    }\n"
127             + "  }\n"
128             + "</script>\n"
129             + "</head><body onload='test()'>\n"
130             + "</body></html>";
131 
132         loadPageVerifyTitle2(html);
133     }
134 }