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  * @bug 6366126
  27  * @summary List throws ArrayIndexOutOfBoundsException when pressing ENTER after removing all the items, Win32
  28  * @author Dmitry Cherepanov area=awt.list
  29  * @run main EmptyListEventTest
  30  */
  31 import java.awt.*;
  32 import java.awt.event.*;
  33 import javax.swing.JFrame;
  34 import javax.swing.JPanel;
  35 import javax.swing.SwingUtilities;
  36 import sun.awt.SunToolkit;
  37 
  38 public class EmptyListEventTest {
  39 
  40     private static List list;
  41 
  42     public static void main(String[] args) throws Exception {
  43 
  44         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  45         Robot robot = new Robot();
  46         robot.setAutoDelay(50);
  47 
  48         SwingUtilities.invokeAndWait(new Runnable() {
  49 
  50             @Override
  51             public void run() {
  52                 createAndShowGUI();
  53             }
  54         });
  55 
  56         toolkit.realSync();
  57 
  58         // press mouse -> ItemEvent
  59         Point point = getClickPoint();
  60         robot.mouseMove(point.x, point.y);
  61         robot.mousePress(InputEvent.BUTTON1_MASK);
  62         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  63 
  64         toolkit.realSync();
  65 
  66         SwingUtilities.invokeAndWait(new Runnable() {
  67 
  68             @Override
  69             public void run() {
  70                 list.requestFocusInWindow();
  71             }
  72         });
  73 
  74         toolkit.realSync();
  75 
  76         if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list) {
  77             throw new RuntimeException("Test failed - list isn't focus owner.");
  78         }
  79 
  80         // press key ENTER -> ActionEvent
  81         robot.keyPress(KeyEvent.VK_ENTER);
  82         robot.keyRelease(KeyEvent.VK_ENTER);
  83         toolkit.realSync();
  84 
  85         // press key SPACE -> ItemEvent
  86         robot.keyPress(KeyEvent.VK_SPACE);
  87         robot.keyRelease(KeyEvent.VK_SPACE);
  88         toolkit.realSync();
  89 
  90         // mouse double click -> ActionEvent
  91         robot.setAutoDelay(10);
  92         robot.mousePress(InputEvent.BUTTON1_MASK);
  93         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  94         robot.mousePress(InputEvent.BUTTON1_MASK);
  95         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  96         toolkit.realSync();
  97     }
  98 
  99     private static Point getClickPoint() throws Exception {
 100         final Point[] result = new Point[1];
 101 
 102         SwingUtilities.invokeAndWait(new Runnable() {
 103 
 104             @Override
 105             public void run() {
 106                 Point point = list.getLocationOnScreen();
 107                 point.translate(list.getWidth() / 2, list.getHeight() / 2);
 108                 result[0] = point;
 109 
 110             }
 111         });
 112 
 113         return result[0];
 114 
 115 
 116     }
 117 
 118     private static void createAndShowGUI() {
 119         JFrame frame = new JFrame();
 120         frame.setSize(200, 200);
 121         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 122 
 123         JPanel panel = new JPanel(new BorderLayout());
 124 
 125         frame.getToolkit().addAWTEventListener(new AWTEventListener() {
 126 
 127             public void eventDispatched(AWTEvent e) {
 128                 System.out.println(e);
 129             }
 130         }, AWTEvent.FOCUS_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK);
 131 
 132 
 133         MyListener listener = new MyListener();
 134 
 135         list = new List(4, true);
 136         list.addActionListener(listener);
 137         list.addItemListener(listener);
 138 
 139         panel.add(list);
 140 
 141         frame.getContentPane().add(panel);
 142         frame.setVisible(true);
 143 
 144     }
 145 
 146     static class MyListener implements ActionListener, ItemListener {
 147 
 148         public void actionPerformed(ActionEvent ae) {
 149             System.err.println(ae);
 150             throw new RuntimeException("Test failed - list is empty so event is redundant");
 151         }
 152 
 153         public void itemStateChanged(ItemEvent ie) {
 154             System.err.println(ie);
 155             throw new RuntimeException("Test failed - list is empty so event is redundant");
 156         }
 157     }
 158 }