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.htmlunit.util.MimeType;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link EventTarget}.
24   *
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   */
28  public class EventTargetTest extends WebDriverTestCase {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      @Alerts("not defined")
35      public void cloneEvent() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><head><script>\n"
38              + LOG_TITLE_FUNCTION
39              + "  function test() {\n"
40              + "    var div = document.getElementById('myId');\n"
41              + "    if (div.attachEvent) {\n"
42              + "      div.attachEvent('onclick', clickFn = function() {\n"
43              + "        log('called!');\n"
44              + "      });\n"
45              + "      var clone = div.cloneNode(true);\n"
46              + "      clone.fireEvent('onclick');\n"
47              + "    } else {\n"
48              + "        log('not defined');\n"
49              + "    }\n"
50              + "  }\n"
51              + "</script></head><body onload='test()'>\n"
52              + "  <div id='myId'></div>\n"
53              + "</body></html>";
54  
55          loadPageVerifyTitle2(html);
56      }
57  
58      /**
59       * @throws Exception if an error occurs
60       */
61      @Test
62      @Alerts({"before dispatchEvent()", "dispatchEvent() listener",
63               "insertBefore start", "insertBefore done", "after dispatchEvent()", "external script"})
64      public void dispatchEventPostponed() throws Exception {
65          getMockWebConnection().setDefaultResponse("log('external script');", MimeType.TEXT_JAVASCRIPT);
66  
67          final String html = DOCTYPE_HTML
68              + "<html><head>\n"
69              + "<script>\n"
70              + LOG_TITLE_FUNCTION
71              + "function test() {\n"
72              + "  var listener = function(evt) {\n"
73              + "    log('dispatchEvent() listener');\n"
74  
75              + "    var newnode = document.createElement('script');\n"
76              + "    try {\n"
77              + "      newnode.setAttribute('src', 'script.js');\n"
78              + "      var outernode = document.getElementById('myId');\n"
79              + "      log('insertBefore start');\n"
80              + "      outernode.insertBefore(newnode, null);\n"
81              + "      log('insertBefore done');\n"
82              + "    } catch(e) { logEx(e); }\n"
83              + "  }\n"
84  
85              + "  document.getElementById('myId').addEventListener('TestEvent', listener);\n"
86  
87              + "  var myEvent = new Event('TestEvent');\n"
88  
89              + "  log('before dispatchEvent()');\n"
90              + "  document.getElementById('myId').dispatchEvent(myEvent);\n"
91              + "  log('after dispatchEvent()');\n"
92              + "}\n"
93              + "</script>\n"
94              + "</head>"
95              + "<body onload='test()'>\n"
96              + "  <div id='myId'></div>\n"
97              + "</body></html>";
98  
99          loadPage2(html);
100         verifyTitle2(DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
101     }
102 }