From 7c2916a999ece43ada42a1964bab3d6b886f3984 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 7 Oct 2010 17:08:54 +0200 Subject: Fix: NEWT/AWT Focus unit tests - Added reset() to EventCountAdapter, to ensure a unqiue start state - Removed 'lost focus' assertion, since this event might be pending and we don't poll on it or have a barrier --- .../test/junit/newt/TestFocus01SwingAWTRobot.java | 20 ++++++++- .../test/junit/newt/TestFocus02SwingAWTRobot.java | 33 +++++++++++++-- .../jogamp/test/junit/util/AWTFocusAdapter.java | 10 ++++- .../com/jogamp/test/junit/util/AWTKeyAdapter.java | 5 +++ .../jogamp/test/junit/util/AWTMouseAdapter.java | 5 +++ .../com/jogamp/test/junit/util/AWTRobotUtil.java | 45 ++++++++++---------- .../jogamp/test/junit/util/EventCountAdapter.java | 2 +- .../test/junit/util/EventCountAdapterUtil.java | 49 ++++++++++++++++++++++ .../jogamp/test/junit/util/NEWTFocusAdapter.java | 7 +++- .../com/jogamp/test/junit/util/NEWTKeyAdapter.java | 5 +++ .../jogamp/test/junit/util/NEWTMouseAdapter.java | 5 +++ 11 files changed, 154 insertions(+), 32 deletions(-) create mode 100644 src/junit/com/jogamp/test/junit/util/EventCountAdapterUtil.java (limited to 'src') diff --git a/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java b/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java index 514749459..97b37aec0 100644 --- a/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java +++ b/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java @@ -42,13 +42,17 @@ import java.awt.Robot; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.lang.reflect.InvocationTargetException; -import java.io.IOException; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.swing.JFrame; +import java.util.ArrayList; +import java.util.List; +import java.util.Iterator; +import java.io.IOException; + import org.junit.BeforeClass; import org.junit.Test; @@ -100,6 +104,8 @@ public class TestFocus01SwingAWTRobot extends UITestCase { private void testFocus01ProgrFocusImpl(Robot robot) throws AWTException, InvocationTargetException, InterruptedException { + ArrayList eventCountAdapters = new ArrayList(); + // Create a window. GLWindow glWindow1 = GLWindow.create(glCaps); glWindow1.setTitle("testNewtChildFocus"); @@ -107,10 +113,12 @@ public class TestFocus01SwingAWTRobot extends UITestCase { TestListenerCom01AWT.setDemoFields(demo1, glWindow1, false); glWindow1.addGLEventListener(demo1); NEWTFocusAdapter glWindow1FA = new NEWTFocusAdapter("GLWindow1"); + eventCountAdapters.add(glWindow1FA); glWindow1.addWindowListener(glWindow1FA); // Monitor NEWT focus and keyboard events. NEWTKeyAdapter glWindow1KA = new NEWTKeyAdapter("GLWindow1"); + eventCountAdapters.add(glWindow1KA); glWindow1.addKeyListener(glWindow1KA); // Wrap the window in a canvas. @@ -119,8 +127,10 @@ public class TestFocus01SwingAWTRobot extends UITestCase { // Monitor AWT focus and keyboard events. AWTKeyAdapter newtCanvasAWTKA = new AWTKeyAdapter("NewtCanvasAWT"); newtCanvasAWT.addKeyListener(newtCanvasAWTKA); + eventCountAdapters.add(newtCanvasAWTKA); AWTFocusAdapter newtCanvasAWTFA = new AWTFocusAdapter("NewtCanvasAWT"); newtCanvasAWT.addFocusListener(newtCanvasAWTFA); + eventCountAdapters.add(newtCanvasAWTFA); // Add the canvas to a frame, and make it all visible. JFrame frame1 = new JFrame("Swing AWT Parent Frame: " @@ -129,8 +139,10 @@ public class TestFocus01SwingAWTRobot extends UITestCase { Button button = new Button("Click me .."); AWTFocusAdapter buttonFA = new AWTFocusAdapter("Button"); button.addFocusListener(buttonFA); + eventCountAdapters.add(buttonFA); AWTKeyAdapter buttonKA = new AWTKeyAdapter("Button"); button.addKeyListener(buttonKA); + eventCountAdapters.add(buttonKA); frame1.getContentPane().add(button, BorderLayout.NORTH); frame1.setSize(width, height); frame1.setVisible(true); @@ -148,7 +160,9 @@ public class TestFocus01SwingAWTRobot extends UITestCase { // Button Focus Thread.sleep(100); // allow event sync System.err.println("FOCUS AWT Button request"); + EventCountAdapterUtil.reset(eventCountAdapters); Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, button, button)); + Assert.assertEquals(1, buttonFA.getCount()); Assert.assertEquals(0, glWindow1FA.getCount()); Assert.assertEquals(0, newtCanvasAWTFA.getCount()); System.err.println("FOCUS AWT Button sync"); @@ -157,9 +171,11 @@ public class TestFocus01SwingAWTRobot extends UITestCase { // 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"); + EventCountAdapterUtil.reset(eventCountAdapters); Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild())); + Assert.assertEquals(1, glWindow1FA.getCount()); Assert.assertEquals(0, newtCanvasAWTFA.getCount()); - Assert.assertEquals(0, buttonFA.getCount()); + // Assert.assertEquals(-1, buttonFA.getCount()); // lost focus System.err.println("FOCUS NEWT Canvas/GLWindow sync"); Assert.assertEquals(2, AWTRobotUtil.testKeyType(robot, 2, glWindow1, glWindow1KA)); Assert.assertEquals("AWT parent canvas received keyboard events", 0, newtCanvasAWTKA.getCount()); diff --git a/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java b/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java index dfca6c10b..a7602b49b 100644 --- a/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java +++ b/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java @@ -43,6 +43,9 @@ import java.awt.Robot; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; +import java.util.ArrayList; +import java.util.List; +import java.util.Iterator; import javax.media.opengl.*; @@ -85,6 +88,8 @@ public class TestFocus02SwingAWTRobot extends UITestCase { int x = 0; int y = 0; + ArrayList eventCountAdapters = new ArrayList(); + /** * JFrame . JPanel . Container . NewtCanvasAWT . GLWindow */ @@ -94,26 +99,35 @@ public class TestFocus02SwingAWTRobot extends UITestCase { glWindow1.addGLEventListener(demo1); NEWTFocusAdapter glWindow1FA = new NEWTFocusAdapter("GLWindow1"); glWindow1.addWindowListener(glWindow1FA); + eventCountAdapters.add(glWindow1FA); NEWTKeyAdapter glWindow1KA = new NEWTKeyAdapter("GLWindow1"); glWindow1.addKeyListener(glWindow1KA); + eventCountAdapters.add(glWindow1KA); NEWTMouseAdapter glWindow1MA = new NEWTMouseAdapter("GLWindow1"); glWindow1.addMouseListener(glWindow1MA); + eventCountAdapters.add(glWindow1MA); NewtCanvasAWT newtCanvasAWT = new NewtCanvasAWT(glWindow1); AWTFocusAdapter newtCanvasAWTFA = new AWTFocusAdapter("NewtCanvasAWT"); newtCanvasAWT.addFocusListener(newtCanvasAWTFA); + eventCountAdapters.add(newtCanvasAWTFA); AWTKeyAdapter newtCanvasAWTKA = new AWTKeyAdapter("NewtCanvasAWT"); newtCanvasAWT.addKeyListener(newtCanvasAWTKA); + eventCountAdapters.add(newtCanvasAWTKA); AWTMouseAdapter newtCanvasAWTMA = new AWTMouseAdapter("NewtCanvasAWT"); newtCanvasAWT.addMouseListener(newtCanvasAWTMA); + eventCountAdapters.add(newtCanvasAWTMA); Button buttonNorthInner = new Button("north"); AWTFocusAdapter buttonNorthInnerFA = new AWTFocusAdapter("ButtonNorthInner"); buttonNorthInner.addFocusListener(buttonNorthInnerFA); + eventCountAdapters.add(buttonNorthInnerFA); AWTKeyAdapter buttonNorthInnerKA = new AWTKeyAdapter("ButtonNorthInner"); buttonNorthInner.addKeyListener(buttonNorthInnerKA); + eventCountAdapters.add(buttonNorthInnerKA); AWTMouseAdapter buttonNorthInnerMA = new AWTMouseAdapter("ButtonNorthInner"); buttonNorthInner.addMouseListener(buttonNorthInnerMA); + eventCountAdapters.add(buttonNorthInnerMA); Container container1 = new Container(); container1.setLayout(new BorderLayout()); container1.add(buttonNorthInner, BorderLayout.NORTH); @@ -125,10 +139,13 @@ public class TestFocus02SwingAWTRobot extends UITestCase { Button buttonNorthOuter = new Button("north"); AWTFocusAdapter buttonNorthOuterFA = new AWTFocusAdapter("ButtonNorthOuter"); buttonNorthOuter.addFocusListener(buttonNorthOuterFA); + eventCountAdapters.add(buttonNorthOuterFA); AWTKeyAdapter buttonNorthOuterKA = new AWTKeyAdapter("ButtonNorthOuter"); buttonNorthOuter.addKeyListener(buttonNorthOuterKA); + eventCountAdapters.add(buttonNorthOuterKA); AWTMouseAdapter buttonNorthOuterMA = new AWTMouseAdapter("ButtonNorthOuter"); buttonNorthOuter.addMouseListener(buttonNorthOuterMA); + eventCountAdapters.add(buttonNorthOuterMA); JPanel jPanel1 = new JPanel(); jPanel1.setLayout(new BorderLayout()); jPanel1.add(buttonNorthOuter, BorderLayout.NORTH); @@ -159,7 +176,9 @@ public class TestFocus02SwingAWTRobot extends UITestCase { // Button Outer Focus Thread.sleep(100); // allow event sync System.err.println("FOCUS AWT Button Outer request"); + EventCountAdapterUtil.reset(eventCountAdapters); Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, buttonNorthOuter, buttonNorthOuter)); + Assert.assertEquals(1, buttonNorthOuterFA.getCount()); Assert.assertEquals(0, glWindow1FA.getCount()); Assert.assertEquals(0, newtCanvasAWTFA.getCount()); Assert.assertEquals(0, buttonNorthInnerFA.getCount()); @@ -174,10 +193,12 @@ public class TestFocus02SwingAWTRobot extends UITestCase { // NEWT Focus Thread.sleep(100); // allow event sync System.err.println("FOCUS NEWT Canvas/GLWindow request"); + EventCountAdapterUtil.reset(eventCountAdapters); 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(-1, buttonNorthOuterFA.getCount()); // lost focus Assert.assertEquals(0, jFrame1FA.getCount()); System.err.println("FOCUS NEWT Canvas/GLWindow sync"); Assert.assertEquals(2, AWTRobotUtil.testKeyType(robot, 2, glWindow1, glWindow1KA)); @@ -191,8 +212,10 @@ public class TestFocus02SwingAWTRobot extends UITestCase { // Button Inner Focus Thread.sleep(100); // allow event sync System.err.println("FOCUS AWT Button request"); + EventCountAdapterUtil.reset(eventCountAdapters); Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, buttonNorthInner, buttonNorthInner)); - Assert.assertEquals(0, glWindow1FA.getCount()); + Assert.assertEquals(1, buttonNorthInnerFA.getCount()); + // Assert.assertEquals(-1, glWindow1FA.getCount()); // lost focus Assert.assertEquals(0, newtCanvasAWTFA.getCount()); Assert.assertEquals(0, buttonNorthOuterFA.getCount()); Assert.assertEquals(0, jFrame1FA.getCount()); @@ -206,9 +229,11 @@ public class TestFocus02SwingAWTRobot extends UITestCase { // NEWT Focus Thread.sleep(100); // allow event sync System.err.println("FOCUS NEWT Canvas/GLWindow request"); + EventCountAdapterUtil.reset(eventCountAdapters); 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(-1, buttonNorthInnerFA.getCount()); // lost focus Assert.assertEquals(0, buttonNorthOuterFA.getCount()); Assert.assertEquals(0, jFrame1FA.getCount()); System.err.println("FOCUS NEWT Canvas/GLWindow sync"); @@ -237,7 +262,7 @@ public class TestFocus02SwingAWTRobot extends UITestCase { glWindow1.destroy(true); } -// @Test + @Test public void testFocus01ProgrFocus() throws AWTException, InterruptedException, InvocationTargetException { testFocus01ProgrFocusImpl(null); } diff --git a/src/junit/com/jogamp/test/junit/util/AWTFocusAdapter.java b/src/junit/com/jogamp/test/junit/util/AWTFocusAdapter.java index 6fa9d47a3..174152639 100644 --- a/src/junit/com/jogamp/test/junit/util/AWTFocusAdapter.java +++ b/src/junit/com/jogamp/test/junit/util/AWTFocusAdapter.java @@ -34,11 +34,12 @@ import java.awt.event.FocusListener; public class AWTFocusAdapter implements EventCountAdapter, FocusListener { String prefix; - int focusGained = 0; - boolean wasTemporary = false; + int focusGained; + boolean wasTemporary; public AWTFocusAdapter(String prefix) { this.prefix = prefix; + reset(); } /** @return the balance of focus gained/lost, ie should be 0 or 1 */ @@ -46,6 +47,11 @@ public class AWTFocusAdapter implements EventCountAdapter, FocusListener { return focusGained; } + public void reset() { + focusGained = 0; + wasTemporary = false; + } + /** @return true, if the last change was temporary */ public boolean getWasTemporary() { return wasTemporary; diff --git a/src/junit/com/jogamp/test/junit/util/AWTKeyAdapter.java b/src/junit/com/jogamp/test/junit/util/AWTKeyAdapter.java index 607ccc220..8e255acb6 100644 --- a/src/junit/com/jogamp/test/junit/util/AWTKeyAdapter.java +++ b/src/junit/com/jogamp/test/junit/util/AWTKeyAdapter.java @@ -35,12 +35,17 @@ public class AWTKeyAdapter extends java.awt.event.KeyAdapter implements EventCou public AWTKeyAdapter(String prefix) { this.prefix = prefix; + reset(); } public int getCount() { return keyTyped; } + public void reset() { + keyTyped = 0; + } + public void keyTyped(java.awt.event.KeyEvent e) { ++keyTyped; System.err.println("KEY AWT TYPED ["+keyTyped+"]: "+prefix+", "+e); diff --git a/src/junit/com/jogamp/test/junit/util/AWTMouseAdapter.java b/src/junit/com/jogamp/test/junit/util/AWTMouseAdapter.java index e841dc8e1..d78b3ed61 100644 --- a/src/junit/com/jogamp/test/junit/util/AWTMouseAdapter.java +++ b/src/junit/com/jogamp/test/junit/util/AWTMouseAdapter.java @@ -34,12 +34,17 @@ public class AWTMouseAdapter extends java.awt.event.MouseAdapter implements Even public AWTMouseAdapter(String prefix) { this.prefix = prefix; + reset(); } public int getCount() { return mouseClicked; } + public void reset() { + mouseClicked = 0; + } + public void mouseClicked(java.awt.event.MouseEvent e) { mouseClicked+=e.getClickCount(); System.err.println("MOUSE AWT CLICKED ["+mouseClicked+"]: "+prefix+", "+e); diff --git a/src/junit/com/jogamp/test/junit/util/AWTRobotUtil.java b/src/junit/com/jogamp/test/junit/util/AWTRobotUtil.java index a167766f5..48563b190 100644 --- a/src/junit/com/jogamp/test/junit/util/AWTRobotUtil.java +++ b/src/junit/com/jogamp/test/junit/util/AWTRobotUtil.java @@ -43,6 +43,8 @@ import javax.swing.JFrame; public class AWTRobotUtil { public static int TIME_OUT = 1000; // 1s + public static int ROBOT_DELAY = 50; // ms + public static int POLL_DIVIDER = 20; // TO/20 public static Point getCenterLocation(Object obj, boolean frameTitlebar) throws InterruptedException, InvocationTargetException { @@ -100,7 +102,7 @@ public class AWTRobotUtil { Point p0 = getCenterLocation(window, false); System.err.println("robot pos: "+p0); robot.mouseMove( (int) p0.getX(), (int) p0.getY() ); - robot.delay(50); + robot.delay(ROBOT_DELAY); final Window f_window = window; javax.swing.SwingUtilities.invokeAndWait(new Runnable() { @@ -109,14 +111,14 @@ public class AWTRobotUtil { f_window.toFront(); f_window.requestFocus(); }}); - robot.delay(200); + robot.delay(ROBOT_DELAY); KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); int wait; - for (wait=0; wait<10 && window != kfm.getFocusedWindow(); wait++) { - Thread.sleep(TIME_OUT/10); + for (wait=0; wait*/ adapters) { + for(Iterator i = adapters.iterator(); i.hasNext(); ) { + EventCountAdapter adapter = (EventCountAdapter) i.next(); + adapter.reset(); + } + } +} + diff --git a/src/junit/com/jogamp/test/junit/util/NEWTFocusAdapter.java b/src/junit/com/jogamp/test/junit/util/NEWTFocusAdapter.java index 48d2fd565..5cbd9fd15 100644 --- a/src/junit/com/jogamp/test/junit/util/NEWTFocusAdapter.java +++ b/src/junit/com/jogamp/test/junit/util/NEWTFocusAdapter.java @@ -34,16 +34,21 @@ import com.jogamp.newt.event.WindowEvent; public class NEWTFocusAdapter extends WindowAdapter implements EventCountAdapter { String prefix; - int focusGained = 0; + int focusGained; public NEWTFocusAdapter(String prefix) { this.prefix = prefix; + reset(); } public int getCount() { return focusGained; } + public void reset() { + focusGained = 0; + } + @Override public void windowGainedFocus(WindowEvent e) { ++focusGained; diff --git a/src/junit/com/jogamp/test/junit/util/NEWTKeyAdapter.java b/src/junit/com/jogamp/test/junit/util/NEWTKeyAdapter.java index ab00f4f61..73caa1846 100644 --- a/src/junit/com/jogamp/test/junit/util/NEWTKeyAdapter.java +++ b/src/junit/com/jogamp/test/junit/util/NEWTKeyAdapter.java @@ -38,12 +38,17 @@ public class NEWTKeyAdapter extends KeyAdapter implements EventCountAdapter { public NEWTKeyAdapter(String prefix) { this.prefix = prefix; + reset(); } public int getCount() { return keyTyped; } + public void reset() { + keyTyped = 0; + } + @Override public void keyTyped(KeyEvent e) { ++keyTyped; diff --git a/src/junit/com/jogamp/test/junit/util/NEWTMouseAdapter.java b/src/junit/com/jogamp/test/junit/util/NEWTMouseAdapter.java index 36943a805..fc5c653f2 100644 --- a/src/junit/com/jogamp/test/junit/util/NEWTMouseAdapter.java +++ b/src/junit/com/jogamp/test/junit/util/NEWTMouseAdapter.java @@ -38,12 +38,17 @@ public class NEWTMouseAdapter extends MouseAdapter implements EventCountAdapter public NEWTMouseAdapter(String prefix) { this.prefix = prefix; + reset(); } public int getCount() { return mouseClicked; } + public void reset() { + mouseClicked = 0; + } + public void mouseClicked(MouseEvent e) { mouseClicked+=e.getClickCount(); System.err.println("MOUSE NEWT CLICKED ["+mouseClicked+"]: "+prefix+", "+e); -- cgit v1.2.3