Adding in backwards-compatiblity support for jQuery().bind/unbind/trigger - and immed...
[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(), no data", function() {
41         expect(1);
42         var handler = function(event) {
43                 ok ( !event.data, "Check that no data is added to the event object" );
44         };
45         jQuery("#firstp").bind("click", handler).trigger("click");
46 });
47
48 test("bind/one/unbind(Object)", function(){
49         expect(6);
50         
51         var clickCounter = 0, mouseoverCounter = 0;
52         function handler(event) {
53                 if (event.type == "click")
54                         clickCounter++;
55                 else if (event.type == "mouseover")
56                         mouseoverCounter++;
57         };
58         
59         function handlerWithData(event) {
60                 if (event.type == "click")
61                         clickCounter += event.data;
62                 else if (event.type == "mouseover")
63                         mouseoverCounter += event.data;
64         };
65         
66         function trigger(){
67                 $elem.trigger("click").trigger("mouseover");
68         }
69         
70         var $elem = jQuery("#firstp")
71                 // Regular bind
72                 .bind({
73                         click:handler,
74                         mouseover:handler
75                 })
76                 // Bind with data
77                 .one({
78                         click:handlerWithData,
79                         mouseover:handlerWithData
80                 }, 2 );
81         
82         trigger();
83         
84         equals( clickCounter, 3, "bind(Object)" );
85         equals( mouseoverCounter, 3, "bind(Object)" );
86         
87         trigger();
88         equals( clickCounter, 4, "bind(Object)" );
89         equals( mouseoverCounter, 4, "bind(Object)" );
90         
91         jQuery("#firstp").unbind({
92                 click:handler,
93                 mouseover:handler
94         });
95
96         trigger();
97         equals( clickCounter, 4, "bind(Object)" );
98         equals( mouseoverCounter, 4, "bind(Object)" );
99 });
100
101 test("bind(), iframes", function() {
102         // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
103         var doc = jQuery("#loadediframe").contents();
104         
105         jQuery("div", doc).bind("click", function() {
106                 ok( true, "Binding to element inside iframe" );
107         }).click().unbind('click');
108 });
109
110 test("bind(), trigger change on select", function() {
111         expect(3);
112         var counter = 0;
113         function selectOnChange(event) {
114                 equals( event.data, counter++, "Event.data is not a global event object" );
115         };
116         jQuery("#form select").each(function(i){
117                 jQuery(this).bind('change', i, selectOnChange);
118         }).trigger('change');
119 });
120
121 test("bind/unbind/trigger on empty jQuery set", function() {
122         expect(1);
123
124         jQuery().bind("test", function(){
125                 equals( this, document, "Handler triggered and bound on document." );
126         });
127
128         jQuery().trigger("test");
129
130         jQuery().unbind("test");
131         jQuery().trigger("test");
132 });
133
134 test("bind(), namespaced events, cloned events", function() {
135         expect(6);
136
137         jQuery("#firstp").bind("custom.test",function(e){
138                 ok(true, "Custom event triggered");
139         });
140
141         jQuery("#firstp").bind("click",function(e){
142                 ok(true, "Normal click triggered");
143         });
144
145         jQuery("#firstp").bind("click.test",function(e){
146                 ok(true, "Namespaced click triggered");
147         });
148
149         // Trigger both bound fn (2)
150         jQuery("#firstp").trigger("click");
151
152         // Trigger one bound fn (1)
153         jQuery("#firstp").trigger("click.test");
154
155         // Remove only the one fn
156         jQuery("#firstp").unbind("click.test");
157
158         // Trigger the remaining fn (1)
159         jQuery("#firstp").trigger("click");
160
161         // Remove the remaining fn
162         jQuery("#firstp").unbind(".test");
163
164         // Trigger the remaining fn (0)
165         jQuery("#firstp").trigger("custom");
166
167         // using contents will get comments regular, text, and comment nodes
168         jQuery("#nonnodes").contents().bind("tester", function () {
169                 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
170         }).trigger("tester");
171
172         // Make sure events stick with appendTo'd elements (which are cloned) #2027
173         jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
174         ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
175 });
176
177 test("bind(), multi-namespaced events", function() {
178         expect(6);
179         
180         var order = [
181                 "click.test.abc",
182                 "click.test.abc",
183                 "click.test",
184                 "click.test.abc",
185                 "click.test",
186                 "custom.test2"
187         ];
188         
189         function check(name, msg){
190                 same(name, order.shift(), msg);
191         }
192
193         jQuery("#firstp").bind("custom.test",function(e){
194                 check("custom.test", "Custom event triggered");
195         });
196
197         jQuery("#firstp").bind("custom.test2",function(e){
198                 check("custom.test2", "Custom event triggered");
199         });
200
201         jQuery("#firstp").bind("click.test",function(e){
202                 check("click.test", "Normal click triggered");
203         });
204
205         jQuery("#firstp").bind("click.test.abc",function(e){
206                 check("click.test.abc", "Namespaced click triggered");
207         });
208         
209         // Those would not trigger/unbind (#5303)
210         jQuery("#firstp").trigger("click.a.test");
211         jQuery("#firstp").unbind("click.a.test");
212
213         // Trigger both bound fn (1)
214         jQuery("#firstp").trigger("click.test.abc");
215
216         // Trigger one bound fn (1)
217         jQuery("#firstp").trigger("click.abc");
218
219         // Trigger two bound fn (2)
220         jQuery("#firstp").trigger("click.test");
221
222         // Remove only the one fn
223         jQuery("#firstp").unbind("click.abc");
224
225         // Trigger the remaining fn (1)
226         jQuery("#firstp").trigger("click");
227
228         // Remove the remaining fn
229         jQuery("#firstp").unbind(".test");
230
231         // Trigger the remaining fn (1)
232         jQuery("#firstp").trigger("custom");
233 });
234
235 test("bind(), with different this object", function() {
236         expect(4);
237         var thisObject = { myThis: true },
238                 data = { myData: true },
239                 handler1 = function( event ) {
240                         equals( this, thisObject, "bind() with different this object" );
241                 },
242                 handler2 = function( event ) {
243                         equals( this, thisObject, "bind() with different this object and data" );
244                         equals( event.data, data, "bind() with different this object and data" );
245                 };
246         
247         jQuery("#firstp")
248                 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
249                 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
250
251         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
252 });
253
254 test("unbind(type)", function() {
255         expect( 0 );
256         
257         var $elem = jQuery("#firstp"),
258                 message;
259
260         function error(){
261                 ok( false, message );
262         }
263         
264         message = "unbind passing function";
265         $elem.bind('error', error).unbind('error',error).triggerHandler('error');
266         
267         message = "unbind all from event";
268         $elem.bind('error', error).unbind('error').triggerHandler('error');
269         
270         message = "unbind all";
271         $elem.bind('error', error).unbind().triggerHandler('error');
272         
273         message = "unbind many with function";
274         $elem.bind('error error2',error)
275                  .unbind('error error2', error )
276                  .trigger('error').triggerHandler('error2');
277
278         message = "unbind many"; // #3538
279         $elem.bind('error error2',error)
280                  .unbind('error error2')
281                  .trigger('error').triggerHandler('error2');
282         
283         message = "unbind without a type or handler";
284         $elem.bind("error error2.test",error)
285                  .unbind()
286                  .trigger("error").triggerHandler("error2");
287 });
288
289 test("unbind(eventObject)", function() {
290         expect(4);
291         
292         var $elem = jQuery("#firstp"),
293                 num;
294
295         function assert( expected ){
296                 num = 0;
297                 $elem.trigger('foo').triggerHandler('bar');
298                 equals( num, expected, "Check the right handlers are triggered" );
299         }
300         
301         $elem
302                 // This handler shouldn't be unbound
303                 .bind('foo', function(){
304                         num += 1;
305                 })
306                 .bind('foo', function(e){
307                         $elem.unbind( e )
308                         num += 2;
309                 })
310                 // Neither this one
311                 .bind('bar', function(){
312                         num += 4;
313                 });
314                 
315         assert( 7 );
316         assert( 5 );
317         
318         $elem.unbind('bar');
319         assert( 1 );
320         
321         $elem.unbind(); 
322         assert( 0 );
323 });
324
325 test("hover()", function() {
326         var times = 0,
327                 handler1 = function( event ) { ++times; },
328                 handler2 = function( event ) { ++times; };
329
330         jQuery("#firstp")
331                 .hover(handler1, handler2)
332                 .mouseenter().mouseleave()
333                 .unbind("mouseenter", handler1)
334                 .unbind("mouseleave", handler2)
335                 .hover(handler1)
336                 .mouseenter().mouseleave()
337                 .unbind("mouseenter mouseleave", handler1)
338                 .mouseenter().mouseleave();
339
340         equals( times, 4, "hover handlers fired" );
341 });
342
343 test("trigger() shortcuts", function() {
344         expect(6);
345         jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
346                 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
347                 equals( close.length, 0, "Context element does not exist, length must be zero" );
348                 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
349                 return false;
350         }).click();
351         
352         jQuery("#check1").click(function() {
353                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
354         }).click();
355         
356         var counter = 0;
357         jQuery('#firstp')[0].onclick = function(event) {
358                 counter++;
359         };
360         jQuery('#firstp').click();
361         equals( counter, 1, "Check that click, triggers onclick event handler also" );
362         
363         var clickCounter = 0;
364         jQuery('#simon1')[0].onclick = function(event) {
365                 clickCounter++;
366         };
367         jQuery('#simon1').click();
368         equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
369         
370         jQuery('<img />').load(function(){
371                 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
372         }).load();
373 });
374
375 test("trigger() bubbling", function() {
376         expect(14);
377
378         var doc = 0, html = 0, body = 0, main = 0, ap = 0;
379
380         jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
381         jQuery("html").bind("click", function(e){ html++; });
382         jQuery("body").bind("click", function(e){ body++; });
383         jQuery("#main").bind("click", function(e){ main++; });
384         jQuery("#ap").bind("click", function(){ ap++; return false; });
385
386         jQuery("html").trigger("click");
387         equals( doc, 1, "HTML bubble" );
388         equals( html, 1, "HTML bubble" );
389
390         jQuery("body").trigger("click");
391         equals( doc, 2, "Body bubble" );
392         equals( html, 2, "Body bubble" );
393         equals( body, 1, "Body bubble" );
394
395         jQuery("#main").trigger("click");
396         equals( doc, 3, "Main bubble" );
397         equals( html, 3, "Main bubble" );
398         equals( body, 2, "Main bubble" );
399         equals( main, 1, "Main bubble" );
400
401         jQuery("#ap").trigger("click");
402         equals( doc, 3, "ap bubble" );
403         equals( html, 3, "ap bubble" );
404         equals( body, 2, "ap bubble" );
405         equals( main, 1, "ap bubble" );
406         equals( ap, 1, "ap bubble" );
407 });
408
409 test("trigger(type, [data], [fn])", function() {
410         expect(12);
411
412         var handler = function(event, a, b, c) {
413                 equals( event.type, "click", "check passed data" );
414                 equals( a, 1, "check passed data" );
415                 equals( b, "2", "check passed data" );
416                 equals( c, "abc", "check passed data" );
417                 return "test";
418         };
419
420         var $elem = jQuery("#firstp");
421
422         // Simulate a "native" click
423         $elem[0].click = function(){
424                 ok( true, "Native call was triggered" );
425         };
426
427         // Triggers handlrs and native
428         // Trigger 5
429         $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
430
431         // Simulate a "native" click
432         $elem[0].click = function(){
433                 ok( false, "Native call was triggered" );
434         };
435
436         // Trigger only the handlers (no native)
437         // Triggers 5
438         equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
439
440         var pass = true;
441         try {
442                 jQuery('#form input:first').hide().trigger('focus');
443         } catch(e) {
444                 pass = false;
445         }
446         ok( pass, "Trigger focus on hidden element" );
447         
448         pass = true;
449         try {
450                 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
451         } catch (e) {
452                 pass = false;
453         }
454         ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
455 });
456
457 test("trigger(eventObject, [data], [fn])", function() {
458         expect(25);
459         
460         var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
461                 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
462         
463         var event = jQuery.Event("noNew");      
464         ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
465         equals( event.type, "noNew", "Verify its type" );
466         
467         equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
468         equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
469         equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
470         
471         event.preventDefault();
472         equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
473         event.stopPropagation();
474         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
475         
476         event.isPropagationStopped = function(){ return false };
477         event.stopImmediatePropagation();
478         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
479         equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
480         
481         $parent.bind('foo',function(e){
482                 // Tries bubbling
483                 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
484                 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
485                 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
486                 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
487         });
488         
489         // test with an event object
490         event = new jQuery.Event("foo");
491         event.secret = 'boo!';
492         $child.trigger(event);
493         
494         // test with a literal object
495         $child.trigger({type:'foo', secret:'boo!'});
496         
497         $parent.unbind();
498
499         function error(){
500                 ok( false, "This assertion shouldn't be reached");
501         }
502         
503         $parent.bind('foo', error );
504         
505         $child.bind('foo',function(e, a, b, c ){
506                 equals( arguments.length, 4, "Check arguments length");
507                 equals( a, 1, "Check first custom argument");
508                 equals( b, 2, "Check second custom argument");
509                 equals( c, 3, "Check third custom argument");
510                 
511                 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
512                 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
513                 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
514                 
515                 // Skips both errors
516                 e.stopImmediatePropagation();
517                 
518                 return "result";
519         });
520         
521         // We should add this back in when we want to test the order
522         // in which event handlers are iterated.
523         //$child.bind('foo', error );
524         
525         event = new jQuery.Event("foo");
526         $child.trigger( event, [1,2,3] ).unbind();
527         equals( event.result, "result", "Check event.result attribute");
528         
529         // Will error if it bubbles
530         $child.triggerHandler('foo');
531         
532         $child.unbind();
533         $parent.unbind().remove();
534 });
535
536 test("jQuery.Event.currentTarget", function(){
537         expect(1);
538         
539         var counter = 0,
540                 $elem = jQuery('<button>a</button>').click(function(e){
541                 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
542         });
543         
544         // Fake event
545         $elem.trigger('click');
546         
547         // Cleanup
548         $elem.unbind();
549 });
550
551 test("toggle(Function, Function, ...)", function() {
552         expect(16);
553         
554         var count = 0,
555                 fn1 = function(e) { count++; },
556                 fn2 = function(e) { count--; },
557                 preventDefault = function(e) { e.preventDefault() },
558                 link = jQuery('#mark');
559         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
560         equals( count, 1, "Check for toggle(fn, fn)" );
561
562         jQuery("#firstp").toggle(function () {
563                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
564         }, function() {}).trigger("click", [ 1, 2, 3 ]);
565
566         var first = 0;
567         jQuery("#simon1").one("click", function() {
568                 ok( true, "Execute event only once" );
569                 jQuery(this).toggle(function() {
570                         equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
571                 }, function() {
572                         equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
573                 });
574                 return false;
575         }).click().click().click();
576         
577         var turn = 0;
578         var fns = [
579                 function(){
580                         turn = 1;
581                 },
582                 function(){
583                         turn = 2;
584                 },
585                 function(){
586                         turn = 3;
587                 }
588         ];
589         
590         var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
591         $div.click();
592         equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
593         $div.click();
594         equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
595         $div.click();
596         equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
597         $div.click();
598         equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
599         $div.click();
600         equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
601         
602         $div.unbind('click',fns[0]);
603         var data = jQuery.data( $div[0], 'events' );
604         ok( !data, "Unbinding one function from toggle unbinds them all");
605
606         // Test Multi-Toggles
607         var a = [], b = [];
608         $div = jQuery("<div/>");
609         $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
610         $div.click();
611         same( a, [1], "Check that a click worked." );
612
613         $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
614         $div.click();
615         same( a, [1,2], "Check that a click worked with a second toggle." );
616         same( b, [1], "Check that a click worked with a second toggle." );
617
618         $div.click();
619         same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
620         same( b, [1,2], "Check that a click worked with a second toggle, second click." );
621 });
622
623 test(".live()/.die()", function() {
624         expect(61);
625
626         var submit = 0, div = 0, livea = 0, liveb = 0;
627
628         jQuery("div").live("submit", function(){ submit++; return false; });
629         jQuery("div").live("click", function(){ div++; });
630         jQuery("div#nothiddendiv").live("click", function(){ livea++; });
631         jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
632
633         // Nothing should trigger on the body
634         jQuery("body").trigger("click");
635         equals( submit, 0, "Click on body" );
636         equals( div, 0, "Click on body" );
637         equals( livea, 0, "Click on body" );
638         equals( liveb, 0, "Click on body" );
639
640         // This should trigger two events
641         jQuery("div#nothiddendiv").trigger("click");
642         equals( submit, 0, "Click on div" );
643         equals( div, 1, "Click on div" );
644         equals( livea, 1, "Click on div" );
645         equals( liveb, 0, "Click on div" );
646
647         // This should trigger three events (w/ bubbling)
648         jQuery("div#nothiddendivchild").trigger("click");
649         equals( submit, 0, "Click on inner div" );
650         equals( div, 2, "Click on inner div" );
651         equals( livea, 2, "Click on inner div" );
652         equals( liveb, 1, "Click on inner div" );
653
654         // This should trigger one submit
655         jQuery("div#nothiddendivchild").trigger("submit");
656         equals( submit, 1, "Submit on div" );
657         equals( div, 2, "Submit on div" );
658         equals( livea, 2, "Submit on div" );
659         equals( liveb, 1, "Submit on div" );
660
661         // Make sure no other events were removed in the process
662         jQuery("div#nothiddendivchild").trigger("click");
663         equals( submit, 1, "die Click on inner div" );
664         equals( div, 3, "die Click on inner div" );
665         equals( livea, 3, "die Click on inner div" );
666         equals( liveb, 2, "die Click on inner div" );
667
668         // Now make sure that the removal works
669         jQuery("div#nothiddendivchild").die("click");
670         jQuery("div#nothiddendivchild").trigger("click");
671         equals( submit, 1, "die Click on inner div" );
672         equals( div, 4, "die Click on inner div" );
673         equals( livea, 4, "die Click on inner div" );
674         equals( liveb, 2, "die Click on inner div" );
675
676         // Make sure that the click wasn't removed too early
677         jQuery("div#nothiddendiv").trigger("click");
678         equals( submit, 1, "die Click on inner div" );
679         equals( div, 5, "die Click on inner div" );
680         equals( livea, 5, "die Click on inner div" );
681         equals( liveb, 2, "die Click on inner div" );
682
683         // Make sure that stopPropgation doesn't stop live events
684         jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
685         jQuery("div#nothiddendivchild").trigger("click");
686         equals( submit, 1, "stopPropagation Click on inner div" );
687         equals( div, 6, "stopPropagation Click on inner div" );
688         equals( livea, 6, "stopPropagation Click on inner div" );
689         equals( liveb, 3, "stopPropagation Click on inner div" );
690
691         jQuery("div#nothiddendivchild").die("click");
692         jQuery("div#nothiddendiv").die("click");
693         jQuery("div").die("click");
694         jQuery("div").die("submit");
695
696         // Test binding with a different context
697         var clicked = 0, container = jQuery('#main')[0];
698         jQuery("#foo", container).live("click", function(e){ clicked++; });
699         jQuery("div").trigger('click');
700         jQuery("#foo").trigger('click');
701         jQuery("#main").trigger('click');
702         jQuery("body").trigger('click');
703         equals( clicked, 2, "live with a context" );
704
705         // Make sure the event is actually stored on the context
706         ok( jQuery.data(container, "events").live, "live with a context" );
707
708         // Test unbinding with a different context
709         jQuery("#foo", container).die("click");
710         jQuery("#foo").trigger('click');
711         equals( clicked, 2, "die with a context");
712
713         // Test binding with event data
714         jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
715         jQuery("#foo").trigger("click").die("click");
716
717         // Test binding with trigger data
718         jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
719         jQuery("#foo").trigger("click", true).die("click");
720
721         // Test binding with different this object
722         jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
723         jQuery("#foo").trigger("click").die("click");
724
725         // Test binding with different this object, event data, and trigger data
726         jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
727                 equals( e.data, true, "live with with different this object, event data, and trigger data" );
728                 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" ); 
729                 equals( data, true, "live with with different this object, event data, and trigger data")
730         }, { foo: "bar" }));
731         jQuery("#foo").trigger("click", true).die("click");
732
733         // Verify that return false prevents default action
734         jQuery("#anchor2").live("click", function(){ return false; });
735         var hash = window.location.hash;
736         jQuery("#anchor2").trigger("click");
737         equals( window.location.hash, hash, "return false worked" );
738         jQuery("#anchor2").die("click");
739
740         // Verify that .preventDefault() prevents default action
741         jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
742         var hash = window.location.hash;
743         jQuery("#anchor2").trigger("click");
744         equals( window.location.hash, hash, "e.preventDefault() worked" );
745         jQuery("#anchor2").die("click");
746
747         // Test binding the same handler to multiple points
748         var called = 0;
749         function callback(){ called++; return false; }
750
751         jQuery("#nothiddendiv").live("click", callback);
752         jQuery("#anchor2").live("click", callback);
753
754         jQuery("#nothiddendiv").trigger("click");
755         equals( called, 1, "Verify that only one click occurred." );
756
757         jQuery("#anchor2").trigger("click");
758         equals( called, 2, "Verify that only one click occurred." );
759
760         // Make sure that only one callback is removed
761         jQuery("#anchor2").die("click", callback);
762
763         jQuery("#nothiddendiv").trigger("click");
764         equals( called, 3, "Verify that only one click occurred." );
765
766         jQuery("#anchor2").trigger("click");
767         equals( called, 3, "Verify that no click occurred." );
768
769         // Make sure that it still works if the selector is the same,
770         // but the event type is different
771         jQuery("#nothiddendiv").live("foo", callback);
772
773         // Cleanup
774         jQuery("#nothiddendiv").die("click", callback);
775
776         jQuery("#nothiddendiv").trigger("click");
777         equals( called, 3, "Verify that no click occurred." );
778
779         jQuery("#nothiddendiv").trigger("foo");
780         equals( called, 4, "Verify that one foo occurred." );
781
782         // Cleanup
783         jQuery("#nothiddendiv").die("foo", callback);
784         
785         // Make sure we don't loose the target by DOM modifications
786         // after the bubble already reached the liveHandler
787         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
788         
789         jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
790         jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
791         
792         jQuery("#nothiddendiv span").click();
793         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
794         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
795         
796         // Cleanup
797         jQuery("#nothiddendivchild").die("click");
798
799         // Verify that .live() ocurs and cancel buble in the same order as
800         // we would expect .bind() and .click() without delegation
801         var lived = 0, livee = 0;
802         
803         // bind one pair in one order
804         jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
805         jQuery('span#liveSpan1').live('click', function(){ livee++; });
806
807         jQuery('span#liveSpan1 a').click();
808         equals( lived, 1, "Verify that only one first handler occurred." );
809         equals( livee, 0, "Verify that second handler doesn't." );
810
811         // and one pair in inverse
812         jQuery('span#liveSpan2').live('click', function(){ livee++; });
813         jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
814
815         lived = 0;
816         livee = 0;
817         jQuery('span#liveSpan2 a').click();
818         equals( lived, 1, "Verify that only one first handler occurred." );
819         equals( livee, 0, "Verify that second handler doesn't." );
820         
821         // Cleanup
822         jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
823         
824         // Test this, target and currentTarget are correct
825         jQuery('span#liveSpan1').live('click', function(e){ 
826                 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
827                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
828                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
829         });
830         
831         jQuery('span#liveSpan1 a').click();
832         
833         jQuery('span#liveSpan1').die('click');
834
835         // Work with deep selectors
836         livee = 0;
837
838         function clickB(){ livee++; }
839
840         jQuery("#nothiddendiv div").live("click", function(){ livee++; });
841         jQuery("#nothiddendiv div").live("click", clickB);
842         jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
843
844         equals( livee, 0, "No clicks, deep selector." );
845
846         livee = 0;
847         jQuery("#nothiddendivchild").trigger("click");
848         equals( livee, 2, "Click, deep selector." );
849
850         livee = 0;
851         jQuery("#nothiddendivchild").trigger("mouseover");
852         equals( livee, 1, "Mouseover, deep selector." );
853
854         jQuery("#nothiddendiv div").die("mouseover");
855
856         livee = 0;
857         jQuery("#nothiddendivchild").trigger("click");
858         equals( livee, 2, "Click, deep selector." );
859
860         livee = 0;
861         jQuery("#nothiddendivchild").trigger("mouseover");
862         equals( livee, 0, "Mouseover, deep selector." );
863
864         jQuery("#nothiddendiv div").die("click", clickB);
865
866         livee = 0;
867         jQuery("#nothiddendivchild").trigger("click");
868         equals( livee, 1, "Click, deep selector." );
869
870         jQuery("#nothiddendiv div").die("click");
871 });
872
873 test("live with change", function(){
874         var selectChange = 0, checkboxChange = 0;
875         
876         var select = jQuery("select[name='S1']")
877         select.live("change", function() {
878                 selectChange++;
879         });
880         
881         var checkbox = jQuery("#check2"), 
882                 checkboxFunction = function(){
883                         checkboxChange++;
884                 }
885         checkbox.live("change", checkboxFunction);
886         
887         // test click on select
888
889         // second click that changed it
890         selectChange = 0;
891         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
892         select.trigger("change");
893         equals( selectChange, 1, "Change on click." );
894         
895         // test keys on select
896         selectChange = 0;
897         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
898         select.trigger("change");
899         equals( selectChange, 1, "Change on keyup." );
900         
901         // test click on checkbox
902         checkbox.trigger("change");
903         equals( checkboxChange, 1, "Change on checkbox." );
904         
905         // test before activate on radio
906         
907         // test blur/focus on textarea
908         var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
909         textarea.live("change", function() {
910                 textareaChange++;
911         });
912
913         textarea.val(oldVal + "foo");
914         textarea.trigger("change");
915         equals( textareaChange, 1, "Change on textarea." );
916
917         textarea.val(oldVal);
918         textarea.die("change");
919         
920         // test blur/focus on text
921         var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
922         text.live("change", function() {
923                 textChange++;
924         });
925
926         text.val(oldVal+"foo");
927         text.trigger("change");
928         equals( textChange, 1, "Change on text input." );
929
930         text.val(oldTextVal);
931         text.die("change");
932         
933         // test blur/focus on password
934         var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
935         password.live("change", function() {
936                 passwordChange++;
937         });
938
939         password.val(oldPasswordVal + "foo");
940         password.trigger("change");
941         equals( passwordChange, 1, "Change on password input." );
942
943         password.val(oldPasswordVal);
944         password.die("change");
945         
946         // make sure die works
947         
948         // die all changes
949         selectChange = 0;
950         select.die("change");
951         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
952         select.trigger("change");
953         equals( selectChange, 0, "Die on click works." );
954
955         selectChange = 0;
956         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
957         select.trigger("change");
958         equals( selectChange, 0, "Die on keyup works." );
959         
960         // die specific checkbox
961         checkbox.die("change", checkboxFunction);
962         checkbox.trigger("change");
963         equals( checkboxChange, 1, "Die on checkbox." );
964 });
965
966 test("live with submit", function() {
967         var count1 = 0, count2 = 0;
968         
969         jQuery("#testForm").live("submit", function(ev) {
970                 count1++;
971                 ev.preventDefault();
972         });
973
974         jQuery("body").live("submit", function(ev) {
975                 count2++;
976                 ev.preventDefault();
977         });
978
979         if ( jQuery.support.submitBubbles ) {
980                 jQuery("#testForm input[name=sub1]")[0].click();
981                 equals(count1,1 );
982                 equals(count2,1);
983         } else {
984                 jQuery("#testForm input[name=sub1]")[0].click();
985                 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
986                 equals(count1,2);
987                 equals(count2,2);
988         }
989         
990         jQuery("#testForm").die("submit");
991         jQuery("body").die("submit");
992 });
993
994 test("live with focus/blur", function(){
995         expect(2);
996
997         // Setup
998         jQuery("<input type='text' id='livefb' />").appendTo("body");
999         
1000         var $child =  jQuery("#livefb"),
1001                 child = $child[0],
1002                 pass = {};
1003
1004         function worked(e){
1005                 pass[e.type] = true;
1006         }
1007         
1008         $child.live("focus", worked);
1009         $child.live("blur", worked);
1010         
1011         // Test
1012         child.focus();
1013         if (pass.focus)
1014                 ok(true, "Test live() with focus event");
1015         else
1016                 ok(true, "Cannot test focus because the window isn't focused");
1017
1018         child.blur();
1019         if (pass.blur)
1020                 ok( true, "Test live() with blur event");
1021         else
1022                 ok(true, "Cannot test blur because the window isn't focused");
1023         
1024         // Teardown
1025         $child.die("focus", worked);
1026         $child.die("blur", worked);
1027         $child.remove();
1028         window.scrollTo(0,0);
1029 });
1030
1031 test("Non DOM element events", function() {
1032         expect(3);
1033
1034         jQuery({})
1035                 .bind('nonelementglobal', function(e) {
1036                         ok( true, "Global event on non-DOM annonymos object triggered" );
1037                 });
1038
1039         var o = {};
1040
1041         jQuery(o)
1042                 .bind('nonelementobj', function(e) {
1043                         ok( true, "Event on non-DOM object triggered" );
1044                 }).bind('nonelementglobal', function() {
1045                         ok( true, "Global event on non-DOM object triggered" );
1046                 });
1047
1048         jQuery(o).trigger('nonelementobj');
1049         jQuery.event.trigger('nonelementglobal');
1050 });
1051
1052 /*
1053 test("jQuery(function($) {})", function() {
1054         stop();
1055         jQuery(function($) {
1056                 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");
1057                 start();
1058         });
1059 });
1060
1061 test("event properties", function() {
1062         stop();
1063         jQuery("#simon1").click(function(event) {
1064                 ok( event.timeStamp, "assert event.timeStamp is present" );
1065                 start();
1066         }).click();
1067 });
1068 */