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.dom;
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.By;
23  import org.openqa.selenium.WebDriver;
24  import org.openqa.selenium.WebElement;
25  
26  /**
27   * Tests for EventNode.
28   *
29   * @author Ahmed Ashour
30   * @author Frank Danek
31   * @author Ronald Brill
32   */
33  @RunWith(BrowserRunner.class)
34  public class EventNodeTest extends WebDriverTestCase {
35  
36      /**
37       * @throws Exception if the test fails
38       */
39      @Test
40      @Alerts("fireEvent not available")
41      public void fireEvent() throws Exception {
42          final String html = DOCTYPE_HTML
43              + "<html><head>\n"
44              + "<script>\n"
45              + LOG_TITLE_FUNCTION
46              + "  function test() {\n"
47              + "    var form = document.getElementById('myForm');\n"
48              + "    if (!form.fireEvent) { log('fireEvent not available'); return }\n"
49              + "    log(form.fireEvent('onsubmit'));\n"
50              + "  }\n"
51              + "</script>\n"
52              + "</head><body onload='test()'>\n"
53              + "  <form id='myForm'>\n"
54              + "  </form>\n"
55              + "</body></html>";
56  
57          loadPageVerifyTitle2(html);
58      }
59  
60      /**
61       * @throws Exception if the test fails
62       */
63      @Test
64      @Alerts("createEventObject not available")
65      public void fireEvent_initFromTemplate() throws Exception {
66          final String html = DOCTYPE_HTML
67              + "<html>\n"
68              + "<head>\n"
69              + "  <script>\n"
70              + LOG_TITLE_FUNCTION
71              + "    function test() {\n"
72              + "      if (!document.createEventObject) { log('createEventObject not available'); return }\n"
73              + "      var myEvent = document.createEventObject();\n"
74              + "      myEvent.eventType = 'onclick';\n"
75              + "      myEvent.foo = 'hello';\n"
76              + "      var butt = document.getElementById('theButton');\n"
77              + "      butt.fireEvent('onclick', myEvent);\n"
78              + "    }\n"
79              + "  </script>\n"
80              + "</head><body onload='test()'>\n"
81              + "  <span id='theButton' onclick='log(event.foo)'>a span</span>\n"
82              + "</body></html>";
83  
84          loadPageVerifyTitle2(html);
85      }
86  
87      /**
88       * @throws Exception if the test fails
89       */
90      @Test
91      @Alerts({"mousedown span", "mouseup span", "click span",
92               "mousedown text", "focus text", "mouseup text", "click text",
93               "mousedown image", "focus image", "mouseup image", "click image",
94               "mousedown textarea", "focus textarea", "mouseup textarea", "click textarea"})
95      public void clickEvents() throws Exception {
96          final String html = DOCTYPE_HTML
97              + "<html>\n"
98              + "<head>\n"
99              + "  <script>\n"
100             + LOG_TEXTAREA_FUNCTION
101             + "  </script>\n"
102             + "</head><body>\n"
103             + "  <span id='testSpan' onfocus=\"log('will not be triggered')\" onmousedown=\"log('mousedown span')\""
104             + " onclick=\"log('click span')\" onmouseup=\"log('mouseup span')\">test span</span>\n"
105             + "  <form>\n"
106             + "    <input type='text' id='testInput' onmousedown=\"log('mousedown text')\""
107             + " onclick=\"log('click text')\" onmouseup=\"log('mouseup text')\" onfocus=\"log('focus text')\">\n"
108             + "    <input type='image' id='testImage' onmousedown=\"log('mousedown image')\""
109             + " onclick=\"log('click image'); return false;\" onmouseup=\"log('mouseup image')\""
110             + " onfocus=\"log('focus image')\">\n"
111             + "    <textarea id='testTextarea' onfocus=\"log('focus textarea')\""
112             + " onmousedown=\"log('mousedown textarea')\" onclick=\"log('click textarea')\""
113             + " onmouseup=\"log('mouseup textarea')\" onfocus=\"log('focus textarea')\"></textarea>\n"
114             + "  </form>\n"
115             + LOG_TEXTAREA
116             + "</body></html>";
117 
118         final WebDriver driver = loadPage2(html);
119         driver.findElement(By.id("testSpan")).click();
120         driver.findElement(By.id("testInput")).click();
121         driver.findElement(By.id("testImage")).click();
122         driver.findElement(By.id("testTextarea")).click();
123 
124         verifyTextArea2(driver, getExpectedAlerts());
125     }
126 
127     /**
128      * @throws Exception if the test fails
129      */
130     @Test
131     @Alerts({"mousedown label", "mouseup label", "click label", "focus text", "click text"})
132     public void clickEventsLabel() throws Exception {
133         final String html = DOCTYPE_HTML
134             + "<html>\n"
135             + "<head>\n"
136             + "  <script>\n"
137             + LOG_TEXTAREA_FUNCTION
138             + "  </script>\n"
139             + "</head><body>\n"
140             + "  <label id='testLabel' for='testInput'"
141             + " onfocus=\"log('will not be triggered')\" onmousedown=\"log('mousedown label')\""
142             + " onclick=\"log('click label')\" onmouseup=\"log('mouseup label')\">test label</label>\n"
143             + "  <form>\n"
144             + "    <input type='text' id='testInput' onmousedown=\"log('mousedown text')\""
145             + " onclick=\"log('click text')\" onmouseup=\"log('mouseup text')\" onfocus=\"log('focus text')\">\n"
146             + "  </form>\n"
147             + LOG_TEXTAREA
148             + "</body></html>";
149 
150         final WebDriver driver = loadPage2(html);
151         driver.findElement(By.id("testLabel")).click();
152 
153         verifyTextArea2(driver, getExpectedAlerts());
154     }
155 
156     /**
157      * Test event order.
158      * @throws Exception if the test fails
159      */
160     @Test
161     @Alerts({"focus", "keydown", "keypress", "keyup", "change", "blur"})
162     public void eventOrder() throws Exception {
163         final String html = DOCTYPE_HTML
164             + "<html>\n"
165             + "<head>\n"
166             + "  <script>\n"
167             + LOG_TEXTAREA_FUNCTION
168             + "  </script>\n"
169             + "</head><body>\n"
170             + "<form>\n"
171             + "  <input name='foo' id='foo' onfocus=\"log('focus')\" onblur=\"log('blur')\" onchange=\"log('change')\""
172             + " onkeydown=\"log('keydown')\" onkeypress=\"log('keypress')\" onkeyup=\"log('keyup')\">\n"
173             + "  <input name='other' id='other'>\n"
174             + "</form>\n"
175             + LOG_TEXTAREA
176             + "</body></html>";
177 
178         final WebDriver webDriver = loadPage2(html);
179         final WebElement textField = webDriver.findElement(By.id("foo"));
180         textField.click(); // to give focus
181         textField.sendKeys("a");
182         webDriver.findElement(By.id("other")).click();
183 
184         verifyTextArea2(webDriver, getExpectedAlerts());
185     }
186 }