.bind() now accepts an optional thisObject as the last argument which is used to...
[jquery.git] / test / unit / event.js
1 module("event");
2
3 test("bind(), with data", function() {
4         expect(3);
5         var handler = function(event) {
6                 ok( event.data, "bind() with data, check passed data exists" );
7                 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
8         };
9         jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
10
11         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
12 });
13
14 test("bind(), with data, trigger with data", function() {
15         expect(4);
16         var handler = function(event, data) {
17                 ok( event.data, "check passed data exists" );
18                 equals( event.data.foo, "bar", "Check value of passed data" );
19                 ok( data, "Check trigger data" );
20                 equals( data.bar, "foo", "Check value of trigger data" );
21         };
22         jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
23 });
24
25 test("bind(), multiple events at once", function() {
26         expect(2);
27         var clickCounter = 0,
28                 mouseoverCounter = 0;
29         var handler = function(event) {
30                 if (event.type == "click")
31                         clickCounter += 1;
32                 else if (event.type == "mouseover")
33                         mouseoverCounter += 1;
34         };
35         jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
36         equals( clickCounter, 1, "bind() with multiple events at once" );
37         equals( mouseoverCounter, 1, "bind() with multiple events at once" );
38 });
39
40 test("bind(), no data", function() {
41         expect(1);
42         var handler = function(event) {
43                 ok ( !event.data, "Check that no data is added to the event object" );
44         };
45         jQuery("#firstp").bind("click", handler).trigger("click");
46 });
47
48 test("bind(), iframes", function() {
49         // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
50         var doc = jQuery("#loadediframe").contents();
51         
52         jQuery("div", doc).bind("click", function() {
53                 ok( true, "Binding to element inside iframe" );
54         }).click().unbind('click');
55 });
56
57 test("bind(), trigger change on select", function() {
58         expect(3);
59         var counter = 0;
60         function selectOnChange(event) {
61                 equals( event.data, counter++, "Event.data is not a global event object" );
62         };
63         jQuery("#form select").each(function(i){
64                 jQuery(this).bind('change', i, selectOnChange);
65         }).trigger('change');
66 });
67
68 test("bind(), namespaced events, cloned events", function() {
69         expect(6);
70
71         jQuery("#firstp").bind("custom.test",function(e){
72                 ok(true, "Custom event triggered");
73         });
74
75         jQuery("#firstp").bind("click",function(e){
76                 ok(true, "Normal click triggered");
77         });
78
79         jQuery("#firstp").bind("click.test",function(e){
80                 ok(true, "Namespaced click triggered");
81         });
82
83         // Trigger both bound fn (2)
84         jQuery("#firstp").trigger("click");
85
86         // Trigger one bound fn (1)
87         jQuery("#firstp").trigger("click.test");
88
89         // Remove only the one fn
90         jQuery("#firstp").unbind("click.test");
91
92         // Trigger the remaining fn (1)
93         jQuery("#firstp").trigger("click");
94
95         // Remove the remaining fn
96         jQuery("#firstp").unbind(".test");
97
98         // Trigger the remaining fn (0)
99         jQuery("#firstp").trigger("custom");
100
101         // using contents will get comments regular, text, and comment nodes
102         jQuery("#nonnodes").contents().bind("tester", function () {
103                 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
104         }).trigger("tester");
105
106         // Make sure events stick with appendTo'd elements (which are cloned) #2027
107         jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
108         ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
109 });
110
111 test("bind(), multi-namespaced events", function() {
112         expect(6);
113         
114         var order = [
115                 "click.test.abc",
116                 "click.test.abc",
117                 "click.test",
118                 "click.test.abc",
119                 "click.test",
120                 "custom.test2"
121         ];
122         
123         function check(name, msg){
124                 same(name, order.shift(), msg);
125         }
126
127         jQuery("#firstp").bind("custom.test",function(e){
128                 check("custom.test", "Custom event triggered");
129         });
130
131         jQuery("#firstp").bind("custom.test2",function(e){
132                 check("custom.test2", "Custom event triggered");
133         });
134
135         jQuery("#firstp").bind("click.test",function(e){
136                 check("click.test", "Normal click triggered");
137         });
138
139         jQuery("#firstp").bind("click.test.abc",function(e){
140                 check("click.test.abc", "Namespaced click triggered");
141         });
142
143         // Trigger both bound fn (1)
144         jQuery("#firstp").trigger("click.test.abc");
145
146         // Trigger one bound fn (1)
147         jQuery("#firstp").trigger("click.abc");
148
149         // Trigger two bound fn (2)
150         jQuery("#firstp").trigger("click.test");
151
152         // Remove only the one fn
153         jQuery("#firstp").unbind("click.abc");
154
155         // Trigger the remaining fn (1)
156         jQuery("#firstp").trigger("click");
157
158         // Remove the remaining fn
159         jQuery("#firstp").unbind(".test");
160
161         // Trigger the remaining fn (1)
162         jQuery("#firstp").trigger("custom");
163 });
164
165 test("bind(), with different this object", function() {
166         expect(4);
167         var thisObject = { myThis: true },
168                 data = { myData: true },
169                 handler1 = function( event ) {
170                         equals( this, thisObject, "bind() with different this object" );
171                 },
172                 handler2 = function( event ) {
173                         equals( this, thisObject, "bind() with different this object and data" );
174                         equals( event.data, data, "bind() with different this object and data" );
175                 };
176         
177         jQuery("#firstp")
178                 .bind("click", handler1, thisObject).click().unbind("click", handler1)
179                 .bind("click", data, handler2, thisObject).click().unbind("click", handler2);
180
181         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
182 });
183
184 test("unbind(type)", function() {
185         expect( 0 );
186         
187         var $elem = jQuery("#firstp"),
188                 message;
189
190         function error(){
191                 ok( false, message );
192         }
193         
194         message = "unbind passing function";
195         $elem.bind('error', error).unbind('error',error).triggerHandler('error');
196         
197         message = "unbind all from event";
198         $elem.bind('error', error).unbind('error').triggerHandler('error');
199         
200         message = "unbind all";
201         $elem.bind('error', error).unbind().triggerHandler('error');
202         
203         message = "unbind many with function";
204         $elem.bind('error error2',error)
205                  .unbind('error error2', error )
206                  .trigger('error').triggerHandler('error2');
207
208         message = "unbind many"; // #3538
209         $elem.bind('error error2',error)
210                  .unbind('error error2')
211                  .trigger('error').triggerHandler('error2');
212         
213         message = "unbind without a type or handler";
214         $elem.bind("error error2.test",error)
215                  .unbind()
216                  .trigger("error").triggerHandler("error2");
217 });
218
219 test("unbind(eventObject)", function() {
220         expect(4);
221         
222         var $elem = jQuery("#firstp"),
223                 num;
224
225         function assert( expected ){
226                 num = 0;
227                 $elem.trigger('foo').triggerHandler('bar');
228                 equals( num, expected, "Check the right handlers are triggered" );
229         }
230         
231         $elem
232                 // This handler shouldn't be unbound
233                 .bind('foo', function(){
234                         num += 1;
235                 })
236                 .bind('foo', function(e){
237                         $elem.unbind( e )
238                         num += 2;
239                 })
240                 // Neither this one
241                 .bind('bar', function(){
242                         num += 4;
243                 });
244                 
245         assert( 7 );
246         assert( 5 );
247         
248         $elem.unbind('bar');
249         assert( 1 );
250         
251         $elem.unbind(); 
252         assert( 0 );
253 });
254
255 test("hover()", function() {
256         var times = 0,
257                 handler1 = function( event ) { ++times; },
258                 handler2 = function( event ) { ++times; };
259
260         jQuery("#firstp")
261                 .hover(handler1, handler2)
262                 .mouseenter().mouseleave()
263                 .unbind("mouseenter", handler1)
264                 .unbind("mouseleave", handler2)
265                 .hover(handler1)
266                 .mouseenter().mouseleave()
267                 .unbind("mouseenter mouseleave", handler1)
268                 .mouseenter().mouseleave();
269
270         equals( times, 4, "hover handlers fired" );
271 });
272
273 test("trigger() shortcuts", function() {
274         expect(6);
275         jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
276                 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
277                 equals( close.length, 0, "Context element does not exist, length must be zero" );
278                 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
279                 return false;
280         }).click();
281         
282         jQuery("#check1").click(function() {
283                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
284         }).click();
285         
286         var counter = 0;
287         jQuery('#firstp')[0].onclick = function(event) {
288                 counter++;
289         };
290         jQuery('#firstp').click();
291         equals( counter, 1, "Check that click, triggers onclick event handler also" );
292         
293         var clickCounter = 0;
294         jQuery('#simon1')[0].onclick = function(event) {
295                 clickCounter++;
296         };
297         jQuery('#simon1').click();
298         equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
299         
300         jQuery('<img />').load(function(){
301                 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
302         }).load();
303 });
304
305 test("trigger() bubbling", function() {
306         expect(14);
307
308         var doc = 0, html = 0, body = 0, main = 0, ap = 0;
309
310         jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
311         jQuery("html").bind("click", function(e){ html++; });
312         jQuery("body").bind("click", function(e){ body++; });
313         jQuery("#main").bind("click", function(e){ main++; });
314         jQuery("#ap").bind("click", function(){ ap++; return false; });
315
316         jQuery("html").trigger("click");
317         equals( doc, 1, "HTML bubble" );
318         equals( html, 1, "HTML bubble" );
319
320         jQuery("body").trigger("click");
321         equals( doc, 2, "Body bubble" );
322         equals( html, 2, "Body bubble" );
323         equals( body, 1, "Body bubble" );
324
325         jQuery("#main").trigger("click");
326         equals( doc, 3, "Main bubble" );
327         equals( html, 3, "Main bubble" );
328         equals( body, 2, "Main bubble" );
329         equals( main, 1, "Main bubble" );
330
331         jQuery("#ap").trigger("click");
332         equals( doc, 3, "ap bubble" );
333         equals( html, 3, "ap bubble" );
334         equals( body, 2, "ap bubble" );
335         equals( main, 1, "ap bubble" );
336         equals( ap, 1, "ap bubble" );
337 });
338
339 test("trigger(type, [data], [fn])", function() {
340         expect(11);
341
342         var handler = function(event, a, b, c) {
343                 equals( event.type, "click", "check passed data" );
344                 equals( a, 1, "check passed data" );
345                 equals( b, "2", "check passed data" );
346                 equals( c, "abc", "check passed data" );
347                 return "test";
348         };
349
350         var $elem = jQuery("#firstp");
351
352         // Simulate a "native" click
353         $elem[0].click = function(){
354                 ok( true, "Native call was triggered" );
355         };
356
357         // Triggers handlrs and native
358         // Trigger 5
359         $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
360
361         // Simulate a "native" click
362         $elem[0].click = function(){
363                 ok( false, "Native call was triggered" );
364         };
365
366         // Trigger only the handlers (no native)
367         // Triggers 5
368         equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
369
370         var pass = true;
371         try {
372                 jQuery('#form input:first').hide().trigger('focus');
373         } catch(e) {
374                 pass = false;
375         }
376         ok( pass, "Trigger focus on hidden element" );
377 });
378
379 test("trigger(eventObject, [data], [fn])", function() {
380         expect(25);
381         
382         var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
383                 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
384         
385         var event = jQuery.Event("noNew");      
386         ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
387         equals( event.type, "noNew", "Verify its type" );
388         
389         equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
390         equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
391         equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
392         
393         event.preventDefault();
394         equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
395         event.stopPropagation();
396         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
397         
398         event.isPropagationStopped = function(){ return false };
399         event.stopImmediatePropagation();
400         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
401         equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
402         
403         $parent.bind('foo',function(e){
404                 // Tries bubbling
405                 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
406                 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
407                 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
408                 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
409         });
410         
411         // test with an event object
412         event = new jQuery.Event("foo");
413         event.secret = 'boo!';
414         $child.trigger(event);
415         
416         // test with a literal object
417         $child.trigger({type:'foo', secret:'boo!'});
418         
419         $parent.unbind();
420
421         function error(){
422                 ok( false, "This assertion shouldn't be reached");
423         }
424         
425         $parent.bind('foo', error );
426         
427         $child.bind('foo',function(e, a, b, c ){
428                 equals( arguments.length, 4, "Check arguments length");
429                 equals( a, 1, "Check first custom argument");
430                 equals( b, 2, "Check second custom argument");
431                 equals( c, 3, "Check third custom argument");
432                 
433                 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
434                 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
435                 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
436                 
437                 // Skips both errors
438                 e.stopImmediatePropagation();
439                 
440                 return "result";
441         });
442         
443         // We should add this back in when we want to test the order
444         // in which event handlers are iterated.
445         //$child.bind('foo', error );
446         
447         event = new jQuery.Event("foo");
448         $child.trigger( event, [1,2,3] ).unbind();
449         equals( event.result, "result", "Check event.result attribute");
450         
451         // Will error if it bubbles
452         $child.triggerHandler('foo');
453         
454         $child.unbind();
455         $parent.unbind().remove();
456 });
457
458 test("jQuery.Event.currentTarget", function(){
459         expect(1);
460         
461         var counter = 0,
462                 $elem = jQuery('<button>a</button>').click(function(e){
463                 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
464         });
465         
466         // Fake event
467         $elem.trigger('click');
468         
469         // Cleanup
470         $elem.unbind();
471 });
472
473 test("toggle(Function, Function, ...)", function() {
474         expect(11);
475         
476         var count = 0,
477                 fn1 = function(e) { count++; },
478                 fn2 = function(e) { count--; },
479                 preventDefault = function(e) { e.preventDefault() },
480                 link = jQuery('#mark');
481         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
482         equals( count, 1, "Check for toggle(fn, fn)" );
483
484         jQuery("#firstp").toggle(function () {
485                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
486         }, function() {}).trigger("click", [ 1, 2, 3 ]);
487
488         var first = 0;
489         jQuery("#simon1").one("click", function() {
490                 ok( true, "Execute event only once" );
491                 jQuery(this).toggle(function() {
492                         equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
493                 }, function() {
494                         equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
495                 });
496                 return false;
497         }).click().click().click();
498         
499         var turn = 0;
500         var fns = [
501                 function(){
502                         turn = 1;
503                 },
504                 function(){
505                         turn = 2;
506                 },
507                 function(){
508                         turn = 3;
509                 }
510         ];
511         
512         var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
513         $div.click();
514         equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
515         $div.click();
516         equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
517         $div.click();
518         equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
519         $div.click();
520         equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
521         $div.click();
522         equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
523         
524         $div.unbind('click',fns[0]);
525         var data = jQuery.data( $div[0], 'events' );
526         ok( !data, "Unbinding one function from toggle unbinds them all");
527 });
528
529 test(".live()/.die()", function() {
530         expect(58);
531
532         var submit = 0, div = 0, livea = 0, liveb = 0;
533
534         jQuery("div").live("submit", function(){ submit++; return false; });
535         jQuery("div").live("click", function(){ div++; });
536         jQuery("div#nothiddendiv").live("click", function(){ livea++; });
537         jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
538
539         // Nothing should trigger on the body
540         jQuery("body").trigger("click");
541         equals( submit, 0, "Click on body" );
542         equals( div, 0, "Click on body" );
543         equals( livea, 0, "Click on body" );
544         equals( liveb, 0, "Click on body" );
545
546         // This should trigger two events
547         jQuery("div#nothiddendiv").trigger("click");
548         equals( submit, 0, "Click on div" );
549         equals( div, 1, "Click on div" );
550         equals( livea, 1, "Click on div" );
551         equals( liveb, 0, "Click on div" );
552
553         // This should trigger three events (w/ bubbling)
554         jQuery("div#nothiddendivchild").trigger("click");
555         equals( submit, 0, "Click on inner div" );
556         equals( div, 2, "Click on inner div" );
557         equals( livea, 2, "Click on inner div" );
558         equals( liveb, 1, "Click on inner div" );
559
560         // This should trigger one submit
561         jQuery("div#nothiddendivchild").trigger("submit");
562         equals( submit, 1, "Submit on div" );
563         equals( div, 2, "Submit on div" );
564         equals( livea, 2, "Submit on div" );
565         equals( liveb, 1, "Submit on div" );
566
567         // Make sure no other events were removed in the process
568         jQuery("div#nothiddendivchild").trigger("click");
569         equals( submit, 1, "die Click on inner div" );
570         equals( div, 3, "die Click on inner div" );
571         equals( livea, 3, "die Click on inner div" );
572         equals( liveb, 2, "die Click on inner div" );
573
574         // Now make sure that the removal works
575         jQuery("div#nothiddendivchild").die("click");
576         jQuery("div#nothiddendivchild").trigger("click");
577         equals( submit, 1, "die Click on inner div" );
578         equals( div, 4, "die Click on inner div" );
579         equals( livea, 4, "die Click on inner div" );
580         equals( liveb, 2, "die Click on inner div" );
581
582         // Make sure that the click wasn't removed too early
583         jQuery("div#nothiddendiv").trigger("click");
584         equals( submit, 1, "die Click on inner div" );
585         equals( div, 5, "die Click on inner div" );
586         equals( livea, 5, "die Click on inner div" );
587         equals( liveb, 2, "die Click on inner div" );
588
589         // Make sure that stopPropgation doesn't stop live events
590         jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
591         jQuery("div#nothiddendivchild").trigger("click");
592         equals( submit, 1, "stopPropagation Click on inner div" );
593         equals( div, 6, "stopPropagation Click on inner div" );
594         equals( livea, 6, "stopPropagation Click on inner div" );
595         equals( liveb, 3, "stopPropagation Click on inner div" );
596
597         jQuery("div#nothiddendivchild").die("click");
598         jQuery("div#nothiddendiv").die("click");
599         jQuery("div").die("click");
600         jQuery("div").die("submit");
601
602         // Test binding with a different context
603         var clicked = 0, container = jQuery('#main')[0];
604         jQuery("#foo", container).live("click", function(e){ clicked++; });
605         jQuery("div").trigger('click');
606         jQuery("#foo").trigger('click');
607         jQuery("#main").trigger('click');
608         jQuery("body").trigger('click');
609         equals( clicked, 2, "live with a context" );
610
611         // Make sure the event is actually stored on the context
612         ok( jQuery.data(container, "events").live, "live with a context" );
613
614         // Test unbinding with a different context
615         jQuery("#foo", container).die("click");
616         jQuery("#foo").trigger('click');
617         equals( clicked, 2, "die with a context");
618
619         // Test binding with event data
620         jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
621         jQuery("#foo").trigger("click").die("click");
622
623         // Test binding with trigger data
624         jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
625         jQuery("#foo").trigger("click", true).die("click");
626
627         // Test binding with different this object
628         jQuery("#foo").live("click", function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" });
629         jQuery("#foo").trigger("click").die("click");
630
631         // Test binding with different this object, event data, and trigger data
632         jQuery("#foo").live("click", true, function(e, data){
633                 equals( e.data, true, "live with with different this object, event data, and trigger data" );
634                 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" ); 
635                 equals( data, true, "live with with different this object, event data, and trigger data")
636         }, { foo: "bar" });
637         jQuery("#foo").trigger("click", true).die("click");
638
639         // Verify that return false prevents default action
640         jQuery("#anchor2").live("click", function(){ return false; });
641         var hash = window.location.hash;
642         jQuery("#anchor2").trigger("click");
643         equals( window.location.hash, hash, "return false worked" );
644         jQuery("#anchor2").die("click");
645
646         // Verify that .preventDefault() prevents default action
647         jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
648         var hash = window.location.hash;
649         jQuery("#anchor2").trigger("click");
650         equals( window.location.hash, hash, "e.preventDefault() worked" );
651         jQuery("#anchor2").die("click");
652
653         // Test binding the same handler to multiple points
654         var called = 0;
655         function callback(){ called++; return false; }
656
657         jQuery("#nothiddendiv").live("click", callback);
658         jQuery("#anchor2").live("click", callback);
659
660         jQuery("#nothiddendiv").trigger("click");
661         equals( called, 1, "Verify that only one click occurred." );
662
663         jQuery("#anchor2").trigger("click");
664         equals( called, 2, "Verify that only one click occurred." );
665
666         // Make sure that only one callback is removed
667         jQuery("#anchor2").die("click", callback);
668
669         jQuery("#nothiddendiv").trigger("click");
670         equals( called, 3, "Verify that only one click occurred." );
671
672         jQuery("#anchor2").trigger("click");
673         equals( called, 3, "Verify that no click occurred." );
674
675         // Make sure that it still works if the selector is the same,
676         // but the event type is different
677         jQuery("#nothiddendiv").live("foo", callback);
678
679         // Cleanup
680         jQuery("#nothiddendiv").die("click", callback);
681
682         jQuery("#nothiddendiv").trigger("click");
683         equals( called, 3, "Verify that no click occurred." );
684
685         jQuery("#nothiddendiv").trigger("foo");
686         equals( called, 4, "Verify that one foo occurred." );
687
688         // Cleanup
689         jQuery("#nothiddendiv").die("foo", callback);
690         
691         // Make sure we don't loose the target by DOM modifications
692         // after the bubble already reached the liveHandler
693         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
694         
695         jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
696         jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
697         
698         jQuery("#nothiddendiv span").click();
699         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
700         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
701         
702         // Cleanup
703         jQuery("#nothiddendivchild").die("click");
704
705         // Verify that .live() ocurs and cancel buble in the same order as
706         // we would expect .bind() and .click() without delegation
707         var lived = 0, livee = 0;
708         
709         // bind one pair in one order
710         jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
711         jQuery('span#liveSpan1').live('click', function(){ livee++; });
712
713         jQuery('span#liveSpan1 a').click();
714         equals( lived, 1, "Verify that only one first handler occurred." );
715         equals( livee, 0, "Verify that second handler don't." );
716
717         // and one pair in inverse
718         jQuery('#liveHandlerOrder span#liveSpan2').live('click', function(){ livee++; });
719         jQuery('#liveHandlerOrder span#liveSpan2 a').live('click', function(){ lived++; return false; });
720
721         jQuery('span#liveSpan2 a').click();
722         equals( lived, 2, "Verify that only one first handler occurred." );
723         equals( livee, 0, "Verify that second handler don't." );
724         
725         // Cleanup
726         jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
727         
728         // Test this, target and currentTarget are correct
729         jQuery('span#liveSpan1').live('click', function(e){ 
730                 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
731                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
732                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
733         });
734         
735         jQuery('span#liveSpan1 a').click();
736         
737         jQuery('span#liveSpan1').die('click');
738 });
739
740 test("Non DOM element events", function() {
741         expect(3);
742
743         jQuery({})
744                 .bind('nonelementglobal', function(e) {
745                         ok( true, "Global event on non-DOM annonymos object triggered" );
746                 });
747
748         var o = {};
749
750         jQuery(o)
751                 .bind('nonelementobj', function(e) {
752                         ok( true, "Event on non-DOM object triggered" );
753                 }).bind('nonelementglobal', function() {
754                         ok( true, "Global event on non-DOM object triggered" );
755                 });
756
757         jQuery(o).trigger('nonelementobj');
758         jQuery.event.trigger('nonelementglobal');
759 });
760
761 /*
762 test("jQuery(function($) {})", function() {
763         stop();
764         jQuery(function($) {
765                 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");
766                 start();
767         });
768 });
769
770 test("event properties", function() {
771         stop();
772         jQuery("#simon1").click(function(event) {
773                 ok( event.timeStamp, "assert event.timeStamp is present" );
774                 start();
775         }).click();
776 });
777 */