jdk/test/javax/swing/SwingTest.java
changeset 3502 821e773cae60
parent 3350 100ac73bfe6e
child 5506 202f599c92aa
--- a/jdk/test/javax/swing/SwingTest.java	Thu Jul 30 14:45:04 2009 +0900
+++ b/jdk/test/javax/swing/SwingTest.java	Fri Jul 31 16:27:35 2009 +0400
@@ -21,7 +21,8 @@
  * have any questions.
  */
 
-import java.io.PrintWriter;
+import sun.awt.SunToolkit;
+import java.awt.Toolkit;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
@@ -30,12 +31,18 @@
 import java.util.Set;
 import java.util.TreeSet;
 import javax.swing.JFrame;
-
-import static javax.swing.SwingUtilities.invokeLater;
+import javax.swing.SwingUtilities;
 
 /**
- * SwingTestHelper is a utility class for writing regression tests
+ * SwingTest is a utility class for writing regression tests
  * that require interacting with the UI.
+ * It uses reflection to invoke all public methods without parameters.
+ * All static methods are starting on the current thread.
+ * Other methods including constructor are starting on the EDT.
+ * Between each method invocation all pending events are processed.
+ * The methods are sorted by name and invoked in that order.
+ * Failure of the test is signaled by any method throwing an exception.
+ * If no methods throw an exception the test is assumed to have passed.
  *
  * @author Sergey A. Malenkov
  */
@@ -44,99 +51,56 @@
     private static final int WIDTH = 640;
     private static final int HEIGHT = 480;
 
-    public static void start(Class<?> type) {
+    public static void start(Class<?> type) throws Throwable {
         new SwingTest(type).start();
     }
 
-    private final PrintWriter writer = new PrintWriter(System.out, true);
+    private final Class<?> type;
+    private final Iterator<Method> methods;
 
-    private Class<?> type;
     private JFrame frame;
-    private Iterator<Method> methods;
     private Object object;
     private Method method;
     private Throwable error;
 
     private SwingTest(Class<?> type) {
+        Set<Method> methods = new TreeSet<Method>(new Comparator<Method>() {
+            public int compare(Method first, Method second) {
+                return first.getName().compareTo(second.getName());
+            }
+        });
+        for (Method method : type.getMethods()) {
+            if (method.getDeclaringClass().equals(type)) {
+                if (method.getReturnType().equals(void.class)) {
+                    if (0 == method.getParameterTypes().length) {
+                        methods.add(method);
+                    }
+                }
+            }
+        }
         this.type = type;
+        this.methods = methods.iterator();
     }
 
     public void run() {
-        synchronized (this.writer) {
-            if (this.error != null) {
-                this.frame.dispose();
-                this.frame = null;
-            }
-            else if (this.object == null) {
-                invoke();
-                Set<Method> methods = new TreeSet<Method>(new Comparator<Method>() {
-                    public int compare(Method first, Method second) {
-                        return first.getName().compareTo(second.getName());
-                    }
-                });
-                for (Method method : this.type.getMethods()) {
-                    if (method.getDeclaringClass().equals(this.type)) {
-                        if (method.getReturnType().equals(void.class)) {
-                            if (0 == method.getParameterTypes().length) {
-                                methods.add(method);
-                            }
-                        }
-                    }
-                }
-                this.methods = methods.iterator();
-            }
-            else if (this.method != null) {
-                invoke();
-            }
-            else if (this.methods.hasNext()) {
-                this.method = this.methods.next();
-            }
-            else {
-                this.frame.dispose();
-                this.frame = null;
-                this.type = null;
-            }
-            this.writer.notifyAll();
-        }
-    }
-
-    private void start() {
-        synchronized (this.writer) {
-            while (this.type != null) {
-                if ((this.method != null) && Modifier.isStatic(this.method.getModifiers())) {
-                    invoke();
-                }
-                else {
-                    invokeLater(this);
-                    try {
-                        this.writer.wait();
-                    }
-                    catch (InterruptedException exception) {
-                        exception.printStackTrace(this.writer);
-                    }
-                }
-                if ((this.frame == null) && (this.error != null)) {
-                    throw new Error("unexpected error", this.error);
-                }
-            }
-        }
-    }
-
-    private void invoke() {
         try {
-            if (this.method != null) {
-                this.writer.println(this.method);
-                this.method.invoke(this.object);
-                this.method = null;
-            }
-            else {
-                this.writer.println(this.type);
+            if (this.object == null) {
+                System.out.println(this.type);
                 this.frame = new JFrame(this.type.getSimpleName());
                 this.frame.setSize(WIDTH, HEIGHT);
                 this.frame.setLocationRelativeTo(null);
-                this.object = this.type.getConstructor(JFrame.class).newInstance(this.frame);
+                this.object = this.type.getConstructor(this.frame.getClass()).newInstance(this.frame);
                 this.frame.setVisible(true);
             }
+            else if (this.method != null) {
+                System.out.println(this.method);
+                this.method.invoke(this.object);
+            }
+            else {
+                System.out.println((this.error == null) ? "PASSED" : "FAILED"); // NON-NLS: debug
+                this.frame.dispose();
+                this.frame = null;
+            }
         }
         catch (NoSuchMethodException exception) {
             this.error = exception;
@@ -156,5 +120,29 @@
         catch (InvocationTargetException exception) {
             this.error = exception.getTargetException();
         }
+        System.out.flush();
+        this.method = this.methods.hasNext() && (this.error == null)
+                ? this.methods.next()
+                : null;
+    }
+
+    private void start() throws Throwable {
+        do {
+            if ((this.method != null) && Modifier.isStatic(this.method.getModifiers())) {
+                run(); // invoke static method on the current thread
+            }
+            else {
+                SwingUtilities.invokeLater(this); // invoke on the event dispatch thread
+            }
+            Toolkit tk = Toolkit.getDefaultToolkit();
+            if (tk instanceof SunToolkit) {
+                SunToolkit stk = (SunToolkit) tk;
+                stk.realSync(); // wait until done
+            }
+        }
+        while (this.frame != null);
+        if (this.error != null) {
+            throw this.error;
+        }
     }
 }