Keyboard shortcut that opens a dialog

27 views
Skip to first unread message

Morten

unread,
Aug 6, 2009, 9:10:32 AM8/6/09
to easytesting
Hi.
I've come across a situation where FEST behaves in a way that I didn't
expect.

My application frame's menu has a menu item that opens a dialog.
A shortcut is defined for the menu action: pressing CTRL+P pops up the
dialog.

When I use the code
{code}
getMyFrame().pressKey(VK_CONTROL).pressAndReleaseKeys
(KeyEvent.VK_P).releaseKey(VK_CONTROL);
getMyFrame().dialog();
{code}
The dialog pops up and is focused, but looses focus as the release of
the CTRL-key sets the main frame focused, and the second line fails
due to missing focus.

This works:
{code}
getMyFrame().pressKey(VK_CONTROL).pressAndReleaseKeys(KeyEvent.VK_P);
getMyFrame().dialog().releaseKey(VK_CONTROL);
{code}

Is that the way to go?
(http://docs.codehaus.org/display/FEST/Simulating+Keyboard+Input
doesn't mention this caveat)

Thanks,
/Morten

Alex Ruiz

unread,
Aug 6, 2009, 5:11:25 PM8/6/09
to easyt...@googlegroups.com
Hi Morten,

I don't have an answer right now. Can you please send me some code to reproduce this issue? It might be a bug in FEST.

Thanks!
-Alex

Morten Mikkelsen

unread,
Aug 7, 2009, 5:19:32 AM8/7/09
to easyt...@googlegroups.com
Hi.
It doesn't seem to be a bug per se.. Maybe I'm just using it wrong:

Here goes:
---------------
import static org.fest.swing.timing.Pause.pause;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import org.fest.swing.core.BasicRobot;
import org.fest.swing.core.Robot;
import org.fest.swing.finder.WindowFinder;
import org.fest.swing.fixture.FrameFixture;
import org.junit.Test;

public class KeyboardShortcutFrame {
   
    public KeyboardShortcutFrame() {
        final JFrame f = new JFrame("TheFrame");
       
        Action a = new AbstractAction("OpeDialog") {           
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                JDialog dlg = new JDialog(f, "ChildDialog", true);
                dlg.setLocation(100,100);
                dlg.getContentPane().add(new JLabel("I'm your dialog"));
                dlg.pack();
                dlg.setVisible(true);
            }
        };
        f.setJMenuBar(createMenuWithOneItem(a));
        f.getContentPane().add(new JLabel("I'm a Frame"));
        f.pack();
        f.setVisible(true);
    }
   
    public static void main(String[] args) {
        KeyboardShortcutFrame frame = new KeyboardShortcutFrame();
    }
   
    public JMenuBar createMenuWithOneItem(Action menuItemAction) {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("The Menu");
        menuBar.add(menu);
        JMenuItem menuItem = new JMenuItem(menuItemAction);
        menuItem.setAccelerator(KeyStroke.getKeyStroke("control P"));
        menu.add(menuItem);
        return menuBar;
    }
   
    @Test
    public void reproduce() {
        Robot robot = BasicRobot.robotWithNewAwtHierarchy();
        new KeyboardShortcutFrame(); //create and show
        System.err.println("Frame created");
        pause(1000);
        FrameFixture ff = WindowFinder.findFrame(JFrame.class).withTimeout(1000).using(robot);
        System.err.println("Frame found");
        pause(1000);
        ff.pressKey(KeyEvent.VK_CONTROL).pressAndReleaseKeys(KeyEvent.VK_P).releaseKey(KeyEvent.VK_CONTROL);
        System.err.println("Dialog opened");
        pause(100);
    }
}
---------------
Having the robot produce the keypresses works, i.e. replacing the ff.pressKey()... line with
        robot.pressKey(KeyEvent.VK_CONTROL);
        robot.pressAndReleaseKeys(KeyEvent.VK_P);
        robot.releaseKey(KeyEvent.VK_CONTROL);

The trace says:
org.fest.swing.exception.ActionFailedException: Focus change to javax.swing.JFrame[name='frame0', title='TheFrame', enabled=true, visible=true, showing=true] failed
    at org.fest.swing.exception.ActionFailedException.actionFailure(ActionFailedException.java:33)
    at org.fest.swing.core.BasicRobot.focus(BasicRobot.java:239)
    at org.fest.swing.core.BasicRobot.focusAndWaitForFocusGain(BasicRobot.java:216)
    at org.fest.swing.driver.ComponentDriver.focusAndWaitForFocusGain(ComponentDriver.java:357)
    at org.fest.swing.driver.ComponentDriver.releaseKey(ComponentDriver.java:343)
    at org.fest.swing.fixture.FrameFixture.releaseKey(FrameFixture.java:248)
    at KeyboardShortcutFrame.reproduce(KeyboardShortcutFrame.java:67)
    [...]
--
/Morten

Alex Ruiz

unread,
Aug 7, 2009, 7:42:22 AM8/7/09
to easyt...@googlegroups.com
Hi Morten,

Thanks for providing a use case. Let me give it a try. By reading your code it seems that you are using the APIs correctly.

Regards,
-Alex
Reply all
Reply to author
Forward
0 new messages