changeset 1555:389cecd0ca18

6865565: Test failed: /test/closed/javax/swing/JInternalFrame/6325652/bug6325652.java Reviewed-by: peterz
author malenkov
date Fri, 31 Jul 2009 16:27:35 +0400
parents 4c6a5ea563ba
children 23dfc2c451e3
files test/javax/swing/JInternalFrame/Test6325652.java test/javax/swing/JInternalFrame/Test6505027.java test/javax/swing/JInternalFrame/Test6802868.java test/javax/swing/JScrollPane/Test6526631.java test/javax/swing/SwingTest.java
diffstat 5 files changed, 274 insertions(+), 221 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/swing/JInternalFrame/Test6325652.java	Fri Jul 31 16:27:35 2009 +0400
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6325652
+ * @summary Tests keyboard shortcuts
+ * @author Sergey Malenkov
+ * @library ..
+ */
+
+import java.awt.AWTException;
+import java.awt.Robot;
+import java.awt.event.KeyEvent;
+import java.beans.PropertyVetoException;
+import javax.swing.JDesktopPane;
+import javax.swing.JFrame;
+import javax.swing.JInternalFrame;
+import javax.swing.JTextArea;
+
+public class Test6325652 {
+
+    private static final int WIDTH = 300;
+    private static final int HEIGHT = 300;
+
+    public static void main(String[] args) throws Throwable {
+        SwingTest.start(Test6325652.class);
+    }
+
+    private static Robot robot;
+    private JInternalFrame internal;
+
+    public Test6325652(JFrame frame) {
+        JDesktopPane desktop = new JDesktopPane();
+        desktop.add(create(0));
+        desktop.add(this.internal = create(1));
+        frame.add(desktop);
+    }
+
+    public void select() throws PropertyVetoException {
+        this.internal.setSelected(true);
+    }
+
+    public static void stepFirst() throws AWTException {
+        robot = new Robot(); // initialize shared static field first time
+        click(KeyEvent.VK_CONTROL, KeyEvent.VK_F9); // iconify internal frame
+    }
+
+    public void stepFirstValidate() {
+        if (!this.internal.isIcon()) {
+            throw new Error("frame should be an icon");
+        }
+    }
+
+    public static void stepSecond() {
+        click(KeyEvent.VK_CONTROL, KeyEvent.VK_F6); // navigate to the icon
+        click(KeyEvent.VK_CONTROL, KeyEvent.VK_F5); // restore the icon
+    }
+
+    public void stepSecondValidate() {
+        if (this.internal.isIcon()) {
+            throw new Error("frame should not be an icon");
+        }
+    }
+
+    private static void click(int... keys) {
+        for (int key : keys) {
+            robot.keyPress(key);
+        }
+        for (int key : keys) {
+            robot.keyRelease(key);
+        }
+    }
+
+    private static JInternalFrame create(int index) {
+        String text = "test" + index; // NON-NLS: frame identification
+        index = index * 3 + 1;
+
+        JInternalFrame internal = new JInternalFrame(text, true, true, true, true);
+        internal.getContentPane().add(new JTextArea(text));
+        internal.setBounds(10 * index, 10 * index, WIDTH, HEIGHT);
+        internal.setVisible(true);
+        return internal;
+    }
+}
--- a/test/javax/swing/JInternalFrame/Test6505027.java	Thu Jul 30 14:45:04 2009 +0900
+++ b/test/javax/swing/JInternalFrame/Test6505027.java	Fri Jul 31 16:27:35 2009 +0400
@@ -26,6 +26,7 @@
  * @bug 6505027
  * @summary Tests focus problem inside internal frame
  * @author Sergey Malenkov
+ * @library ..
  */
 
 import java.awt.AWTException;
@@ -45,11 +46,10 @@
 import javax.swing.JTable;
 import javax.swing.JTextField;
 import javax.swing.SwingUtilities;
-import javax.swing.WindowConstants;
 import javax.swing.table.DefaultTableModel;
 import javax.swing.table.TableColumn;
 
