|
| 1 | +/* |
| 2 | + * Copyright (c) 2005, 2024, 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 6293432 |
| 27 | + * @summary Key events ('SPACE', 'UP', 'DOWN') aren't blocked |
| 28 | + * if mouse is kept in 'PRESSED' state for List |
| 29 | + * @key headful |
| 30 | + * @run main HandlingKeyEventIfMousePressedTest |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.EventQueue; |
| 34 | +import java.awt.FlowLayout; |
| 35 | +import java.awt.Frame; |
| 36 | +import java.awt.List; |
| 37 | +import java.awt.Point; |
| 38 | +import java.awt.Robot; |
| 39 | +import java.awt.event.ActionEvent; |
| 40 | +import java.awt.event.ActionListener; |
| 41 | +import java.awt.event.FocusAdapter; |
| 42 | +import java.awt.event.FocusEvent; |
| 43 | +import java.awt.event.InputEvent; |
| 44 | +import java.awt.event.ItemEvent; |
| 45 | +import java.awt.event.ItemListener; |
| 46 | +import java.awt.event.KeyEvent; |
| 47 | +import java.awt.event.MouseAdapter; |
| 48 | +import java.awt.event.MouseEvent; |
| 49 | +import java.awt.event.MouseMotionAdapter; |
| 50 | + |
| 51 | +public class HandlingKeyEventIfMousePressedTest { |
| 52 | + |
| 53 | + static Frame frame; |
| 54 | + static List list; |
| 55 | + static volatile Point loc; |
| 56 | + |
| 57 | + public static void main(String[] args) throws Exception { |
| 58 | + Robot robot = new Robot(); |
| 59 | + robot.setAutoDelay(100); |
| 60 | + try { |
| 61 | + EventQueue.invokeAndWait(() -> createUI()); |
| 62 | + robot.waitForIdle(); |
| 63 | + robot.delay(1000); |
| 64 | + EventQueue.invokeAndWait(() -> { |
| 65 | + loc = list.getLocationOnScreen(); |
| 66 | + }); |
| 67 | + robot.mouseMove(loc.x + 10, loc.y + 10); |
| 68 | + robot.waitForIdle(); |
| 69 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 70 | + |
| 71 | + // key pressing when the mouse is kept in the 'pressed' state |
| 72 | + robot.keyPress(KeyEvent.VK_DOWN); |
| 73 | + robot.keyRelease(KeyEvent.VK_DOWN); |
| 74 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 75 | + robot.waitForIdle(); |
| 76 | + |
| 77 | + int selectedIndex = list.getSelectedIndex(); |
| 78 | + if (selectedIndex != 0) { |
| 79 | + throw new RuntimeException("Test failed: list.getCurrentItem = " + selectedIndex); |
| 80 | + } |
| 81 | + } finally { |
| 82 | + EventQueue.invokeAndWait(() -> { |
| 83 | + if (frame != null) { |
| 84 | + frame.dispose(); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static void createUI() { |
| 91 | + frame = new Frame("HandlingKeyEventIfMousePressedTest"); |
| 92 | + list = new List(10, false); |
| 93 | + |
| 94 | + list.add("111"); |
| 95 | + list.add("222"); |
| 96 | + list.add("333"); |
| 97 | + list.add("444"); |
| 98 | + frame.add(list); |
| 99 | + |
| 100 | + addListeners(); |
| 101 | + |
| 102 | + frame.setLayout(new FlowLayout()); |
| 103 | + frame.pack(); |
| 104 | + frame.setLocationRelativeTo(null); |
| 105 | + frame.setVisible(true); |
| 106 | + } |
| 107 | + |
| 108 | + // added in order to have more information in failed case |
| 109 | + private static void addListeners() { |
| 110 | + |
| 111 | + list.addMouseMotionListener( |
| 112 | + new MouseMotionAdapter() { |
| 113 | + @Override |
| 114 | + public void mouseDragged(MouseEvent me) { |
| 115 | + System.out.println(me); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void mouseMoved(MouseEvent me) { |
| 120 | + System.out.println(me); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + list.addMouseListener( |
| 125 | + new MouseAdapter(){ |
| 126 | + public void mousePressed(MouseEvent me) { |
| 127 | + System.out.println(me); |
| 128 | + } |
| 129 | + public void mouseClicked(MouseEvent me) { |
| 130 | + System.out.println(me); |
| 131 | + } |
| 132 | + public void mouseEntered(MouseEvent me) { |
| 133 | + System.out.println(me); |
| 134 | + } |
| 135 | + public void mouseExited(MouseEvent me) { |
| 136 | + System.out.println(me); |
| 137 | + } |
| 138 | + public void mouseReleased(MouseEvent me) { |
| 139 | + System.out.println(me); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + list.addActionListener( |
| 144 | + new ActionListener() { |
| 145 | + public void actionPerformed(ActionEvent ae) { |
| 146 | + System.out.println(ae); |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + list.addItemListener( |
| 151 | + new ItemListener() { |
| 152 | + public void itemStateChanged(ItemEvent ie) { |
| 153 | + System.out.println(ie); |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + list.addFocusListener( |
| 158 | + new FocusAdapter() { |
| 159 | + public void focusGained(FocusEvent fe) { |
| 160 | + System.out.println(fe); |
| 161 | + } |
| 162 | + public void focusLost(FocusEvent fe) { |
| 163 | + System.out.println(fe); |
| 164 | + } |
| 165 | + }); |
| 166 | + } |
| 167 | +} |
0 commit comments