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.junit.annotation.HtmlUnitNYI;
20  import org.junit.jupiter.api.Test;
21  import org.openqa.selenium.By;
22  import org.openqa.selenium.JavascriptExecutor;
23  import org.openqa.selenium.WebDriver;
24  import org.openqa.selenium.WebElement;
25  
26  /**
27   * Tests that when DOM events such as "onclick" have access
28   * to an {@link Event} object with context information.
29   *
30   * @author <a href="mailto:chriseldredge@comcast.net">Chris Eldredge</a>
31   * @author Ahmed Ashour
32   * @author Daniel Gredler
33   * @author Marc Guillemot
34   * @author Ronald Brill
35   * @author Frank Danek
36   */
37  public class EventTest extends WebDriverTestCase {
38  
39      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
40          + "    log(event);\n"
41          + "    log(event.type);\n"
42          + "    log(event.bubbles);\n"
43          + "    log(event.cancelable);\n"
44          + "    log(event.composed);\n"
45          + "  }\n";
46  
47      /**
48       * @throws Exception if the test fails
49       */
50      @Test
51      @Alerts({"[object Event]", "event", "false", "false", "false"})
52      public void create_ctor() throws Exception {
53          final String html = DOCTYPE_HTML
54              + "<html><head><script>\n"
55              + LOG_TITLE_FUNCTION
56              + "  function test() {\n"
57              + "    try {\n"
58              + "      var event = new Event('event');\n"
59              + "      dump(event);\n"
60              + "    } catch(e) { logEx(e) }\n"
61              + "  }\n"
62              + DUMP_EVENT_FUNCTION
63              + "</script></head><body onload='test()'>\n"
64              + "</body></html>";
65  
66          loadPageVerifyTitle2(html);
67      }
68  
69      /**
70       * @throws Exception if the test fails
71       */
72      @Test
73      @Alerts({"[object Event]", "event", "true", "false", "false"})
74      public void create_ctorWithDetails() throws Exception {
75          final String html = DOCTYPE_HTML
76              + "<html><head><script>\n"
77              + LOG_TITLE_FUNCTION
78              + "  function test() {\n"
79              + "    try {\n"
80              + "      var event = new Event('event', {\n"
81              + "        'bubbles': true\n"
82              + "      });\n"
83              + "      dump(event);\n"
84              + "    } catch(e) { logEx(e) }\n"
85              + "  }\n"
86              + DUMP_EVENT_FUNCTION
87              + "</script></head><body onload='test()'>\n"
88              + "</body></html>";
89  
90          loadPageVerifyTitle2(html);
91      }
92  
93      /**
94       * @throws Exception if the test fails
95       */
96      @Test
97      @Alerts({"[object Event]", "event", "true", "false", "false"})
98      public void create_ctorWithDetailsBoolAsString() throws Exception {
99          final String html = DOCTYPE_HTML
100             + "<html><head><script>\n"
101             + LOG_TITLE_FUNCTION
102             + "  function test() {\n"
103             + "    try {\n"
104             + "      var event = new Event('event', {\n"
105             + "        'bubbles': 'true'\n"
106             + "      });\n"
107             + "      dump(event);\n"
108             + "    } catch(e) { logEx(e) }\n"
109             + "  }\n"
110             + DUMP_EVENT_FUNCTION
111             + "</script></head><body onload='test()'>\n"
112             + "</body></html>";
113 
114         loadPageVerifyTitle2(html);
115     }
116 
117     /**
118      * @throws Exception if the test fails
119      */
120     @Test
121     @Alerts({"[object Event]", "event", "true", "false", "false"})
122     public void create_ctorWithDetailsBoolAsNumber() throws Exception {
123         final String html = DOCTYPE_HTML
124             + "<html><head><script>\n"
125             + LOG_TITLE_FUNCTION
126             + "  function test() {\n"
127             + "    try {\n"
128             + "      var event = new Event('event', {\n"
129             + "        'bubbles': 1\n"
130             + "      });\n"
131             + "      dump(event);\n"
132             + "    } catch(e) { logEx(e) }\n"
133             + "  }\n"
134             + DUMP_EVENT_FUNCTION
135             + "</script></head><body onload='test()'>\n"
136             + "</body></html>";
137 
138         loadPageVerifyTitle2(html);
139     }
140 
141     /**
142      * @throws Exception if the test fails
143      */
144     @Test
145     @Alerts({"[object Event]", "event", "true", "false", "false"})
146     public void create_ctorWithDetailsBoolAsObject() throws Exception {
147         final String html = DOCTYPE_HTML
148             + "<html><head><script>\n"
149             + LOG_TITLE_FUNCTION
150             + "  function test() {\n"
151             + "    try {\n"
152             + "      var event = new Event('event', {\n"
153             + "        'bubbles': {}\n"
154             + "      });\n"
155             + "      dump(event);\n"
156             + "    } catch(e) { logEx(e) }\n"
157             + "  }\n"
158             + DUMP_EVENT_FUNCTION
159             + "</script></head><body onload='test()'>\n"
160             + "</body></html>";
161 
162         loadPageVerifyTitle2(html);
163     }
164 
165     /**
166      * @throws Exception if the test fails
167      */
168     @Test
169     @Alerts({"[object Event]", "event", "false", "false", "false"})
170     public void create_ctorWithDetailsBoolAsUndefined() throws Exception {
171         final String html = DOCTYPE_HTML
172             + "<html><head><script>\n"
173             + LOG_TITLE_FUNCTION
174             + "  function test() {\n"
175             + "    try {\n"
176             + "      var event = new Event('event', {\n"
177             + "        'bubbles': undefined\n"
178             + "      });\n"
179             + "      dump(event);\n"
180             + "    } catch(e) { logEx(e) }\n"
181             + "  }\n"
182             + DUMP_EVENT_FUNCTION
183             + "</script></head><body onload='test()'>\n"
184             + "</body></html>";
185 
186         loadPageVerifyTitle2(html);
187     }
188 
189     /**
190      * @throws Exception if the test fails
191      */
192     @Test
193     @Alerts({"[object Event]", "event", "false", "false", "false"})
194     public void create_ctorWithDetailsBoolAsNull() throws Exception {
195         final String html = DOCTYPE_HTML
196             + "<html><head><script>\n"
197             + LOG_TITLE_FUNCTION
198             + "  function test() {\n"
199             + "    try {\n"
200             + "      var event = new Event('event', {\n"
201             + "        'bubbles': null\n"
202             + "      });\n"
203             + "      dump(event);\n"
204             + "    } catch(e) { logEx(e) }\n"
205             + "  }\n"
206             + DUMP_EVENT_FUNCTION
207             + "</script></head><body onload='test()'>\n"
208             + "</body></html>";
209 
210         loadPageVerifyTitle2(html);
211     }
212 
213     /**
214      * @throws Exception if the test fails
215      */
216     @Test
217     @Alerts({"[object Event]", "", "false", "false", "false"})
218     public void create_createEvent() throws Exception {
219         final String html = DOCTYPE_HTML
220             + "<html><head><script>\n"
221             + LOG_TITLE_FUNCTION
222             + "  function test() {\n"
223             + "    try {\n"
224             + "      var event = document.createEvent('Event');\n"
225             + "      dump(event);\n"
226             + "    } catch(e) { logEx(e) }\n"
227             + "  }\n"
228             + DUMP_EVENT_FUNCTION
229             + "</script></head><body onload='test()'>\n"
230             + "</body></html>";
231 
232         loadPageVerifyTitle2(html);
233     }
234 
235     /**
236      * @throws Exception if the test fails
237      */
238     @Test
239     @Alerts({"DOM2: [object Event]", "DOM3: [object Event]", "vendor: [object Event]"})
240     public void create_createEventForDifferentTypes() throws Exception {
241         final String html = DOCTYPE_HTML
242             + "<html><head><script>\n"
243             + LOG_TITLE_FUNCTION
244             + "  function test() {\n"
245             + "    try {\n"
246             + "      log('DOM2: ' + document.createEvent('HTMLEvents'));\n"
247             + "    } catch(e) {log('DOM2: exception')}\n"
248             + "    try {\n"
249             + "      log('DOM3: ' + document.createEvent('Event'));\n"
250             + "    } catch(e) {log('DOM3: exception')}\n"
251             + "    try {\n"
252             + "      log('vendor: ' + document.createEvent('Events'));\n"
253             + "    } catch(e) {log('vendor: exception')}\n"
254             + "  }\n"
255             + "</script></head><body onload='test()'>\n"
256             + "</body></html>";
257 
258         loadPageVerifyTitle2(html);
259     }
260 
261     /**
262      * @throws Exception if the test fails
263      */
264     @Test
265     @Alerts({"[object Event]", "event", "true", "false", "false"})
266     public void initEvent() throws Exception {
267         final String html = DOCTYPE_HTML
268             + "<html><head><script>\n"
269             + LOG_TITLE_FUNCTION
270             + "  function test() {\n"
271             + "    try {\n"
272             + "      var event = document.createEvent('Event');\n"
273             + "      event.initEvent('event', true, false);\n"
274             + "      dump(event);\n"
275             + "    } catch(e) { logEx(e) }\n"
276             + "  }\n"
277             + DUMP_EVENT_FUNCTION
278             + "</script></head><body onload='test()'>\n"
279             + "</body></html>";
280 
281         loadPageVerifyTitle2(html);
282     }
283 
284     /**
285      * Verify the "this" object refers to the Element being clicked when an
286      * event handler is invoked.
287      * @throws Exception if the test fails
288      */
289     @Test
290     @Alerts("clickId")
291     public void thisDefined() throws Exception {
292         final String content = DOCTYPE_HTML
293             + "<html><head></head><body>\n"
294             + "<input type='button' id='clickId'/>\n"
295             + "<script>\n"
296             + LOG_TITLE_FUNCTION
297             + "  function handler(event) { log(this.getAttribute('id')); }\n"
298             + "  document.getElementById('clickId').onclick = handler;</script>\n"
299             + "</body></html>";
300         onClickPageTest(content);
301     }
302 
303     /**
304      * Verify setting a previously undefined/non-existent property on an Element
305      * is accessible from inside an event handler.
306      * @throws Exception if the test fails
307      */
308     @Test
309     @Alerts("foo")
310     public void setPropOnThisDefined() throws Exception {
311         final String content = DOCTYPE_HTML
312             + "<html><head></head><body>\n"
313             + "<input type='button' id='clickId'/>\n"
314             + "<script>\n"
315             + LOG_TITLE_FUNCTION
316             + "  function handler(event) { log(this.madeUpProperty); }\n"
317             + "  document.getElementById('clickId').onclick = handler;\n"
318             + "  document.getElementById('clickId').madeUpProperty = 'foo';\n"
319             + "</script>\n"
320             + "</body></html>";
321         onClickPageTest(content);
322     }
323 
324     /**
325      * Verify that JavaScript snippets have a variable named 'event' available to them.
326      * @throws Exception if the test fails
327      */
328     @Test
329     @Alerts("defined")
330     public void eventArgDefinedByWrapper() throws Exception {
331         final String content = DOCTYPE_HTML
332             + "<html><head>\n"
333             + "<script>\n"
334             + LOG_TITLE_FUNCTION
335             + "</script>\n"
336             + "</head><body>\n"
337             + "<input type='button' id='clickId' onclick=\"log(event ? 'defined' : 'undefined')\"/>\n"
338             + "</body></html>";
339         onClickPageTest(content);
340     }
341 
342     /**
343      * Verify that when event handler is invoked an argument is passed in.
344      * @throws Exception if the test fails
345      */
346     @Test
347     @Alerts("defined")
348     public void eventArgDefined() throws Exception {
349         final String content = DOCTYPE_HTML
350             + "<html><head></head>\n"
351             + "<body>\n"
352             + "<input type='button' id='clickId'/>\n"
353             + "<script>\n"
354             + LOG_TITLE_FUNCTION
355             + "  function handler(event) { log(event ? 'defined' : 'undefined'); }\n"
356             + "  document.getElementById('clickId').onclick = handler;\n"
357             + "</script>\n"
358             + "</body></html>";
359         onClickPageTest(content);
360     }
361 
362     /**
363      * @throws Exception if the test fails
364      */
365     @Test
366     @Alerts("pass")
367     public void eventTargetSameAsThis() throws Exception {
368         final String content = DOCTYPE_HTML
369             + "<html><head></head>\n"
370             + "<body>\n"
371             + "<input type='button' id='clickId'/>\n"
372             + "<script>\n"
373             + LOG_TITLE_FUNCTION
374             + "  function handler(event) {\n"
375             + "    log(event.target == this ? 'pass' : event.target + '!=' + this);\n"
376             + "  }\n"
377             + "  document.getElementById('clickId').onclick = handler;\n"
378             + "</script>\n"
379             + "</body></html>";
380         onClickPageTest(content);
381     }
382 
383     /**
384      * @throws Exception if the test fails
385      */
386     @Test
387     @Alerts({"[object HTMLInputElement]", "true"})
388     public void eventSrcElementSameAsThis() throws Exception {
389         final String content = DOCTYPE_HTML
390             + "<html><head></head><body>\n"
391             + "<input type='button' id='clickId'/>\n"
392             + "<script>\n"
393             + LOG_TITLE_FUNCTION
394             + "  function handler(event) {\n"
395             + "    event = event ? event : window.event;\n"
396             + "    log(event.srcElement);\n"
397             + "    log(event.srcElement == this);\n"
398             + "  }\n"
399             + "  document.getElementById('clickId').onclick = handler;\n"
400             + "</script>\n"
401             + "</body></html>";
402         onClickPageTest(content);
403     }
404 
405     /**
406      * Verify that <tt>event.currentTarget == this</tt> inside JavaScript event handler.
407      * @throws Exception if the test fails
408      */
409     @Test
410     @Alerts("pass")
411     public void eventCurrentTargetSameAsThis() throws Exception {
412         final String content = DOCTYPE_HTML
413             + "<html><head></head>\n"
414             + "<body>\n"
415             + "<input type='button' id='clickId'/>\n"
416             + "<script>\n"
417             + LOG_TITLE_FUNCTION
418             + "  function handler(event) {\n"
419             + "    log(event.currentTarget == this ? 'pass' : event.currentTarget + '!=' + this);\n"
420             + "  }\n"
421             + "  document.getElementById('clickId').onclick = handler;\n"
422             + "</script>\n"
423             + "</body></html>";
424         onClickPageTest(content);
425     }
426 
427     /**
428      * Property currentTarget needs to be set again depending on the listener invoked.
429      * @throws Exception if the test fails
430      */
431     @Test
432     @Alerts({"[object Window]", "[object HTMLDivElement]"})
433     public void currentTarget_sameListenerForEltAndWindow() throws Exception {
434         final String content = DOCTYPE_HTML
435             + "<html><head></head><body>\n"
436             + "<div id='clickId'>click me</div>\n"
437             + "<script>\n"
438             + LOG_TITLE_FUNCTION
439             + "  function handler(event) {\n"
440             + "    log(event.currentTarget);\n"
441             + "  }\n"
442             + "  document.getElementById('clickId').onmousedown = handler;\n"
443             + "  window.addEventListener('mousedown', handler, true);\n"
444             + "</script>\n"
445             + "</body></html>";
446         onClickPageTest(content);
447     }
448 
449     /**
450      * When adding a null event listener browsers react different.
451      * @throws Exception if the test fails
452      */
453     @Test
454     public void addEventListener_HandlerNull() throws Exception {
455         final String content = DOCTYPE_HTML
456             + "<html><head></head><body>\n"
457             + "<script>\n"
458             + LOG_TITLE_FUNCTION
459             + "try {\n"
460             + "  window.addEventListener('mousedown', null, true);\n"
461             + "} catch(e) { logEx(e); }\n"
462             + "</script>\n"
463             + "</body></html>";
464         loadPageVerifyTitle2(content);
465     }
466 
467     /**
468      * @throws Exception if an error occurs
469      */
470     @Test
471     @Alerts({"123a4a", "1a2a3ab4ab1ab2ab3abc4abc"})
472     public void typing_input_text() throws Exception {
473         testTyping("<input type='text'", "");
474     }
475 
476     /**
477      * @throws Exception if an error occurs
478      */
479     @Test
480     @Alerts({"123a4a", "1a2a3ab4ab1ab2ab3abc4abc"})
481     public void typing_input_password() throws Exception {
482         testTyping("<input type='password'", "");
483     }
484 
485     /**
486      * @throws Exception if an error occurs
487      */
488     @Test
489     @Alerts({"123a4a", "1a2a3ab4ab1ab2ab3abc4abc"})
490     public void typing_input_textarea() throws Exception {
491         testTyping("<textarea", "</textarea>");
492     }
493 
494     /**
495      * @throws Exception if an error occurs
496      */
497     @Test
498     @Alerts({"123a4a", "1a2a3ab4ab1ab2ab3abc4abc"})
499     public void typing_input_tel() throws Exception {
500         testTyping("<input type='tel'", "");
501     }
502 
503     /**
504      * @throws Exception if an error occurs
505      */
506     @Test
507     @Alerts({"123a4a", "1a2a3ab4ab1ab2ab3abc4abc"})
508     public void typing_input_search() throws Exception {
509         testTyping("<input type='search'", "");
510     }
511 
512     /**
513      * @throws Exception if an error occurs
514      */
515     @Test
516     @Alerts(DEFAULT = {"124", "124124"},
517             FF = {"1234", "12341234"},
518             FF_ESR = {"1234", "12341234"})
519     @HtmlUnitNYI(CHROME = {"1234", "12341234"},
520             EDGE = {"1234", "12341234"})
521     public void typing_input_number() throws Exception {
522         testTyping("<input type='number'", "");
523     }
524 
525     /**
526      * @throws Exception if an error occurs
527      */
528     @Test
529     @Alerts({"123a4a", "1a2a3ab4ab1ab2ab3abc4abc"})
530     public void typing_textara() throws Exception {
531         testTyping("<textarea", "</textarea>");
532     }
533 
534     private void testTyping(final String opening, final String closing) throws Exception {
535         final String html = DOCTYPE_HTML
536             + "<html><body>\n"
537             + "<script>\n"
538             + LOG_TITLE_FUNCTION
539             + "var x = '';\n"
540             + "function msg(s) { x += s; }</script>\n"
541             + "<form>\n"
542             + opening + " id='t' onkeydown='msg(1 + this.value)' "
543             + "onkeypress='msg(2 + this.value)' "
544             + "oninput='msg(3 + this.value)'"
545             + "onkeyup='msg(4 + this.value)'>" + closing
546             + "</form>\n"
547             + "<div id='d' onclick='log(x); x=\"\"'>abc</div>\n"
548             + "</body></html>";
549 
550         final WebDriver driver = loadPage2(html);
551         driver.findElement(By.id("t")).sendKeys("a");
552         driver.findElement(By.id("d")).click();
553         verifyTitle2(driver, getExpectedAlerts()[0]);
554 
555         driver.findElement(By.id("t")).sendKeys("bc");
556         driver.findElement(By.id("d")).click();
557         verifyTitle2(driver, getExpectedAlerts()[0], getExpectedAlerts()[1]);
558     }
559 
560     private void onClickPageTest(final String html) throws Exception {
561         final WebDriver driver = loadPage2(html);
562         driver.findElement(By.id("clickId")).click();
563 
564         verifyTitle2(driver, getExpectedAlerts());
565     }
566 
567     /**
568      * @throws Exception if the test fails
569      */
570     @Test
571     @Alerts("frame1")
572     public void thisInEventHandler() throws Exception {
573         final String html = DOCTYPE_HTML
574             + "<html>\n"
575             + "<head>\n"
576             + "<script>\n"
577             + LOG_TITLE_FUNCTION
578             + "</script>\n"
579             + "</head>\n"
580             + "<body>\n"
581             + "  <button name='button1' id='button1' onclick='log(this.name)'>1</button>\n"
582             + "  <iframe src='default' name='frame1' id='frame1'></iframe>\n"
583             + "  <script>\n"
584             + "    var e = document.getElementById('frame1');\n"
585             + "    e.onload = document.getElementById('button1').onclick;\n"
586             + "  </script>\n"
587             + "</body></html>";
588 
589         getMockWebConnection().setDefaultResponse("<html><body></body></html>");
590         loadPageVerifyTitle2(html);
591     }
592 
593     /**
594      * @throws Exception if the test fails
595      */
596     @Test
597     @Alerts("called")
598     public void iframeOnload() throws Exception {
599         final String html = DOCTYPE_HTML
600             + "<html><head>\n"
601             + "<script>\n"
602             + LOG_TITLE_FUNCTION
603             + "  function test() {\n"
604             + "    log('called');\n"
605             + "  }\n"
606             + "</script>\n"
607             + "</head>\n"
608             + "<body>\n"
609             + "<iframe src='default' name='frame1' id='frame1'></iframe>\n"
610             + "<script>\n"
611             + "  var e = document.getElementById('frame1');\n"
612             + "  e.onload = test;\n"
613             + "</script>\n"
614             + "</body></html>";
615 
616         getMockWebConnection().setDefaultResponse("<html><body></body></html>");
617         loadPageVerifyTitle2(html);
618     }
619 
620     /**
621      * @throws Exception if the test fails
622      */
623     @Test
624     @Alerts({"inline", "null"})
625     public void iframeOnload2() throws Exception {
626         final String html = DOCTYPE_HTML
627             + "<html>\n"
628             + "<body>\n"
629             + "<iframe src='about:blank' name='frame1' id='frame1'></iframe>\n"
630             + "<script>\n"
631             + LOG_TITLE_FUNCTION
632             + "  var e = document.getElementById('frame1');\n"
633             + "  e.onload = log('inline');\n"
634             + "  log(e.onload);\n"
635             + "</script>\n"
636             + "</body></html>";
637 
638         loadPageVerifyTitle2(html);
639     }
640 
641     /**
642      * Test that the event property of the window is available.
643      * @throws Exception if the test fails
644      */
645     @Test
646     @Alerts({"false", "false"})
647     public void ieWindowEvent() throws Exception {
648         final String html = DOCTYPE_HTML
649             + "<html><head>\n"
650             + "<script>\n"
651             + LOG_TITLE_FUNCTION
652             + "function test() {\n"
653             + "  log(window.event == null);\n"
654             + "  try {\n"
655             + "    log(event == null);\n"
656             + "  } catch(e) { logEx(e); }\n"
657             + "}\n"
658             + "</script>\n"
659             + "</head><body onload='test()'></body></html>";
660 
661         loadPageVerifyTitle2(html);
662     }
663 
664     /**
665      * Test that the event handler is correctly parsed even if it contains comments.
666      * It seems that it is correctly parsed and stored in non public field
667      * org.apache.xerces.util.XMLAttributesImpl#nonNormalizedValue
668      * but that getValue(i) returns a normalized value. Furthermore access seems not possible as
669      * we just see an org.apache.xerces.parsers.AbstractSAXParser.AttributesProxy
670      * @throws Exception if the test fails
671      */
672     @Test
673     @Alerts({"1", "2"})
674     public void commentInEventHandlerDeclaration() throws Exception {
675         final String html = DOCTYPE_HTML
676             + "<html><head>\n"
677             + "<script>\n"
678             + LOG_TITLE_FUNCTION
679             + "</script>\n"
680             + "</head>\n"
681             + "<body onload='log(1);\n"
682             + "// a comment within the onload declaration\n"
683             + "log(2)'>\n"
684             + "</body></html>";
685 
686         loadPageVerifyTitle2(html);
687     }
688 
689     /**
690      * Test value for null event handler: null for IE, while 'undefined' for Firefox.
691      * @throws Exception if the test fails
692      */
693     @Test
694     @Alerts("null")
695     public void nullEventHandler() throws Exception {
696         final String html = DOCTYPE_HTML
697             + "<html><head>\n"
698             + "<script>\n"
699             + LOG_TITLE_FUNCTION
700             + "  function test() {\n"
701             + "    var div = document.getElementById('myDiv');\n"
702             + "    log(div.onclick);\n"
703             + "  }\n"
704             + "</script></head><body onload='test()'>\n"
705             + "<div id='myDiv'/>\n"
706             + "</body></html>";
707 
708         loadPageVerifyTitle2(html);
709     }
710 
711     /**
712      * @throws Exception if an error occurs
713      */
714     @Test
715     @Alerts({"[object Event]", "load", "false", "false", "false"})
716     public void onload() throws Exception {
717         final String html = DOCTYPE_HTML
718             + "<html><body onload='test(event)'>\n"
719             + "<script>\n"
720             + LOG_TITLE_FUNCTION
721             + "    function test(e) {\n"
722             + "      dump(e);\n"
723             + "    }\n"
724             + DUMP_EVENT_FUNCTION
725             + "</script></body></html>";
726 
727         loadPageVerifyTitle2(html);
728     }
729 
730     /**
731      * @throws Exception if an error occurs
732      */
733     @Test
734     @Alerts({"[object Event]", "number"})
735     public void timeStamp() throws Exception {
736         final String html = DOCTYPE_HTML
737             + "<html><body onload='test(event)'>\n"
738             + "<script>\n"
739             + LOG_TITLE_FUNCTION
740             + "  function test(e) {\n"
741             + "    log(e);\n"
742             + "    log(typeof e.timeStamp);\n"
743             + "  }\n"
744             + "</script></body></html>";
745 
746         loadPageVerifyTitle2(html);
747     }
748 
749     /**
750      * @throws Exception if an error occurs
751      */
752     @Test
753     @Alerts({"click", "true", "true", "click", "false", "false"})
754     public void initEventClick() throws Exception {
755         testInitEvent("click");
756     }
757 
758     /**
759      * @throws Exception if an error occurs
760      */
761     @Test
762     @Alerts({"dblclick", "true", "true", "dblclick", "false", "false"})
763     public void initEventDblClick() throws Exception {
764         testInitEvent("dblclick");
765     }
766 
767     /**
768      * @throws Exception if an error occurs
769      */
770     @Test
771     @Alerts({"unknown", "true", "true", "unknown", "false", "false"})
772     public void initEventUnknown() throws Exception {
773         testInitEvent("unknown");
774     }
775 
776     /**
777      * @throws Exception if an error occurs
778      */
779     @Test
780     @Alerts({"cASe", "true", "true", "cASe", "false", "false"})
781     public void initEventCaseSensitive() throws Exception {
782         testInitEvent("cASe");
783     }
784 
785     private void testInitEvent(final String eventType) throws Exception {
786         final String html = DOCTYPE_HTML
787             + "<html><head><script>\n"
788             + LOG_TITLE_FUNCTION
789             + "  function test() {\n"
790             + "    var e = document.createEvent('Event');\n"
791             + "    try {\n"
792             + "      e.initEvent('" + eventType + "', true, true);\n"
793             + "      log(e.type);\n"
794             + "      log(e.bubbles);\n"
795             + "      log(e.cancelable);\n"
796             + "    } catch(e) { log('e-' + '" + eventType + "'); logEx(e); }\n"
797 
798             + "    var e = document.createEvent('Event');\n"
799             + "    try {\n"
800             + "      e.initEvent('" + eventType + "', false, false);\n"
801             + "      log(e.type);\n"
802             + "      log(e.bubbles);\n"
803             + "      log(e.cancelable);\n"
804             + "    } catch(e) { log('e2-' + '" + eventType + "'); logEx(e); }\n"
805             + "  }\n"
806             + "</script></head>\n"
807             + "<body onload='test()'></body></html>";
808 
809         loadPageVerifyTitle2(html);
810     }
811 
812     /**
813      * @throws Exception if an error occurs
814      */
815     @Test
816     @Alerts({"true", "I was here"})
817     public void firedEvent_equals_original_event() throws Exception {
818         final String html = DOCTYPE_HTML
819             + "<html><head>\n"
820             + "<script>\n"
821             + LOG_TITLE_FUNCTION
822             + "function test() {\n"
823             + "  var e = document.getElementById('myDiv');\n"
824             + "  \n"
825             + "  var myEvent;\n"
826             + "  var listener = function(x) {\n"
827             + "    log(x == myEvent);\n"
828             + "    x.foo = 'I was here';\n"
829             + "  }\n"
830             + "  \n"
831             + "  e.addEventListener('click', listener, false);\n"
832             + "  myEvent = document.createEvent('HTMLEvents');\n"
833             + "  myEvent.initEvent('click', true, true);\n"
834             + "  e.dispatchEvent(myEvent);\n"
835             + "  log(myEvent.foo);\n"
836             + "}\n"
837             + "</script>\n"
838             + "</head><body onload='test()'>\n"
839             + "<div id='myDiv'>toti</div>\n"
840             + "</body></html>";
841 
842         loadPageVerifyTitle2(html);
843     }
844 
845     /**
846      * @throws Exception if an error occurs
847      */
848     @Test
849     @Alerts(DEFAULT = {"e-0", "e-1", "e-2", "e-3", "e-4", "e-5",
850                        "e-6", "e-7", "e-8", "e-9", "e-10", "e-11",
851                        "e-12", "e-13", "e-14", "e-15", "e-16", "e-17", "e-18",
852                        "e-19", "e-20", "e-21", "e-22", "e-23", "e-24",
853                        "e-25", "e-26", "e-27", "e-28", "e-29", "e-30", "e-31", "e-32",
854                        "e-33"},
855             FF = {"e-0", "1", "e-2", "e-3", "e-4", "e-5",
856                   "2", "e-7", "e-8", "e-9", "e-10", "e-11",
857                   "e-12", "e-13", "e-14", "e-15", "e-16", "e-17", "8",
858                   "e-19", "e-20", "e-21", "e-22", "e-23", "e-24",
859                   "e-25", "e-26", "e-27", "e-28", "e-29", "4", "e-31", "e-32",
860                   "e-33"},
861             FF_ESR = {"e-0", "1", "e-2", "e-3", "e-4", "e-5",
862                       "2", "e-7", "e-8", "e-9", "e-10", "e-11",
863                       "e-12", "e-13", "e-14", "e-15", "e-16", "e-17", "8",
864                       "e-19", "e-20", "e-21", "e-22", "e-23", "e-24",
865                       "e-25", "e-26", "e-27", "e-28", "e-29", "4", "e-31", "e-32",
866                       "e-33"})
867     public void constants() throws Exception {
868         final String html = DOCTYPE_HTML
869             + "<html><body>\n"
870             + "<script>\n"
871             + LOG_TITLE_FUNCTION
872             + "  var constants = [Event.ABORT, Event.ALT_MASK, Event.BACK, Event.BLUR, Event.CHANGE, Event.CLICK, "
873             + "Event.CONTROL_MASK, Event.DBLCLICK, Event.DRAGDROP, Event.ERROR, Event.FOCUS, Event.FORWARD, "
874             + "Event.HELP, Event.KEYDOWN, Event.KEYPRESS, Event.KEYUP, Event.LOAD, Event.LOCATE, Event.META_MASK, "
875             + "Event.MOUSEDOWN, Event.MOUSEDRAG, Event.MOUSEMOVE, Event.MOUSEOUT, Event.MOUSEOVER, Event.MOUSEUP, "
876             + "Event.MOVE, Event.RESET, Event.RESIZE, Event.SCROLL, Event.SELECT, Event.SHIFT_MASK, Event.SUBMIT, "
877             + "Event.UNLOAD, Event.XFER_DONE];\n"
878             + "  for (var x in constants) {\n"
879             + "    try {\n"
880             + "      log(constants[x].toString(16));\n"
881             + "    } catch(e) { log('e-' + x); }\n"
882             + "  }\n"
883             + "</script>\n"
884             + "</body></html>";
885 
886         loadPageVerifyTitle2(html);
887     }
888 
889     /**
890      * @throws Exception if an error occurs
891      */
892     @Test
893     @Alerts("TypeError")
894     public void text() throws Exception {
895         final String html = DOCTYPE_HTML
896             + "<html><body onload='test(event)'><script>\n"
897             + LOG_TITLE_FUNCTION
898             + "  function test(e) {\n"
899             + "    try {\n"
900             + "      log(e.TEXT.toString(16));\n"// But Event.TEXT is undefined!!!
901             + "    } catch(e) { logEx(e); }\n"
902             + "  }\n"
903             + "</script>\n"
904             + "</body></html>";
905 
906         loadPageVerifyTitle2(html);
907     }
908 
909     /**
910      * Regression test for bug
911      * <a href="http://sourceforge.net/p/htmlunit/bugs/898/">898</a>.
912      * Name resolution doesn't work the same in inline handlers than in "normal" JS code!
913      * @throws Exception if the test fails
914      */
915     @Test
916     @Alerts({"form1 -> custom", "form2 -> [object HTMLFormElement]",
917              "form1: [object HTMLFormElement]", "form2: [object HTMLFormElement]",
918              "form1 -> custom", "form2 -> [object HTMLFormElement]"})
919     public void nameResolution() throws Exception {
920         final String html = DOCTYPE_HTML
921             + "<html><head><script>\n"
922             + LOG_TITLE_FUNCTION
923             + "var form1 = 'custom';\n"
924             + "function testFunc() {\n"
925             + "  log('form1 -> ' + form1);\n"
926             + "  log('form2 -> ' + form2);\n"
927             + "}\n"
928             + "</script></head>\n"
929             + "<body onload='testFunc()'>\n"
930             + "<form name='form1'></form>\n"
931             + "<form name='form2'></form>\n"
932             + "<button onclick=\"log('form1: ' + form1); log('form2: ' + form2); testFunc()\">click me</button>\n"
933             + "</body></html>";
934 
935         final String[] alerts = getExpectedAlerts();
936         int i = 0;
937 
938         final WebDriver driver = loadPage2(html);
939         verifyTitle2(driver, alerts[i++], alerts[i++]);
940 
941         i = 0;
942         driver.findElement(By.tagName("button")).click();
943         verifyTitle2(driver, alerts[i++], alerts[i++], alerts[i++], alerts[i++], alerts[i++], alerts[i++]);
944     }
945 
946     /**
947      * @throws Exception if the test fails
948      */
949     @Test
950     @Alerts(DEFAULT = "activeElement BODY",
951             FF = {"activeElement BODY", "focus #document", "handler: activeElement BODY"},
952             FF_ESR = {"activeElement BODY", "focus #document", "handler: activeElement BODY"})
953     // http://code.google.com/p/selenium/issues/detail?id=4665
954     @HtmlUnitNYI(FF = {"activeElement BODY", "focus undefined", "handler: activeElement BODY"},
955             FF_ESR = {"activeElement BODY", "focus undefined", "handler: activeElement BODY"})
956     public void document_focus() throws Exception {
957         final String html = DOCTYPE_HTML
958                 + "<html>\n"
959                 + "<head>\n"
960                 + "<script>\n"
961                 + LOG_TEXTAREA_FUNCTION
962                 + "  function test() {\n"
963                 + "    handle(document);\n"
964                 + "    log('activeElement ' + document.activeElement.nodeName);\n"
965                 + "  }\n"
966                 + "  function handle(obj) {\n"
967                 + "    obj.addEventListener('focus', handler, true);\n"
968                 + "  }\n"
969                 + "  function handler(e) {\n"
970                 + "    var src = e.srcElement;\n"
971                 + "    if (!src)\n"
972                 + "      src = e.target;\n"
973                 + "    log(e.type + ' ' + src.nodeName);\n"
974                 + "    log('handler: activeElement ' + document.activeElement.nodeName);\n"
975                 + "  }\n"
976                 + "</script>\n"
977                 + "</head>\n"
978                 + "<body onload='test()'>\n"
979                 + LOG_TEXTAREA
980                 + "</body></html>";
981 
982         loadPageVerifyTextArea2(html);
983     }
984 
985     /**
986      * @throws Exception if the test fails
987      */
988     @Test
989     @Alerts({"focus INPUT", "focus INPUT"})
990     @HtmlUnitNYI(FF = {"focus undefined", "focus INPUT", "focus INPUT"},
991             FF_ESR = {"focus undefined", "focus INPUT", "focus INPUT"})
992     public void document_input_focus() throws Exception {
993         document_input("focus");
994     }
995 
996     /**
997      * @throws Exception if the test fails
998      */
999     @Test
1000     @Alerts("blur INPUT")
1001     public void document_input_blur() throws Exception {
1002         document_input("blur");
1003     }
1004 
1005     private void document_input(final String event) throws Exception {
1006         final String html = DOCTYPE_HTML
1007                 + "<html>\n"
1008                 + "<head>\n"
1009                 + "<script>\n"
1010                 + LOG_TEXTAREA_FUNCTION
1011                 + "  function test() {\n"
1012                 + "    handle(document);\n"
1013                 + "  }\n"
1014                 + "  function handle(obj) {\n"
1015                 + "    obj.addEventListener('" + event + "', handler, true);\n"
1016                 + "  }\n"
1017                 + "  function handler(e) {\n"
1018                 + "    var src = e.srcElement;\n"
1019                 + "    if (!src)\n"
1020                 + "      src = e.target;\n"
1021                 + "    log(e.type + ' ' + src.nodeName);\n"
1022                 + "  }\n"
1023                 + "</script>\n"
1024                 + "</head>\n"
1025                 + "<body onload='test()'>\n"
1026                 + "  <div id=\"div\">\n"
1027                 + "    <input id=\"input1\" type=\"text\">\n"
1028                 + "    <input id=\"input2\" type=\"text\">\n"
1029                 + "  </div>\n"
1030                 + LOG_TEXTAREA
1031                 + "</body></html>";
1032 
1033         final WebDriver driver = loadPage2(html);
1034 
1035         driver.findElement(By.id("input1")).click();
1036         driver.findElement(By.id("input2")).click();
1037 
1038         verifyTextArea2(driver, getExpectedAlerts());
1039     }
1040 
1041     /**
1042      * Test that the parent scope of the event handler defined in HTML attributes is "document".
1043      * @throws Exception if the test fails
1044      */
1045     @Test
1046     @Alerts({"2from window", "1from document"})
1047     public void eventHandlersParentScope() throws Exception {
1048         final String html = DOCTYPE_HTML
1049             + "<html>\n"
1050             + "<head>\n"
1051             + "<script>\n"
1052             + LOG_TITLE_FUNCTION
1053             + "</script>\n"
1054             + "</head>\n"
1055             + "<body>\n"
1056             + "<button name='button1' id='button1' onclick='log(1 + foo)'>click me</button>\n"
1057             + "<script>\n"
1058             + "  window.addEventListener('click', function() { log(2 + foo); }, true);\n"
1059             + "  document.foo = 'from document';\n"
1060             + "  var foo = 'from window';\n"
1061             + "</script>\n"
1062             + "</body></html>";
1063 
1064         final WebDriver driver = loadPage2(html);
1065         driver.findElement(By.id("button1")).click();
1066 
1067         verifyTitle2(driver, getExpectedAlerts());
1068     }
1069 
1070     /**
1071      * Test that the parent scopes chain for an event handler.
1072      * @throws Exception if the test fails
1073      */
1074     @Test
1075     @Alerts({"from theField", "from theForm", "from document", "from window"})
1076     public void eventHandlersParentScopeChain_formFields() throws Exception {
1077         eventHandlersParentScopeChain("<button", "</button>");
1078         eventHandlersParentScopeChain("<select", "</select>");
1079         eventHandlersParentScopeChain("<textarea", "</textarea>");
1080 
1081         eventHandlersParentScopeChain("<input type='text'", "");
1082         eventHandlersParentScopeChain("<input type='password'", "");
1083 
1084         eventHandlersParentScopeChain("<input type='checkbox'", "");
1085         eventHandlersParentScopeChain("<input type='radio'", "");
1086 
1087         // eventHandlersParentScopeChain("<input type='file'", "");
1088         eventHandlersParentScopeChain("<input type='image'", "");
1089 
1090         eventHandlersParentScopeChain("<input type='button'", "");
1091 
1092         eventHandlersParentScopeChain("<input type='submit' value='xxx'", "");
1093         // case without value attribute was failing first with IE due to the way the value attribute was added
1094         eventHandlersParentScopeChain("<input type='submit'", "");
1095 
1096         eventHandlersParentScopeChain("<input type='reset' value='xxx'", "");
1097         // case without value attribute was failing first with IE due to the way the value attribute was added
1098         eventHandlersParentScopeChain("<input type='reset'", "");
1099     }
1100 
1101     /**
1102      * Test that the parent scopes chain for an event handler.
1103      * @throws Exception if the test fails
1104      */
1105     @Test
1106     @Alerts({"from theField", "from document", "from document", "from window"})
1107     public void eventHandlersParentScopeChain_span() throws Exception {
1108         eventHandlersParentScopeChain("<span", "</span>");
1109     }
1110 
1111     private void eventHandlersParentScopeChain(final String startTag, final String endTag) throws Exception {
1112         final String html = DOCTYPE_HTML
1113             + "<html><html>\n"
1114             + "<head>\n"
1115             + "<script>\n"
1116             + LOG_TITLE_FUNCTION
1117             + "</script>\n"
1118             + "</head>\n"
1119             + "<body id='body'>\n"
1120             + "<form id='theForm'>\n"
1121             + "  <div id='theDiv'>\n"
1122             + "    " + startTag + " id='theField' onclick='log(foo); return false;'>click me" + endTag + "\n"
1123             + "  </div>\n"
1124             + "</form>\n"
1125             + "<script>\n"
1126             + "  var foo = 'from window';\n"
1127             + "  document.foo = 'from document';\n"
1128             + "  document.body.foo = 'from body';\n"
1129             + "  document.getElementById('theForm').foo = 'from theForm';\n"
1130             + "  document.getElementById('theDiv').foo = 'from theDiv';\n"
1131             + "  document.getElementById('theField').foo = 'from theField';\n"
1132             + "</script>\n"
1133             + "</body></html>";
1134 
1135         final String[] alerts = getExpectedAlerts();
1136         final WebDriver driver = loadPage2(html);
1137         final WebElement field = driver.findElement(By.id("theField"));
1138         field.click();
1139         verifyTitle2(driver, alerts[0]);
1140 
1141         final JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
1142 
1143         // remove property on field
1144         jsExecutor.executeScript("delete document.getElementById('theField').foo");
1145         field.click();
1146         verifyTitle2(driver, alerts[0], alerts[1]);
1147 
1148         // remove property on form
1149         jsExecutor.executeScript("delete document.getElementById('theForm').foo");
1150         field.click();
1151         verifyTitle2(driver, alerts[0], alerts[1], alerts[2]);
1152 
1153         // remove property on document
1154         jsExecutor.executeScript("delete document.foo");
1155         field.click();
1156         verifyTitle2(driver, alerts[0], alerts[1], alerts[2], alerts[3]);
1157     }
1158 
1159     /**
1160      * Test that the function open resolves to document.open within a handler defined by an attribute.
1161      * @throws Exception if the test fails
1162      */
1163     @Test
1164     @Alerts("from document")
1165     public void eventHandlers_functionOpen() throws Exception {
1166         final String html = DOCTYPE_HTML
1167             + "<html><body>\n"
1168             + "<button id='button1' onclick='identify(open)'>click me</button>\n"
1169             + "<script>\n"
1170             + LOG_TITLE_FUNCTION
1171             + "function identify(fnOpen) {\n"
1172             + "  var origin = 'unknown';\n"
1173             + "  if (fnOpen === window.open) {\n"
1174             + "    origin = 'from window';\n"
1175             + "  }\n"
1176             + "  else if (fnOpen === document.open) {\n"
1177             + "    origin = 'from document';\n"
1178             + "  }\n"
1179             + "  log(origin);\n"
1180             + "}\n"
1181             + "</script>\n"
1182             + "</body></html>";
1183 
1184         final WebDriver driver = loadPage2(html);
1185         driver.findElement(By.id("button1")).click();
1186 
1187         verifyTitle2(driver, getExpectedAlerts());
1188     }
1189 
1190     /**
1191      * @throws Exception if the test fails
1192      */
1193     @Test
1194     @Alerts({"false", "boolean"})
1195     public void defaultPrevented() throws Exception {
1196         final String html = DOCTYPE_HTML
1197             + "<html><head><script>\n"
1198             + LOG_TITLE_FUNCTION
1199             + "  function test() {\n"
1200             + "    try {\n"
1201             + "      var event = document.createEvent('Event');\n"
1202             + "      log(event.defaultPrevented);\n"
1203             + "      log(typeof event.defaultPrevented);\n"
1204             + "    } catch(e) { logEx(e) }\n"
1205             + "  }\n"
1206             + "</script></head><body onload='test()'>\n"
1207             + "</body></html>";
1208 
1209         loadPageVerifyTitle2(html);
1210     }
1211 
1212     /**
1213      * @throws Exception if the test fails
1214      */
1215     @Test
1216     @Alerts({"true", "boolean"})
1217     public void returnValue() throws Exception {
1218         final String html = DOCTYPE_HTML
1219             + "<html><head><script>\n"
1220             + LOG_TITLE_FUNCTION
1221             + "  function test() {\n"
1222             + "    try {\n"
1223             + "      var event = document.createEvent('Event');\n"
1224             + "      log(event.returnValue);\n"
1225             + "      log(typeof event.returnValue);\n"
1226             + "    } catch(e) { logEx(e) }\n"
1227             + "  }\n"
1228             + "</script></head><body onload='test()'>\n"
1229             + "</body></html>";
1230 
1231         loadPageVerifyTitle2(html);
1232     }
1233 
1234     /**
1235      * @throws Exception if the test fails
1236      */
1237     @Test
1238     @Alerts({"true", "boolean", "false - false",
1239              "true", "true - false",
1240              "false", "boolean",
1241              "true", "boolean", "false - false",
1242              "true", "boolean"})
1243     public void returnValueSetter() throws Exception {
1244         final String html = DOCTYPE_HTML
1245             + "<html><head><script>\n"
1246             + LOG_TITLE_FUNCTION
1247             + "  function test() {\n"
1248             + "    try {\n"
1249             + "      var event = document.createEvent('Event');\n"
1250             + "      log(event.returnValue);\n"
1251             + "      log(typeof event.returnValue);\n"
1252             + "      log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1253 
1254             + "      event.initEvent('click', 'true', 'true');\n"
1255             + "      log(event.returnValue);\n"
1256             + "      log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1257 
1258             + "      event.preventDefault();\n"
1259             + "      log(event.returnValue);\n"
1260             + "      log(typeof event.returnValue);\n"
1261 
1262             + "      event = document.createEvent('Event');\n"
1263             + "      log(event.returnValue);\n"
1264             + "      log(typeof event.returnValue);\n"
1265             + "      log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1266 
1267             + "      event.preventDefault();\n"
1268             + "      log(event.returnValue);\n"
1269             + "      log(typeof event.returnValue);\n"
1270             + "    } catch(e) { logEx(e) }\n"
1271             + "  }\n"
1272             + "</script></head><body onload='test()'>\n"
1273             + "</body></html>";
1274 
1275         loadPageVerifyTitle2(html);
1276     }
1277 
1278     /**
1279      * @throws Exception if the test fails
1280      */
1281     @Test
1282     @Alerts({"true", "boolean", "false - false",
1283              "true", "true - false",
1284              "false", "boolean", "false",
1285              "true", "boolean", "false - false",
1286              "true", "boolean", "true",
1287              "true", "boolean", "true - false",
1288              "false", "boolean", "false"})
1289     public void returnValueSetterFalse() throws Exception {
1290         returnValueSetterUndefined("false");
1291     }
1292 
1293     /**
1294      * @throws Exception if the test fails
1295      */
1296     @Test
1297     @Alerts({"true", "boolean", "false - false",
1298              "true", "true - false",
1299              "true", "boolean", "false",
1300              "true", "boolean", "false - false",
1301              "true", "boolean", "true",
1302              "true", "boolean", "true - false",
1303              "true", "boolean", "false"})
1304     public void returnValueSetterTrue() throws Exception {
1305         returnValueSetterUndefined("true");
1306     }
1307 
1308     /**
1309      * @throws Exception if the test fails
1310      */
1311     @Test
1312     @Alerts({"true", "boolean", "false - false",
1313              "true", "true - false",
1314              "true", "boolean", "false",
1315              "true", "boolean", "false - false",
1316              "true", "boolean", "true",
1317              "true", "boolean", "true - false",
1318              "true", "boolean", "false"})
1319     public void returnValueSetterString() throws Exception {
1320         returnValueSetterUndefined("'test'");
1321     }
1322 
1323     /**
1324      * @throws Exception if the test fails
1325      */
1326     @Test
1327     @Alerts({"true", "boolean", "false - false",
1328              "true", "true - false",
1329              "false", "boolean", "false",
1330              "true", "boolean", "false - false",
1331              "true", "boolean", "true",
1332              "true", "boolean", "true - false",
1333              "false", "boolean", "false"})
1334     public void returnValueSetterZero() throws Exception {
1335         returnValueSetterUndefined("0");
1336     }
1337 
1338     /**
1339      * @throws Exception if the test fails
1340      */
1341     @Test
1342     @Alerts({"true", "boolean", "false - false",
1343              "true", "true - false",
1344              "true", "boolean", "false",
1345              "true", "boolean", "false - false",
1346              "true", "boolean", "true",
1347              "true", "boolean", "true - false",
1348              "true", "boolean", "false"})
1349     public void returnValueSetterOne() throws Exception {
1350         returnValueSetterUndefined("1");
1351     }
1352 
1353     /**
1354      * @throws Exception if the test fails
1355      */
1356     @Test
1357     @Alerts({"true", "boolean", "false - false",
1358              "true", "true - false",
1359              "true", "boolean", "false",
1360              "true", "boolean", "false - false",
1361              "true", "boolean", "true",
1362              "true", "boolean", "true - false",
1363              "true", "boolean", "false"})
1364     public void returnValueSetterMinusOne() throws Exception {
1365         returnValueSetterUndefined("-1");
1366     }
1367 
1368     /**
1369      * @throws Exception if the test fails
1370      */
1371     @Test
1372     @Alerts({"true", "boolean", "false - false",
1373              "true", "true - false",
1374              "false", "boolean", "false",
1375              "true", "boolean", "false - false",
1376              "true", "boolean", "true",
1377              "true", "boolean", "true - false",
1378              "false", "boolean", "false"})
1379     public void returnValueSetterUndefined() throws Exception {
1380         returnValueSetterUndefined("undefined");
1381     }
1382 
1383     private void returnValueSetterUndefined(final String value) throws Exception {
1384         final String html = DOCTYPE_HTML
1385             + "<html>\n"
1386             + "  <head></head>\n"
1387             + "  <body onload='test()'>\n"
1388             + "    <div><a id='triggerClick' href='#'>click event</a></div>\n"
1389 
1390             + "    <script>\n"
1391             + LOG_TITLE_FUNCTION
1392 
1393             + "      function test() {\n"
1394             + "        try {\n"
1395             + "          var event = document.createEvent('Event');\n"
1396             + "          log(event.returnValue);\n"
1397             + "          log(typeof event.returnValue);\n"
1398             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1399 
1400             + "          event.initEvent('click', 'true', 'true');\n"
1401             + "          log(event.returnValue);\n"
1402             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1403 
1404             + "          event.returnValue = " + value + ";\n"
1405             + "          log(event.returnValue);\n"
1406             + "          log(typeof event.returnValue);\n"
1407 
1408             + "          event.returnValue = !event.returnValue;\n"
1409             + "          log(event.returnValue);\n"
1410 
1411             + "          event = document.createEvent('Event');\n"
1412             + "          log(event.returnValue);\n"
1413             + "          log(typeof event.returnValue);\n"
1414             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1415 
1416             + "          event.returnValue = " + value + ";\n"
1417             + "          log(event.returnValue);\n"
1418             + "          log(typeof event.returnValue);\n"
1419 
1420             + "          event.returnValue = !event.returnValue;\n"
1421             + "          log(event.returnValue);\n"
1422             + "        } catch(e) { logEx(e) }\n"
1423             + "      }\n"
1424 
1425             + "      triggerClick.addEventListener('click', function (event) {\n"
1426             + "          log(event.returnValue);\n"
1427             + "          log(typeof event.returnValue);\n"
1428             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1429 
1430             + "          event.returnValue = " + value + ";\n"
1431             + "          log(event.returnValue);\n"
1432             + "          log(typeof event.returnValue);\n"
1433 
1434             + "          event.returnValue = !event.returnValue;\n"
1435             + "          log(event.returnValue);\n"
1436             + "        });\n"
1437             + "    </script>\n"
1438             + "  </body>\n"
1439             + "</html>";
1440 
1441         final WebDriver driver = loadPage2(html);
1442         driver.findElement(By.id("triggerClick")).click();
1443 
1444         verifyTitle2(driver, getExpectedAlerts());
1445     }
1446 
1447     /**
1448      * @throws Exception if the test fails
1449      */
1450     @Test
1451     @Alerts({"false - false", "true - false", "true - true",
1452              "false - false", "false - false", "false - false",
1453              "false - false", "true - false"})
1454     public void preventDefault() throws Exception {
1455         final String html = DOCTYPE_HTML
1456             + "<html>\n"
1457             + "  <head>\n"
1458             + "    <script>\n"
1459             + LOG_TITLE_FUNCTION
1460 
1461             + "      function test() {\n"
1462             + "        try {\n"
1463             + "          var event = document.createEvent('Event');\n"
1464             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1465 
1466             + "          event.initEvent('click', 'true', 'true');\n"
1467             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1468 
1469             + "          event.preventDefault();\n"
1470             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1471 
1472             + "          event = document.createEvent('Event');\n"
1473             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1474 
1475             + "          event.preventDefault();\n"
1476             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1477 
1478             + "          event = document.createEvent('Event');\n"
1479             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1480 
1481             + "          event.preventDefault();\n"
1482             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1483 
1484             + "          event.initEvent('click', 'true', 'true');\n"
1485             + "          log(event.cancelable + ' - ' + event.defaultPrevented);\n"
1486             + "        } catch(e) { logEx(e) }\n"
1487             + "      }\n"
1488             + "    </script>\n"
1489             + "  </head>\n"
1490             + "  <body onload='test()'>\n"
1491             + "  </body>\n"
1492             + "</html>";
1493 
1494         loadPageVerifyTitle2(html);
1495     }
1496 
1497     /**
1498      * @throws Exception if the test fails
1499      */
1500     @Test
1501     @Alerts("OK")
1502     public void domEventNameUsedAsFunctionName() throws Exception {
1503         final String html = DOCTYPE_HTML
1504             + "<html>\n"
1505             + "<head>\n"
1506             + "<script>\n"
1507             + LOG_TITLE_FUNCTION
1508             + "function onclick() {\n"
1509             + "  log('OK');\n"
1510             + "}\n"
1511             + "</script>\n"
1512             + "</head>\n"
1513             + "<body onload='onclick()'>\n"
1514             + "</body></html>";
1515 
1516         loadPageVerifyTitle2(html);
1517     }
1518 
1519     /**
1520      * @throws Exception if the test fails
1521      */
1522     @Test
1523     @Alerts(DEFAULT = {"[object Event]", "scroll", "false", "false", "false"},
1524             FF = {"[object UIEvent]", "scroll", "false", "true", "false"},
1525             FF_ESR = {"[object UIEvent]", "scroll", "false", "true", "false"})
1526     public void scrollEventFromScrollIntoView() throws Exception {
1527         final String html = DOCTYPE_HTML
1528             + "<html>\n"
1529             + "<head>\n"
1530             + "<script>\n"
1531             + LOG_TITLE_FUNCTION
1532             + DUMP_EVENT_FUNCTION
1533             + "</script>\n"
1534             + "</head>\n"
1535 
1536             + "<body>\n"
1537             + "  <div id='container' style='overflow-y: scroll; height: 100px;'>\n"
1538             + "    <div style='height: 1000px;'>spacer</div>\n"
1539             + "    <div id='target' style='background: red;'>Target</div>\n"
1540             + "  </div>\n"
1541 
1542             + "  <script>\n"
1543             + "    var c = document.getElementById('container');\n"
1544             + "    c.addEventListener('scroll', function(e) { dump(e); });\n"
1545 
1546             + "    var s = document.getElementById('target');"
1547             + "    s.scrollIntoView();\n"
1548             + "  </script>\n"
1549             + "</body>\n"
1550             + "</html>";
1551 
1552         loadPageVerifyTitle2(html);
1553     }
1554 
1555     /**
1556      * @throws Exception if the test fails
1557      */
1558     @Test
1559     @Alerts(DEFAULT = {"[object Event]", "scroll", "true", "false", "false"},
1560             FF = {"[object UIEvent]", "scroll", "true", "true", "false"},
1561             FF_ESR = {"[object UIEvent]", "scroll", "true", "true", "false"})
1562     public void scrollEventWindowFromScrollIntoView() throws Exception {
1563         final String html = DOCTYPE_HTML
1564             + "<html>\n"
1565             + "<head>\n"
1566             + "<script>\n"
1567             + LOG_TITLE_FUNCTION
1568             + DUMP_EVENT_FUNCTION
1569             + "</script>\n"
1570             + "</head>\n"
1571 
1572             + "<body>\n"
1573             + "  <div id='container' style='height: 10000px;'>\n"
1574             + "  </div>\n"
1575             + "  <div id='target' style='background: red;'>Target</div>\n"
1576 
1577             + "  <script>\n"
1578             + "    window.addEventListener('scroll', function(e) { dump(e); });\n"
1579 
1580             + "    var s = document.getElementById('target');"
1581             + "    s.scrollIntoView();\n"
1582             + "  </script>\n"
1583             + "</body>\n"
1584             + "</html>";
1585 
1586         loadPageVerifyTitle2(html);
1587     }
1588 
1589     /**
1590      * @throws Exception if the test fails
1591      */
1592     @Test
1593     @Alerts(DEFAULT = {"[object Event]", "scroll", "true", "false", "false"},
1594             FF = {"[object UIEvent]", "scroll", "true", "true", "false"},
1595             FF_ESR = {"[object UIEvent]", "scroll", "true", "true", "false"})
1596     public void scrollEventFromScrollBy() throws Exception {
1597         final String html = DOCTYPE_HTML
1598             + "<html>\n"
1599             + "<head>\n"
1600             + "<script>\n"
1601             + LOG_TITLE_FUNCTION
1602             + DUMP_EVENT_FUNCTION
1603             + "</script>\n"
1604             + "</head>\n"
1605 
1606             + "<body>\n"
1607             + "  <div style='height: 1000px;'></div>\n"
1608 
1609             + "  <script>\n"
1610             + "    document.addEventListener('scroll', function(e) { dump(e) });\n"
1611             + "    window.scrollBy(10, 20);\n"
1612             + "  </script>\n"
1613             + "</body>\n"
1614             + "</html>";
1615 
1616         loadPageVerifyTitle2(html);
1617     }
1618 
1619     /**
1620      * @throws Exception if the test fails
1621      */
1622     @Test
1623     @Alerts(DEFAULT = {"[object Event]", "scroll", "true", "false", "false"},
1624             FF = {"[object UIEvent]", "scroll", "true", "true", "false"},
1625             FF_ESR = {"[object UIEvent]", "scroll", "true", "true", "false"})
1626     public void scrollEventWindowFromScrollBy() throws Exception {
1627         final String html = DOCTYPE_HTML
1628             + "<html>\n"
1629             + "<head>\n"
1630             + "<script>\n"
1631             + LOG_TITLE_FUNCTION
1632             + DUMP_EVENT_FUNCTION
1633             + "</script>\n"
1634             + "</head>\n"
1635 
1636             + "<body>\n"
1637             + "  <div style='height: 1000px;'></div>\n"
1638 
1639             + "  <script>\n"
1640             + "    window.addEventListener('scroll', function(e) { dump(e) });\n"
1641             + "    window.scrollBy(10, 20);\n"
1642             + "  </script>\n"
1643             + "</body>\n"
1644             + "</html>";
1645 
1646         loadPageVerifyTitle2(html);
1647     }
1648 
1649     /**
1650      * @throws Exception if the test fails
1651      */
1652     @Test
1653     @Alerts(DEFAULT = {"[object Event]", "scroll", "true", "false", "false"},
1654             FF = {"[object UIEvent]", "scroll", "true", "true", "false"},
1655             FF_ESR = {"[object UIEvent]", "scroll", "true", "true", "false"})
1656     public void scrollEventFromScrollTo() throws Exception {
1657         final String html = DOCTYPE_HTML
1658             + "<html>\n"
1659             + "<head>\n"
1660             + "<script>\n"
1661             + LOG_TITLE_FUNCTION
1662             + DUMP_EVENT_FUNCTION
1663             + "</script>\n"
1664             + "</head>\n"
1665 
1666             + "<body>\n"
1667             + "  <div style='height: 1000px;'></div>\n"
1668 
1669             + "  <script>\n"
1670             + "    document.addEventListener('scroll', function(e) { dump(e) });\n"
1671             + "    window.scrollTo(10, 20);\n"
1672             + "  </script>\n"
1673             + "</body>\n"
1674             + "</html>";
1675 
1676         loadPageVerifyTitle2(html);
1677     }
1678 
1679     /**
1680      * @throws Exception if the test fails
1681      */
1682     @Test
1683     @Alerts(DEFAULT = {"[object Event]", "scroll", "true", "false", "false"},
1684             FF = {"[object UIEvent]", "scroll", "true", "true", "false"},
1685             FF_ESR = {"[object UIEvent]", "scroll", "true", "true", "false"})
1686     public void scrollEventWindowFromScrollTo() throws Exception {
1687         final String html = DOCTYPE_HTML
1688             + "<html>\n"
1689             + "<head>\n"
1690             + "<script>\n"
1691             + LOG_TITLE_FUNCTION
1692             + DUMP_EVENT_FUNCTION
1693             + "</script>\n"
1694             + "</head>\n"
1695 
1696             + "<body>\n"
1697             + "  <div style='height: 1000px;'></div>\n"
1698 
1699             + "  <script>\n"
1700             + "    window.addEventListener('scroll', function(e) { dump(e) });\n"
1701             + "    window.scrollTo(10, 20);\n"
1702             + "  </script>\n"
1703             + "</body>\n"
1704             + "</html>";
1705 
1706         loadPageVerifyTitle2(html);
1707     }
1708 }