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