1 /*
   2  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @key headful
  27  * @bug 6366126
  28  * @summary List throws ArrayIndexOutOfBoundsException when pressing ENTER after removing all the items, Win32
  29  * @author Dmitry Cherepanov area=awt.list
  30  * @run main EmptyListEventTest
  31  */
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import javax.swing.JFrame;
  35 import javax.swing.JPanel;
  36 import javax.swing.SwingUtilities;
  37 import sun.awt.SunToolkit;
  38 
  39 public class EmptyListEventTest {
  40 
  41     private static List list;
  42 
  43     public static void main(String[] args) throws Exception {
  44 
  45         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  46         Robot robot = new Robot();
  47         robot.setAutoDelay(50);
  48 
  49         SwingUtilities.invokeAndWait(new Runnable() {
  50 
  51             @Override
  52             public void run() {
  53                 createAndShowGUI();
  54             }
  55         });
  56 
  57         toolkit.realSync();
  58 
  59         // press mouse -> ItemEvent
  60         Point point = getClickPoint();
  61         robot.mouseMove(point.x, point.y);
  62         robot.mousePress(InputEvent.BUTTON1_MASK);
  63         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  64 
  65         toolkit.realSync();
  66 
  67         SwingUtilities.invokeAndWait(new Runnable() {
  68 
  69             @Override
  70             public void run() {
  71                 list.requestFocusInWindow();
  72             }
  73         });
  74 
  75         toolkit.realSync();
  76 
  77         if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list) {
  78             throw new RuntimeException("Test failed - list isn't focus owner.");
  79         }
  80 
  81         // press key ENTER -> ActionEvent
  82         robot.keyPress(KeyEvent.VK_ENTER);
  83         robot.keyRelease(KeyEvent.VK_ENTER);
  84         toolkit.realSync();
  85 
  86         // press key SPACE -> ItemEvent
  87         robot.keyPress(KeyEvent.VK_SPACE);
  88         robot.keyRelease(KeyEvent.VK_SPACE);
  89         toolkit.realSync();
  90 
  91         // mouse double click -> ActionEvent
  92         robot.setAutoDelay(10);
  93         robot.mousePress(InputEvent.BUTTON1_MASK);
  94         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  95         robot.mousePress(InputEvent.BUTTON1_MASK);
  96         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  97         toolkit.realSync();
  98     }
  99 
 100     private static Point getClickPoint() throws Exception {
 101         final Point[] result = new Point[1];
 102 
 103         SwingUtilities.invokeAndWait(new Runnable() {
 104 
 105             @Override
 106             public void run() {
 107                 Point point = list.getLocationOnScreen();
 108                 point.translate(list.getWidth() / 2, list.getHeight() / 2);
 109                 result[0] = point;
 110 
 111             }
 112         });
 113 
 114         return result[0];
 115 
 116 
 117     }
 118 
 119     private static void createAndShowGUI() {
 120         JFrame frame = new JFrame();
 121         frame.setSize(200, 200);
 122         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 123 
 124         JPanel panel = new JPanel(new BorderLayout());
 125 
 126         frame.getToolkit().addAWTEventListener(new AWTEventListener() {
 127 
 128             public void eventDispatched(AWTEvent e) {
 129                 System.out.println(e);
 130             }
 131         }, AWTEvent.FOCUS_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK);
 132 
 133 
 134         MyListener listener = new MyListener();
 135 
 136         list = new List(4, true);
 137         list.addActionListener(listener);
 138         list.addItemListener(listener);
 139 
 140         panel.add(list);
 141 
 142         frame.getContentPane().add(panel);
 143         frame.setVisible(true);
 144 
 145     }
 146 
 147     static class MyListener implements ActionListener, ItemListener {
 148 
 149         public void actionPerformed(ActionEvent ae) {
 150             System.err.println(ae);
 151             throw new RuntimeException("Test failed - list is empty so event is redundant");
 152         }
 153 
 154         public void itemStateChanged(ItemEvent ie) {
 155             System.err.println(ie);
 156             throw new RuntimeException("Test failed - list is empty so event is redundant");
 157         }
 158     }
 159 }