alexruiz / fest-swing-1.x

FEST Functional Swing Testing
http://fest.easytesting.org
109 stars 34 forks source link

Race condition in BasicRobot.focus(Component target, boolean wait) - results in ActionFailedException #3

Open brettvascon opened 12 years ago

brettvascon commented 12 years ago

We are hitting this quite frequently, since focus runs on the main thread, it is possible for the focus change to occur between the call to inEdtFocusOwner and the call to FocusMonitor.attachTo(target) in the code below:

  private void focus(Component target, boolean wait) {
    Component currentOwner = inEdtFocusOwner();
    if (currentOwner == target) return;
    FocusMonitor focusMonitor = FocusMonitor.attachTo(target);

The result is that focusMonitor never gets a focusGained event since the focus transition has already occurred, and therefore hasFocus returns false even though focus has indeed changed to the target. The timeout loop further down ends up throwing an ActionFailedException "Focus change to X failed"

The fix would be to call inEdtFocusOwner() again after the attachTo call, and if it returns the target, remove the focus listener and return.