From 6fac0b835c2c775f0693246efdf098191b3961ad Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Tue, 28 Sep 2010 04:50:40 +0300
Subject: NEWT Unit Focus Test: Use AWT permanent focus check on request.

---
 make/scripts/tests.sh                              |  6 +--
 .../test/junit/newt/TestFocus01SwingAWTRobot.java  | 19 ++--------
 .../test/junit/newt/TestFocus02SwingAWTRobot.java  | 39 +++-----------------
 .../jogamp/test/junit/util/AWTFocusAdapter.java    | 13 ++++++-
 .../com/jogamp/test/junit/util/AWTRobotUtil.java   | 43 +++++++++++++++++++++-
 5 files changed, 64 insertions(+), 56 deletions(-)

diff --git a/make/scripts/tests.sh b/make/scripts/tests.sh
index ab5320f07..e391698fb 100755
--- a/make/scripts/tests.sh
+++ b/make/scripts/tests.sh
@@ -27,9 +27,9 @@ spath=`dirname $0`
 # $spath/java-run-all.sh ../build-x86_64 com.jogamp.test.junit.newt.parenting.TestParenting01cSwingAWT $*
 # $spath/java-run-all.sh ../build-x86_64 com.jogamp.test.junit.newt.parenting.TestParenting02AWT $*
 
-$spath/java-run-all.sh ../build-x86_64 com.jogamp.test.junit.jogl.awt.TestSwingAWTUsageBeforeJOGLInitBug411 $*
+# $spath/java-run-all.sh ../build-x86_64 com.jogamp.test.junit.jogl.awt.TestSwingAWTRobotUsageBeforeJOGLInitBug411 $*
 
-# $spath/java-run-all.sh ../build-x86_64 com.jogamp.test.junit.newt.TestFocus01SwingAWT $*
-# $spath/java-run-all.sh ../build-x86_64 com.jogamp.test.junit.newt.TestFocus02SwingAWT $*
+# $spath/java-run-all.sh ../build-x86_64 com.jogamp.test.junit.newt.TestFocus01SwingAWTRobot $*
+$spath/java-run-all.sh ../build-x86_64 com.jogamp.test.junit.newt.TestFocus02SwingAWTRobot $*
 
 $spath/count-edt-start.sh java-run.log
diff --git a/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java b/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java
index cdba796e8..ccc66b85a 100644
--- a/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java
+++ b/src/junit/com/jogamp/test/junit/newt/TestFocus01SwingAWTRobot.java
@@ -128,7 +128,7 @@ public class TestFocus01SwingAWTRobot {
         frame1.getContentPane().add(button, BorderLayout.NORTH);
         frame1.setSize(width, height);
         frame1.setVisible(true);
-        AWTRobotUtil.toFront(robot, frame1);
+        Assert.assertTrue(AWTRobotUtil.toFront(robot, frame1));
 
         int wait=0;
         while(wait<10 && glWindow1.getTotalFrames()<1) { Thread.sleep(100); wait++; }
@@ -142,13 +142,7 @@ public class TestFocus01SwingAWTRobot {
         // Button Focus
         Thread.sleep(100); // allow event sync
         System.err.println("FOCUS AWT  Button request");
-        AWTRobotUtil.requestFocus(robot, button);
-        for (wait=0; wait<10 && !button.hasFocus(); wait++) {
-            Thread.sleep(100);
-        }
-        Assert.assertTrue(button.hasFocus());
-        Assert.assertFalse(newtCanvasAWT.getNEWTChild().hasFocus());
-        Assert.assertFalse(newtCanvasAWT.hasFocus());
+        Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, button, button));
         Assert.assertEquals(0, glWindow1FA.getCount());
         Assert.assertEquals(0, newtCanvasAWTFA.getCount());
         Assert.assertEquals(1, buttonFA.getCount());
@@ -157,14 +151,7 @@ public class TestFocus01SwingAWTRobot {
         // 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");
-        AWTRobotUtil.requestFocus(robot, newtCanvasAWT);
-        for (wait=0; wait<10 && !newtCanvasAWT.getNEWTChild().hasFocus(); wait++) {
-            Thread.sleep(100);
-        }
-        // Verify focus status.
-        Assert.assertFalse("AWT parent canvas has focus", newtCanvasAWT.hasFocus());
-        Assert.assertTrue(newtCanvasAWT.getNEWTChild().hasFocus());
-        Assert.assertFalse(button.hasFocus());
+        Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild()));
         Assert.assertEquals(1, glWindow1FA.getCount());
         Assert.assertEquals(0, newtCanvasAWTFA.getCount());
         Assert.assertEquals(0, buttonFA.getCount());