-public class Test6505027 implements Runnable {
+public class Test6505027 {
 
     private static final boolean INTERNAL = true;
     private static final boolean TERMINATE = true;
@@ -57,80 +57,53 @@
     private static final int WIDTH = 450;
     private static final int HEIGHT = 200;
     private static final int OFFSET = 10;
-    private static final long PAUSE = 2048L;
 
-    private static final String[] COLUMNS = { "Size", "Shape" }; // NON-NLS
-    private static final String[] ITEMS = { "a", "b", "c", "d" }; // NON-NLS
-    private static final String KEY = "terminateEditOnFocusLost"; // NON-NLS
+    private static final String[] COLUMNS = { "Size", "Shape" }; // NON-NLS: column names
+    private static final String[] ITEMS = { "a", "b", "c", "d" }; // NON-NLS: combobox content
+    private static final String KEY = "terminateEditOnFocusLost"; // NON-NLS: property name
 
-    public static void main(String[] args) {
-        SwingUtilities.invokeLater(new Test6505027());
+    public static void main(String[] args) throws Throwable {
+        SwingTest.start(Test6505027.class);
+    }
 
-        Component component = null;
-        while (component == null) {
-            try {
-                Thread.sleep(PAUSE);
-            }
-            catch (InterruptedException exception) {
-                // ignore interrupted exception
-            }
-            component = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
+    private final JTable table = new JTable(new DefaultTableModel(COLUMNS, 2));
+
+    public Test6505027(JFrame main) {
+        Container container = main;
+        if (INTERNAL) {
+            JInternalFrame frame = new JInternalFrame();
+            frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT);
+            frame.setVisible(true);
+
+            JDesktopPane desktop = new JDesktopPane();
+            desktop.add(frame, new Integer(1));
+
+            container.add(desktop);
+            container = frame;
         }
+        if (TERMINATE) {
+            this.table.putClientProperty(KEY, Boolean.TRUE);
+        }
+        TableColumn column = this.table.getColumn(COLUMNS[1]);
+        column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS)));
+
+        container.add(BorderLayout.NORTH, new JTextField());
+        container.add(BorderLayout.CENTER, new JScrollPane(this.table));
+    }
+
+    public void press() throws AWTException {
+        Point point = this.table.getCellRect(1, 1, false).getLocation();
+        SwingUtilities.convertPointToScreen(point, this.table);
+
+        Robot robot = new Robot();
+        robot.mouseMove(point.x + 1, point.y + 1);
+        robot.mousePress(InputEvent.BUTTON1_MASK);
+    }
+
+    public static void validate() {
+        Component component = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
         if (!component.getClass().equals(JComboBox.class)) {
             throw new Error("unexpected focus owner: " + component);
         }
-        SwingUtilities.getWindowAncestor(component).dispose();
-    }
-
-    private JTable table;
-    private Point point;
-
-    public void run() {
-        if (this.table == null) {
-            JFrame main = new JFrame();
-            main.setSize(WIDTH + OFFSET * 3, HEIGHT + OFFSET * 5);
-            main.setLocationRelativeTo(null);
-            main.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
-            main.setVisible(true);
-
-            Container container = main;
-            if (INTERNAL) {
-                JInternalFrame frame = new JInternalFrame();
-                frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT);
-                frame.setVisible(true);
-
-                JDesktopPane desktop = new JDesktopPane();
-                desktop.add(frame, new Integer(1));
-
-                container.add(desktop);
-                container = frame;
-            }
-            this.table = new JTable(new DefaultTableModel(COLUMNS, 2));
-            if (TERMINATE) {
-                this.table.putClientProperty(KEY, Boolean.TRUE);
-            }
-            TableColumn column = this.table.getColumn(COLUMNS[1]);
-            column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS)));
-
-            container.add(BorderLayout.NORTH, new JTextField());
-            container.add(BorderLayout.CENTER, new JScrollPane(this.table));
-
-            SwingUtilities.invokeLater(this);
-        }
-        else if (this.point == null) {
-            this.point = this.table.getCellRect(1, 1, false).getLocation();
-            SwingUtilities.convertPointToScreen(this.point, this.table);
-            SwingUtilities.invokeLater(this);
-        }
-        else {
-            try {
-                Robot robot = new Robot();
-                robot.mouseMove(this.point.x + 1, this.point.y + 1);
-                robot.mousePress(InputEvent.BUTTON1_MASK);
-            }
-            catch (AWTException exception) {
-                throw new Error("unexpected exception", exception);
-            }
-        }
     }
 }
--- a/test/javax/swing/JInternalFrame/Test6802868.java	Thu Jul 30 14:45:04 2009 +0900
+++ b/test/javax/swing/JInternalFrame/Test6802868.java	Fri Jul 31 16:27:35 2009 +0400
@@ -26,83 +26,73 @@
  * @bug 6802868
  * @summary JInternalFrame is not maximized when maximized parent frame
  * @author Alexander Potochkin
+ * @library ..
  */
 
-import sun.awt.SunToolkit;
-
 import java.awt.Dimension;
 import java.awt.Point;
-import java.awt.Robot;
-import java.awt.Toolkit;
 import java.beans.PropertyVetoException;
 import javax.swing.JDesktopPane;
 import javax.swing.JFrame;
 import javax.swing.JInternalFrame;
