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