Simplified a selector in the .live() tests.
[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", handler1, thisObject).click().unbind("click", handler1)
236                 .bind("click", data, 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(58);
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         jQuery("div#nothiddendivchild").die("click");
679         jQuery("div#nothiddendiv").die("click");
680         jQuery("div").die("click");
681         jQuery("div").die("submit");
682
683         // Test binding with a different context
684         var clicked = 0, container = jQuery('#main')[0];
685         jQuery("#foo", container).live("click", function(e){ clicked++; });
686         jQuery("div").trigger('click');
687         jQuery("#foo").trigger('click');
688         jQuery("#main").trigger('click');
689         jQuery("body").trigger('click');
690         equals( clicked, 2, "live with a context" );
691
692         // Make sure the event is actually stored on the context
693         ok( jQuery.data(container, "events").live, "live with a context" );
694
695         // Test unbinding with a different context
696         jQuery("#foo", container).die("click");
697         jQuery("#foo").trigger('click');
698         equals( clicked, 2, "die with a context");
699
700         // Test binding with event data
701         jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
702         jQuery("#foo").trigger("click").die("click");
703
704         // Test binding with trigger data
705         jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
706         jQuery("#foo").trigger("click", true).die("click");
707
708         // Test binding with different this object
709         jQuery("#foo").live("click", function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" });
710         jQuery("#foo").trigger("click").die("click");
711
712         // Test binding with different this object, event data, and trigger data
713         jQuery("#foo").live("click", true, function(e, data){
714                 equals( e.data, true, "live with with different this object, event data, and trigger data" );
715                 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" ); 
716                 equals( data, true, "live with with different this object, event data, and trigger data")
717         }, { foo: "bar" });
718         jQuery("#foo").trigger("click", true).die("click");
719
720         // Verify that return false prevents default action
721         jQuery("#anchor2").live("click", function(){ return false; });
722         var hash = window.location.hash;
723         jQuery("#anchor2").trigger("click");
724         equals( window.location.hash, hash, "return false worked" );
725         jQuery("#anchor2").die("click");
726
727         // Verify that .preventDefault() prevents default action
728         jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
729         var hash = window.location.hash;
730         jQuery("#anchor2").trigger("click");
731         equals( window.location.hash, hash, "e.preventDefault() worked" );
732         jQuery("#anchor2").die("click");
733
734         // Test binding the same handler to multiple points
735         var called = 0;
736         function callback(){ called++; return false; }
737
738         jQuery("#nothiddendiv").live("click", callback);
739         jQuery("#anchor2").live("click", callback);
740
741         jQuery("#nothiddendiv").trigger("click");
742         equals( called, 1, "Verify that only one click occurred." );
743
744         jQuery("#anchor2").trigger("click");
745         equals( called, 2, "Verify that only one click occurred." );
746
747         // Make sure that only one callback is removed
748         jQuery("#anchor2").die("click", callback);
749
750         jQuery("#nothiddendiv").trigger("click");
751         equals( called, 3, "Verify that only one click occurred." );
752
753         jQuery("#anchor2").trigger("click");
754         equals( called, 3, "Verify that no click occurred." );
755
756         // Make sure that it still works if the selector is the same,
757         // but the event type is different
758         jQuery("#nothiddendiv").live("foo", callback);
759
760         // Cleanup
761         jQuery("#nothiddendiv").die("click", callback);
762
763         jQuery("#nothiddendiv").trigger("click");
764         equals( called, 3, "Verify that no click occurred." );
765
766         jQuery("#nothiddendiv").trigger("foo");
767         equals( called, 4, "Verify that one foo occurred." );
768
769         // Cleanup
770         jQuery("#nothiddendiv").die("foo", callback);
771         
772         // Make sure we don't loose the target by DOM modifications
773         // after the bubble already reached the liveHandler
774         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
775         
776         jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
777         jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
778         
779         jQuery("#nothiddendiv span").click();
780         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
781         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
782         
783         // Cleanup
784         jQuery("#nothiddendivchild").die("click");
785
786         // Verify that .live() ocurs and cancel buble in the same order as
787         // we would expect .bind() and .click() without delegation
788         var lived = 0, livee = 0;
789         
790         // bind one pair in one order
791         jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
792         jQuery('span#liveSpan1').live('click', function(){ livee++; });
793
794         jQuery('span#liveSpan1 a').click();
795         equals( lived, 1, "Verify that only one first handler occurred." );
796         equals( livee, 0, "Verify that second handler doesn't." );
797
798         // and one pair in inverse
799         jQuery('span#liveSpan2').live('click', function(){ livee++; });
800         jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
801
802         lived = 0;
803         livee = 0;
804         jQuery('span#liveSpan2 a').click();
805         equals( lived, 1, "Verify that only one first handler occurred." );
806         equals( livee, 0, "Verify that second handler doesn't." );
807         
808         // Cleanup
809         jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
810         
811         // Test this, target and currentTarget are correct
812         jQuery('span#liveSpan1').live('click', function(e){ 
813                 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
814                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
815                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
816         });
817         
818         jQuery('span#liveSpan1 a').click();
819         
820         jQuery('span#liveSpan1').die('click');
821 });
822
823 test("live with submit", function() {
824         var count = 0;
825         
826         jQuery("#testForm").live("submit", function() {
827                 count++;
828                 return false;
829         });
830         
831         jQuery("#testForm input[name=sub1]")[0].click();
832         jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
833         
834         equals(2, count);
835         
836         jQuery("#testForm").die("submit");
837 });
838
839 test("live with focus/blur", function(){
840         expect(2);
841
842         // Setup
843         jQuery("<input type='text' id='livefb' />").appendTo("body");
844         
845         var $child =  jQuery("#livefb"),
846                 child = $child[0],
847                 pass = {};
848
849         function worked(e){
850                 pass[e.type] = true;
851         }
852         
853         $child.live("focus", worked);
854         $child.live("blur", worked);
855         
856         // Test
857         child.focus();
858         if (pass.focus)
859                 ok(true, "Test live() with focus event");
860         else
861                 ok(true, "Cannot test focus because the window isn't focused");
862
863         child.blur();
864         if (pass.blur)
865                 ok( true, "Test live() with blur event");
866         else
867                 ok(true, "Cannot test blur because the window isn't focused");
868         
869         // Teardown
870         $child.die("focus", worked);
871         $child.die("blur", worked);
872         $child.remove();
873         window.scrollTo(0,0);
874 });
875
876 test("Non DOM element events", function() {
877         expect(3);
878
879         jQuery({})
880                 .bind('nonelementglobal', function(e) {
881                         ok( true, "Global event on non-DOM annonymos object triggered" );
882                 });
883
884         var o = {};
885
886         jQuery(o)
887                 .bind('nonelementobj', function(e) {
888                         ok( true, "Event on non-DOM object triggered" );
889                 }).bind('nonelementglobal', function() {
890                         ok( true, "Global event on non-DOM object triggered" );
891                 });
892
893         jQuery(o).trigger('nonelementobj');
894         jQuery.event.trigger('nonelementglobal');
895 });
896
897 /*
898 test("jQuery(function($) {})", function() {
899         stop();
900         jQuery(function($) {
901                 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");
902                 start();
903         });
904 });
905
906 test("event properties", function() {
907         stop();
908         jQuery("#simon1").click(function(event) {
909                 ok( event.timeStamp, "assert event.timeStamp is present" );
910                 start();
911         }).click();
912 });
913 */