-import javax.swing.SwingUtilities;
 
 public class Test6802868 {
-    static JInternalFrame jif;
-    static JFrame frame;
-    static Dimension size;
-    static Point location;
 
-    public static void main(String[] args) throws Exception {
-        Robot robot = new Robot();
-        robot.setAutoDelay(20);
-        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
+    public static void main(String[] args) throws Throwable {
+        SwingTest.start(Test6802868.class);
+    }
 
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                frame = new JFrame();
-                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+    private final JFrame frame;
+    private final JInternalFrame internal;
+    private Dimension size;
+    private Point location;
 
-                JDesktopPane jdp = new JDesktopPane();
-                frame.getContentPane().add(jdp);
+    public Test6802868(JFrame frame) {
+        JDesktopPane desktop = new JDesktopPane();
 
-                jif = new JInternalFrame("Title", true, true, true, true);
-                jdp.add(jif);
-                jif.setVisible(true);
+        this.frame = frame;
+        this.frame.add(desktop);
 
-                frame.setSize(200, 200);
-                frame.setLocationRelativeTo(null);
-                frame.setVisible(true);
+        this.internal = new JInternalFrame(getClass().getName(), true, true, true, true);
+        this.internal.setVisible(true);
 
-                try {
-                    jif.setMaximum(true);
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            }
-        });
-        toolkit.realSync();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                size = jif.getSize();
-                frame.setSize(300, 300);
-            }
-        });
-        toolkit.realSync();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                if (jif.getSize().equals(size)) {
-                    throw new RuntimeException("InternalFrame hasn't changed its size");
-                }
-                try {
-                    jif.setIcon(true);
-                } catch (PropertyVetoException e) {
-                    e.printStackTrace();
-                }
-                location = jif.getDesktopIcon().getLocation();
-                frame.setSize(400, 400);
-            }
-        });
-        toolkit.realSync();
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                if (jif.getDesktopIcon().getLocation().equals(location)) {
-                    throw new RuntimeException("JDesktopIcon hasn't moved");
-                }
-            }
-        });
+        desktop.add(this.internal);
+    }
+
+    public void firstAction() throws PropertyVetoException {
+        this.internal.setMaximum(true);
+    }
+
+    public void firstTest() {
+        this.size = this.internal.getSize();
+        resizeFrame();
+    }
+
+    public void firstValidation() {
+        if (this.internal.getSize().equals(this.size)) {
+            throw new Error("InternalFrame hasn't changed its size");
+        }
+    }
+
+    public void secondAction() throws PropertyVetoException {
+        this.internal.setIcon(true);
+    }
+
+    public void secondTest() {
+        this.location = this.internal.getDesktopIcon().getLocation();
+        resizeFrame();
+    }
+
+    public void secondValidation() {
+        if (this.internal.getDesktopIcon().getLocation().equals(this.location)) {
+            throw new Error("JDesktopIcon hasn't moved");
+        }
+    }
+
+    private void resizeFrame() {
+        Dimension size = this.frame.getSize();
+        size.width += 10;
+        size.height += 10;
+        this.frame.setSize(size);
     }
 }
--- a/test/javax/swing/JScrollPane/Test6526631.java	Thu Jul 30 14:45:04 2009 +0900
+++ b/test/javax/swing/JScrollPane/Test6526631.java	Fri Jul 31 16:27:35 2009 +0400
@@ -27,10 +27,9 @@
  * @summary Resizes right-oriented scroll pane
  * @author Sergey Malenkov
  * @library ..
- * @build SwingTest
- * @run main Test6526631
  */
 
+import java.awt.ComponentOrientation;
 import java.awt.Dimension;
 import javax.swing.JFrame;
 import javax.swing.JScrollBar;
@@ -38,15 +37,13 @@
 import javax.swing.JTextArea;
 import javax.swing.JViewport;
 
-import static java.awt.ComponentOrientation.RIGHT_TO_LEFT;
-
 public class Test6526631 {
 
     private static final int COLS = 90;
     private static final int ROWS = 50;
     private static final int OFFSET = 10;
 
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Throwable {
         SwingTest.start(Test6526631.class);
     }
 
@@ -55,7 +52,7 @@
 
     public Test6526631(JFrame frame) {
         this.pane = new JScrollPane(new JTextArea(ROWS, COLS));
-        this.pane.setComponentOrientation(RIGHT_TO_LEFT);
+        this.pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
         this.frame = frame;
         this.frame.add(this.pane);
     }
@@ -79,24 +76,24 @@
     public void validateThird() {
         JViewport viewport = this.pane.getViewport();
         JScrollBar scroller = this.pane.getHorizontalScrollBar();
-        if (!scroller.getComponentOrientation().equals(RIGHT_TO_LEFT)) {
-            throw new IllegalStateException("unexpected component orientation");
+        if (!scroller.getComponentOrientation().equals(ComponentOrientation.RIGHT_TO_LEFT)) {
+            throw new Error("unexpected component orientation");
         }
         int value = scroller.getValue();
         if (value != 0) {
-            throw new IllegalStateException("unexpected scroll value");
+            throw new Error("unexpected scroll value");
         }
         int extent = viewport.getExtentSize().width;
         if (extent != scroller.getVisibleAmount()) {
-            throw new IllegalStateException("unexpected visible amount");
+            throw new Error("unexpected visible amount");
         }
         int size = viewport.getViewSize().width;
         if (size != scroller.getMaximum()) {
-            throw new IllegalStateException("unexpected maximum");
+            throw new Error("unexpected maximum");
         }
         int pos = size - extent - value;
         if (pos != viewport.getViewPosition().x) {
-            throw new IllegalStateException("unexpected position");
+            throw new Error("unexpected position");
         }
     }
 }
--- a/test/javax/swing/SwingTest.java	Thu Jul 30 14:45:04 2009 +0900
+++ b/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;
+        }
     }
 }