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