Datepicker Tests: Fix IE tests by accounting for async nature of focus/blur and by correctly not double focusing a programmatically focused date picker.

A partial fix was implemented to resolve #6694, and this commit completes the fix so we can programmatically focus a date picker without focus being fired twice.(cherry picked from commit 1c1b64fcf0)
This commit is contained in:
Mike Sherov 2013-04-07 14:16:31 -04:00 committed by Scott González
parent 5bcf0bf50b
commit 2879b3ac25
3 changed files with 89 additions and 79 deletions

View File

@ -100,15 +100,13 @@ asyncTest("invocation", function() {
ok( button.length === 0, "Focus - button absent" ); ok( button.length === 0, "Focus - button absent" );
image = inp.siblings( "img" ); image = inp.siblings( "img" );
ok( image.length === 0, "Focus - image absent" ); ok( image.length === 0, "Focus - image absent" );
inp[0].focus();
setTimeout(function() { inp.one( "focus", function() {
ok( dp.is( ":visible" ), "Focus - rendered on focus" ); ok( dp.is( ":visible" ), "Focus - rendered on focus" );
inp.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } ); inp.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
ok( !dp.is( ":visible" ), "Focus - hidden on exit" ); ok( !dp.is( ":visible" ), "Focus - hidden on exit" );
inp[0].blur();
setTimeout(function() { TestHelpers.datepicker.onBlurThenFocus( inp, function() {
inp[0].focus();
setTimeout(function() {
ok( dp.is( ":visible" ), "Focus - rendered on focus" ); ok( dp.is( ":visible" ), "Focus - rendered on focus" );
body.simulate( "mousedown", {} ); body.simulate( "mousedown", {} );
ok( !dp.is( ":visible" ), "Focus - hidden on external click" ); ok( !dp.is( ":visible" ), "Focus - hidden on external click" );
@ -116,8 +114,7 @@ asyncTest("invocation", function() {
step2(); step2();
}); });
}); })[ 0 ].focus();
});
} }
function step2() { function step2() {
@ -129,8 +126,8 @@ asyncTest("invocation", function() {
ok( button.length === 1, "Button - button present" ); ok( button.length === 1, "Button - button present" );
ok( image.length === 0, "Button - image absent" ); ok( image.length === 0, "Button - image absent" );
equal( button.text(), "Popup", "Button - button text" ); equal( button.text(), "Popup", "Button - button text" );
inp[0].focus();
setTimeout(function() { TestHelpers.datepicker.onBlurThenFocus( inp, function() {
ok( !dp.is( ":visible" ), "Button - not rendered on focus" ); ok( !dp.is( ":visible" ), "Button - not rendered on focus" );
button.click(); button.click();
ok( dp.is( ":visible" ), "Button - rendered on button click" ); ok( dp.is( ":visible" ), "Button - rendered on button click" );
@ -144,8 +141,12 @@ asyncTest("invocation", function() {
function step3() { function step3() {
// On image button // On image button
inp = TestHelpers.datepicker.init("#inp", {showOn: "button", buttonImageOnly: true, inp = TestHelpers.datepicker.init( "#inp", {
buttonImage: "images/calendar.gif", buttonText: "Cal"}); showOn: "button",
buttonImageOnly: true,
buttonImage: "images/calendar.gif",
buttonText: "Cal"
});
ok( !dp.is( ":visible" ), "Image button - initially hidden" ); ok( !dp.is( ":visible" ), "Image button - initially hidden" );
button = inp.siblings( "button" ); button = inp.siblings( "button" );
ok( button.length === 0, "Image button - button absent" ); ok( button.length === 0, "Image button - button absent" );
@ -153,8 +154,8 @@ asyncTest("invocation", function() {
ok( image.length === 1, "Image button - image present" ); ok( image.length === 1, "Image button - image present" );
equal( image.attr( "src" ), "images/calendar.gif", "Image button - image source" ); equal( image.attr( "src" ), "images/calendar.gif", "Image button - image source" );
equal( image.attr( "title" ), "Cal", "Image button - image text" ); equal( image.attr( "title" ), "Cal", "Image button - image text" );
inp[0].focus();
setTimeout(function() { TestHelpers.datepicker.onBlurThenFocus( inp, function() {
ok( !dp.is( ":visible" ), "Image button - not rendered on focus" ); ok( !dp.is( ":visible" ), "Image button - not rendered on focus" );
image.click(); image.click();
ok( dp.is( ":visible" ), "Image button - rendered on image click" ); ok( dp.is( ":visible" ), "Image button - rendered on image click" );
@ -176,10 +177,8 @@ asyncTest("invocation", function() {
ok( image.length === 0, "Both - image absent" ); ok( image.length === 0, "Both - image absent" );
image = button.children( "img" ); image = button.children( "img" );
ok( image.length === 1, "Both - button image present" ); ok( image.length === 1, "Both - button image present" );
inp[0].blur();
setTimeout(function() { TestHelpers.datepicker.onBlurThenFocus( inp, function() {
inp[0].focus();
setTimeout(function() {
ok( dp.is( ":visible" ), "Both - rendered on focus" ); ok( dp.is( ":visible" ), "Both - rendered on focus" );
body.simulate( "mousedown", {} ); body.simulate( "mousedown", {} );
ok( !dp.is( ":visible" ), "Both - hidden on external click" ); ok( !dp.is( ":visible" ), "Both - hidden on external click" );
@ -191,7 +190,6 @@ asyncTest("invocation", function() {
start(); start();
}); });
});
} }
step1(); step1();

View File

@ -18,5 +18,12 @@ TestHelpers.datepicker = {
$.datepicker.setDefaults($.datepicker.regional[""]); $.datepicker.setDefaults($.datepicker.regional[""]);
return $(id).datepicker($.extend({showAnim: ""}, options || {})); return $(id).datepicker($.extend({showAnim: ""}, options || {}));
}, },
onBlurThenFocus: function( element, callback ) {
element.one( "blur", function(){
element.one( "focus", function(){
callback();
})[ 0 ].focus();
})[ 0 ].blur();
},
PROP_NAME: "datepicker" PROP_NAME: "datepicker"
}; };

View File

@ -752,9 +752,10 @@ $.extend(Datepicker.prototype, {
inst.dpDiv[showAnim || "show"](showAnim ? duration : null); inst.dpDiv[showAnim || "show"](showAnim ? duration : null);
} }
if (inst.input.is(":visible") && !inst.input.is(":disabled")) { if ( $.datepicker._shouldFocusInput( inst ) ) {
inst.input.focus(); inst.input.focus();
} }
$.datepicker._curInst = inst; $.datepicker._curInst = inst;
} }
}, },
@ -781,10 +782,7 @@ $.extend(Datepicker.prototype, {
inst.dpDiv[(this._get(inst, "isRTL") ? "add" : "remove") + inst.dpDiv[(this._get(inst, "isRTL") ? "add" : "remove") +
"Class"]("ui-datepicker-rtl"); "Class"]("ui-datepicker-rtl");
// #6694 - don't focus the input if it's already focused if (inst === $.datepicker._curInst && $.datepicker._datepickerShowing && $.datepicker._shouldFocusInput( inst ) ) {
// this breaks the change event in IE
if (inst === $.datepicker._curInst && $.datepicker._datepickerShowing && inst.input &&
inst.input.is(":visible") && !inst.input.is(":disabled") && inst.input[0] !== document.activeElement) {
inst.input.focus(); inst.input.focus();
} }
@ -801,6 +799,13 @@ $.extend(Datepicker.prototype, {
} }
}, },
// #6694 - don't focus the input if it's already focused
// this breaks the change event in IE
// Support: IE and jQuery <1.9
_shouldFocusInput: function( inst ) {
return inst.input && inst.input.is( ":visible" ) && !inst.input.is( ":disabled" ) && !inst.input.is( ":focus" );
},
/* Check positioning to remain on screen. */ /* Check positioning to remain on screen. */
_checkOffset: function(inst, offset, isFixed) { _checkOffset: function(inst, offset, isFixed) {
var dpWidth = inst.dpDiv.outerWidth(), var dpWidth = inst.dpDiv.outerWidth(),