]> source.dussan.org Git - jquery-ui.git/commitdiff
Tests (Simulate): Added focus and blur support.
authorScott González <scott.gonzalez@gmail.com>
Mon, 31 Oct 2011 21:28:00 +0000 (17:28 -0400)
committerScott González <scott.gonzalez@gmail.com>
Mon, 31 Oct 2011 21:28:00 +0000 (17:28 -0400)
tests/jquery.simulate.js
tests/unit/autocomplete/autocomplete_events.js
tests/unit/tooltip/tooltip_events.js

index 9713a3038b1fbc279b3b45cc47199e0d5fc1909d..c1d28f982d99a1dda7d5e47d33591ac281c76c08 100644 (file)
@@ -24,6 +24,8 @@ $.simulate = function( el, type, options ) {
 
        if ( type === "drag" ) {
                this[ type ].apply( this, [ this.target, options ] );
+       } else if ( type === "focus" || type === "blur" ) {
+               this[ type ]();
        } else {
                this.simulateEvent( el, type, options );
        }
@@ -157,6 +159,53 @@ $.extend( $.simulate.prototype, {
                        x: o.left + el.outerWidth() / 2 - d.scrollLeft(),
                        y: o.top + el.outerHeight() / 2 - d.scrollTop()
                };
+       },
+
+       focus: function() {
+               var focusinEvent,
+                       triggered = false,
+                       element = $( this.target );
+
+               function trigger() {
+                       triggered = true;
+               }
+
+               element.bind( "focus", trigger );
+               element[ 0 ].focus();
+
+               if ( !triggered ) {
+                       focusinEvent = $.Event( "focusin" );
+                       focusinEvent.preventDefault();
+                       element.trigger( focusinEvent );
+                       element.triggerHandler( "focus" );
+               }
+               element.unbind( "focus", trigger );
+       },
+
+       blur: function() {
+               var focusoutEvent,
+                       triggered = false,
+                       element = $( this.target );
+
+               function trigger() {
+                       triggered = true;
+               }
+
+               element.bind( "blur", trigger );
+               element[ 0 ].blur();
+
+               // Some versions of IE don't actually .blur() on an element - so we focus the body
+               if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) {
+                       element[ 0 ].ownerDocument.body.focus();
+               }
+
+               if ( !triggered ) {
+                       focusoutEvent = $.Event( "focusout" );
+                       focusoutEvent.preventDefault();
+                       element.trigger( focusoutEvent );
+                       element.triggerHandler( "blur" );
+               }
+               element.unbind( "blur", trigger );
        }
 });
 
index 6813cfa71d51224abed475fc040cfcab95ce878d..dc945c286358b7e8182387a37652d41584a7a3c9 100644 (file)
@@ -62,15 +62,13 @@ $.each([
                                        }
                                }),
                        menu = element.autocomplete( "widget" );
-       
-               element.focus()[ settings.valueMethod ]( "j" ).keydown();
+
+               element.simulate( "focus" )[ settings.valueMethod ]( "j" ).keydown();
                setTimeout(function() {
                        ok( menu.is( ":visible" ), "menu is visible after delay" );
                        element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
                        element.simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
-                       // blurring through jQuery causes a bug in IE 6 which causes the
-                       // autocompletechange event to occur twice
-                       element[0].blur();
+                       element.simulate( "blur" );
                }, 50 );
        });
 });
index 963acaf596c980259145e8131f7c928124d18003..99e1fbd7977f04b0d0bbc32f22b50d4f56dbece6 100644 (file)
@@ -59,7 +59,7 @@ asyncTest( "mixed events", function() {
        element.one( "tooltipopen", function( event ) {
                same( event.originalEvent.type, "focusin" );
        });
-       element[0].focus();
+       element.simulate( "focus" );
 
        element.one( "tooltipopen", function() {
                ok( false, "open triggered while already open" );
@@ -77,7 +77,7 @@ asyncTest( "mixed events", function() {
                same( event.originalEvent.type, "blur" );
                start();
        });
-       element[0].blur();
+       element.simulate( "blur" );
 });
 
 }( jQuery ) );