diff options
author | Sven Gothel <[email protected]> | 2011-05-01 06:40:46 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-05-01 06:40:46 +0200 |
commit | cf2ebbf3e584e7f7a15c323633a495b119ece37e (patch) | |
tree | 666ea397394763b5a0dda24614aa53e5db2d526a /src/test | |
parent | 05a11c5918f12e376d89b0fcb237e4afe356278c (diff) |
UI Tests/Robot: Attempt to stabilize UI tests (focus/input)
Focus: Always programmatic, clear global focus state upfront
Key/Mouse Input: Retry w/ optional focus request if lost ..
Dropped redundant robot delays, ie between keypress/release.
Diffstat (limited to 'src/test')
3 files changed, 241 insertions, 160 deletions
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 8be7d1c85..79196bd4c 100644 --- a/src/test/com/jogamp/opengl/test/junit/newt/TestFocus01SwingAWTRobot.java +++ b/src/test/com/jogamp/opengl/test/junit/newt/TestFocus01SwingAWTRobot.java @@ -119,6 +119,8 @@ public class TestFocus01SwingAWTRobot extends UITestCase { // Add the canvas to a frame, and make it all visible. JFrame frame1 = new JFrame("Swing AWT Parent Frame: " + glWindow1.getTitle()); + AWTFocusAdapter frame1FA = new AWTFocusAdapter("frame1"); + frame1.addFocusListener(frame1FA); frame1.getContentPane().add(newtCanvasAWT, BorderLayout.CENTER); Button button = new Button("Click me .."); AWTFocusAdapter buttonFA = new AWTFocusAdapter("Button"); @@ -141,29 +143,31 @@ public class TestFocus01SwingAWTRobot extends UITestCase { // Continuous animation .. Animator animator = new Animator(glWindow1); animator.start(); + AWTRobotUtil.assertRequestFocusAndWait(robot, frame1, frame1, frame1FA, null); // 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, buttonFA, null)); - Assert.assertEquals(1, buttonFA.getCount()); - Assert.assertEquals(0, glWindow1FA.getCount()); - Assert.assertEquals(0, newtCanvasAWTFA.getCount()); + AWTRobotUtil.assertRequestFocusAndWait(robot, button, button, buttonFA, null); + Assert.assertEquals(true, buttonFA.hasFocus()); + Assert.assertEquals(false, glWindow1FA.hasFocus()); + Assert.assertEquals(false, newtCanvasAWTFA.hasFocus()); System.err.println("FOCUS AWT Button sync"); - Assert.assertEquals(2, AWTRobotUtil.testKeyType(robot, 2, button, buttonKA)); + AWTRobotUtil.assertKeyType(robot, java.awt.event.KeyEvent.VK_A, 2, button, 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"); EventCountAdapterUtil.reset(eventCountAdapters); - Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild(), glWindow1FA, buttonFA)); - Assert.assertTrue(AWTRobotUtil.waitForCount(0, newtCanvasAWTFA)); - Assert.assertEquals(1, glWindow1FA.getCount()); - Assert.assertEquals(0, newtCanvasAWTFA.getCount()); - Assert.assertEquals(-1, buttonFA.getCount()); // lost focus + AWTRobotUtil.assertRequestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild(), glWindow1FA, buttonFA); + Assert.assertTrue(AWTRobotUtil.waitForFocusCount(false, newtCanvasAWTFA)); + Assert.assertEquals(true, glWindow1FA.hasFocus()); + Assert.assertEquals(false, newtCanvasAWTFA.hasFocus()); + Assert.assertEquals(false, buttonFA.hasFocus()); System.err.println("FOCUS NEWT Canvas/GLWindow sync"); - Assert.assertEquals(2, AWTRobotUtil.testKeyType(robot, 2, glWindow1, glWindow1KA)); + AWTRobotUtil.assertKeyType(robot, java.awt.event.KeyEvent.VK_A, 2, glWindow1, glWindow1KA); Assert.assertEquals("AWT parent canvas received keyboard events", 0, newtCanvasAWTKA.getCount()); // Remove listeners to avoid logging during dispose/destroy. 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 996ce9ca8..47d986fcb 100644 --- a/src/test/com/jogamp/opengl/test/junit/newt/TestFocus02SwingAWTRobot.java +++ b/src/test/com/jogamp/opengl/test/junit/newt/TestFocus02SwingAWTRobot.java @@ -172,82 +172,82 @@ public class TestFocus02SwingAWTRobot extends UITestCase { // Continuous animation .. Animator animator1 = new Animator(glWindow1); animator1.start(); + AWTRobotUtil.assertRequestFocusAndWait(robot, jFrame1, jFrame1, jFrame1FA, null); // 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, buttonNorthOuterFA, null)); - Assert.assertEquals(1, buttonNorthOuterFA.getCount()); - Assert.assertEquals(0, glWindow1FA.getCount()); - Assert.assertEquals(0, newtCanvasAWTFA.getCount()); - Assert.assertEquals(0, buttonNorthInnerFA.getCount()); - Assert.assertEquals(0, jFrame1FA.getCount()); + AWTRobotUtil.assertRequestFocusAndWait(robot, buttonNorthOuter, buttonNorthOuter, buttonNorthOuterFA, null); + Assert.assertEquals(true, buttonNorthOuterFA.hasFocus()); + Assert.assertEquals(false, glWindow1FA.hasFocus()); + Assert.assertEquals(false, newtCanvasAWTFA.hasFocus()); + Assert.assertEquals(false, buttonNorthInnerFA.hasFocus()); + Assert.assertEquals(false, jFrame1FA.hasFocus()); System.err.println("FOCUS AWT Button Outer sync"); - Assert.assertEquals(2, AWTRobotUtil.testKeyType(robot, 2, buttonNorthOuter, buttonNorthOuterKA)); - Assert.assertEquals(1, AWTRobotUtil.testMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 1, - buttonNorthOuter, buttonNorthOuterMA)); - Assert.assertEquals(3, AWTRobotUtil.testMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 2, - buttonNorthOuter, buttonNorthOuterMA)); + AWTRobotUtil.assertKeyType(robot, java.awt.event.KeyEvent.VK_A, 2, buttonNorthOuter, buttonNorthOuterKA); + AWTRobotUtil.assertMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 1, + buttonNorthOuter, buttonNorthOuterMA); + AWTRobotUtil.assertMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 2, + buttonNorthOuter, buttonNorthOuterMA); // 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(), glWindow1FA, buttonNorthOuterFA)); - Assert.assertTrue(AWTRobotUtil.waitForCount(0, newtCanvasAWTFA)); - Assert.assertEquals(1, glWindow1FA.getCount()); - Assert.assertEquals(0, newtCanvasAWTFA.getCount()); - Assert.assertEquals(0, buttonNorthInnerFA.getCount()); - Assert.assertEquals(-1, buttonNorthOuterFA.getCount()); // lost focus - Assert.assertEquals(0, jFrame1FA.getCount()); + AWTRobotUtil.assertRequestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild(), glWindow1FA, buttonNorthOuterFA); + Assert.assertTrue(AWTRobotUtil.waitForFocusCount(false, newtCanvasAWTFA)); + Assert.assertEquals(true, glWindow1FA.hasFocus()); + Assert.assertEquals(false, newtCanvasAWTFA.hasFocus()); + Assert.assertEquals(false, buttonNorthInnerFA.hasFocus()); + Assert.assertEquals(false, buttonNorthOuterFA.hasFocus()); + Assert.assertEquals(false, jFrame1FA.hasFocus()); System.err.println("FOCUS NEWT Canvas/GLWindow sync"); - Assert.assertEquals(2, AWTRobotUtil.testKeyType(robot, 2, glWindow1, glWindow1KA)); + AWTRobotUtil.assertKeyType(robot, java.awt.event.KeyEvent.VK_A, 2, glWindow1, glWindow1KA); Assert.assertEquals("AWT parent canvas received keyboard events", 0, newtCanvasAWTKA.getCount()); - Assert.assertEquals(1, AWTRobotUtil.testMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 1, - glWindow1, glWindow1MA)); - Assert.assertEquals(3, AWTRobotUtil.testMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 2, - glWindow1, glWindow1MA)); + AWTRobotUtil.assertMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 1, + glWindow1, glWindow1MA); + AWTRobotUtil.assertMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 2, + glWindow1, glWindow1MA); Assert.assertEquals("AWT parent canvas received mouse events", 0, newtCanvasAWTMA.getCount()); // 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, buttonNorthInnerFA, glWindow1FA)); - 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()); + AWTRobotUtil.assertRequestFocusAndWait(robot, buttonNorthInner, buttonNorthInner, buttonNorthInnerFA, glWindow1FA); + Assert.assertEquals(true, buttonNorthInnerFA.hasFocus()); + Assert.assertEquals(false, glWindow1FA.hasFocus()); + Assert.assertEquals(false, newtCanvasAWTFA.hasFocus()); + Assert.assertEquals(false, buttonNorthOuterFA.hasFocus()); + Assert.assertEquals(false, jFrame1FA.hasFocus()); System.err.println("FOCUS AWT Button sync"); - Assert.assertEquals(2, AWTRobotUtil.testKeyType(robot, 2, buttonNorthInner, buttonNorthInnerKA)); - Assert.assertEquals(1, AWTRobotUtil.testMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 1, - buttonNorthInner, buttonNorthInnerMA)); - Assert.assertEquals(3, AWTRobotUtil.testMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 2, - buttonNorthInner, buttonNorthInnerMA)); + AWTRobotUtil.assertKeyType(robot, java.awt.event.KeyEvent.VK_A, 2, buttonNorthInner, buttonNorthInnerKA); + AWTRobotUtil.assertMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 1, + buttonNorthInner, buttonNorthInnerMA); + AWTRobotUtil.assertMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 2, + buttonNorthInner, buttonNorthInnerMA); // 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(), glWindow1FA, buttonNorthInnerFA)); - Assert.assertTrue(AWTRobotUtil.waitForCount(0, newtCanvasAWTFA)); - Assert.assertEquals(1, glWindow1FA.getCount()); - Assert.assertEquals(0, newtCanvasAWTFA.getCount()); - Assert.assertEquals(-1, buttonNorthInnerFA.getCount()); // lost focus - Assert.assertEquals(0, buttonNorthOuterFA.getCount()); - Assert.assertEquals(0, jFrame1FA.getCount()); + AWTRobotUtil.assertRequestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild(), glWindow1FA, buttonNorthInnerFA); + Assert.assertTrue(AWTRobotUtil.waitForFocusCount(false, newtCanvasAWTFA)); + Assert.assertEquals(true, glWindow1FA.hasFocus()); + Assert.assertEquals(false, newtCanvasAWTFA.hasFocus()); + Assert.assertEquals(false, buttonNorthInnerFA.hasFocus()); + Assert.assertEquals(false, buttonNorthOuterFA.hasFocus()); + Assert.assertEquals(false, jFrame1FA.hasFocus()); System.err.println("FOCUS NEWT Canvas/GLWindow sync"); - Assert.assertEquals(2, AWTRobotUtil.testKeyType(robot, 2, glWindow1, glWindow1KA)); + AWTRobotUtil.assertKeyType(robot, java.awt.event.KeyEvent.VK_A, 2, glWindow1, glWindow1KA); Assert.assertEquals("AWT parent canvas received keyboard events", 0, newtCanvasAWTKA.getCount()); - Assert.assertEquals(1, AWTRobotUtil.testMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 1, - glWindow1, glWindow1MA)); - Assert.assertEquals(3, AWTRobotUtil.testMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 2, - glWindow1, glWindow1MA)); + AWTRobotUtil.assertMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 1, + glWindow1, glWindow1MA); + AWTRobotUtil.assertMouseClick(robot, java.awt.event.InputEvent.BUTTON1_MASK, 2, + glWindow1, glWindow1MA); Assert.assertEquals("AWT parent canvas received mouse events", 0, newtCanvasAWTMA.getCount()); - animator1.stop(); Assert.assertEquals(false, animator1.isAnimating()); @@ -284,7 +284,9 @@ public class TestFocus02SwingAWTRobot extends UITestCase { return i; } - public static void main(String args[]) throws IOException { + public static void main(String args[]) + throws IOException, AWTException, InterruptedException, InvocationTargetException + { for(int i=0; i<args.length; i++) { if(args[i].equals("-time")) { durationPerTest = atoi(args[++i]); @@ -294,8 +296,16 @@ public class TestFocus02SwingAWTRobot extends UITestCase { } System.err.println("durationPerTest "+durationPerTest); System.err.println("waitReparent "+waitReparent); - String tstname = TestFocus02SwingAWTRobot.class.getName(); - org.junit.runner.JUnitCore.main(tstname); + if(true) { + String tstname = TestFocus02SwingAWTRobot.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } else { + TestFocus02SwingAWTRobot.initClass(); + TestFocus02SwingAWTRobot test = new TestFocus02SwingAWTRobot(); + test.testFocus01ProgrFocus(); + test.testFocus02RobotFocus(); + TestFocus02SwingAWTRobot.release(); + } } } 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 d0f9172bc..07f25ec40 100644 --- a/src/test/com/jogamp/opengl/test/junit/util/AWTRobotUtil.java +++ b/src/test/com/jogamp/opengl/test/junit/util/AWTRobotUtil.java @@ -40,16 +40,23 @@ import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.event.InputEvent; + import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; +import org.junit.Assert; + 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 + static final boolean DEBUG = false; + + public static int RETRY_NUMBER = 5; + public static int ROBOT_DELAY = 100; // ms + public static int TIME_OUT = 1000; // 1s + public static int POLL_DIVIDER = 20; // TO/20 + public static int TIME_SLICE = TIME_OUT / POLL_DIVIDER ; - public static Point getCenterLocation(Object obj, boolean frameTitlebar) + public static Point getCenterLocation(Object obj) throws InterruptedException, InvocationTargetException { Component comp = null; com.jogamp.newt.Window win = null; @@ -66,7 +73,7 @@ public class AWTRobotUtil { if(null!=comp) { Point p0 = comp.getLocationOnScreen(); Rectangle r0 = comp.getBounds(); - if( frameTitlebar && comp instanceof JFrame ) { + if( comp instanceof JFrame ) { JFrame jFrame = (JFrame) comp; Container cont = jFrame.getContentPane(); Point p1 = cont.getLocationOnScreen(); @@ -102,7 +109,7 @@ public class AWTRobotUtil { robot = new Robot(); robot.setAutoWaitForIdle(true); } - Point p0 = getCenterLocation(window, false); + Point p0 = getCenterLocation(window); System.err.println("robot pos: "+p0); robot.mouseMove( (int) p0.getX(), (int) p0.getY() ); robot.delay(ROBOT_DELAY); @@ -118,7 +125,7 @@ public class AWTRobotUtil { KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); int wait; for (wait=0; wait<POLL_DIVIDER && window != kfm.getFocusedWindow(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } return wait<POLL_DIVIDER; } @@ -134,7 +141,7 @@ public class AWTRobotUtil { robot.setAutoWaitForIdle(true); } - Point p0 = getCenterLocation(obj, false); + Point p0 = getCenterLocation(obj); System.err.println("robot pos: "+p0); robot.mouseMove( (int) p0.getX(), (int) p0.getY() ); @@ -147,37 +154,41 @@ public class AWTRobotUtil { */ public static void requestFocus(Robot robot, Object obj) throws AWTException, InterruptedException, InvocationTargetException { - Component comp = null; - com.jogamp.newt.Window win = null; + final Component comp; + final com.jogamp.newt.Window win; + KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner(); + if(obj instanceof com.jogamp.newt.Window) { win = (com.jogamp.newt.Window) obj; + comp = null; } else if(obj instanceof Component) { + win = null; comp = (Component) obj; } else { throw new RuntimeException("Neither AWT nor NEWT: "+obj); } - - if(null == robot) { - if(null!=comp) { - final Component f_comp = comp; - javax.swing.SwingUtilities.invokeAndWait(new Runnable() { - public void run() { - f_comp.requestFocus(); - }}); - } else { - win.requestFocus(); - } - return; + + if(null!=comp) { + javax.swing.SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + comp.requestFocus(); + }}); + } else { + win.requestFocus(); } + } - centerMouse(robot, obj); - - robot.delay(ROBOT_DELAY); - robot.mousePress(InputEvent.BUTTON1_MASK); - robot.delay(ROBOT_DELAY); - robot.mouseRelease(InputEvent.BUTTON1_MASK); - robot.delay(ROBOT_DELAY); + public static boolean hasFocus(Object obj) { + if(obj instanceof Component) { + final Component comp = (Component) obj; + final KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); + return comp == kfm.getPermanentFocusOwner(); + } else if(obj instanceof com.jogamp.newt.Window) { + return ((com.jogamp.newt.Window) obj).hasFocus(); + } else { + throw new RuntimeException("Neither AWT nor NEWT: "+obj); + } } /** @@ -187,15 +198,15 @@ public class AWTRobotUtil { public static boolean waitForFocus(Object obj) throws InterruptedException { int wait; if(obj instanceof Component) { - Component comp = (Component) obj; - KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); + final Component comp = (Component) obj; + final KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); for (wait=0; wait<POLL_DIVIDER && comp != kfm.getPermanentFocusOwner(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } } else if(obj instanceof com.jogamp.newt.Window) { - com.jogamp.newt.Window win = (com.jogamp.newt.Window) obj; + final com.jogamp.newt.Window win = (com.jogamp.newt.Window) obj; for (wait=0; wait<POLL_DIVIDER && !win.hasFocus(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } } else { throw new RuntimeException("Neither AWT nor NEWT: "+obj); @@ -207,49 +218,76 @@ public class AWTRobotUtil { * * @return True if the Window became the global focused Window within TIME_OUT */ - public static boolean waitForFocus(Object obj, int gainT0, EventCountAdapter gain, - int lostT0, EventCountAdapter lost) throws InterruptedException { + public static boolean waitForFocus(Object obj, FocusEventCountAdapter gain, + FocusEventCountAdapter lost) throws InterruptedException { if(!waitForFocus(obj)) { return false; } + if(null == gain) { + return true; + } + int wait; for (wait=0; wait<POLL_DIVIDER; wait++) { - int gainT1 = gain.getCount(); - int lostT1 = (null!=lost) ? lost.getCount() : -1; - if(gainT1-gainT0==1 && lostT1-lostT0==-1) { + if( ( null == lost || !lost.hasFocus() ) && gain.hasFocus() ) { return true; } - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } return false; } - public static boolean requestFocusAndWait(Robot robot, Object requestFocus, Object waitForFocus) + public static void assertRequestFocusAndWait(Robot robot, Object requestFocus, Object waitForFocus, + FocusEventCountAdapter gain, FocusEventCountAdapter lost) throws AWTException, InterruptedException, InvocationTargetException { - requestFocus(robot, requestFocus); - return waitForFocus(waitForFocus); + int i = 0; + boolean hasFocus = false; + + for(i=0; i < RETRY_NUMBER && !hasFocus; i++) { + requestFocus(robot, requestFocus); + hasFocus = waitForFocus(waitForFocus, gain, lost); + } + Assert.assertTrue("Did not gain focus", hasFocus); } - public static boolean requestFocusAndWait(Robot robot, Object requestFocus, Object waitForFocus, - EventCountAdapter gain, EventCountAdapter lost) - throws AWTException, InterruptedException, InvocationTargetException { - - int gainT0 = gain.getCount(); - int lostT0 = (null!=lost) ? lost.getCount() : 0; - - requestFocus(robot, requestFocus); - return waitForFocus(waitForFocus, gainT0, gain, lostT0, lost); + static int keyType(int i, Robot robot, int keyCode, + Object obj, InputEventCountAdapter counter) throws InterruptedException, AWTException, InvocationTargetException + { + int tc = 0; + int j; + final long t0 = System.currentTimeMillis(); + + for(j=0; 1 > tc && j<RETRY_NUMBER; j++) { + if(!hasFocus(obj)) { + // focus lost for some reason, regain it programmatic + if(DEBUG) { System.err.println(i+":"+j+" KC1.0: "+counter+" - regain focus"); } + requestFocus(null, obj); + } + final int c0 = counter.getCount(); + if(DEBUG) { System.err.println(i+":"+j+" KC1.1: "+counter); } + robot.waitForIdle(); + robot.keyPress(keyCode); + robot.keyRelease(keyCode); + if(DEBUG) { System.err.println(i+":"+j+" KC1.2: "+counter); } + tc = counter.getCount() - c0; + for (int wait=0; wait<POLL_DIVIDER && 1 > tc; wait++) { + robot.delay(TIME_SLICE); + tc = counter.getCount() - c0; + } + if(DEBUG) { System.err.println(i+":"+j+" KC1.X: tc "+tc+", "+counter); } + } + Assert.assertEquals("Key ("+i+":"+j+") not typed one time", 1, tc); + return (int) ( System.currentTimeMillis() - t0 ) ; } - + /** - * @param keyTypedCounter shall return the number of keys typed (press + release) - * @return True if typeCount keys within TIME_OUT has been received + * @param keyCode TODO + * @param counter shall return the number of keys typed (press + release) */ - public static int testKeyType(Robot robot, int typeCount, Object obj, EventCountAdapter keyTypedCounter) + public static void assertKeyType(Robot robot, int keyCode, int typeCount, + Object obj, InputEventCountAdapter counter) throws AWTException, InterruptedException, InvocationTargetException { - Component comp = null; - com.jogamp.newt.Window win = null; if(null == robot) { robot = new Robot(); @@ -258,30 +296,59 @@ public class AWTRobotUtil { centerMouse(robot, obj); - int c0 = keyTypedCounter.getCount(); + Assert.assertEquals("Key already pressed", false, counter.isPressed()); + + if(DEBUG) { + System.err.println("**************************************"); + System.err.println("KC0: "+counter); + } + + final int c0 = counter.getCount(); for(int i=0; i<typeCount; i++) { - robot.keyPress(java.awt.event.KeyEvent.VK_A); - robot.delay(ROBOT_DELAY); - robot.keyRelease(java.awt.event.KeyEvent.VK_A); - robot.delay(ROBOT_DELAY); + keyType(i, robot, keyCode, obj, counter); } - // Wait for the key events to be processed. - int wait; - for (wait=0; wait<POLL_DIVIDER && (keyTypedCounter.getCount()-c0)<typeCount; wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); - } - return keyTypedCounter.getCount()-c0; + if(DEBUG) { System.err.println("KC3.0: "+counter); } + Assert.assertEquals("Wrong key count", typeCount, counter.getCount()-c0); } + static int mouseClick(int i, Robot robot, int mouseButton, + Object obj, InputEventCountAdapter counter) throws InterruptedException, AWTException, InvocationTargetException + { + int j; + int tc = 0; + final long t0 = System.currentTimeMillis(); + + for(j=0; 1 > tc && j<RETRY_NUMBER; j++) { + if(!hasFocus(obj)) { + // focus lost for some reason, regain it programmatic + if(DEBUG) { System.err.println(i+":"+j+" MC1.0: "+counter+" - regain focus"); } + requestFocus(null, obj); + } + final int c0 = counter.getCount(); + if(DEBUG) { System.err.println(i+":"+j+" MC1.1: "+counter); } + robot.waitForIdle(); + robot.mousePress(mouseButton); + robot.mouseRelease(mouseButton); + if(DEBUG) { System.err.println(i+":"+j+" MC1.2: "+counter); } + tc = counter.getCount() - c0; + for (int wait=0; wait<POLL_DIVIDER && 1 > tc; wait++) { + robot.delay(TIME_SLICE); + tc = counter.getCount() - c0; + } + if(DEBUG) { System.err.println(i+":"+j+" MC1.X: tc "+tc+", "+counter); } + } + Assert.assertEquals("Mouse ("+i+":"+j+") not clicked one time", 1, tc); + return (int) ( System.currentTimeMillis() - t0 ) ; + } + /** * @param mouseButton ie InputEvent.BUTTON1_MASK * @param clickCount ie 1, or 2 - * @return True if the desired clickCount within TIME_OUT has been received */ - public static int testMouseClick(Robot robot, int mouseButton, int clickCount, - Object obj, EventCountAdapter mouseClickCounter) + public static void assertMouseClick(Robot robot, int mouseButton, int clickCount, + Object obj, InputEventCountAdapter counter) throws AWTException, InterruptedException, InvocationTargetException { if(null == robot) { @@ -293,35 +360,35 @@ public class AWTRobotUtil { centerMouse(robot, obj); - robot.delay(2*clickTO); - - int c0 = mouseClickCounter.getCount(); - + Assert.assertEquals("Mouse already pressed", false, counter.isPressed()); + + if(DEBUG) { + System.err.println("**************************************"); + System.err.println("MC0: "+counter); + } + + final int c0 = counter.getCount(); + for(int i=0; i<clickCount; i++) { - robot.mousePress(mouseButton); - robot.delay(clickTO/4); - robot.mouseRelease(mouseButton); - robot.delay(clickTO/4); + final int waited = mouseClick(i, robot, mouseButton, obj, counter); + if(DEBUG) { System.err.println(i+": MC2.X: "+counter+", consumed: "+waited); } + robot.delay( clickTO + 1 ); } - // Wait for the key events to be processed. - int wait; - for (wait=0; wait<POLL_DIVIDER && (mouseClickCounter.getCount()-c0)<clickCount; wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); - } - return mouseClickCounter.getCount()-c0; + if(DEBUG) { System.err.println("MC3.0: "+counter); } + Assert.assertEquals("Wrong mouse click count", clickCount, counter.getCount() - c0); } /** * - * @return True if the EventCountAdapter became the desired value within TIME_OUT + * @return True if the FocusEventCountAdapter became the desired value within TIME_OUT */ - public static boolean waitForCount(int desired, EventCountAdapter eca) throws InterruptedException { + public static boolean waitForFocusCount(boolean desired, FocusEventCountAdapter eca) throws InterruptedException { for (int wait=0; wait<POLL_DIVIDER; wait++) { - if( eca.getCount() == desired ) { + if( eca.hasFocus() == desired ) { return true; } - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } return false; } @@ -335,12 +402,12 @@ public class AWTRobotUtil { if(obj instanceof Component) { Component comp = (Component) obj; for (wait=0; wait<POLL_DIVIDER && visible != comp.isVisible(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } } else if(obj instanceof com.jogamp.newt.Window) { com.jogamp.newt.Window win = (com.jogamp.newt.Window) obj; for (wait=0; wait<POLL_DIVIDER && visible != win.isVisible(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } } else { throw new RuntimeException("Neither AWT nor NEWT: "+obj); @@ -357,31 +424,31 @@ public class AWTRobotUtil { if (obj instanceof Component) { Component comp = (Component) obj; for (wait=0; wait<POLL_DIVIDER && realized != comp.isDisplayable(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } // if GLCanvas, ensure it got also painted -> drawable.setRealized(true); if(wait<POLL_DIVIDER && comp instanceof GLCanvas) { GLCanvas glcanvas = (GLCanvas) comp; for (wait=0; wait<POLL_DIVIDER && realized != glcanvas.isRealized(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } if(wait>=POLL_DIVIDER) { // for some reason GLCanvas hasn't been painted yet, force it! System.err.println("XXX: FORCE REPAINT PRE - canvas: "+glcanvas); glcanvas.repaint(); for (wait=0; wait<POLL_DIVIDER && realized != glcanvas.isRealized(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } System.err.println("XXX: FORCE REPAINT POST - canvas: "+glcanvas); } for (wait=0; wait<POLL_DIVIDER && realized != glcanvas.isRealized(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } } } else if(obj instanceof com.jogamp.newt.Window) { com.jogamp.newt.Window win = (com.jogamp.newt.Window) obj; for (wait=0; wait<POLL_DIVIDER && realized != win.isNativeValid(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } } else { throw new RuntimeException("Neither AWT nor NEWT: "+obj); @@ -414,11 +481,11 @@ public class AWTRobotUtil { } int wait; for (wait=0; wait<POLL_DIVIDER && !closingListener.isWindowClosing(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } if(wait<POLL_DIVIDER && willClose) { for (wait=0; wait<POLL_DIVIDER && !closingListener.isWindowClosed(); wait++) { - Thread.sleep(TIME_OUT/POLL_DIVIDER); + Thread.sleep(TIME_SLICE); } } return wait<POLL_DIVIDER; |