Making sure that you can bind multiple toggles to a single element without problems...
[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         // Trigger both bound fn (1)
197         jQuery("#firstp").trigger("click.test.abc");
198
199         // Trigger one bound fn (1)
200         jQuery("#firstp").trigger("click.abc");
201
202         // Trigger two bound fn (2)
203         jQuery("#firstp").trigger("click.test");
204
205         // Remove only the one fn
206         jQuery("#firstp").unbind("click.abc");
207
208         // Trigger the remaining fn (1)
209         jQuery("#firstp").trigger("click");
210
211         // Remove the remaining fn
212         jQuery("#firstp").unbind(".test");
213
214         // Trigger the remaining fn (1)
215         jQuery("#firstp").trigger("custom");
216 });
217
218 test("bind(), with different this object", function() {
219         expect(4);
220         var thisObject = { myThis: true },
221                 data = { myData: true },
222                 handler1 = function( event ) {
223                         equals( this, thisObject, "bind() with different this object" );
224                 },
225                 handler2 = function( event ) {
226                         equals( this, thisObject, "bind() with different this object and data" );
227                         equals( event.data, data, "bind() with different this object and data" );
228                 };
229         
230         jQuery("#firstp")
231                 .bind("click", handler1, thisObject).click().unbind("click", handler1)
232                 .bind("click", data, handler2, thisObject).click().unbind("click", handler2);
233
234         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
235 });
236
237 test("unbind(type)", function() {
238         expect( 0 );
239         
240         var $elem = jQuery("#firstp"),
241                 message;
242
243         function error(){
244                 ok( false, message );
245         }
246         
247         message = "unbind passing function";
248         $elem.bind('error', error).unbind('error',error).triggerHandler('error');
249         
250         message = "unbind all from event";
251         $elem.bind('error', error).unbind('error').triggerHandler('error');
252         
253         message = "unbind all";
254         $elem.bind('error', error).unbind().triggerHandler('error');
255         
256         message = "unbind many with function";
257         $elem.bind('error error2',error)
258                  .unbind('error error2', error )
259                  .trigger('error').triggerHandler('error2');
260
261         message = "unbind many"; // #3538
262         $elem.bind('error error2',error)
263                  .unbind('error error2')
264                  .trigger('error').triggerHandler('error2');
265         
266         message = "unbind without a type or handler";
267         $elem.bind("error error2.test",error)
268                  .unbind()
269                  .trigger("error").triggerHandler("error2");
270 });
271
272 test("unbind(eventObject)", function() {
273         expect(4);
274         
275         var $elem = jQuery("#firstp"),
276                 num;
277
278         function assert( expected ){
279                 num = 0;
280                 $elem.trigger('foo').triggerHandler('bar');
281                 equals( num, expected, "Check the right handlers are triggered" );
282         }
283         
284         $elem
285                 // This handler shouldn't be unbound
286                 .bind('foo', function(){
287                         num += 1;
288                 })
289                 .bind('foo', function(e){
290                         $elem.unbind( e )
291                         num += 2;
292                 })
293                 // Neither this one
294                 .bind('bar', function(){
295                         num += 4;
296                 });
297                 
298         assert( 7 );
299         assert( 5 );
300         
301         $elem.unbind('bar');
302         assert( 1 );
303         
304         $elem.unbind(); 
305         assert( 0 );
306 });
307
308 test("hover()", function() {
309         var times = 0,
310                 handler1 = function( event ) { ++times; },
311                 handler2 = function( event ) { ++times; };
312
313         jQuery("#firstp")
314                 .hover(handler1, handler2)
315                 .mouseenter().mouseleave()
316                 .unbind("mouseenter", handler1)
317                 .unbind("mouseleave", handler2)
318                 .hover(handler1)
319                 .mouseenter().mouseleave()
320                 .unbind("mouseenter mouseleave", handler1)
321                 .mouseenter().mouseleave();
322
323         equals( times, 4, "hover handlers fired" );
324 });
325
326 test("trigger() shortcuts", function() {
327         expect(6);
328         jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
329                 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
330                 equals( close.length, 0, "Context element does not exist, length must be zero" );
331                 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
332                 return false;
333         }).click();
334         
335         jQuery("#check1").click(function() {
336                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
337         }).click();
338         
339         var counter = 0;
340         jQuery('#firstp')[0].onclick = function(event) {
341                 counter++;
342         };
343         jQuery('#firstp').click();
344         equals( counter, 1, "Check that click, triggers onclick event handler also" );
345         
346         var clickCounter = 0;
347         jQuery('#simon1')[0].onclick = function(event) {
348                 clickCounter++;
349         };
350         jQuery('#simon1').click();
351         equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
352         
353         jQuery('<img />').load(function(){
354                 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
355         }).load();
356 });
357
358 test("trigger() bubbling", function() {
359         expect(14);
360
361         var doc = 0, html = 0, body = 0, main = 0, ap = 0;
362
363         jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
364         jQuery("html").bind("click", function(e){ html++; });
365         jQuery("body").bind("click", function(e){ body++; });
366         jQuery("#main").bind("click", function(e){ main++; });
367         jQuery("#ap").bind("click", function(){ ap++; return false; });
368
369         jQuery("html").trigger("click");
370         equals( doc, 1, "HTML bubble" );
371         equals( html, 1, "HTML bubble" );
372
373         jQuery("body").trigger("click");
374         equals( doc, 2, "Body bubble" );
375         equals( html, 2, "Body bubble" );
376         equals( body, 1, "Body bubble" );
377
378         jQuery("#main").trigger("click");
379         equals( doc, 3, "Main bubble" );
380         equals( html, 3, "Main bubble" );
381         equals( body, 2, "Main bubble" );
382         equals( main, 1, "Main bubble" );
383
384         jQuery("#ap").trigger("click");
385         equals( doc, 3, "ap bubble" );
386         equals( html, 3, "ap bubble" );
387         equals( body, 2, "ap bubble" );
388         equals( main, 1, "ap bubble" );
389         equals( ap, 1, "ap bubble" );
390 });
391
392 test("trigger(type, [data], [fn])", function() {
393         expect(12);
394
395         var handler = function(event, a, b, c) {
396                 equals( event.type, "click", "check passed data" );
397                 equals( a, 1, "check passed data" );
398                 equals( b, "2", "check passed data" );
399                 equals( c, "abc", "check passed data" );
400                 return "test";
401         };
402
403         var $elem = jQuery("#firstp");
404
405         // Simulate a "native" click
406         $elem[0].click = function(){
407                 ok( true, "Native call was triggered" );
408         };
409
410         // Triggers handlrs and native
411         // Trigger 5
412         $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
413
414         // Simulate a "native" click
415         $elem[0].click = function(){
416                 ok( false, "Native call was triggered" );
417         };
418
419         // Trigger only the handlers (no native)
420         // Triggers 5
421         equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
422
423         var pass = true;
424         try {
425                 jQuery('#form input:first').hide().trigger('focus');
426         } catch(e) {
427                 pass = false;
428         }
429         ok( pass, "Trigger focus on hidden element" );
430         
431         pass = true;
432         try {
433                 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
434         } catch (e) {
435                 pass = false;
436         }
437         ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
438 });
439
440 test("trigger(eventObject, [data], [fn])", function() {
441         expect(25);
442         
443         var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
444                 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
445         
446         var event = jQuery.Event("noNew");      
447         ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
448         equals( event.type, "noNew", "Verify its type" );
449         
450         equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
451         equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
452         equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
453         
454         event.preventDefault();
455         equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
456         event.stopPropagation();
457         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
458         
459         event.isPropagationStopped = function(){ return false };
460         event.stopImmediatePropagation();
461         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
462         equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
463         
464         $parent.bind('foo',function(e){
465                 // Tries bubbling
466                 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
467                 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
468                 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
469                 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
470         });
471         
472         // test with an event object
473         event = new jQuery.Event("foo");
474         event.secret = 'boo!';
475         $child.trigger(event);
476         
477         // test with a literal object
478         $child.trigger({type:'foo', secret:'boo!'});
479         
480         $parent.unbind();
481
482         function error(){
483                 ok( false, "This assertion shouldn't be reached");
484         }
485         
486         $parent.bind('foo', error );
487         
488         $child.bind('foo',function(e, a, b, c ){
489                 equals( arguments.length, 4, "Check arguments length");
490                 equals( a, 1, "Check first custom argument");
491                 equals( b, 2, "Check second custom argument");
492                 equals( c, 3, "Check third custom argument");
493                 
494                 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
495                 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
496                 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
497                 
498                 // Skips both errors
499                 e.stopImmediatePropagation();
500                 
501                 return "result";
502         });
503         
504         // We should add this back in when we want to test the order
505         // in which event handlers are iterated.
506         //$child.bind('foo', error );
507         
508         event = new jQuery.Event("foo");
509         $child.trigger( event, [1,2,3] ).unbind();
510         equals( event.result, "result", "Check event.result attribute");
511         
512         // Will error if it bubbles
513         $child.triggerHandler('foo');
514         
515         $child.unbind();
516         $parent.unbind().remove();
517 });
518
519 test("jQuery.Event.currentTarget", function(){
520         expect(1);
521         
522         var counter = 0,
523                 $elem = jQuery('<button>a</button>').click(function(e){
524                 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
525         });
526         
527         // Fake event
528         $elem.trigger('click');
529         
530         // Cleanup
531         $elem.unbind();
532 });
533
534 test("toggle(Function, Function, ...)", function() {
535         expect(16);
536         
537         var count = 0,
538                 fn1 = function(e) { count++; },
539                 fn2 = function(e) { count--; },
540                 preventDefault = function(e) { e.preventDefault() },
541                 link = jQuery('#mark');
542         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
543         equals( count, 1, "Check for toggle(fn, fn)" );
544
545         jQuery("#firstp").toggle(function () {
546                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
547         }, function() {}).trigger("click", [ 1, 2, 3 ]);
548
549         var first = 0;
550         jQuery("#simon1").one("click", function() {
551                 ok( true, "Execute event only once" );
552                 jQuery(this).toggle(function() {
553                         equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
554                 }, function() {
555                         equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
556                 });
557                 return false;
558         }).click().click().click();
559         
560         var turn = 0;
561         var fns = [
562                 function(){
563                         turn = 1;
564                 },
565                 function(){
566                         turn = 2;
567                 },
568                 function(){
569                         turn = 3;
570                 }
571         ];
572         
573         var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
574         $div.click();
575         equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
576         $div.click();
577         equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
578         $div.click();
579         equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
580         $div.click();
581         equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
582         $div.click();
583         equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
584         
585         $div.unbind('click',fns[0]);
586         var data = jQuery.data( $div[0], 'events' );
587         ok( !data, "Unbinding one function from toggle unbinds them all");
588
589         // Test Multi-Toggles
590         var a = [], b = [];
591         $div = jQuery("<div/>");
592         $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
593         $div.click();
594         same( a, [1], "Check that a click worked." );
595
596         $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
597         $div.click();
598         same( a, [1,2], "Check that a click worked with a second toggle." );
599         same( b, [1], "Check that a click worked with a second toggle." );
600
601         $div.click();
602         same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
603         same( b, [1,2], "Check that a click worked with a second toggle, second click." );
604 });
605
606 test(".live()/.die()", function() {
607         expect(58);
608
609         var submit = 0, div = 0, livea = 0, liveb = 0;
610
611         jQuery("div").live("submit", function(){ submit++; return false; });
612         jQuery("div").live("click", function(){ div++; });
613         jQuery("div#nothiddendiv").live("click", function(){ livea++; });
614         jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
615
616         // Nothing should trigger on the body
617         jQuery("body").trigger("click");
618         equals( submit, 0, "Click on body" );
619         equals( div, 0, "Click on body" );
620         equals( livea, 0, "Click on body" );
621         equals( liveb, 0, "Click on body" );
622
623         // This should trigger two events
624         jQuery("div#nothiddendiv").trigger("click");
625         equals( submit, 0, "Click on div" );
626         equals( div, 1, "Click on div" );
627         equals( livea, 1, "Click on div" );
628         equals( liveb, 0, "Click on div" );
629
630         // This should trigger three events (w/ bubbling)
631         jQuery("div#nothiddendivchild").trigger("click");
632         equals( submit, 0, "Click on inner div" );
633         equals( div, 2, "Click on inner div" );
634         equals( livea, 2, "Click on inner div" );
635         equals( liveb, 1, "Click on inner div" );
636
637         // This should trigger one submit
638         jQuery("div#nothiddendivchild").trigger("submit");
639         equals( submit, 1, "Submit on div" );
640         equals( div, 2, "Submit on div" );
641         equals( livea, 2, "Submit on div" );
642         equals( liveb, 1, "Submit on div" );
643
644         // Make sure no other events were removed in the process
645         jQuery("div#nothiddendivchild").trigger("click");
646         equals( submit, 1, "die Click on inner div" );
647         equals( div, 3, "die Click on inner div" );
648         equals( livea, 3, "die Click on inner div" );
649         equals( liveb, 2, "die Click on inner div" );
650
651         // Now make sure that the removal works
652         jQuery("div#nothiddendivchild").die("click");
653         jQuery("div#nothiddendivchild").trigger("click");
654         equals( submit, 1, "die Click on inner div" );
655         equals( div, 4, "die Click on inner div" );
656         equals( livea, 4, "die Click on inner div" );
657         equals( liveb, 2, "die Click on inner div" );
658
659         // Make sure that the click wasn't removed too early
660         jQuery("div#nothiddendiv").trigger("click");
661         equals( submit, 1, "die Click on inner div" );
662         equals( div, 5, "die Click on inner div" );
663         equals( livea, 5, "die Click on inner div" );
664         equals( liveb, 2, "die Click on inner div" );
665
666         // Make sure that stopPropgation doesn't stop live events
667         jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
668         jQuery("div#nothiddendivchild").trigger("click");
669         equals( submit, 1, "stopPropagation Click on inner div" );
670         equals( div, 6, "stopPropagation Click on inner div" );
671         equals( livea, 6, "stopPropagation Click on inner div" );
672         equals( liveb, 3, "stopPropagation Click on inner div" );
673
674         jQuery("div#nothiddendivchild").die("click");
675         jQuery("div#nothiddendiv").die("click");
676         jQuery("div").die("click");
677         jQuery("div").die("submit");
678
679         // Test binding with a different context
680         var clicked = 0, container = jQuery('#main')[0];
681         jQuery("#foo", container).live("click", function(e){ clicked++; });
682         jQuery("div").trigger('click');
683         jQuery("#foo").trigger('click');
684         jQuery("#main").trigger('click');
685         jQuery("body").trigger('click');
686         equals( clicked, 2, "live with a context" );
687
688         // Make sure the event is actually stored on the context
689         ok( jQuery.data(container, "events").live, "live with a context" );
690
691         // Test unbinding with a different context
692         jQuery("#foo", container).die("click");
693         jQuery("#foo").trigger('click');
694         equals( clicked, 2, "die with a context");
695
696         // Test binding with event data
697         jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
698         jQuery("#foo").trigger("click").die("click");
699
700         // Test binding with trigger data
701         jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
702         jQuery("#foo").trigger("click", true).die("click");
703
704         // Test binding with different this object
705         jQuery("#foo").live("click", function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" });
706         jQuery("#foo").trigger("click").die("click");
707
708         // Test binding with different this object, event data, and trigger data
709         jQuery("#foo").live("click", true, function(e, data){
710                 equals( e.data, true, "live with with different this object, event data, and trigger data" );
711                 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" ); 
712                 equals( data, true, "live with with different this object, event data, and trigger data")
713         }, { foo: "bar" });
714         jQuery("#foo").trigger("click", true).die("click");
715
716         // Verify that return false prevents default action
717         jQuery("#anchor2").live("click", function(){ return false; });
718         var hash = window.location.hash;
719         jQuery("#anchor2").trigger("click");
720         equals( window.location.hash, hash, "return false worked" );
721         jQuery("#anchor2").die("click");
722
723         // Verify that .preventDefault() prevents default action
724         jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
725         var hash = window.location.hash;
726         jQuery("#anchor2").trigger("click");
727         equals( window.location.hash, hash, "e.preventDefault() worked" );
728         jQuery("#anchor2").die("click");
729
730         // Test binding the same handler to multiple points
731         var called = 0;
732         function callback(){ called++; return false; }
733
734         jQuery("#nothiddendiv").live("click", callback);
735         jQuery("#anchor2").live("click", callback);
736
737         jQuery("#nothiddendiv").trigger("click");
738         equals( called, 1, "Verify that only one click occurred." );
739
740         jQuery("#anchor2").trigger("click");
741         equals( called, 2, "Verify that only one click occurred." );
742
743         // Make sure that only one callback is removed
744         jQuery("#anchor2").die("click", callback);
745
746         jQuery("#nothiddendiv").trigger("click");
747         equals( called, 3, "Verify that only one click occurred." );
748
749         jQuery("#anchor2").trigger("click");
750         equals( called, 3, "Verify that no click occurred." );
751
752         // Make sure that it still works if the selector is the same,
753         // but the event type is different
754         jQuery("#nothiddendiv").live("foo", callback);
755
756         // Cleanup
757         jQuery("#nothiddendiv").die("click", callback);
758
759         jQuery("#nothiddendiv").trigger("click");
760         equals( called, 3, "Verify that no click occurred." );
761
762         jQuery("#nothiddendiv").trigger("foo");
763         equals( called, 4, "Verify that one foo occurred." );
764
765         // Cleanup
766         jQuery("#nothiddendiv").die("foo", callback);
767         
768         // Make sure we don't loose the target by DOM modifications
769         // after the bubble already reached the liveHandler
770         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
771         
772         jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
773         jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
774         
775         jQuery("#nothiddendiv span").click();
776         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
777         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
778         
779         // Cleanup
780         jQuery("#nothiddendivchild").die("click");
781
782         // Verify that .live() ocurs and cancel buble in the same order as
783         // we would expect .bind() and .click() without delegation
784         var lived = 0, livee = 0;
785         
786         // bind one pair in one order
787         jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
788         jQuery('span#liveSpan1').live('click', function(){ livee++; });
789
790         jQuery('span#liveSpan1 a').click();
791         equals( lived, 1, "Verify that only one first handler occurred." );
792         equals( livee, 0, "Verify that second handler don't." );
793
794         // and one pair in inverse
795         jQuery('#liveHandlerOrder span#liveSpan2').live('click', function(){ livee++; });
796         jQuery('#liveHandlerOrder span#liveSpan2 a').live('click', function(){ lived++; return false; });
797
798         jQuery('span#liveSpan2 a').click();
799         equals( lived, 2, "Verify that only one first handler occurred." );
800         equals( livee, 0, "Verify that second handler don't." );
801         
802         // Cleanup
803         jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
804         
805         // Test this, target and currentTarget are correct
806         jQuery('span#liveSpan1').live('click', function(e){ 
807                 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
808                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
809                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
810         });
811         
812         jQuery('span#liveSpan1 a').click();
813         
814         jQuery('span#liveSpan1').die('click');
815 });
816
817 test("live with submit", function() {
818         var count = 0;
819         
820         jQuery("#testForm").live("submit", function() {
821                 count++;
822                 return false;
823         });
824         
825         jQuery("#testForm input[name=sub1]")[0].click();
826         jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
827         
828         equals(2, count);
829         
830         jQuery("#testForm").die("submit");
831 });
832
833 test("live with focus/blur", function(){
834         expect(2);
835
836         // Setup
837         jQuery("<input type='text' id='livefb' />").appendTo("body");
838         
839         var $child =  jQuery("#livefb"),
840                 child = $child[0],
841                 pass = {};
842
843         function worked(e){
844                 pass[e.type] = true;
845         }
846         
847         $child.live("focus", worked);
848         $child.live("blur", worked);
849         
850         // Test
851         child.focus();
852         if (pass.focus)
853                 ok(true, "Test live() with focus event");
854         else
855                 ok(true, "Cannot test focus because the window isn't focused");
856
857         child.blur();
858         if (pass.blur)
859                 ok( true, "Test live() with blur event");
860         else
861                 ok(true, "Cannot test blur because the window isn't focused");
862         
863         // Teardown
864         $child.die("focus", worked);
865         $child.die("blur", worked);
866         $child.remove();
867         window.scrollTo(0,0);
868 });
869
870 test("Non DOM element events", function() {
871         expect(3);
872
873         jQuery({})
874                 .bind('nonelementglobal', function(e) {
875                         ok( true, "Global event on non-DOM annonymos object triggered" );
876                 });
877
878         var o = {};
879
880         jQuery(o)
881                 .bind('nonelementobj', function(e) {
882                         ok( true, "Event on non-DOM object triggered" );
883                 }).bind('nonelementglobal', function() {
884                         ok( true, "Global event on non-DOM object triggered" );
885                 });
886
887         jQuery(o).trigger('nonelementobj');
888         jQuery.event.trigger('nonelementglobal');
889 });
890
891 /*
892 test("jQuery(function($) {})", function() {
893         stop();
894         jQuery(function($) {
895                 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");
896                 start();
897         });
898 });
899
900 test("event properties", function() {
901         stop();
902         jQuery("#simon1").click(function(event) {
903                 ok( event.timeStamp, "assert event.timeStamp is present" );
904                 start();
905         }).click();
906 });
907 */