Make sure that it's possible to preventDefault natively-triggered (submit, focus...
[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(14);
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         var form = jQuery("<form action=''></form>").appendTo("body");
444
445         // Make sure it can be prevented locally
446         form.submit(function(){
447                 ok( true, "Local bind still works." );
448                 return false;
449         });
450
451         // Trigger 1
452         form.trigger("submit");
453
454         form.unbind("submit");
455
456         jQuery(document).submit(function(){
457                 ok( true, "Make sure bubble works up to document." );
458                 return false;
459         });
460
461         // Trigger 1
462         form.trigger("submit");
463
464         jQuery(document).unbind("submit");
465
466         form.remove();
467 });
468
469 test("jQuery.Event.currentTarget", function(){
470 });
471
472 test("trigger(eventObject, [data], [fn])", function() {
473         expect(25);
474         
475         var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
476                 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
477         
478         var event = jQuery.Event("noNew");      
479         ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
480         equals( event.type, "noNew", "Verify its type" );
481         
482         equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
483         equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
484         equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
485         
486         event.preventDefault();
487         equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
488         event.stopPropagation();
489         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
490         
491         event.isPropagationStopped = function(){ return false };
492         event.stopImmediatePropagation();
493         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
494         equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
495         
496         $parent.bind('foo',function(e){
497                 // Tries bubbling
498                 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
499                 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
500                 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
501                 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
502         });
503         
504         // test with an event object
505         event = new jQuery.Event("foo");
506         event.secret = 'boo!';
507         $child.trigger(event);
508         
509         // test with a literal object
510         $child.trigger({type:'foo', secret:'boo!'});
511         
512         $parent.unbind();
513
514         function error(){
515                 ok( false, "This assertion shouldn't be reached");
516         }
517         
518         $parent.bind('foo', error );
519         
520         $child.bind('foo',function(e, a, b, c ){
521                 equals( arguments.length, 4, "Check arguments length");
522                 equals( a, 1, "Check first custom argument");
523                 equals( b, 2, "Check second custom argument");
524                 equals( c, 3, "Check third custom argument");
525                 
526                 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
527                 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
528                 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
529                 
530                 // Skips both errors
531                 e.stopImmediatePropagation();
532                 
533                 return "result";
534         });
535         
536         // We should add this back in when we want to test the order
537         // in which event handlers are iterated.
538         //$child.bind('foo', error );
539         
540         event = new jQuery.Event("foo");
541         $child.trigger( event, [1,2,3] ).unbind();
542         equals( event.result, "result", "Check event.result attribute");
543         
544         // Will error if it bubbles
545         $child.triggerHandler('foo');
546         
547         $child.unbind();
548         $parent.unbind().remove();
549 });
550
551 test("jQuery.Event.currentTarget", function(){
552         expect(1);
553         
554         var counter = 0,
555                 $elem = jQuery('<button>a</button>').click(function(e){
556                 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
557         });
558         
559         // Fake event
560         $elem.trigger('click');
561         
562         // Cleanup
563         $elem.unbind();
564 });
565
566 test("toggle(Function, Function, ...)", function() {
567         expect(16);
568         
569         var count = 0,
570                 fn1 = function(e) { count++; },
571                 fn2 = function(e) { count--; },
572                 preventDefault = function(e) { e.preventDefault() },
573                 link = jQuery('#mark');
574         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
575         equals( count, 1, "Check for toggle(fn, fn)" );
576
577         jQuery("#firstp").toggle(function () {
578                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
579         }, function() {}).trigger("click", [ 1, 2, 3 ]);
580
581         var first = 0;
582         jQuery("#simon1").one("click", function() {
583                 ok( true, "Execute event only once" );
584                 jQuery(this).toggle(function() {
585                         equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
586                 }, function() {
587                         equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
588                 });
589                 return false;
590         }).click().click().click();
591         
592         var turn = 0;
593         var fns = [
594                 function(){
595                         turn = 1;
596                 },
597                 function(){
598                         turn = 2;
599                 },
600                 function(){
601                         turn = 3;
602                 }
603         ];
604         
605         var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
606         $div.click();
607         equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
608         $div.click();
609         equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
610         $div.click();
611         equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
612         $div.click();
613         equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
614         $div.click();
615         equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
616         
617         $div.unbind('click',fns[0]);
618         var data = jQuery.data( $div[0], 'events' );
619         ok( !data, "Unbinding one function from toggle unbinds them all");
620
621         // Test Multi-Toggles
622         var a = [], b = [];
623         $div = jQuery("<div/>");
624         $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
625         $div.click();
626         same( a, [1], "Check that a click worked." );
627
628         $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
629         $div.click();
630         same( a, [1,2], "Check that a click worked with a second toggle." );
631         same( b, [1], "Check that a click worked with a second toggle." );
632
633         $div.click();
634         same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
635         same( b, [1,2], "Check that a click worked with a second toggle, second click." );
636 });
637
638 test(".live()/.die()", function() {
639         expect(62);
640
641         var submit = 0, div = 0, livea = 0, liveb = 0;
642
643         jQuery("div").live("submit", function(){ submit++; return false; });
644         jQuery("div").live("click", function(){ div++; });
645         jQuery("div#nothiddendiv").live("click", function(){ livea++; });
646         jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
647
648         // Nothing should trigger on the body
649         jQuery("body").trigger("click");
650         equals( submit, 0, "Click on body" );
651         equals( div, 0, "Click on body" );
652         equals( livea, 0, "Click on body" );
653         equals( liveb, 0, "Click on body" );
654
655         // This should trigger two events
656         jQuery("div#nothiddendiv").trigger("click");
657         equals( submit, 0, "Click on div" );
658         equals( div, 1, "Click on div" );
659         equals( livea, 1, "Click on div" );
660         equals( liveb, 0, "Click on div" );
661
662         // This should trigger three events (w/ bubbling)
663         jQuery("div#nothiddendivchild").trigger("click");
664         equals( submit, 0, "Click on inner div" );
665         equals( div, 2, "Click on inner div" );
666         equals( livea, 2, "Click on inner div" );
667         equals( liveb, 1, "Click on inner div" );
668
669         // This should trigger one submit
670         jQuery("div#nothiddendivchild").trigger("submit");
671         equals( submit, 1, "Submit on div" );
672         equals( div, 2, "Submit on div" );
673         equals( livea, 2, "Submit on div" );
674         equals( liveb, 1, "Submit on div" );
675
676         // Make sure no other events were removed in the process
677         jQuery("div#nothiddendivchild").trigger("click");
678         equals( submit, 1, "die Click on inner div" );
679         equals( div, 3, "die Click on inner div" );
680         equals( livea, 3, "die Click on inner div" );
681         equals( liveb, 2, "die Click on inner div" );
682
683         // Now make sure that the removal works
684         jQuery("div#nothiddendivchild").die("click");
685         jQuery("div#nothiddendivchild").trigger("click");
686         equals( submit, 1, "die Click on inner div" );
687         equals( div, 4, "die Click on inner div" );
688         equals( livea, 4, "die Click on inner div" );
689         equals( liveb, 2, "die Click on inner div" );
690
691         // Make sure that the click wasn't removed too early
692         jQuery("div#nothiddendiv").trigger("click");
693         equals( submit, 1, "die Click on inner div" );
694         equals( div, 5, "die Click on inner div" );
695         equals( livea, 5, "die Click on inner div" );
696         equals( liveb, 2, "die Click on inner div" );
697
698         // Make sure that stopPropgation doesn't stop live events
699         jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
700         jQuery("div#nothiddendivchild").trigger("click");
701         equals( submit, 1, "stopPropagation Click on inner div" );
702         equals( div, 6, "stopPropagation Click on inner div" );
703         equals( livea, 6, "stopPropagation Click on inner div" );
704         equals( liveb, 3, "stopPropagation Click on inner div" );
705
706         // Make sure click events only fire with primary click
707         var event = jQuery.Event("click");
708         event.button = 1;
709         jQuery("div#nothiddendiv").trigger(event);
710
711         equals( livea, 6, "live secondary click" );
712
713         jQuery("div#nothiddendivchild").die("click");
714         jQuery("div#nothiddendiv").die("click");
715         jQuery("div").die("click");
716         jQuery("div").die("submit");
717
718         // Test binding with a different context
719         var clicked = 0, container = jQuery('#main')[0];
720         jQuery("#foo", container).live("click", function(e){ clicked++; });
721         jQuery("div").trigger('click');
722         jQuery("#foo").trigger('click');
723         jQuery("#main").trigger('click');
724         jQuery("body").trigger('click');
725         equals( clicked, 2, "live with a context" );
726
727         // Make sure the event is actually stored on the context
728         ok( jQuery.data(container, "events").live, "live with a context" );
729
730         // Test unbinding with a different context
731         jQuery("#foo", container).die("click");
732         jQuery("#foo").trigger('click');
733         equals( clicked, 2, "die with a context");
734
735         // Test binding with event data
736         jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
737         jQuery("#foo").trigger("click").die("click");
738
739         // Test binding with trigger data
740         jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
741         jQuery("#foo").trigger("click", true).die("click");
742
743         // Test binding with different this object
744         jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
745         jQuery("#foo").trigger("click").die("click");
746
747         // Test binding with different this object, event data, and trigger data
748         jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
749                 equals( e.data, true, "live with with different this object, event data, and trigger data" );
750                 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" ); 
751                 equals( data, true, "live with with different this object, event data, and trigger data")
752         }, { foo: "bar" }));
753         jQuery("#foo").trigger("click", true).die("click");
754
755         // Verify that return false prevents default action
756         jQuery("#anchor2").live("click", function(){ return false; });
757         var hash = window.location.hash;
758         jQuery("#anchor2").trigger("click");
759         equals( window.location.hash, hash, "return false worked" );
760         jQuery("#anchor2").die("click");
761
762         // Verify that .preventDefault() prevents default action
763         jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
764         var hash = window.location.hash;
765         jQuery("#anchor2").trigger("click");
766         equals( window.location.hash, hash, "e.preventDefault() worked" );
767         jQuery("#anchor2").die("click");
768
769         // Test binding the same handler to multiple points
770         var called = 0;
771         function callback(){ called++; return false; }
772
773         jQuery("#nothiddendiv").live("click", callback);
774         jQuery("#anchor2").live("click", callback);
775
776         jQuery("#nothiddendiv").trigger("click");
777         equals( called, 1, "Verify that only one click occurred." );
778
779         jQuery("#anchor2").trigger("click");
780         equals( called, 2, "Verify that only one click occurred." );
781
782         // Make sure that only one callback is removed
783         jQuery("#anchor2").die("click", callback);
784
785         jQuery("#nothiddendiv").trigger("click");
786         equals( called, 3, "Verify that only one click occurred." );
787
788         jQuery("#anchor2").trigger("click");
789         equals( called, 3, "Verify that no click occurred." );
790
791         // Make sure that it still works if the selector is the same,
792         // but the event type is different
793         jQuery("#nothiddendiv").live("foo", callback);
794
795         // Cleanup
796         jQuery("#nothiddendiv").die("click", callback);
797
798         jQuery("#nothiddendiv").trigger("click");
799         equals( called, 3, "Verify that no click occurred." );
800
801         jQuery("#nothiddendiv").trigger("foo");
802         equals( called, 4, "Verify that one foo occurred." );
803
804         // Cleanup
805         jQuery("#nothiddendiv").die("foo", callback);
806         
807         // Make sure we don't loose the target by DOM modifications
808         // after the bubble already reached the liveHandler
809         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
810         
811         jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
812         jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
813         
814         jQuery("#nothiddendiv span").click();
815         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
816         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
817         
818         // Cleanup
819         jQuery("#nothiddendivchild").die("click");
820
821         // Verify that .live() ocurs and cancel buble in the same order as
822         // we would expect .bind() and .click() without delegation
823         var lived = 0, livee = 0;
824         
825         // bind one pair in one order
826         jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
827         jQuery('span#liveSpan1').live('click', function(){ livee++; });
828
829         jQuery('span#liveSpan1 a').click();
830         equals( lived, 1, "Verify that only one first handler occurred." );
831         equals( livee, 0, "Verify that second handler doesn't." );
832
833         // and one pair in inverse
834         jQuery('span#liveSpan2').live('click', function(){ livee++; });
835         jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
836
837         lived = 0;
838         livee = 0;
839         jQuery('span#liveSpan2 a').click();
840         equals( lived, 1, "Verify that only one first handler occurred." );
841         equals( livee, 0, "Verify that second handler doesn't." );
842         
843         // Cleanup
844         jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
845         
846         // Test this, target and currentTarget are correct
847         jQuery('span#liveSpan1').live('click', function(e){ 
848                 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
849                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
850                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
851         });
852         
853         jQuery('span#liveSpan1 a').click();
854         
855         jQuery('span#liveSpan1').die('click');
856
857         // Work with deep selectors
858         livee = 0;
859
860         function clickB(){ livee++; }
861
862         jQuery("#nothiddendiv div").live("click", function(){ livee++; });
863         jQuery("#nothiddendiv div").live("click", clickB);
864         jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
865
866         equals( livee, 0, "No clicks, deep selector." );
867
868         livee = 0;
869         jQuery("#nothiddendivchild").trigger("click");
870         equals( livee, 2, "Click, deep selector." );
871
872         livee = 0;
873         jQuery("#nothiddendivchild").trigger("mouseover");
874         equals( livee, 1, "Mouseover, deep selector." );
875
876         jQuery("#nothiddendiv div").die("mouseover");
877
878         livee = 0;
879         jQuery("#nothiddendivchild").trigger("click");
880         equals( livee, 2, "Click, deep selector." );
881
882         livee = 0;
883         jQuery("#nothiddendivchild").trigger("mouseover");
884         equals( livee, 0, "Mouseover, deep selector." );
885
886         jQuery("#nothiddendiv div").die("click", clickB);
887
888         livee = 0;
889         jQuery("#nothiddendivchild").trigger("click");
890         equals( livee, 1, "Click, deep selector." );
891
892         jQuery("#nothiddendiv div").die("click");
893 });
894
895 test("die all bound events", function(){
896         expect(1);
897
898         var count = 0;
899         var div = jQuery("div#nothiddendivchild");
900
901         div.live("click submit", function(){ count++; });
902         div.die();
903
904         div.trigger("click");
905         div.trigger("submit");
906
907         equals( count, 0, "Make sure no events were triggered." );
908 });
909
910 test("live with multiple events", function(){
911         expect(1);
912
913         var count = 0;
914         var div = jQuery("div#nothiddendivchild");
915
916         div.live("click submit", function(){ count++; });
917
918         div.trigger("click");
919         div.trigger("submit");
920
921         equals( count, 2, "Make sure both the click and submit were triggered." );
922 });
923
924 test("live with change", function(){
925         var selectChange = 0, checkboxChange = 0;
926         
927         var select = jQuery("select[name='S1']")
928         select.live("change", function() {
929                 selectChange++;
930         });
931         
932         var checkbox = jQuery("#check2"), 
933                 checkboxFunction = function(){
934                         checkboxChange++;
935                 }
936         checkbox.live("change", checkboxFunction);
937         
938         // test click on select
939
940         // second click that changed it
941         selectChange = 0;
942         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
943         select.trigger("change");
944         equals( selectChange, 1, "Change on click." );
945         
946         // test keys on select
947         selectChange = 0;
948         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
949         select.trigger("change");
950         equals( selectChange, 1, "Change on keyup." );
951         
952         // test click on checkbox
953         checkbox.trigger("change");
954         equals( checkboxChange, 1, "Change on checkbox." );
955         
956         // test before activate on radio
957         
958         // test blur/focus on textarea
959         var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
960         textarea.live("change", function() {
961                 textareaChange++;
962         });
963
964         textarea.val(oldVal + "foo");
965         textarea.trigger("change");
966         equals( textareaChange, 1, "Change on textarea." );
967
968         textarea.val(oldVal);
969         textarea.die("change");
970         
971         // test blur/focus on text
972         var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
973         text.live("change", function() {
974                 textChange++;
975         });
976
977         text.val(oldVal+"foo");
978         text.trigger("change");
979         equals( textChange, 1, "Change on text input." );
980
981         text.val(oldTextVal);
982         text.die("change");
983         
984         // test blur/focus on password
985         var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
986         password.live("change", function() {
987                 passwordChange++;
988         });
989
990         password.val(oldPasswordVal + "foo");
991         password.trigger("change");
992         equals( passwordChange, 1, "Change on password input." );
993
994         password.val(oldPasswordVal);
995         password.die("change");
996         
997         // make sure die works
998         
999         // die all changes
1000         selectChange = 0;
1001         select.die("change");
1002         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1003         select.trigger("change");
1004         equals( selectChange, 0, "Die on click works." );
1005
1006         selectChange = 0;
1007         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1008         select.trigger("change");
1009         equals( selectChange, 0, "Die on keyup works." );
1010         
1011         // die specific checkbox
1012         checkbox.die("change", checkboxFunction);
1013         checkbox.trigger("change");
1014         equals( checkboxChange, 1, "Die on checkbox." );
1015 });
1016
1017 test("live with submit", function() {
1018         var count1 = 0, count2 = 0;
1019         
1020         jQuery("#testForm").live("submit", function(ev) {
1021                 count1++;
1022                 ev.preventDefault();
1023         });
1024
1025         jQuery("body").live("submit", function(ev) {
1026                 count2++;
1027                 ev.preventDefault();
1028         });
1029
1030         if ( jQuery.support.submitBubbles ) {
1031                 jQuery("#testForm input[name=sub1]")[0].click();
1032                 equals(count1,1 );
1033                 equals(count2,1);
1034         } else {
1035                 jQuery("#testForm input[name=sub1]")[0].click();
1036                 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1037                 equals(count1,2);
1038                 equals(count2,2);
1039         }
1040         
1041         jQuery("#testForm").die("submit");
1042         jQuery("body").die("submit");
1043 });
1044
1045 test("live with focus/blur", function(){
1046         expect(2);
1047
1048         // Setup
1049         jQuery("<input type='text' id='livefb' />").appendTo("body");
1050         
1051         var $child =  jQuery("#livefb"),
1052                 child = $child[0],
1053                 pass = {};
1054
1055         function worked(e){
1056                 pass[e.type] = true;
1057         }
1058         
1059         $child.live("focus", worked);
1060         $child.live("blur", worked);
1061         
1062         // Test
1063         child.focus();
1064         if (pass.focus)
1065                 ok(true, "Test live() with focus event");
1066         else
1067                 ok(true, "Cannot test focus because the window isn't focused");
1068
1069         child.blur();
1070         if (pass.blur)
1071                 ok( true, "Test live() with blur event");
1072         else
1073                 ok(true, "Cannot test blur because the window isn't focused");
1074         
1075         // Teardown
1076         $child.die("focus", worked);
1077         $child.die("blur", worked);
1078         $child.remove();
1079         window.scrollTo(0,0);
1080 });
1081
1082 test("Non DOM element events", function() {
1083         expect(3);
1084
1085         jQuery({})
1086                 .bind('nonelementglobal', function(e) {
1087                         ok( true, "Global event on non-DOM annonymos object triggered" );
1088                 });
1089
1090         var o = {};
1091
1092         jQuery(o)
1093                 .bind('nonelementobj', function(e) {
1094                         ok( true, "Event on non-DOM object triggered" );
1095                 }).bind('nonelementglobal', function() {
1096                         ok( true, "Global event on non-DOM object triggered" );
1097                 });
1098
1099         jQuery(o).trigger('nonelementobj');
1100         jQuery.event.trigger('nonelementglobal');
1101 });
1102
1103 /*
1104 test("jQuery(function($) {})", function() {
1105         stop();
1106         jQuery(function($) {
1107                 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");
1108                 start();
1109         });
1110 });
1111
1112 test("event properties", function() {
1113         stop();
1114         jQuery("#simon1").click(function(event) {
1115                 ok( event.timeStamp, "assert event.timeStamp is present" );
1116                 start();
1117         }).click();
1118 });
1119 */