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