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