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