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