|
| 1 | +/* |
| 2 | + * Copyright (c) 2005, 2023, 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 6246503 |
| 27 | + @summary Disabling a choice after selection locks keyboard, \ |
| 28 | + mouse and makes the system unusable |
| 29 | + @key headful |
| 30 | + @run main OpenedChoiceHangs |
| 31 | +*/ |
| 32 | + |
| 33 | +import java.awt.AWTException; |
| 34 | +import java.awt.Button; |
| 35 | +import java.awt.Choice; |
| 36 | +import java.awt.Color; |
| 37 | +import java.awt.EventQueue; |
| 38 | +import java.awt.FlowLayout; |
| 39 | +import java.awt.Frame; |
| 40 | +import java.awt.Robot; |
| 41 | +import java.awt.event.FocusEvent; |
| 42 | +import java.awt.event.InputEvent; |
| 43 | +import java.awt.event.ItemEvent; |
| 44 | +import java.awt.event.ItemListener; |
| 45 | +import java.awt.event.KeyEvent; |
| 46 | +import java.lang.reflect.InvocationTargetException; |
| 47 | + |
| 48 | +public class OpenedChoiceHangs implements ItemListener { |
| 49 | + static final Object FOCUS_LOCK = new Object(); |
| 50 | + |
| 51 | + Frame frame; |
| 52 | + Choice ch = new Choice(); |
| 53 | + Button b = new Button("A button"); |
| 54 | + Robot robot; |
| 55 | + |
| 56 | + public static void main(String[] args) |
| 57 | + throws InterruptedException, InvocationTargetException { |
| 58 | + OpenedChoiceHangs openedChoiceHangs = new OpenedChoiceHangs(); |
| 59 | + EventQueue.invokeAndWait(openedChoiceHangs::init); |
| 60 | + openedChoiceHangs.test(); |
| 61 | + } |
| 62 | + |
| 63 | + public void init() { |
| 64 | + frame = new Frame(); |
| 65 | + |
| 66 | + frame.setLayout(new FlowLayout()); |
| 67 | + for (int i = 1; i < 10; i++) { |
| 68 | + ch.add("item " + i); |
| 69 | + } |
| 70 | + frame.add(ch); |
| 71 | + frame.add(b); |
| 72 | + ch.setBackground(new Color(255, 0, 0)); |
| 73 | + ch.setForeground(new Color(255, 0, 0)); |
| 74 | + ch.addItemListener(this); |
| 75 | + |
| 76 | + frame.setSize(200, 200); |
| 77 | + frame.setVisible(true); |
| 78 | + frame.setLocationRelativeTo(null); |
| 79 | + frame.validate(); |
| 80 | + } |
| 81 | + |
| 82 | + public void test() { |
| 83 | + try { |
| 84 | + robot = new Robot(); |
| 85 | + robot.setAutoDelay(100); |
| 86 | + robot.setAutoWaitForIdle(true); |
| 87 | + robot.delay(1000); |
| 88 | + robot.mouseMove(ch.getLocationOnScreen().x + ch.getWidth() / 2, |
| 89 | + ch.getLocationOnScreen().y + ch.getHeight() / 2); |
| 90 | + |
| 91 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 92 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 93 | + robot.delay(1000); |
| 94 | + if (!ch.isFocusOwner()) { |
| 95 | + synchronized (FOCUS_LOCK) { |
| 96 | + FOCUS_LOCK.wait(3000); |
| 97 | + } |
| 98 | + } |
| 99 | + if (!ch.isFocusOwner()){ |
| 100 | + throw new RuntimeException( |
| 101 | + "Test failed. Choice has no focus after mouse press."); |
| 102 | + } |
| 103 | + robot.keyPress(KeyEvent.VK_DOWN); |
| 104 | + robot.keyRelease(KeyEvent.VK_DOWN); |
| 105 | + robot.delay(1000); |
| 106 | + |
| 107 | + robot.keyPress(KeyEvent.VK_UP); |
| 108 | + robot.keyRelease(KeyEvent.VK_UP); |
| 109 | + robot.delay(1000); |
| 110 | + |
| 111 | + Color color = robot.getPixelColor( |
| 112 | + ch.getLocationOnScreen().x + ch.getWidth() / 2, |
| 113 | + ch.getLocationOnScreen().y + ch.getHeight() * 4); |
| 114 | + System.out.println("Color is " + color); |
| 115 | + if (color.equals(new Color(255, 0,0))){ |
| 116 | + throw new RuntimeException( |
| 117 | + "Test failed. Choice is disabled and still opened. "); |
| 118 | + } |
| 119 | + } catch (AWTException e) { |
| 120 | + throw new RuntimeException( |
| 121 | + "Test interrupted due to AWTException. Robot=" + robot, e); |
| 122 | + } catch (InterruptedException e) { |
| 123 | + throw new RuntimeException("Test interrupted. Robot=" + robot, e); |
| 124 | + } finally { |
| 125 | + EventQueue.invokeLater(frame::dispose); |
| 126 | + } |
| 127 | + |
| 128 | + System.out.println("Test passed: Choice became closed after disabling."); |
| 129 | + } |
| 130 | + |
| 131 | + public void itemStateChanged (ItemEvent ie) { |
| 132 | + System.out.println("Choice Item has changed: "+ie); |
| 133 | + ch.setEnabled(false); |
| 134 | + } |
| 135 | + public void focusGained(FocusEvent fEvent){ |
| 136 | + System.out.println("focusGained"+fEvent); |
| 137 | + synchronized(FOCUS_LOCK){ |
| 138 | + FOCUS_LOCK.notify(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public void focusLost(FocusEvent fEvent){ |
| 143 | + System.out.println("focusLost"+fEvent); |
| 144 | + } |
| 145 | +} |
0 commit comments