From 6eb90f6e63c2449bad1215caf7163f7d2497d46e Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 28 Sep 2010 06:17:08 +0300 Subject: NEWT Unit Focus Test: AWT Permanent focus check without focus count (due to temp loss) - instead test focus with key input. --- .../test/junit/newt/TestFocus01SwingAWTRobot.java | 39 ++++++---------------- .../test/junit/newt/TestFocus02SwingAWTRobot.java | 18 +++++++--- .../com/jogamp/test/junit/util/AWTRobotUtil.java | 34 +++++++++++++++++++ 3 files changed, 59 insertions(+), 32 deletions(-) (limited to 'src/junit/com/jogamp') diff --git a/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java b/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java index ccc66b85a..d6a408061 100644 --- a/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java +++ b/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java @@ -106,15 +106,15 @@ public class TestFocus01SwingAWTRobot { glWindow1.addWindowListener(glWindow1FA); // Monitor NEWT focus and keyboard events. - NEWTKeyAdapter newtKeyAdapter = new NEWTKeyAdapter("GLWindow1"); - glWindow1.addKeyListener(newtKeyAdapter); + NEWTKeyAdapter glWindow1KA = new NEWTKeyAdapter("GLWindow1"); + glWindow1.addKeyListener(glWindow1KA); // Wrap the window in a canvas. final NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1); // Monitor AWT focus and keyboard events. - AWTKeyAdapter awtKeyAdapter = new AWTKeyAdapter("NewtCanvasAWT"); - newtCanvasAWT.addKeyListener(awtKeyAdapter); + AWTKeyAdapter newtCanvasAWTKA = new AWTKeyAdapter("NewtCanvasAWT"); + newtCanvasAWT.addKeyListener(newtCanvasAWTKA); AWTFocusAdapter newtCanvasAWTFA = new AWTFocusAdapter("NewtCanvasAWT"); newtCanvasAWT.addFocusListener(newtCanvasAWTFA); @@ -125,6 +125,8 @@ public class TestFocus01SwingAWTRobot { Button button = new Button("Click me .."); AWTFocusAdapter buttonFA = new AWTFocusAdapter("Button"); button.addFocusListener(buttonFA); + AWTKeyAdapter buttonKA = new AWTKeyAdapter("Button"); + button.addKeyListener(buttonKA); frame1.getContentPane().add(button, BorderLayout.NORTH); frame1.setSize(width, height); frame1.setVisible(true); @@ -145,42 +147,23 @@ public class TestFocus01SwingAWTRobot { Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, button, button)); Assert.assertEquals(0, glWindow1FA.getCount()); Assert.assertEquals(0, newtCanvasAWTFA.getCount()); - Assert.assertEquals(1, buttonFA.getCount()); System.err.println("FOCUS AWT Button sync"); + Assert.assertTrue(AWTRobotUtil.testKeyInput(robot, buttonKA)); // Request the AWT focus, which should automatically provide the NEWT window with focus. Thread.sleep(100); // allow event sync System.err.println("FOCUS NEWT Canvas/GLWindow request"); Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild())); - Assert.assertEquals(1, glWindow1FA.getCount()); Assert.assertEquals(0, newtCanvasAWTFA.getCount()); Assert.assertEquals(0, buttonFA.getCount()); System.err.println("FOCUS NEWT Canvas/GLWindow sync"); - - // Type two keys, which should be directed to the focused window. - if(null == robot) { - robot = new Robot(); - robot.setAutoWaitForIdle(true); - } - robot.keyPress(java.awt.event.KeyEvent.VK_A); - robot.keyRelease(java.awt.event.KeyEvent.VK_A); - robot.keyPress(java.awt.event.KeyEvent.VK_B); - robot.keyRelease(java.awt.event.KeyEvent.VK_B); - - // Wait for the key events to be processed. - for (wait=0; wait<10 && newtKeyAdapter.getCount()<2; wait++) { - Thread.sleep(100); - } - - Assert.assertEquals(0, newtCanvasAWTFA.getCount()); - Assert.assertEquals(1, glWindow1FA.getCount()); - Assert.assertEquals("AWT parent canvas received keyboard events", 0, awtKeyAdapter.getCount()); - Assert.assertEquals(2, newtKeyAdapter.getCount()); + Assert.assertTrue(AWTRobotUtil.testKeyInput(robot, glWindow1KA)); + Assert.assertEquals("AWT parent canvas received keyboard events", 0, newtCanvasAWTKA.getCount()); // Remove listeners to avoid logging during dispose/destroy. - glWindow1.removeKeyListener(newtKeyAdapter); + glWindow1.removeKeyListener(glWindow1KA); glWindow1.removeWindowListener(glWindow1FA); - newtCanvasAWT.removeKeyListener(awtKeyAdapter); + newtCanvasAWT.removeKeyListener(newtCanvasAWTKA); newtCanvasAWT.removeFocusListener(newtCanvasAWTFA); // Shutdown the test. diff --git a/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java b/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java index 379672cb2..cee4c31df 100644 --- a/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java +++ b/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java @@ -106,14 +106,20 @@ public class TestFocus02SwingAWTRobot { glWindow1.addGLEventListener(demo1); NEWTFocusAdapter glWindow1FA = new NEWTFocusAdapter("GLWindow1"); glWindow1.addWindowListener(glWindow1FA); + NEWTKeyAdapter glWindow1KA = new NEWTKeyAdapter("GLWindow1"); + glWindow1.addKeyListener(glWindow1KA); NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1); AWTFocusAdapter newtCanvasAWTFA = new AWTFocusAdapter("NewtCanvasAWT"); newtCanvasAWT.addFocusListener(newtCanvasAWTFA); + AWTKeyAdapter newtCanvasAWTKA = new AWTKeyAdapter("NewtCanvasAWT"); + newtCanvasAWT.addKeyListener(newtCanvasAWTKA); Button buttonNorthInner = new Button("north"); AWTFocusAdapter buttonNorthInnerFA = new AWTFocusAdapter("ButtonNorthInner"); buttonNorthInner.addFocusListener(buttonNorthInnerFA); + AWTKeyAdapter buttonNorthInnerKA = new AWTKeyAdapter("ButtonNorthInner"); + buttonNorthInner.addKeyListener(buttonNorthInnerKA); Container container1 = new Container(); container1.setLayout(new BorderLayout()); container1.add(buttonNorthInner, BorderLayout.NORTH); @@ -125,6 +131,8 @@ public class TestFocus02SwingAWTRobot { Button buttonNorthOuter = new Button("north"); AWTFocusAdapter buttonNorthOuterFA = new AWTFocusAdapter("ButtonNorthOuter"); buttonNorthOuter.addFocusListener(buttonNorthOuterFA); + AWTKeyAdapter buttonNorthOuterKA = new AWTKeyAdapter("ButtonNorthOuter"); + buttonNorthOuter.addKeyListener(buttonNorthOuterKA); JPanel jPanel1 = new JPanel(); jPanel1.setLayout(new BorderLayout()); jPanel1.add(buttonNorthOuter, BorderLayout.NORTH); @@ -158,21 +166,22 @@ public class TestFocus02SwingAWTRobot { Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, buttonNorthOuter, buttonNorthOuter)); Assert.assertEquals(0, glWindow1FA.getCount()); Assert.assertEquals(0, newtCanvasAWTFA.getCount()); - Assert.assertEquals(1, buttonNorthOuterFA.getCount()); Assert.assertEquals(0, buttonNorthInnerFA.getCount()); Assert.assertEquals(0, jFrame1FA.getCount()); System.err.println("FOCUS AWT Button Outer sync"); + Assert.assertTrue(AWTRobotUtil.testKeyInput(robot, buttonNorthOuterKA)); // NEWT Focus Thread.sleep(100); // allow event sync System.err.println("FOCUS NEWT Canvas/GLWindow request"); Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild())); - Assert.assertEquals(1, glWindow1FA.getCount()); Assert.assertEquals(0, newtCanvasAWTFA.getCount()); Assert.assertEquals(0, buttonNorthInnerFA.getCount()); Assert.assertEquals(0, buttonNorthOuterFA.getCount()); Assert.assertEquals(0, jFrame1FA.getCount()); System.err.println("FOCUS NEWT Canvas/GLWindow sync"); + Assert.assertTrue(AWTRobotUtil.testKeyInput(robot, glWindow1KA)); + Assert.assertEquals("AWT parent canvas received keyboard events", 0, newtCanvasAWTKA.getCount()); // Button Inner Focus Thread.sleep(100); // allow event sync @@ -180,21 +189,22 @@ public class TestFocus02SwingAWTRobot { Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, buttonNorthInner, buttonNorthInner)); Assert.assertEquals(0, glWindow1FA.getCount()); Assert.assertEquals(0, newtCanvasAWTFA.getCount()); - Assert.assertEquals(1, buttonNorthInnerFA.getCount()); Assert.assertEquals(0, buttonNorthOuterFA.getCount()); Assert.assertEquals(0, jFrame1FA.getCount()); System.err.println("FOCUS AWT Button sync"); + Assert.assertTrue(AWTRobotUtil.testKeyInput(robot, buttonNorthInnerKA)); // NEWT Focus Thread.sleep(100); // allow event sync System.err.println("FOCUS NEWT Canvas/GLWindow request"); Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild())); - Assert.assertEquals(1, glWindow1FA.getCount()); Assert.assertEquals(0, newtCanvasAWTFA.getCount()); Assert.assertEquals(0, buttonNorthInnerFA.getCount()); Assert.assertEquals(0, buttonNorthOuterFA.getCount()); Assert.assertEquals(0, jFrame1FA.getCount()); System.err.println("FOCUS NEWT Canvas/GLWindow sync"); + Assert.assertTrue(AWTRobotUtil.testKeyInput(robot, glWindow1KA)); + Assert.assertEquals("AWT parent canvas received keyboard events", 0, newtCanvasAWTKA.getCount()); animator1.stop(); Assert.assertEquals(false, animator1.isAnimating()); diff --git a/src/junit/com/jogamp/test/junit/util/AWTRobotUtil.java b/src/junit/com/jogamp/test/junit/util/AWTRobotUtil.java index e0467a6ee..b654b2693 100644 --- a/src/junit/com/jogamp/test/junit/util/AWTRobotUtil.java +++ b/src/junit/com/jogamp/test/junit/util/AWTRobotUtil.java @@ -174,5 +174,39 @@ public class AWTRobotUtil { requestFocus(robot, requestFocus); return waitForFocus(waitForFocus); } + + /** + * @param keyTypedCounter shall return the number of keys typed (press + release) + * @return True if the object received 2 keys within TIME_OUT + */ + public static boolean testKeyInput(Robot robot, TestEventCountAdapter keyTypedCounter) + throws AWTException, InterruptedException, InvocationTargetException { + Component comp = null; + com.jogamp.newt.Window win = null; + + int c0 = keyTypedCounter.getCount(); + + if(null == robot) { + robot = new Robot(); + robot.setAutoWaitForIdle(true); + } + + robot.keyPress(java.awt.event.KeyEvent.VK_A); + robot.delay(50); + robot.keyRelease(java.awt.event.KeyEvent.VK_A); + robot.delay(50); + robot.keyPress(java.awt.event.KeyEvent.VK_B); + robot.delay(50); + robot.keyRelease(java.awt.event.KeyEvent.VK_B); + robot.delay(50); + + // Wait for the key events to be processed. + int wait; + for (wait=0; wait<10 && (keyTypedCounter.getCount()-c0)<2; wait++) { + Thread.sleep(TIME_OUT/10); + } + return wait<10; + } + } -- cgit v1.2.3