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