diff --git a/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java b/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java
index 7e6c34bd1..379672cb2 100644
--- a/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java
+++ b/src/junit/com/jogamp/test/junit/newt/TestFocus02SwingAWTRobot.java
@@ -141,7 +141,7 @@ public class TestFocus02SwingAWTRobot {
         jFrame1.setContentPane(jPanel1);
         jFrame1.setSize(width, height);
         jFrame1.setVisible(true); // from here on, we need to run modifications on EDT
-        AWTRobotUtil.toFront(robot, jFrame1);
+        Assert.assertTrue(AWTRobotUtil.toFront(robot, jFrame1));
 
         int wait=0;
         while(wait<10 && glWindow1.getTotalFrames()<1) { Thread.sleep(100); wait++; }
@@ -155,13 +155,7 @@ public class TestFocus02SwingAWTRobot {
         // Button Outer Focus
         Thread.sleep(100); // allow event sync
         System.err.println("FOCUS AWT  Button Outer request");
-        AWTRobotUtil.requestFocus(robot, buttonNorthOuter);
-        for (wait=0; wait<10 && !buttonNorthOuter.hasFocus(); wait++) {
-            Thread.sleep(100);
-        }
-        Assert.assertTrue(buttonNorthOuter.hasFocus());
-        Assert.assertFalse(newtCanvasAWT.getNEWTChild().hasFocus());
-        Assert.assertFalse(newtCanvasAWT.hasFocus());
+        Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, buttonNorthOuter, buttonNorthOuter));
         Assert.assertEquals(0, glWindow1FA.getCount());
         Assert.assertEquals(0, newtCanvasAWTFA.getCount());
         Assert.assertEquals(1, buttonNorthOuterFA.getCount());
@@ -172,15 +166,7 @@ public class TestFocus02SwingAWTRobot {
         // NEWT Focus
         Thread.sleep(100); // allow event sync
         System.err.println("FOCUS NEWT Canvas/GLWindow request");
-        AWTRobotUtil.requestFocus(robot, newtCanvasAWT);
-        for (wait=0; wait<10 && !newtCanvasAWT.getNEWTChild().hasFocus(); wait++) {
-            Thread.sleep(100);
-        }
-        Assert.assertTrue(newtCanvasAWT.getNEWTChild().hasFocus());
-        Assert.assertFalse("AWT Frame has focus", jFrame1.hasFocus());
-        Assert.assertFalse("AWT Button Inner has focus", buttonNorthInner.hasFocus());
-        Assert.assertFalse("AWT Button Outer has focus", buttonNorthOuter.hasFocus());
-        Assert.assertFalse("AWT parent canvas has focus", newtCanvasAWT.hasFocus());
+        Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild()));
         Assert.assertEquals(1, glWindow1FA.getCount());
         Assert.assertEquals(0, newtCanvasAWTFA.getCount());
         Assert.assertEquals(0, buttonNorthInnerFA.getCount());
@@ -191,14 +177,7 @@ public class TestFocus02SwingAWTRobot {
         // Button Inner Focus
         Thread.sleep(100); // allow event sync
         System.err.println("FOCUS AWT  Button request");
-        AWTRobotUtil.requestFocus(robot, buttonNorthInner);
-        for (wait=0; wait<10 && !buttonNorthInner.hasFocus(); wait++) {
-            Thread.sleep(100);
-        }
-        Assert.assertTrue(buttonNorthInner.hasFocus());
-        Assert.assertFalse(buttonNorthOuter.hasFocus());
-        Assert.assertFalse(newtCanvasAWT.getNEWTChild().hasFocus());
-        Assert.assertFalse(newtCanvasAWT.hasFocus());
+        Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, buttonNorthInner, buttonNorthInner));
         Assert.assertEquals(0, glWindow1FA.getCount());
         Assert.assertEquals(0, newtCanvasAWTFA.getCount());
         Assert.assertEquals(1, buttonNorthInnerFA.getCount());
