import com.sun.glass.ui.Robot; import java.awt.Dimension; import java.awt.Point; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.lang.reflect.InvocationTargetException; import javafx.embed.swing.SwingNode; import javafx.stage.Stage; import javax.swing.JTextArea; import javax.swing.SwingUtilities; /** * * @author Andrei Eremeev */ public class SwingNodeKeyEvents extends SwingNodeBaseTest { private JTextArea text; private boolean flag; private final static String TXT = "early to bed and early to rise makes a man wise"; @Override protected void init(final SwingNode node) throws Exception { flag = true; SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { node.setContent(text = new JTextArea("")); } }); } @Override protected void doTest(Stage stage) throws InterruptedException, InvocationTargetException { Robot robot = createRobot(); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { text.requestFocus(); } }); if (!text.isFocusOwner()) { flag = false; System.err.println("[ERROR] : text did not get focus"); return; } for (int i = 0; i < TXT.length(); ++i) { int keyCode = KeyEvent.getExtendedKeyCodeForChar(TXT.charAt(i)); robot.keyPress(keyCode); delay(25); robot.keyRelease(keyCode); } delay(1000); String actual = text.getText(); flag &= TXT.equals(actual); if (!flag) { System.err.println("[ERROR] : typed text expected \"" + TXT + "\", actual \"" + actual + "\""); } Point loc = text.getLocationOnScreen(); Dimension size = text.getSize(); robot.mouseMove(loc.x, loc.y); delay(500); robot.mousePress(MouseEvent.BUTTON1); delay(50); robot.mouseMove(loc.x + size.width, loc.y + size.height); delay(500); flag &= TXT.equals(text.getSelectedText()); if (!flag) { System.err.println("[ERROR] : selected text expected \"" + TXT + "\", actual \"" + actual + "\""); } robot.keyPress(KeyEvent.VK_DELETE); delay(50); robot.keyRelease(KeyEvent.VK_DELETE); delay(500); actual = text.getText(); flag &= actual.isEmpty(); if (!flag) { System.err.println("[ERROR] : expected \"\", but " + actual); } } @Override protected boolean check() { return flag; } }