02824a9a30dd5faefa14f07f4f83fb6c0e85613d
[jquery.git] / test / unit / event.js
1 module("event", { teardown: moduleTeardown });
2
3 test("null or undefined handler", function() {
4         expect(2);
5   // Supports Fixes bug #7229
6   try {
7
8     jQuery("#firstp").click(null);
9
10     ok(true, "Passing a null handler will not throw an exception");
11
12   } catch (e) {}
13
14   try {
15
16     jQuery("#firstp").click(undefined);
17
18     ok(true, "Passing an undefined handler will not throw an exception");
19
20   } catch (e) {}
21 });
22
23 test("bind(), with data", function() {
24         expect(3);
25         var handler = function(event) {
26                 ok( event.data, "bind() with data, check passed data exists" );
27                 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
28         };
29         jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
30
31         ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
32 });
33
34 test("click(), with data", function() {
35         expect(3);
36         var handler = function(event) {
37                 ok( event.data, "bind() with data, check passed data exists" );
38                 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
39         };
40         jQuery("#firstp").click({foo: "bar"}, handler).click().unbind("click", handler);
41
42         ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
43 });
44
45 test("bind(), with data, trigger with data", function() {
46         expect(4);
47         var handler = function(event, data) {
48                 ok( event.data, "check passed data exists" );
49                 equals( event.data.foo, "bar", "Check value of passed data" );
50                 ok( data, "Check trigger data" );
51                 equals( data.bar, "foo", "Check value of trigger data" );
52         };
53         jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
54 });
55
56 test("bind(), multiple events at once", function() {
57         expect(2);
58         var clickCounter = 0,
59                 mouseoverCounter = 0;
60         var handler = function(event) {
61                 if (event.type == "click")
62                         clickCounter += 1;
63                 else if (event.type == "mouseover")
64                         mouseoverCounter += 1;
65         };
66         jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
67         equals( clickCounter, 1, "bind() with multiple events at once" );
68         equals( mouseoverCounter, 1, "bind() with multiple events at once" );
69 });
70
71 test("bind(), multiple events at once and namespaces", function() {
72         expect(7);
73
74         var cur, obj = {};
75
76         var div = jQuery("<div/>").bind("focusin.a", function(e) {
77                 equals( e.type, cur, "Verify right single event was fired." );
78         });
79
80         cur = "focusin";
81         div.trigger("focusin.a");
82
83         // manually clean up detached elements
84         div.remove();
85
86         div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
87                 equals( e.type, cur, "Verify right multi event was fired." );
88                 equals( e.data, obj, "Make sure the data came in correctly." );
89         });
90
91         cur = "click";
92         div.trigger("click");
93
94         cur = "mouseover";
95         div.trigger("mouseover");
96
97         // manually clean up detached elements
98         div.remove();
99
100         div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
101                 equals( e.type, cur, "Verify right multi event was fired." );
102         });
103
104         cur = "focusin";
105         div.trigger("focusin.a");
106
107         cur = "focusout";
108         div.trigger("focusout.b");
109
110         // manually clean up detached elements
111         div.remove();
112 });
113
114 test("bind(), namespace with special add", function() {
115         expect(24);
116
117         var div = jQuery("<div/>").bind("test", function(e) {
118                 ok( true, "Test event fired." );
119         });
120
121         var i = 0;
122
123         jQuery.event.special.test = {
124                 _default: function(e) {
125                         equals( this, document, "Make sure we're at the top of the chain." );
126                         equals( e.type, "test", "And that we're still dealing with a test event." );
127                         equals( e.target, div[0], "And that the target is correct." );
128                 },
129                 setup: function(){},
130                 teardown: function(){
131                         ok(true, "Teardown called.");
132                 },
133                 add: function( handleObj ) {
134                         var handler = handleObj.handler;
135                         handleObj.handler = function(e) {
136                                 e.xyz = ++i;
137                                 handler.apply( this, arguments );
138                         };
139                 },
140                 remove: function() {
141                         ok(true, "Remove called.");
142                 }
143         };
144
145         div.bind("test.a", {x: 1}, function(e) {
146                 ok( !!e.xyz, "Make sure that the data is getting passed through." );
147                 equals( e.data.x, 1, "Make sure data is attached properly." );
148         });
149
150         div.bind("test.b", {x: 2}, function(e) {
151                 ok( !!e.xyz, "Make sure that the data is getting passed through." );
152                 equals( e.data.x, 2, "Make sure data is attached properly." );
153         });
154
155         // Should trigger 5
156         div.trigger("test");
157
158         // Should trigger 2
159         div.trigger("test.a");
160
161         // Should trigger 2
162         div.trigger("test.b");
163
164         // Should trigger 4
165         div.unbind("test");
166
167         div = jQuery("<div/>").bind("test", function(e) {
168                 ok( true, "Test event fired." );
169         });
170
171         // Should trigger 2
172         div.appendTo("#main").remove();
173
174         delete jQuery.event.special.test;
175 });
176
177 test("bind(), no data", function() {
178         expect(1);
179         var handler = function(event) {
180                 ok ( !event.data, "Check that no data is added to the event object" );
181         };
182         jQuery("#firstp").bind("click", handler).trigger("click");
183 });
184
185 test("bind/one/unbind(Object)", function(){
186         expect(6);
187
188         var clickCounter = 0, mouseoverCounter = 0;
189         function handler(event) {
190                 if (event.type == "click")
191                         clickCounter++;
192                 else if (event.type == "mouseover")
193                         mouseoverCounter++;
194         };
195
196         function handlerWithData(event) {
197                 if (event.type == "click")
198                         clickCounter += event.data;
199                 else if (event.type == "mouseover")
200                         mouseoverCounter += event.data;
201         };
202
203         function trigger(){
204                 $elem.trigger("click").trigger("mouseover");
205         }
206
207         var $elem = jQuery("#firstp")
208                 // Regular bind
209                 .bind({
210                         click:handler,
211                         mouseover:handler
212                 })
213                 // Bind with data
214                 .one({
215                         click:handlerWithData,
216                         mouseover:handlerWithData
217                 }, 2 );
218
219         trigger();
220
221         equals( clickCounter, 3, "bind(Object)" );
222         equals( mouseoverCounter, 3, "bind(Object)" );
223
224         trigger();
225         equals( clickCounter, 4, "bind(Object)" );
226         equals( mouseoverCounter, 4, "bind(Object)" );
227
228         jQuery("#firstp").unbind({
229                 click:handler,
230                 mouseover:handler
231         });
232
233         trigger();
234         equals( clickCounter, 4, "bind(Object)" );
235         equals( mouseoverCounter, 4, "bind(Object)" );
236 });
237
238 test("live/die(Object), delegate/undelegate(String, Object)", function() {
239         expect(6);
240
241         var clickCounter = 0, mouseoverCounter = 0,
242                 $p = jQuery("#firstp"), $a = $p.find("a:first");
243
244         var events = {
245                 click: function( event ) {
246                         clickCounter += ( event.data || 1 );
247                 },
248                 mouseover: function( event ) {
249                         mouseoverCounter += ( event.data || 1 );
250                 }
251         };
252
253         function trigger() {
254                 $a.trigger("click").trigger("mouseover");
255         }
256
257         $a.live( events );
258         $p.delegate( "a", events, 2 );
259
260         trigger();
261         equals( clickCounter, 3, "live/delegate" );
262         equals( mouseoverCounter, 3, "live/delegate" );
263
264         $p.undelegate( "a", events );
265
266         trigger();
267         equals( clickCounter, 4, "undelegate" );
268         equals( mouseoverCounter, 4, "undelegate" );
269
270         $a.die( events );
271
272         trigger();
273         equals( clickCounter, 4, "die" );
274         equals( mouseoverCounter, 4, "die" );
275 });
276
277 test("live/delegate immediate propagation", function() {
278         expect(2);
279
280         var $p = jQuery("#firstp"), $a = $p.find("a:first"), lastClick;
281
282         lastClick = "";
283         $a.live( "click", function(e) {
284                 lastClick = "click1";
285                 e.stopImmediatePropagation();
286         });
287         $a.live( "click", function(e) {
288                 lastClick = "click2";
289         });
290         $a.trigger( "click" );
291         equals( lastClick, "click1", "live stopImmediatePropagation" );
292         $a.die( "click" );
293
294         lastClick = "";
295         $p.delegate( "a", "click", function(e) {
296                 lastClick = "click1";
297                 e.stopImmediatePropagation();
298         });
299         $p.delegate( "a", "click", function(e) {
300                 lastClick = "click2";
301         });
302         $a.trigger( "click" );
303         equals( lastClick, "click1", "delegate stopImmediatePropagation" );
304         $p.undelegate( "click" );
305 });
306
307 test("bind/delegate bubbling, isDefaultPrevented", function() {
308         expect(2);
309         var $anchor2 = jQuery( "#anchor2" ),
310                 $main = jQuery( "#main" ),
311                 fakeClick = function($jq) {
312                         // Use a native click so we don't get jQuery simulated bubbling
313                         if ( document.createEvent ) {
314                                 var e = document.createEvent( 'MouseEvents' );
315                                 e.initEvent( "click", true, true ); 
316                                 $jq[0].dispatchEvent(e);
317                         }
318                         else if ( $jq[0].click ) {
319                                 $jq[0].click(); // IE
320                         }
321                 };
322         $anchor2.click(function(e) {
323                 e.preventDefault();
324         });
325         $main.delegate("#foo", "click", function(e) {
326                 var orig = e.originalEvent;
327
328                 if ( typeof(orig.defaultPrevented) === "boolean" || typeof(orig.returnValue) === "boolean" || orig.getPreventDefault ) {
329                         equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
330
331                 } else {
332                         // Opera < 11 doesn't implement any interface we can use, so give it a pass
333                         ok( true, "isDefaultPrevented not supported by this browser, test skipped" );
334                 }
335         });
336         fakeClick( $anchor2 );
337         $anchor2.unbind( "click" );
338         $main.undelegate( "click" );
339         $anchor2.click(function(e) {
340                 // Let the default action occur
341         });
342         $main.delegate("#foo", "click", function(e) {
343                 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
344         });
345         fakeClick( $anchor2 );
346         $anchor2.unbind( "click" );
347         $main.undelegate( "click" );
348 });
349
350 test("bind(), iframes", function() {
351         // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
352         var doc = jQuery("#loadediframe").contents();
353
354         jQuery("div", doc).bind("click", function() {
355                 ok( true, "Binding to element inside iframe" );
356         }).click().unbind('click');
357 });
358
359 test("bind(), trigger change on select", function() {
360         expect(5);
361         var counter = 0;
362         function selectOnChange(event) {
363                 equals( event.data, counter++, "Event.data is not a global event object" );
364         };
365         jQuery("#form select").each(function(i){
366                 jQuery(this).bind('change', i, selectOnChange);
367         }).trigger('change');
368 });
369
370 test("bind(), namespaced events, cloned events", function() {
371         expect(6);
372
373         jQuery("#firstp").bind("custom.test",function(e){
374                 ok(true, "Custom event triggered");
375         });
376
377         jQuery("#firstp").bind("click",function(e){
378                 ok(true, "Normal click triggered");
379         });
380
381         jQuery("#firstp").bind("click.test",function(e){
382                 ok(true, "Namespaced click triggered");
383         });
384
385         // Trigger both bound fn (2)
386         jQuery("#firstp").trigger("click");
387
388         // Trigger one bound fn (1)
389         jQuery("#firstp").trigger("click.test");
390
391         // Remove only the one fn
392         jQuery("#firstp").unbind("click.test");
393
394         // Trigger the remaining fn (1)
395         jQuery("#firstp").trigger("click");
396
397         // Remove the remaining fn
398         jQuery("#firstp").unbind(".test");
399
400         // Trigger the remaining fn (0)
401         jQuery("#firstp").trigger("custom");
402
403         // using contents will get comments regular, text, and comment nodes
404         jQuery("#nonnodes").contents().bind("tester", function () {
405                 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
406         }).trigger("tester");
407
408         // Make sure events stick with appendTo'd elements (which are cloned) #2027
409         jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
410         ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
411 });
412
413 test("bind(), multi-namespaced events", function() {
414         expect(6);
415
416         var order = [
417                 "click.test.abc",
418                 "click.test.abc",
419                 "click.test",
420                 "click.test.abc",
421                 "click.test",
422                 "custom.test2"
423         ];
424
425         function check(name, msg){
426                 same(name, order.shift(), msg);
427         }
428
429         jQuery("#firstp").bind("custom.test",function(e){
430                 check("custom.test", "Custom event triggered");
431         });
432
433         jQuery("#firstp").bind("custom.test2",function(e){
434                 check("custom.test2", "Custom event triggered");
435         });
436
437         jQuery("#firstp").bind("click.test",function(e){
438                 check("click.test", "Normal click triggered");
439         });
440
441         jQuery("#firstp").bind("click.test.abc",function(e){
442                 check("click.test.abc", "Namespaced click triggered");
443         });
444
445         // Those would not trigger/unbind (#5303)
446         jQuery("#firstp").trigger("click.a.test");
447         jQuery("#firstp").unbind("click.a.test");
448
449         // Trigger both bound fn (1)
450         jQuery("#firstp").trigger("click.test.abc");
451
452         // Trigger one bound fn (1)
453         jQuery("#firstp").trigger("click.abc");
454
455         // Trigger two bound fn (2)
456         jQuery("#firstp").trigger("click.test");
457
458         // Remove only the one fn
459         jQuery("#firstp").unbind("click.abc");
460
461         // Trigger the remaining fn (1)
462         jQuery("#firstp").trigger("click");
463
464         // Remove the remaining fn
465         jQuery("#firstp").unbind(".test");
466
467         // Trigger the remaining fn (1)
468         jQuery("#firstp").trigger("custom");
469 });
470
471 test("bind(), with same function", function() {
472         expect(2)
473
474         var count = 0 ,  func = function(){
475                 count++;
476         };
477
478         jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
479         jQuery("#liveHandlerOrder").trigger("foo.bar");
480
481         equals(count, 1, "Verify binding function with multiple namespaces." );
482
483         jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
484         jQuery("#liveHandlerOrder").trigger("foo.bar");
485
486         equals(count, 1, "Verify that removing events still work." );
487 });
488
489 test("bind(), make sure order is maintained", function() {
490         expect(1);
491
492         var elem = jQuery("#firstp"), log = [], check = [];
493
494         for ( var i = 0; i < 100; i++ ) (function(i){
495                 elem.bind( "click", function(){
496                         log.push( i );
497                 });
498
499                 check.push( i );
500         })(i);
501
502         elem.trigger("click");
503
504         equals( log.join(","), check.join(","), "Make sure order was maintained." );
505
506         elem.unbind("click");
507 });
508
509 test("bind(), with different this object", function() {
510         expect(4);
511         var thisObject = { myThis: true },
512                 data = { myData: true },
513                 handler1 = function( event ) {
514                         equals( this, thisObject, "bind() with different this object" );
515                 },
516                 handler2 = function( event ) {
517                         equals( this, thisObject, "bind() with different this object and data" );
518                         equals( event.data, data, "bind() with different this object and data" );
519                 };
520
521         jQuery("#firstp")
522                 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
523                 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
524
525         ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
526 });
527
528 test("bind(name, false), unbind(name, false)", function() {
529         expect(3);
530
531         var main = 0;
532         jQuery("#main").bind("click", function(e){ main++; });
533         jQuery("#ap").trigger("click");
534         equals( main, 1, "Verify that the trigger happened correctly." );
535
536         main = 0;
537         jQuery("#ap").bind("click", false);
538         jQuery("#ap").trigger("click");
539         equals( main, 0, "Verify that no bubble happened." );
540
541         main = 0;
542         jQuery("#ap").unbind("click", false);
543         jQuery("#ap").trigger("click");
544         equals( main, 1, "Verify that the trigger happened correctly." );
545
546         // manually clean up events from elements outside the fixture
547         jQuery("#main").unbind("click");
548 });
549
550 test("bind()/trigger()/unbind() on plain object", function() {
551         expect( 8 );
552
553         var obj = {};
554
555         // Make sure it doesn't complain when no events are found
556         jQuery(obj).trigger("test");
557
558         // Make sure it doesn't complain when no events are found
559         jQuery(obj).unbind("test");
560
561         jQuery(obj).bind({
562                 test: function() {
563                         ok( true, "Custom event run." );
564                 },
565                 submit: function() {
566                         ok( true, "Custom submit event run." );
567                 }
568         });
569
570         var events = jQuery._data(obj, "events");
571         ok( events, "Object has events bound." );
572         equals( obj.events, undefined, "Events object on plain objects is not events" );
573         equals( typeof events, "function", "'events' expando is a function on plain objects." );
574         equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
575         equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
576
577         // Should trigger 1
578         jQuery(obj).trigger("test");
579         jQuery(obj).trigger("submit");
580
581         jQuery(obj).unbind("test");
582         jQuery(obj).unbind("submit");
583
584         // Should trigger 0
585         jQuery(obj).trigger("test");
586
587         // Make sure it doesn't complain when no events are found
588         jQuery(obj).unbind("test");
589
590         equals( obj && obj[ jQuery.expando ] &&
591                     obj[ jQuery.expando ][ jQuery.expando ] &&
592                         obj[ jQuery.expando ][ jQuery.expando ].events, undefined, "Make sure events object is removed" );
593 });
594
595 test("unbind(type)", function() {
596         expect( 0 );
597
598         var $elem = jQuery("#firstp"),
599                 message;
600
601         function error(){
602                 ok( false, message );
603         }
604
605         message = "unbind passing function";
606         $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
607
608         message = "unbind all from event";
609         $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
610
611         message = "unbind all";
612         $elem.bind('error1', error).unbind().triggerHandler('error1');
613
614         message = "unbind many with function";
615         $elem.bind('error1 error2',error)
616                  .unbind('error1 error2', error )
617                  .trigger('error1').triggerHandler('error2');
618
619         message = "unbind many"; // #3538
620         $elem.bind('error1 error2',error)
621                  .unbind('error1 error2')
622                  .trigger('error1').triggerHandler('error2');
623
624         message = "unbind without a type or handler";
625         $elem.bind("error1 error2.test",error)
626                  .unbind()
627                  .trigger("error1").triggerHandler("error2");
628 });
629
630 test("unbind(eventObject)", function() {
631         expect(4);
632
633         var $elem = jQuery("#firstp"),
634                 num;
635
636         function assert( expected ){
637                 num = 0;
638                 $elem.trigger('foo').triggerHandler('bar');
639                 equals( num, expected, "Check the right handlers are triggered" );
640         }
641
642         $elem
643                 // This handler shouldn't be unbound
644                 .bind('foo', function(){
645                         num += 1;
646                 })
647                 .bind('foo', function(e){
648                         $elem.unbind( e )
649                         num += 2;
650                 })
651                 // Neither this one
652                 .bind('bar', function(){
653                         num += 4;
654                 });
655
656         assert( 7 );
657         assert( 5 );
658
659         $elem.unbind('bar');
660         assert( 1 );
661
662         $elem.unbind();
663         assert( 0 );
664 });
665
666 test("hover()", function() {
667         var times = 0,
668                 handler1 = function( event ) { ++times; },
669                 handler2 = function( event ) { ++times; };
670
671         jQuery("#firstp")
672                 .hover(handler1, handler2)
673                 .mouseenter().mouseleave()
674                 .unbind("mouseenter", handler1)
675                 .unbind("mouseleave", handler2)
676                 .hover(handler1)
677                 .mouseenter().mouseleave()
678                 .unbind("mouseenter mouseleave", handler1)
679                 .mouseenter().mouseleave();
680
681         equals( times, 4, "hover handlers fired" );
682 });
683
684 test("trigger() shortcuts", function() {
685         expect(6);
686
687         var elem = jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL');
688         elem.find('a').bind('click', function() {
689                 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
690                 equals( close.length, 0, "Context element does not exist, length must be zero" );
691                 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
692                 return false;
693         }).click();
694
695         // manually clean up detached elements
696         elem.remove();
697
698         jQuery("#check1").click(function() {
699                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
700         }).click();
701
702         var counter = 0;
703         jQuery('#firstp')[0].onclick = function(event) {
704                 counter++;
705         };
706         jQuery('#firstp').click();
707         equals( counter, 1, "Check that click, triggers onclick event handler also" );
708
709         var clickCounter = 0;
710         jQuery('#simon1')[0].onclick = function(event) {
711                 clickCounter++;
712         };
713         jQuery('#simon1').click();
714         equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
715
716         elem = jQuery('<img />').load(function(){
717                 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
718         }).load();
719
720         // manually clean up detached elements
721         elem.remove();
722 });
723
724 test("trigger() bubbling", function() {
725         expect(14);
726
727         var doc = 0, html = 0, body = 0, main = 0, ap = 0;
728
729         jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
730         jQuery("html").bind("click", function(e){ html++; });
731         jQuery("body").bind("click", function(e){ body++; });
732         jQuery("#main").bind("click", function(e){ main++; });
733         jQuery("#ap").bind("click", function(){ ap++; return false; });
734
735         jQuery("html").trigger("click");
736         equals( doc, 1, "HTML bubble" );
737         equals( html, 1, "HTML bubble" );
738
739         jQuery("body").trigger("click");
740         equals( doc, 2, "Body bubble" );
741         equals( html, 2, "Body bubble" );
742         equals( body, 1, "Body bubble" );
743
744         jQuery("#main").trigger("click");
745         equals( doc, 3, "Main bubble" );
746         equals( html, 3, "Main bubble" );
747         equals( body, 2, "Main bubble" );
748         equals( main, 1, "Main bubble" );
749
750         jQuery("#ap").trigger("click");
751         equals( doc, 3, "ap bubble" );
752         equals( html, 3, "ap bubble" );
753         equals( body, 2, "ap bubble" );
754         equals( main, 1, "ap bubble" );
755         equals( ap, 1, "ap bubble" );
756
757         // manually clean up events from elements outside the fixture
758         jQuery(document).unbind("click");
759         jQuery("html, body, #main").unbind("click");
760 });
761
762 test("trigger(type, [data], [fn])", function() {
763         expect(14);
764
765         var handler = function(event, a, b, c) {
766                 equals( event.type, "click", "check passed data" );
767                 equals( a, 1, "check passed data" );
768                 equals( b, "2", "check passed data" );
769                 equals( c, "abc", "check passed data" );
770                 return "test";
771         };
772
773         var $elem = jQuery("#firstp");
774
775         // Simulate a "native" click
776         $elem[0].click = function(){
777                 ok( true, "Native call was triggered" );
778         };
779
780         // Triggers handlrs and native
781         // Trigger 5
782         $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
783
784         // Simulate a "native" click
785         $elem[0].click = function(){
786                 ok( false, "Native call was triggered" );
787         };
788
789         // Trigger only the handlers (no native)
790         // Triggers 5
791         equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
792
793         var pass = true;
794         try {
795                 jQuery('#form input:first').hide().trigger('focus');
796         } catch(e) {
797                 pass = false;
798         }
799         ok( pass, "Trigger focus on hidden element" );
800
801         pass = true;
802         try {
803                 jQuery('#main table:first').bind('test:test', function(){}).trigger('test:test');
804         } catch (e) {
805                 pass = false;
806         }
807         ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
808
809         var form = jQuery("<form action=''></form>").appendTo("body");
810
811         // Make sure it can be prevented locally
812         form.submit(function(){
813                 ok( true, "Local bind still works." );
814                 return false;
815         });
816
817         // Trigger 1
818         form.trigger("submit");
819
820         form.unbind("submit");
821
822         jQuery(document).submit(function(){
823                 ok( true, "Make sure bubble works up to document." );
824                 return false;
825         });
826
827         // Trigger 1
828         form.trigger("submit");
829
830         jQuery(document).unbind("submit");
831
832         form.remove();
833 });
834
835 test("jQuery.Event.currentTarget", function(){
836 });
837
838 test("trigger(eventObject, [data], [fn])", function() {
839         expect(25);
840
841         var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
842                 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
843
844         var event = jQuery.Event("noNew");
845         ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
846         equals( event.type, "noNew", "Verify its type" );
847
848         equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
849         equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
850         equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
851
852         event.preventDefault();
853         equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
854         event.stopPropagation();
855         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
856
857         event.isPropagationStopped = function(){ return false };
858         event.stopImmediatePropagation();
859         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
860         equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
861
862         $parent.bind('foo',function(e){
863                 // Tries bubbling
864                 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
865                 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
866                 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
867                 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
868         });
869
870         // test with an event object
871         event = new jQuery.Event("foo");
872         event.secret = 'boo!';
873         $child.trigger(event);
874
875         // test with a literal object
876         $child.trigger({type:'foo', secret:'boo!'});
877
878         $parent.unbind();
879
880         function error(){
881                 ok( false, "This assertion shouldn't be reached");
882         }
883
884         $parent.bind('foo', error );
885
886         $child.bind('foo',function(e, a, b, c ){
887                 equals( arguments.length, 4, "Check arguments length");
888                 equals( a, 1, "Check first custom argument");
889                 equals( b, 2, "Check second custom argument");
890                 equals( c, 3, "Check third custom argument");
891
892                 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
893                 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
894                 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
895
896                 // Skips both errors
897                 e.stopImmediatePropagation();
898
899                 return "result";
900         });
901
902         // We should add this back in when we want to test the order
903         // in which event handlers are iterated.
904         //$child.bind('foo', error );
905
906         event = new jQuery.Event("foo");
907         $child.trigger( event, [1,2,3] ).unbind();
908         equals( event.result, "result", "Check event.result attribute");
909
910         // Will error if it bubbles
911         $child.triggerHandler('foo');
912
913         $child.unbind();
914         $parent.unbind().remove();
915 });
916
917 test("jQuery.Event.currentTarget", function(){
918         expect(1);
919
920         var counter = 0,
921                 $elem = jQuery('<button>a</button>').click(function(e){
922                 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
923         });
924
925         // Fake event
926         $elem.trigger('click');
927
928         // Cleanup
929         $elem.unbind();
930 });
931
932 test("toggle(Function, Function, ...)", function() {
933         expect(16);
934
935         var count = 0,
936                 fn1 = function(e) { count++; },
937                 fn2 = function(e) { count--; },
938                 preventDefault = function(e) { e.preventDefault() },
939                 link = jQuery('#mark');
940         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
941         equals( count, 1, "Check for toggle(fn, fn)" );
942
943         jQuery("#firstp").toggle(function () {
944                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
945         }, function() {}).trigger("click", [ 1, 2, 3 ]);
946
947         var first = 0;
948         jQuery("#simon1").one("click", function() {
949                 ok( true, "Execute event only once" );
950                 jQuery(this).toggle(function() {
951                         equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
952                 }, function() {
953                         equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
954                 });
955                 return false;
956         }).click().click().click();
957
958         var turn = 0;
959         var fns = [
960                 function(){
961                         turn = 1;
962                 },
963                 function(){
964                         turn = 2;
965                 },
966                 function(){
967                         turn = 3;
968                 }
969         ];
970
971         var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
972         $div.click();
973         equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
974         $div.click();
975         equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
976         $div.click();
977         equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
978         $div.click();
979         equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
980         $div.click();
981         equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
982
983         $div.unbind('click',fns[0]);
984         var data = jQuery._data( $div[0], 'events' );
985         ok( !data, "Unbinding one function from toggle unbinds them all");
986
987         // manually clean up detached elements
988         $div.remove();
989
990         // Test Multi-Toggles
991         var a = [], b = [];
992         $div = jQuery("<div/>");
993         $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
994         $div.click();
995         same( a, [1], "Check that a click worked." );
996
997         $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
998         $div.click();
999         same( a, [1,2], "Check that a click worked with a second toggle." );
1000         same( b, [1], "Check that a click worked with a second toggle." );
1001
1002         $div.click();
1003         same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
1004         same( b, [1,2], "Check that a click worked with a second toggle, second click." );
1005
1006         // manually clean up detached elements
1007         $div.remove();
1008 });
1009
1010 test(".live()/.die()", function() {
1011         expect(66);
1012
1013         var submit = 0, div = 0, livea = 0, liveb = 0;
1014
1015         jQuery("div").live("submit", function(){ submit++; return false; });
1016         jQuery("div").live("click", function(){ div++; });
1017         jQuery("div#nothiddendiv").live("click", function(){ livea++; });
1018         jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
1019
1020         // Nothing should trigger on the body
1021         jQuery("body").trigger("click");
1022         equals( submit, 0, "Click on body" );
1023         equals( div, 0, "Click on body" );
1024         equals( livea, 0, "Click on body" );
1025         equals( liveb, 0, "Click on body" );
1026
1027         // This should trigger two events
1028         submit = 0, div = 0, livea = 0, liveb = 0;
1029         jQuery("div#nothiddendiv").trigger("click");
1030         equals( submit, 0, "Click on div" );
1031         equals( div, 1, "Click on div" );
1032         equals( livea, 1, "Click on div" );
1033         equals( liveb, 0, "Click on div" );
1034
1035         // This should trigger three events (w/ bubbling)
1036         submit = 0, div = 0, livea = 0, liveb = 0;
1037         jQuery("div#nothiddendivchild").trigger("click");
1038         equals( submit, 0, "Click on inner div" );
1039         equals( div, 2, "Click on inner div" );
1040         equals( livea, 1, "Click on inner div" );
1041         equals( liveb, 1, "Click on inner div" );
1042
1043         // This should trigger one submit
1044         submit = 0, div = 0, livea = 0, liveb = 0;
1045         jQuery("div#nothiddendivchild").trigger("submit");
1046         equals( submit, 1, "Submit on div" );
1047         equals( div, 0, "Submit on div" );
1048         equals( livea, 0, "Submit on div" );
1049         equals( liveb, 0, "Submit on div" );
1050
1051         // Make sure no other events were removed in the process
1052         submit = 0, div = 0, livea = 0, liveb = 0;
1053         jQuery("div#nothiddendivchild").trigger("click");
1054         equals( submit, 0, "die Click on inner div" );
1055         equals( div, 2, "die Click on inner div" );
1056         equals( livea, 1, "die Click on inner div" );
1057         equals( liveb, 1, "die Click on inner div" );
1058
1059         // Now make sure that the removal works
1060         submit = 0, div = 0, livea = 0, liveb = 0;
1061         jQuery("div#nothiddendivchild").die("click");
1062         jQuery("div#nothiddendivchild").trigger("click");
1063         equals( submit, 0, "die Click on inner div" );
1064         equals( div, 2, "die Click on inner div" );
1065         equals( livea, 1, "die Click on inner div" );
1066         equals( liveb, 0, "die Click on inner div" );
1067
1068         // Make sure that the click wasn't removed too early
1069         submit = 0, div = 0, livea = 0, liveb = 0;
1070         jQuery("div#nothiddendiv").trigger("click");
1071         equals( submit, 0, "die Click on inner div" );
1072         equals( div, 1, "die Click on inner div" );
1073         equals( livea, 1, "die Click on inner div" );
1074         equals( liveb, 0, "die Click on inner div" );
1075
1076         // Make sure that stopPropgation doesn't stop live events
1077         submit = 0, div = 0, livea = 0, liveb = 0;
1078         jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1079         jQuery("div#nothiddendivchild").trigger("click");
1080         equals( submit, 0, "stopPropagation Click on inner div" );
1081         equals( div, 1, "stopPropagation Click on inner div" );
1082         equals( livea, 0, "stopPropagation Click on inner div" );
1083         equals( liveb, 1, "stopPropagation Click on inner div" );
1084
1085         // Make sure click events only fire with primary click
1086         submit = 0, div = 0, livea = 0, liveb = 0;
1087         var event = jQuery.Event("click");
1088         event.button = 1;
1089         jQuery("div#nothiddendiv").trigger(event);
1090
1091         equals( livea, 0, "live secondary click" );
1092
1093         jQuery("div#nothiddendivchild").die("click");
1094         jQuery("div#nothiddendiv").die("click");
1095         jQuery("div").die("click");
1096         jQuery("div").die("submit");
1097
1098         // Test binding with a different context
1099         var clicked = 0, container = jQuery('#main')[0];
1100         jQuery("#foo", container).live("click", function(e){ clicked++; });
1101         jQuery("div").trigger('click');
1102         jQuery("#foo").trigger('click');
1103         jQuery("#main").trigger('click');
1104         jQuery("body").trigger('click');
1105         equals( clicked, 2, "live with a context" );
1106
1107         // Make sure the event is actually stored on the context
1108         ok( jQuery._data(container, "events").live, "live with a context" );
1109
1110         // Test unbinding with a different context
1111         jQuery("#foo", container).die("click");
1112         jQuery("#foo").trigger('click');
1113         equals( clicked, 2, "die with a context");
1114
1115         // Test binding with event data
1116         jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1117         jQuery("#foo").trigger("click").die("click");
1118
1119         // Test binding with trigger data
1120         jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1121         jQuery("#foo").trigger("click", true).die("click");
1122
1123         // Test binding with different this object
1124         jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1125         jQuery("#foo").trigger("click").die("click");
1126
1127         // Test binding with different this object, event data, and trigger data
1128         jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1129                 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1130                 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1131                 equals( data, true, "live with with different this object, event data, and trigger data")
1132         }, { foo: "bar" }));
1133         jQuery("#foo").trigger("click", true).die("click");
1134
1135         // Verify that return false prevents default action
1136         jQuery("#anchor2").live("click", function(){ return false; });
1137         var hash = window.location.hash;
1138         jQuery("#anchor2").trigger("click");
1139         equals( window.location.hash, hash, "return false worked" );
1140         jQuery("#anchor2").die("click");
1141
1142         // Verify that .preventDefault() prevents default action
1143         jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1144         var hash = window.location.hash;
1145         jQuery("#anchor2").trigger("click");
1146         equals( window.location.hash, hash, "e.preventDefault() worked" );
1147         jQuery("#anchor2").die("click");
1148
1149         // Test binding the same handler to multiple points
1150         var called = 0;
1151         function callback(){ called++; return false; }
1152
1153         jQuery("#nothiddendiv").live("click", callback);
1154         jQuery("#anchor2").live("click", callback);
1155
1156         jQuery("#nothiddendiv").trigger("click");
1157         equals( called, 1, "Verify that only one click occurred." );
1158
1159         called = 0;
1160         jQuery("#anchor2").trigger("click");
1161         equals( called, 1, "Verify that only one click occurred." );
1162
1163         // Make sure that only one callback is removed
1164         jQuery("#anchor2").die("click", callback);
1165
1166         called = 0;
1167         jQuery("#nothiddendiv").trigger("click");
1168         equals( called, 1, "Verify that only one click occurred." );
1169
1170         called = 0;
1171         jQuery("#anchor2").trigger("click");
1172         equals( called, 0, "Verify that no click occurred." );
1173
1174         // Make sure that it still works if the selector is the same,
1175         // but the event type is different
1176         jQuery("#nothiddendiv").live("foo", callback);
1177
1178         // Cleanup
1179         jQuery("#nothiddendiv").die("click", callback);
1180
1181         called = 0;
1182         jQuery("#nothiddendiv").trigger("click");
1183         equals( called, 0, "Verify that no click occurred." );
1184
1185         called = 0;
1186         jQuery("#nothiddendiv").trigger("foo");
1187         equals( called, 1, "Verify that one foo occurred." );
1188
1189         // Cleanup
1190         jQuery("#nothiddendiv").die("foo", callback);
1191
1192         // Make sure we don't loose the target by DOM modifications
1193         // after the bubble already reached the liveHandler
1194         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1195
1196         jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1197         jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1198
1199         jQuery("#nothiddendiv span").click();
1200         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1201         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1202
1203         // Cleanup
1204         jQuery("#nothiddendivchild").die("click");
1205
1206         // Verify that .live() ocurs and cancel buble in the same order as
1207         // we would expect .bind() and .click() without delegation
1208         var lived = 0, livee = 0;
1209
1210         // bind one pair in one order
1211         jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1212         jQuery('span#liveSpan1').live('click', function(){ livee++; });
1213
1214         jQuery('span#liveSpan1 a').click();
1215         equals( lived, 1, "Verify that only one first handler occurred." );
1216         equals( livee, 0, "Verify that second handler doesn't." );
1217
1218         // and one pair in inverse
1219         jQuery('span#liveSpan2').live('click', function(){ livee++; });
1220         jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1221
1222         lived = 0;
1223         livee = 0;
1224         jQuery('span#liveSpan2 a').click();
1225         equals( lived, 1, "Verify that only one first handler occurred." );
1226         equals( livee, 0, "Verify that second handler doesn't." );
1227
1228         // Cleanup
1229         jQuery("span#liveSpan1 a").die("click")
1230         jQuery("span#liveSpan1").die("click");
1231         jQuery("span#liveSpan2 a").die("click");
1232         jQuery("span#liveSpan2").die("click");
1233
1234         // Test this, target and currentTarget are correct
1235         jQuery('span#liveSpan1').live('click', function(e){
1236                 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1237                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1238                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1239         });
1240
1241         jQuery('span#liveSpan1 a').click();
1242
1243         jQuery('span#liveSpan1').die('click');
1244
1245         // Work with deep selectors
1246         livee = 0;
1247
1248         function clickB(){ livee++; }
1249
1250         jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1251         jQuery("#nothiddendiv div").live("click", clickB);
1252         jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1253
1254         equals( livee, 0, "No clicks, deep selector." );
1255
1256         livee = 0;
1257         jQuery("#nothiddendivchild").trigger("click");
1258         equals( livee, 2, "Click, deep selector." );
1259
1260         livee = 0;
1261         jQuery("#nothiddendivchild").trigger("mouseover");
1262         equals( livee, 1, "Mouseover, deep selector." );
1263
1264         jQuery("#nothiddendiv div").die("mouseover");
1265
1266         livee = 0;
1267         jQuery("#nothiddendivchild").trigger("click");
1268         equals( livee, 2, "Click, deep selector." );
1269
1270         livee = 0;
1271         jQuery("#nothiddendivchild").trigger("mouseover");
1272         equals( livee, 0, "Mouseover, deep selector." );
1273
1274         jQuery("#nothiddendiv div").die("click", clickB);
1275
1276         livee = 0;
1277         jQuery("#nothiddendivchild").trigger("click");
1278         equals( livee, 1, "Click, deep selector." );
1279
1280         jQuery("#nothiddendiv div").die("click");
1281
1282         jQuery("#nothiddendiv div").live("blur", function(){
1283                 ok( true, "Live div trigger blur." );
1284         });
1285
1286         jQuery("#nothiddendiv div").trigger("blur");
1287
1288         jQuery("#nothiddendiv div").die("blur");
1289 });
1290
1291 test("die all bound events", function(){
1292         expect(1);
1293
1294         var count = 0;
1295         var div = jQuery("div#nothiddendivchild");
1296
1297         div.live("click submit", function(){ count++; });
1298         div.die();
1299
1300         div.trigger("click");
1301         div.trigger("submit");
1302
1303         equals( count, 0, "Make sure no events were triggered." );
1304 });
1305
1306 test("live with multiple events", function(){
1307         expect(1);
1308
1309         var count = 0;
1310         var div = jQuery("div#nothiddendivchild");
1311
1312         div.live("click submit", function(){ count++; });
1313
1314         div.trigger("click");
1315         div.trigger("submit");
1316
1317         equals( count, 2, "Make sure both the click and submit were triggered." );
1318
1319         // manually clean up events from elements outside the fixture
1320         div.die();
1321 });
1322
1323 test("live with namespaces", function(){
1324         expect(12);
1325
1326         var count1 = 0, count2 = 0;
1327
1328         jQuery("#liveSpan1").live("foo.bar", function(e){
1329                 count1++;
1330         });
1331
1332         jQuery("#liveSpan1").live("foo.zed", function(e){
1333                 count2++;
1334         });
1335
1336         jQuery("#liveSpan1").trigger("foo.bar");
1337         equals( count1, 1, "Got live foo.bar" );
1338         equals( count2, 0, "Got live foo.bar" );
1339
1340         count1 = 0, count2 = 0;
1341
1342         jQuery("#liveSpan1").trigger("foo.zed");
1343         equals( count1, 0, "Got live foo.zed" );
1344         equals( count2, 1, "Got live foo.zed" );
1345
1346         //remove one
1347         count1 = 0, count2 = 0;
1348
1349         jQuery("#liveSpan1").die("foo.zed");
1350         jQuery("#liveSpan1").trigger("foo.bar");
1351
1352         equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1353         equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1354
1355         count1 = 0, count2 = 0;
1356
1357         jQuery("#liveSpan1").trigger("foo.zed");
1358         equals( count1, 0, "Got live foo.zed" );
1359         equals( count2, 0, "Got live foo.zed" );
1360
1361         //remove the other
1362         jQuery("#liveSpan1").die("foo.bar");
1363
1364         count1 = 0, count2 = 0;
1365
1366         jQuery("#liveSpan1").trigger("foo.bar");
1367         equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1368         equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1369
1370         jQuery("#liveSpan1").trigger("foo.zed");
1371         equals( count1, 0, "Did not trigger foo.zed again" );
1372         equals( count2, 0, "Did not trigger foo.zed again" );
1373 });
1374
1375 test("live with change", function(){
1376         expect(8);
1377
1378         var selectChange = 0, checkboxChange = 0;
1379
1380         var select = jQuery("select[name='S1']")
1381         select.live("change", function() {
1382                 selectChange++;
1383         });
1384
1385         var checkbox = jQuery("#check2"),
1386                 checkboxFunction = function(){
1387                         checkboxChange++;
1388                 }
1389         checkbox.live("change", checkboxFunction);
1390
1391         // test click on select
1392
1393         // second click that changed it
1394         selectChange = 0;
1395         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1396         select.trigger("change");
1397         equals( selectChange, 1, "Change on click." );
1398
1399         // test keys on select
1400         selectChange = 0;
1401         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1402         select.trigger("change");
1403         equals( selectChange, 1, "Change on keyup." );
1404
1405         // test click on checkbox
1406         checkbox.trigger("change");
1407         equals( checkboxChange, 1, "Change on checkbox." );
1408
1409         // test blur/focus on text
1410         var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1411         text.live("change", function() {
1412                 textChange++;
1413         });
1414
1415         text.val(oldTextVal+"foo");
1416         text.trigger("change");
1417         equals( textChange, 1, "Change on text input." );
1418
1419         text.val(oldTextVal);
1420         text.die("change");
1421
1422         // test blur/focus on password
1423         var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1424         password.live("change", function() {
1425                 passwordChange++;
1426         });
1427
1428         password.val(oldPasswordVal + "foo");
1429         password.trigger("change");
1430         equals( passwordChange, 1, "Change on password input." );
1431
1432         password.val(oldPasswordVal);
1433         password.die("change");
1434
1435         // make sure die works
1436
1437         // die all changes
1438         selectChange = 0;
1439         select.die("change");
1440         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1441         select.trigger("change");
1442         equals( selectChange, 0, "Die on click works." );
1443
1444         selectChange = 0;
1445         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1446         select.trigger("change");
1447         equals( selectChange, 0, "Die on keyup works." );
1448
1449         // die specific checkbox
1450         checkbox.die("change", checkboxFunction);
1451         checkbox.trigger("change");
1452         equals( checkboxChange, 1, "Die on checkbox." );
1453 });
1454
1455 test("live with submit", function() {
1456         var count1 = 0, count2 = 0;
1457
1458         jQuery("#testForm").live("submit", function(ev) {
1459                 count1++;
1460                 ev.preventDefault();
1461         });
1462
1463         jQuery("body").live("submit", function(ev) {
1464                 count2++;
1465                 ev.preventDefault();
1466         });
1467
1468         jQuery("#testForm input[name=sub1]").submit();
1469         equals( count1, 1, "Verify form submit." );
1470         equals( count2, 1, "Verify body submit." );
1471
1472         jQuery("#testForm").die("submit");
1473         jQuery("body").die("submit");
1474 });
1475
1476 test("live with special events", function() {
1477         expect(13);
1478
1479         jQuery.event.special.foo = {
1480                 setup: function( data, namespaces, handler ) {
1481                         ok( true, "Setup run." );
1482                 },
1483                 teardown: function( namespaces ) {
1484                         ok( true, "Teardown run." );
1485                 },
1486                 add: function( handleObj ) {
1487                         ok( true, "Add run." );
1488                 },
1489                 remove: function( handleObj ) {
1490                         ok( true, "Remove run." );
1491                 },
1492                 _default: function( event ) {
1493                         ok( true, "Default run." );
1494                 }
1495         };
1496
1497         // Run: setup, add
1498         jQuery("#liveSpan1").live("foo.a", function(e){
1499                 ok( true, "Handler 1 run." );
1500         });
1501
1502         // Run: add
1503         jQuery("#liveSpan1").live("foo.b", function(e){
1504                 ok( true, "Handler 2 run." );
1505         });
1506
1507         // Run: Handler 1, Handler 2, Default
1508         jQuery("#liveSpan1").trigger("foo");
1509
1510         // Run: Handler 1, Default
1511         // TODO: Namespace doesn't trigger default (?)
1512         jQuery("#liveSpan1").trigger("foo.a");
1513
1514         // Run: remove
1515         jQuery("#liveSpan1").die("foo.a");
1516
1517         // Run: Handler 2, Default
1518         jQuery("#liveSpan1").trigger("foo");
1519
1520         // Run: remove, teardown
1521         jQuery("#liveSpan1").die("foo");
1522
1523         delete jQuery.event.special.foo;
1524 });
1525
1526 test(".delegate()/.undelegate()", function() {
1527         expect(65);
1528
1529         var submit = 0, div = 0, livea = 0, liveb = 0;
1530
1531         jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1532         jQuery("#body").delegate("div", "click", function(){ div++; });
1533         jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1534         jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1535
1536         // Nothing should trigger on the body
1537         jQuery("body").trigger("click");
1538         equals( submit, 0, "Click on body" );
1539         equals( div, 0, "Click on body" );
1540         equals( livea, 0, "Click on body" );
1541         equals( liveb, 0, "Click on body" );
1542
1543         // This should trigger two events
1544         submit = 0, div = 0, livea = 0, liveb = 0;
1545         jQuery("div#nothiddendiv").trigger("click");
1546         equals( submit, 0, "Click on div" );
1547         equals( div, 1, "Click on div" );
1548         equals( livea, 1, "Click on div" );
1549         equals( liveb, 0, "Click on div" );
1550
1551         // This should trigger three events (w/ bubbling)
1552         submit = 0, div = 0, livea = 0, liveb = 0;
1553         jQuery("div#nothiddendivchild").trigger("click");
1554         equals( submit, 0, "Click on inner div" );
1555         equals( div, 2, "Click on inner div" );
1556         equals( livea, 1, "Click on inner div" );
1557         equals( liveb, 1, "Click on inner div" );
1558
1559         // This should trigger one submit
1560         submit = 0, div = 0, livea = 0, liveb = 0;
1561         jQuery("div#nothiddendivchild").trigger("submit");
1562         equals( submit, 1, "Submit on div" );
1563         equals( div, 0, "Submit on div" );
1564         equals( livea, 0, "Submit on div" );
1565         equals( liveb, 0, "Submit on div" );
1566
1567         // Make sure no other events were removed in the process
1568         submit = 0, div = 0, livea = 0, liveb = 0;
1569         jQuery("div#nothiddendivchild").trigger("click");
1570         equals( submit, 0, "undelegate Click on inner div" );
1571         equals( div, 2, "undelegate Click on inner div" );
1572         equals( livea, 1, "undelegate Click on inner div" );
1573         equals( liveb, 1, "undelegate Click on inner div" );
1574
1575         // Now make sure that the removal works
1576         submit = 0, div = 0, livea = 0, liveb = 0;
1577         jQuery("#body").undelegate("div#nothiddendivchild", "click");
1578         jQuery("div#nothiddendivchild").trigger("click");
1579         equals( submit, 0, "undelegate Click on inner div" );
1580         equals( div, 2, "undelegate Click on inner div" );
1581         equals( livea, 1, "undelegate Click on inner div" );
1582         equals( liveb, 0, "undelegate Click on inner div" );
1583
1584         // Make sure that the click wasn't removed too early
1585         submit = 0, div = 0, livea = 0, liveb = 0;
1586         jQuery("div#nothiddendiv").trigger("click");
1587         equals( submit, 0, "undelegate Click on inner div" );
1588         equals( div, 1, "undelegate Click on inner div" );
1589         equals( livea, 1, "undelegate Click on inner div" );
1590         equals( liveb, 0, "undelegate Click on inner div" );
1591
1592         // Make sure that stopPropgation doesn't stop live events
1593         submit = 0, div = 0, livea = 0, liveb = 0;
1594         jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1595         jQuery("div#nothiddendivchild").trigger("click");
1596         equals( submit, 0, "stopPropagation Click on inner div" );
1597         equals( div, 1, "stopPropagation Click on inner div" );
1598         equals( livea, 0, "stopPropagation Click on inner div" );
1599         equals( liveb, 1, "stopPropagation Click on inner div" );
1600
1601         // Make sure click events only fire with primary click
1602         submit = 0, div = 0, livea = 0, liveb = 0;
1603         var event = jQuery.Event("click");
1604         event.button = 1;
1605         jQuery("div#nothiddendiv").trigger(event);
1606
1607         equals( livea, 0, "delegate secondary click" );
1608
1609         jQuery("#body").undelegate("div#nothiddendivchild", "click");
1610         jQuery("#body").undelegate("div#nothiddendiv", "click");
1611         jQuery("#body").undelegate("div", "click");
1612         jQuery("#body").undelegate("div", "submit");
1613
1614         // Test binding with a different context
1615         var clicked = 0, container = jQuery('#main')[0];
1616         jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1617         jQuery("div").trigger('click');
1618         jQuery("#foo").trigger('click');
1619         jQuery("#main").trigger('click');
1620         jQuery("body").trigger('click');
1621         equals( clicked, 2, "delegate with a context" );
1622
1623         // Make sure the event is actually stored on the context
1624         ok( jQuery._data(container, "events").live, "delegate with a context" );
1625
1626         // Test unbinding with a different context
1627         jQuery("#main").undelegate("#foo", "click");
1628         jQuery("#foo").trigger('click');
1629         equals( clicked, 2, "undelegate with a context");
1630
1631         // Test binding with event data
1632         jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1633         jQuery("#foo").trigger("click");
1634         jQuery("#body").undelegate("#foo", "click");
1635
1636         // Test binding with trigger data
1637         jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1638         jQuery("#foo").trigger("click", true);
1639         jQuery("#body").undelegate("#foo", "click");
1640
1641         // Test binding with different this object
1642         jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1643         jQuery("#foo").trigger("click");
1644         jQuery("#body").undelegate("#foo", "click");
1645
1646         // Test binding with different this object, event data, and trigger data
1647         jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1648                 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1649                 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1650                 equals( data, true, "delegate with with different this object, event data, and trigger data")
1651         }, { foo: "bar" }));
1652         jQuery("#foo").trigger("click", true);
1653         jQuery("#body").undelegate("#foo", "click");
1654
1655         // Verify that return false prevents default action
1656         jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1657         var hash = window.location.hash;
1658         jQuery("#anchor2").trigger("click");
1659         equals( window.location.hash, hash, "return false worked" );
1660         jQuery("#body").undelegate("#anchor2", "click");
1661
1662         // Verify that .preventDefault() prevents default action
1663         jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1664         var hash = window.location.hash;
1665         jQuery("#anchor2").trigger("click");
1666         equals( window.location.hash, hash, "e.preventDefault() worked" );
1667         jQuery("#body").undelegate("#anchor2", "click");
1668
1669         // Test binding the same handler to multiple points
1670         var called = 0;
1671         function callback(){ called++; return false; }
1672
1673         jQuery("#body").delegate("#nothiddendiv", "click", callback);
1674         jQuery("#body").delegate("#anchor2", "click", callback);
1675
1676         jQuery("#nothiddendiv").trigger("click");
1677         equals( called, 1, "Verify that only one click occurred." );
1678
1679         called = 0;
1680         jQuery("#anchor2").trigger("click");
1681         equals( called, 1, "Verify that only one click occurred." );
1682
1683         // Make sure that only one callback is removed
1684         jQuery("#body").undelegate("#anchor2", "click", callback);
1685
1686         called = 0;
1687         jQuery("#nothiddendiv").trigger("click");
1688         equals( called, 1, "Verify that only one click occurred." );
1689
1690         called = 0;
1691         jQuery("#anchor2").trigger("click");
1692         equals( called, 0, "Verify that no click occurred." );
1693
1694         // Make sure that it still works if the selector is the same,
1695         // but the event type is different
1696         jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1697
1698         // Cleanup
1699         jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1700
1701         called = 0;
1702         jQuery("#nothiddendiv").trigger("click");
1703         equals( called, 0, "Verify that no click occurred." );
1704
1705         called = 0;
1706         jQuery("#nothiddendiv").trigger("foo");
1707         equals( called, 1, "Verify that one foo occurred." );
1708
1709         // Cleanup
1710         jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1711
1712         // Make sure we don't loose the target by DOM modifications
1713         // after the bubble already reached the liveHandler
1714         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1715
1716         jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1717         jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1718
1719         jQuery("#nothiddendiv span").click();
1720         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1721         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1722
1723         // Cleanup
1724         jQuery("#body").undelegate("#nothiddendivchild", "click");
1725
1726         // Verify that .live() ocurs and cancel buble in the same order as
1727         // we would expect .bind() and .click() without delegation
1728         var lived = 0, livee = 0;
1729
1730         // bind one pair in one order
1731         jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1732         jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1733
1734         jQuery('span#liveSpan1 a').click();
1735         equals( lived, 1, "Verify that only one first handler occurred." );
1736         equals( livee, 0, "Verify that second handler doesn't." );
1737
1738         // and one pair in inverse
1739         jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1740         jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1741
1742         lived = 0;
1743         livee = 0;
1744         jQuery('span#liveSpan2 a').click();
1745         equals( lived, 1, "Verify that only one first handler occurred." );
1746         equals( livee, 0, "Verify that second handler doesn't." );
1747
1748         // Cleanup
1749         jQuery("#body").undelegate("click");
1750
1751         // Test this, target and currentTarget are correct
1752         jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1753                 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1754                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1755                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1756         });
1757
1758         jQuery('span#liveSpan1 a').click();
1759
1760         jQuery("#body").undelegate('span#liveSpan1', 'click');
1761
1762         // Work with deep selectors
1763         livee = 0;
1764
1765         function clickB(){ livee++; }
1766
1767         jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1768         jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1769         jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1770
1771         equals( livee, 0, "No clicks, deep selector." );
1772
1773         livee = 0;
1774         jQuery("#nothiddendivchild").trigger("click");
1775         equals( livee, 2, "Click, deep selector." );
1776
1777         livee = 0;
1778         jQuery("#nothiddendivchild").trigger("mouseover");
1779         equals( livee, 1, "Mouseover, deep selector." );
1780
1781         jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1782
1783         livee = 0;
1784         jQuery("#nothiddendivchild").trigger("click");
1785         equals( livee, 2, "Click, deep selector." );
1786
1787         livee = 0;
1788         jQuery("#nothiddendivchild").trigger("mouseover");
1789         equals( livee, 0, "Mouseover, deep selector." );
1790
1791         jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1792
1793         livee = 0;
1794         jQuery("#nothiddendivchild").trigger("click");
1795         equals( livee, 1, "Click, deep selector." );
1796
1797         jQuery("#body").undelegate("#nothiddendiv div", "click");
1798 });
1799
1800 test("undelegate all bound events", function(){
1801         expect(1);
1802
1803         var count = 0;
1804         var div = jQuery("#body");
1805
1806         div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1807         div.undelegate();
1808
1809         jQuery("div#nothiddendivchild").trigger("click");
1810         jQuery("div#nothiddendivchild").trigger("submit");
1811
1812         equals( count, 0, "Make sure no events were triggered." );
1813 });
1814
1815 test("delegate with multiple events", function(){
1816         expect(1);
1817
1818         var count = 0;
1819         var div = jQuery("#body");
1820
1821         div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1822
1823         jQuery("div#nothiddendivchild").trigger("click");
1824         jQuery("div#nothiddendivchild").trigger("submit");
1825
1826         equals( count, 2, "Make sure both the click and submit were triggered." );
1827
1828         jQuery("#body").undelegate();
1829 });
1830
1831 test("delegate with change", function(){
1832         expect(8);
1833
1834         var selectChange = 0, checkboxChange = 0;
1835
1836         var select = jQuery("select[name='S1']");
1837         jQuery("#body").delegate("select[name='S1']", "change", function() {
1838                 selectChange++;
1839         });
1840
1841         var checkbox = jQuery("#check2"),
1842                 checkboxFunction = function(){
1843                         checkboxChange++;
1844                 }
1845         jQuery("#body").delegate("#check2", "change", checkboxFunction);
1846
1847         // test click on select
1848
1849         // second click that changed it
1850         selectChange = 0;
1851         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1852         select.trigger("change");
1853         equals( selectChange, 1, "Change on click." );
1854
1855         // test keys on select
1856         selectChange = 0;
1857         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1858         select.trigger("change");
1859         equals( selectChange, 1, "Change on keyup." );
1860
1861         // test click on checkbox
1862         checkbox.trigger("change");
1863         equals( checkboxChange, 1, "Change on checkbox." );
1864
1865         // test blur/focus on text
1866         var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1867         jQuery("#body").delegate("#name", "change", function() {
1868                 textChange++;
1869         });
1870
1871         text.val(oldTextVal+"foo");
1872         text.trigger("change");
1873         equals( textChange, 1, "Change on text input." );
1874
1875         text.val(oldTextVal);
1876         jQuery("#body").die("change");
1877
1878         // test blur/focus on password
1879         var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1880         jQuery("#body").delegate("#name", "change", function() {
1881                 passwordChange++;
1882         });
1883
1884         password.val(oldPasswordVal + "foo");
1885         password.trigger("change");
1886         equals( passwordChange, 1, "Change on password input." );
1887
1888         password.val(oldPasswordVal);
1889         jQuery("#body").undelegate("#name", "change");
1890
1891         // make sure die works
1892
1893         // die all changes
1894         selectChange = 0;
1895         jQuery("#body").undelegate("select[name='S1']", "change");
1896         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1897         select.trigger("change");
1898         equals( selectChange, 0, "Die on click works." );
1899
1900         selectChange = 0;
1901         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1902         select.trigger("change");
1903         equals( selectChange, 0, "Die on keyup works." );
1904
1905         // die specific checkbox
1906         jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1907         checkbox.trigger("change");
1908         equals( checkboxChange, 1, "Die on checkbox." );
1909 });
1910
1911 test("delegate with submit", function() {
1912         var count1 = 0, count2 = 0;
1913
1914         jQuery("#body").delegate("#testForm", "submit", function(ev) {
1915                 count1++;
1916                 ev.preventDefault();
1917         });
1918
1919         jQuery(document).delegate("body", "submit", function(ev) {
1920                 count2++;
1921                 ev.preventDefault();
1922         });
1923
1924         jQuery("#testForm input[name=sub1]").submit();
1925         equals( count1, 1, "Verify form submit." );
1926         equals( count2, 1, "Verify body submit." );
1927
1928         jQuery("#body").undelegate();
1929         jQuery(document).undelegate();
1930 });
1931
1932 test("Non DOM element events", function() {
1933         expect(1);
1934
1935         var o = {};
1936
1937         jQuery(o).bind('nonelementobj', function(e) {
1938                 ok( true, "Event on non-DOM object triggered" );
1939         });
1940
1941         jQuery(o).trigger('nonelementobj');
1942 });
1943
1944 test("window resize", function() {
1945         expect(2);
1946
1947         jQuery(window).unbind();
1948
1949         jQuery(window).bind("resize", function(){
1950                 ok( true, "Resize event fired." );
1951         }).resize().unbind("resize");
1952
1953         ok( !jQuery._data(window, "__events__"), "Make sure all the events are gone." );
1954 });
1955
1956 test("focusin bubbles", function() {
1957         //create an input and focusin on it
1958         var input = jQuery("<input/>"), order = 0;
1959
1960         input.prependTo("body");
1961
1962         jQuery("body").bind("focusin.focusinBubblesTest",function(){
1963                 equals(1,order++,"focusin on the body second")
1964         });
1965
1966         input.bind("focusin.focusinBubblesTest",function(){
1967                 equals(0,order++,"focusin on the element first")
1968         });
1969
1970         input[0].focus();
1971         input.remove();
1972
1973         jQuery("body").unbind("focusin.focusinBubblesTest");
1974 });
1975
1976 /*
1977 test("jQuery(function($) {})", function() {
1978         stop();
1979         jQuery(function($) {
1980                 equals(jQuery, $, "ready doesn't provide an event object, instead it provides a reference to the jQuery function, see http://docs.jquery.com/Events/ready#fn");
1981                 start();
1982         });
1983 });
1984
1985 test("event properties", function() {
1986         stop();
1987         jQuery("#simon1").click(function(event) {
1988                 ok( event.timeStamp, "assert event.timeStamp is present" );
1989                 start();
1990         }).click();
1991 });
1992 */