From d19383f49de8cdf964e3a15cd5a0d73c5eadc6fe Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 20 Nov 2011 02:13:26 +0100 Subject: Tests: Use WindowFocusListener for AWTRobot toFront(.. window), more reliable --- .../test/junit/newt/TestFocus01SwingAWTRobot.java | 8 ++- .../test/junit/newt/TestFocus02SwingAWTRobot.java | 7 ++- .../opengl/test/junit/util/AWTRobotUtil.java | 7 ++- .../test/junit/util/AWTWindowFocusAdapter.java | 71 ++++++++++++++++++++++ 4 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/test/com/jogamp/opengl/test/junit/util/AWTWindowFocusAdapter.java (limited to 'src/test/com/jogamp') diff --git a/src/test/com/jogamp/opengl/test/junit/newt/TestFocus01SwingAWTRobot.java b/src/test/com/jogamp/opengl/test/junit/newt/TestFocus01SwingAWTRobot.java index be1b7a4e9..fe7fef09f 100644 --- a/src/test/com/jogamp/opengl/test/junit/newt/TestFocus01SwingAWTRobot.java +++ b/src/test/com/jogamp/opengl/test/junit/newt/TestFocus01SwingAWTRobot.java @@ -35,6 +35,7 @@ import org.junit.Assume; import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.Button; +import java.awt.Color; import java.awt.Robot; import java.lang.reflect.InvocationTargetException; @@ -119,7 +120,7 @@ public class TestFocus01SwingAWTRobot extends UITestCase { AWTFocusAdapter frame1FA = new AWTFocusAdapter("frame1"); frame1.addFocusListener(frame1FA); frame1.getContentPane().add(newtCanvasAWT, BorderLayout.CENTER); - Button button = new Button("Click me .."); + final Button button = new Button("Click me .."); AWTFocusAdapter buttonFA = new AWTFocusAdapter("Button"); button.addFocusListener(buttonFA); AWTKeyAdapter buttonKA = new AWTKeyAdapter("Button"); @@ -127,7 +128,10 @@ public class TestFocus01SwingAWTRobot extends UITestCase { eventCountAdapters.add(buttonKA); frame1.getContentPane().add(button, BorderLayout.NORTH); frame1.setSize(width, height); - frame1.setVisible(true); + javax.swing.SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame1.setVisible(true); + } } ); Assert.assertTrue(AWTRobotUtil.toFront(robot, frame1)); Thread.sleep(durationPerTest); // manual testing diff --git a/src/test/com/jogamp/opengl/test/junit/newt/TestFocus02SwingAWTRobot.java b/src/test/com/jogamp/opengl/test/junit/newt/TestFocus02SwingAWTRobot.java index fb71c4ba0..b9eb748b7 100644 --- a/src/test/com/jogamp/opengl/test/junit/newt/TestFocus02SwingAWTRobot.java +++ b/src/test/com/jogamp/opengl/test/junit/newt/TestFocus02SwingAWTRobot.java @@ -145,14 +145,17 @@ public class TestFocus02SwingAWTRobot extends UITestCase { jPanel1.add(new Button("west"), BorderLayout.WEST); jPanel1.add(container1, BorderLayout.CENTER); - JFrame jFrame1 = new JFrame("Swing Parent JFrame"); + final JFrame jFrame1 = new JFrame("Swing Parent JFrame"); AWTFocusAdapter jFrame1FA = new AWTFocusAdapter("JFrame1"); jFrame1.addFocusListener(jFrame1FA); // jFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // equivalent to Frame, use windowClosing event! jFrame1.setContentPane(jPanel1); jFrame1.setSize(width, height); - jFrame1.setVisible(true); // from here on, we need to run modifications on EDT + javax.swing.SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + jFrame1.setVisible(true); + } } ); Assert.assertTrue(AWTRobotUtil.toFront(robot, jFrame1)); int wait=0; diff --git a/src/test/com/jogamp/opengl/test/junit/util/AWTRobotUtil.java b/src/test/com/jogamp/opengl/test/junit/util/AWTRobotUtil.java index 131e3a714..a27bdd7a2 100644 --- a/src/test/com/jogamp/opengl/test/junit/util/AWTRobotUtil.java +++ b/src/test/com/jogamp/opengl/test/junit/util/AWTRobotUtil.java @@ -102,6 +102,9 @@ public class AWTRobotUtil { public static boolean toFront(Robot robot, final java.awt.Window window) throws AWTException, InterruptedException, InvocationTargetException { + AWTWindowFocusAdapter winFA = new AWTWindowFocusAdapter("window"); + window.addWindowFocusListener(winFA); + if(null == robot) { robot = new Robot(); robot.setAutoWaitForIdle(true); @@ -119,11 +122,11 @@ public class AWTRobotUtil { }}); robot.delay(ROBOT_DELAY); - KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); int wait; - for (wait=0; wait0; + } + + public void reset() { + focusCount = 0; + } + + /* @Override */ + public void windowGainedFocus(WindowEvent e) { + if(focusCount<0) { focusCount=0; } + focusCount++; + System.err.println("FOCUS AWT GAINED (Window) [fc "+focusCount+"]: "+prefix+", "+e); + } + + /* @Override */ + public void windowLostFocus(WindowEvent e) { + if(focusCount>0) { focusCount=0; } + focusCount--; + System.err.println("FOCUS AWT LOST (Window) [fc "+focusCount+"]: "+prefix+", "+e); + } + + public String toString() { return prefix+"[focusCount "+focusCount +"]"; } +} -- cgit v1.2.3