Path: blob/master/test/jdk/java/awt/List/EmptyListEventTest/EmptyListEventTest.java
43154 views
/*1* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 6366126 819800027* @summary List throws ArrayIndexOutOfBoundsException when pressing ENTER after removing all the items, Win3228* @author Dmitry Cherepanov area=awt.list29* @run main EmptyListEventTest30*/31import java.awt.*;32import java.awt.event.*;33import javax.swing.JFrame;34import javax.swing.JPanel;35import javax.swing.SwingUtilities;3637public class EmptyListEventTest {3839private static List list;4041public static void main(String[] args) throws Exception {4243Robot robot = new Robot();44robot.setAutoDelay(50);4546SwingUtilities.invokeAndWait(new Runnable() {4748@Override49public void run() {50createAndShowGUI();51}52});5354robot.waitForIdle();5556// press mouse -> ItemEvent57Point point = getClickPoint();58robot.mouseMove(point.x, point.y);59robot.mousePress(InputEvent.BUTTON1_MASK);60robot.mouseRelease(InputEvent.BUTTON1_MASK);6162robot.waitForIdle();6364SwingUtilities.invokeAndWait(new Runnable() {6566@Override67public void run() {68list.requestFocusInWindow();69}70});7172robot.waitForIdle();7374if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list) {75throw new RuntimeException("Test failed - list isn't focus owner.");76}7778// press key ENTER -> ActionEvent79robot.keyPress(KeyEvent.VK_ENTER);80robot.keyRelease(KeyEvent.VK_ENTER);81robot.waitForIdle();8283// press key SPACE -> ItemEvent84robot.keyPress(KeyEvent.VK_SPACE);85robot.keyRelease(KeyEvent.VK_SPACE);86robot.waitForIdle();8788// mouse double click -> ActionEvent89robot.setAutoDelay(10);90robot.mousePress(InputEvent.BUTTON1_MASK);91robot.mouseRelease(InputEvent.BUTTON1_MASK);92robot.mousePress(InputEvent.BUTTON1_MASK);93robot.mouseRelease(InputEvent.BUTTON1_MASK);94robot.waitForIdle();95}9697private static Point getClickPoint() throws Exception {98final Point[] result = new Point[1];99100SwingUtilities.invokeAndWait(new Runnable() {101102@Override103public void run() {104Point point = list.getLocationOnScreen();105point.translate(list.getWidth() / 2, list.getHeight() / 2);106result[0] = point;107108}109});110111return result[0];112113114}115116private static void createAndShowGUI() {117JFrame frame = new JFrame();118frame.setSize(200, 200);119frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);120121JPanel panel = new JPanel(new BorderLayout());122123frame.getToolkit().addAWTEventListener(new AWTEventListener() {124125public void eventDispatched(AWTEvent e) {126System.out.println(e);127}128}, AWTEvent.FOCUS_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK);129130131MyListener listener = new MyListener();132133list = new List(4, true);134list.addActionListener(listener);135list.addItemListener(listener);136137panel.add(list);138139frame.getContentPane().add(panel);140frame.setVisible(true);141142}143144static class MyListener implements ActionListener, ItemListener {145146public void actionPerformed(ActionEvent ae) {147System.err.println(ae);148throw new RuntimeException("Test failed - list is empty so event is redundant");149}150151public void itemStateChanged(ItemEvent ie) {152System.err.println(ie);153throw new RuntimeException("Test failed - list is empty so event is redundant");154}155}156}157158159