changeset 4656:61f551d646ea

7124515: [macosx] Test fail like 6366126 (ArrayIndexOutOfBoundException pressing ENTER after removing items) Reviewed-by: art
author alexsch
date Fri, 13 Jan 2012 17:30:14 +0400
parents 08f0789e1c0b
children e6af2dd87c5b
files src/macosx/classes/sun/lwawt/LWListPeer.java test/java/awt/List/EmptyListEventTest/EmptyListEventTest.java
diffstat 2 files changed, 166 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/macosx/classes/sun/lwawt/LWListPeer.java	Thu Jan 12 14:23:10 2012 -0800
+++ b/src/macosx/classes/sun/lwawt/LWListPeer.java	Fri Jan 13 17:30:14 2012 +0400
@@ -257,8 +257,10 @@
                 super.processMouseEvent(e);
                 if (e.getID() == MouseEvent.MOUSE_CLICKED && e.getClickCount() == 2) {
                     final int index = locationToIndex(e.getPoint());
-                    LWListPeer.this.postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,
+                    if (0 <= index && index < getModel().getSize()) {
+                        LWListPeer.this.postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,
                             getModel().getElementAt(index).toString(), e.getWhen(), e.getModifiers()));
+                    }
                 }
             }
 
@@ -266,9 +268,11 @@
             protected void processKeyEvent(final KeyEvent e) {
                 super.processKeyEvent(e);
                 if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_ENTER) {
-                    final int index = getSelectedIndex();
-                    LWListPeer.this.postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,
-                            getModel().getElementAt(index).toString(), e.getWhen(), e.getModifiers()));
+                    final Object selectedValue = getSelectedValue();
+                    if(selectedValue != null){
+                        LWListPeer.this.postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED,
+                            selectedValue.toString(), e.getWhen(), e.getModifiers()));
+                    }
                 }
             }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/List/EmptyListEventTest/EmptyListEventTest.java	Fri Jan 13 17:30:14 2012 +0400
@@ -0,0 +1,158 @@
+/*
+ * Copyright (c) 2008, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6366126
+ * @summary List throws ArrayIndexOutOfBoundsException when pressing ENTER after removing all the items, Win32
+ * @author Dmitry Cherepanov area=awt.list
+ * @run main EmptyListEventTest
+ */
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
+import sun.awt.SunToolkit;
+
+public class EmptyListEventTest {
+
+    private static List list;
+
+    public static void main(String[] args) throws Exception {
+
+        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
+        Robot robot = new Robot();
+        robot.setAutoDelay(50);
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+
+            @Override
+            public void run() {
+                createAndShowGUI();
+            }
+        });
+
+        toolkit.realSync();
+
+        // press mouse -> ItemEvent
+        Point point = getClickPoint();
+        robot.mouseMove(point.x, point.y);
+        robot.mousePress(InputEvent.BUTTON1_MASK);
+        robot.mouseRelease(InputEvent.BUTTON1_MASK);
+
+        toolkit.realSync();
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+
+            @Override
+            public void run() {
+                list.requestFocusInWindow();
+            }
+        });
+
+        toolkit.realSync();
+
+        if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list) {
+            throw new RuntimeException("Test failed - list isn't focus owner.");
+        }
+
+        // press key ENTER -> ActionEvent
+        robot.keyPress(KeyEvent.VK_ENTER);
+        robot.keyRelease(KeyEvent.VK_ENTER);
+        toolkit.realSync();
+
+        // press key SPACE -> ItemEvent
+        robot.keyPress(KeyEvent.VK_SPACE);
+        robot.keyRelease(KeyEvent.VK_SPACE);
+        toolkit.realSync();
+
+        // mouse double click -> ActionEvent
+        robot.setAutoDelay(10);
+        robot.mousePress(InputEvent.BUTTON1_MASK);
+        robot.mouseRelease(InputEvent.BUTTON1_MASK);
+        robot.mousePress(InputEvent.BUTTON1_MASK);
+        robot.mouseRelease(InputEvent.BUTTON1_MASK);
+        toolkit.realSync();
+    }
+
+    private static Point getClickPoint() throws Exception {
+        final Point[] result = new Point[1];
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+
+            @Override
+            public void run() {
+                Point point = list.getLocationOnScreen();
+                point.translate(list.getWidth() / 2, list.getHeight() / 2);
+                result[0] = point;
+
+            }
+        });
+
+        return result[0];
+
+
+    }
+
+    private static void createAndShowGUI() {
+        JFrame frame = new JFrame();
+        frame.setSize(200, 200);
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+        JPanel panel = new JPanel(new BorderLayout());
+
+        frame.getToolkit().addAWTEventListener(new AWTEventListener() {
+
+            public void eventDispatched(AWTEvent e) {
+                System.out.println(e);
+            }
+        }, AWTEvent.FOCUS_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK);
+
+
+        MyListener listener = new MyListener();
+
+        list = new List(4, true);
+        list.addActionListener(listener);
+        list.addItemListener(listener);
+
+        panel.add(list);
+
+        frame.getContentPane().add(panel);
+        frame.setVisible(true);
+
+    }
+
+    static class MyListener implements ActionListener, ItemListener {
+
+        public void actionPerformed(ActionEvent ae) {
+            System.err.println(ae);
+            throw new RuntimeException("Test failed - list is empty so event is redundant");
+        }
+
+        public void itemStateChanged(ItemEvent ie) {
+            System.err.println(ie);
+            throw new RuntimeException("Test failed - list is empty so event is redundant");
+        }
+    }
+}