@@ -209,15 +188,7 @@ public class TestFocus02SwingAWTRobot {
         // NEWT Focus
         Thread.sleep(100); // allow event sync
         System.err.println("FOCUS NEWT Canvas/GLWindow request");
-        AWTRobotUtil.requestFocus(robot, newtCanvasAWT);
-        for (wait=0; wait<10 && !newtCanvasAWT.getNEWTChild().hasFocus(); wait++) {
-            Thread.sleep(100);
-        }
-        Assert.assertTrue(newtCanvasAWT.getNEWTChild().hasFocus());
-        Assert.assertFalse("AWT Frame has focus", jFrame1.hasFocus());
-        Assert.assertFalse("AWT Button has focus", buttonNorthInner.hasFocus());
-        Assert.assertFalse("AWT Button Outer has focus", buttonNorthOuter.hasFocus());
-        Assert.assertFalse("AWT parent canvas has focus", newtCanvasAWT.hasFocus());
+        Assert.assertTrue(AWTRobotUtil.requestFocusAndWait(robot, newtCanvasAWT, newtCanvasAWT.getNEWTChild()));
         Assert.assertEquals(1, glWindow1FA.getCount());
         Assert.assertEquals(0, newtCanvasAWTFA.getCount());
         Assert.assertEquals(0, buttonNorthInnerFA.getCount());
diff --git a/src/junit/com/jogamp/test/junit/util/AWTFocusAdapter.java b/src/junit/com/jogamp/test/junit/util/AWTFocusAdapter.java
index ed8f4b323..0fee699e7 100644
--- a/src/junit/com/jogamp/test/junit/util/AWTFocusAdapter.java
+++ b/src/junit/com/jogamp/test/junit/util/AWTFocusAdapter.java
@@ -35,24 +35,33 @@ public class AWTFocusAdapter implements TestEventCountAdapter, FocusListener {
 
     String prefix;
     int focusGained = 0;
+    boolean wasTemporary = false;
 
     public AWTFocusAdapter(String prefix) {
         this.prefix = prefix;
     }
 
+    /** @return the balance of focus gained/lost, ie should be 0 or 1 */
     public int getCount() {
         return focusGained;
     }
 
+    /** @return true, if the last change was temporary */
+    public boolean getWasTemporary() {
+        return wasTemporary;
+    }
+
     @Override 
     public void focusGained(FocusEvent e) {
         ++focusGained;
-        System.err.println("FOCUS AWT  GAINED ["+focusGained+"]: "+prefix+", "+e);
+        wasTemporary = e.isTemporary();
+        System.err.println("FOCUS AWT  GAINED "+(wasTemporary?"TEMP":"PERM")+" ["+focusGained+"]: "+prefix+", "+e);
     }
 
     @Override
     public void focusLost(FocusEvent e) {
         --focusGained;
-        System.err.println("FOCUS AWT  LOST   ["+focusGained+"]: "+prefix+", "+e);
+        wasTemporary = e.isTemporary();
+        System.err.println("FOCUS AWT  LOST   "+(wasTemporary?"TEMP":"PERM")+" ["+focusGained+"]: "+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 ba11df4d6..e0467a6ee 100644
--- a/src/junit/com/jogamp/test/junit/util/AWTRobotUtil.java
+++ b/src/junit/com/jogamp/test/junit/util/AWTRobotUtil.java
@@ -32,6 +32,7 @@ import java.lang.reflect.InvocationTargetException;
 import java.awt.AWTException;
 import java.awt.Component;
 import java.awt.Container;
+import java.awt.KeyboardFocusManager;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.Robot;
@@ -41,12 +42,16 @@ import javax.swing.JFrame;
 
 public class AWTRobotUtil {
 
+    public static int TIME_OUT = 1000; // 1s
+
     /**
      * toFront, call setVisible(true) and toFront(),
      * after positioning the mouse in the middle of the window via robot.
      * If the given robot is null, a new one is created (waitForIdle=true).
+     *
+     * @return True if the Window became the global focused Window within TIME_OUT
      */
-    public static void toFront(Robot robot, Window window) 
+    public static boolean toFront(Robot robot, Window window) 
         throws AWTException, InterruptedException, InvocationTargetException {
 
         if(null == robot) {
@@ -71,6 +76,13 @@ public class AWTRobotUtil {
                 f_window.requestFocus();
             }});
         robot.delay(200);
+
+        KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
+        int wait;
+        for (wait=0; wait<10 && window != kfm.getFocusedWindow(); wait++) {
+            Thread.sleep(TIME_OUT/10);
+        }
+        return wait<10;
     }
 
     /**
@@ -133,5 +145,34 @@ public class AWTRobotUtil {
         robot.delay(50);
     }
 
+    /**
+     *
+     * @return True if the Window became the global focused Window within TIME_OUT
+     */
+    public static boolean waitForFocus(Object obj) throws InterruptedException {
+        int wait;
+        if(obj instanceof Component) {
+            Component comp = (Component) obj;
+            KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
+            for (wait=0; wait<10 && comp != kfm.getPermanentFocusOwner(); wait++) {
+                Thread.sleep(TIME_OUT/10);
+            }
+        } else if(obj instanceof com.jogamp.newt.Window) {
+            com.jogamp.newt.Window win = (com.jogamp.newt.Window) obj;
+            for (wait=0; wait<10 && !win.hasFocus(); wait++) {
+                Thread.sleep(TIME_OUT/10);
+            }
+        } else {
+            throw new RuntimeException("Neither AWT nor NEWT: "+obj);
+        }
+        return wait<10;
+    }
+
+    public static boolean requestFocusAndWait(Robot robot, Object requestFocus, Object waitForFocus)
+        throws InterruptedException, InvocationTargetException {
+
+        requestFocus(robot, requestFocus);
+        return waitForFocus(waitForFocus);
+    }
 }
 
-- 
cgit v1.2.3