|
| 1 | +/* |
| 2 | + * Copyright (c) 2004, 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 4722671 |
| 27 | + @summary Accessibility problem in JRE Finder |
| 28 | + @key headful |
| 29 | + @run main FocusForRemovedComponentTest |
| 30 | +*/ |
| 31 | + |
| 32 | +import java.awt.AWTException; |
| 33 | +import java.awt.BorderLayout; |
| 34 | +import java.awt.Button; |
| 35 | +import java.awt.Dimension; |
| 36 | +import java.awt.EventQueue; |
| 37 | +import java.awt.Frame; |
| 38 | +import java.awt.Point; |
| 39 | +import java.awt.Robot; |
| 40 | + |
| 41 | +import java.awt.event.ActionEvent; |
| 42 | +import java.awt.event.ActionListener; |
| 43 | +import java.awt.event.FocusAdapter; |
| 44 | +import java.awt.event.FocusEvent; |
| 45 | +import java.awt.event.InputEvent; |
| 46 | +import java.lang.reflect.InvocationTargetException; |
| 47 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 48 | + |
| 49 | +public class FocusForRemovedComponentTest |
| 50 | + implements ActionListener { |
| 51 | + static int ACTIVATION_TIMEOUT = 2000; |
| 52 | + static long WAIT_TIMEOUT = 3000; |
| 53 | + volatile Frame frame; |
| 54 | + volatile Button btnFirst; |
| 55 | + volatile Button btnSecond; |
| 56 | + volatile Button btnThird; |
| 57 | + |
| 58 | + public void start() throws InterruptedException, InvocationTargetException { |
| 59 | + try { |
| 60 | + EventQueue.invokeAndWait(() -> { |
| 61 | + frame = new Frame("FocusForRemovedComponentTest"); |
| 62 | + btnFirst = new Button("First Button"); |
| 63 | + btnSecond = new Button("Second Button"); |
| 64 | + btnThird = new Button("Third Button"); |
| 65 | + frame.add(btnFirst, BorderLayout.NORTH); |
| 66 | + frame.add(btnSecond, BorderLayout.CENTER); |
| 67 | + btnFirst.addActionListener(this); |
| 68 | + btnFirst.requestFocusInWindow(); |
| 69 | + frame.pack(); |
| 70 | + frame.setVisible(true); |
| 71 | + }); |
| 72 | + |
| 73 | + try { |
| 74 | + Robot robot = new Robot(); |
| 75 | + robot.delay(ACTIVATION_TIMEOUT); |
| 76 | + int[] location = new int[2]; |
| 77 | + EventQueue.invokeAndWait(() -> { |
| 78 | + Point button_location = btnFirst.getLocationOnScreen(); |
| 79 | + Dimension button_size = btnFirst.getSize(); |
| 80 | + location[0] = button_location.x + button_size.width / 2; |
| 81 | + location[1] = button_location.y + button_size.height / 2; |
| 82 | + }); |
| 83 | + robot.mouseMove(location[0], location[1]); |
| 84 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 85 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 86 | + |
| 87 | + Object monitor = new Object(); |
| 88 | + final MonitoredFocusListener monitorer = new MonitoredFocusListener(monitor); |
| 89 | + AtomicBoolean isFocused = new AtomicBoolean(false); |
| 90 | + synchronized (monitor) { |
| 91 | + EventQueue.invokeAndWait(() -> { |
| 92 | + btnThird.addFocusListener(monitorer); |
| 93 | + isFocused.set(btnThird.isFocusOwner()); |
| 94 | + }); |
| 95 | + |
| 96 | + if (!isFocused.get()) { |
| 97 | + monitor.wait(WAIT_TIMEOUT); |
| 98 | + EventQueue.invokeAndWait(() -> { |
| 99 | + isFocused.set(btnThird.isFocusOwner()); |
| 100 | + }); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (!isFocused.get()) { |
| 105 | + throw new RuntimeException("TEST FAILED. The third button is not focus owner."); |
| 106 | + } else { |
| 107 | + System.out.println("TEST PASSED."); |
| 108 | + } |
| 109 | + } catch (AWTException e) { |
| 110 | + e.printStackTrace(); |
| 111 | + throw new RuntimeException("Some AWTException occurred."); |
| 112 | + } catch (InterruptedException e) { |
| 113 | + e.printStackTrace(); |
| 114 | + throw new RuntimeException("Test was interrupted."); |
| 115 | + } |
| 116 | + } finally { |
| 117 | + if (frame != null) { |
| 118 | + EventQueue.invokeAndWait(frame::dispose); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public void actionPerformed(ActionEvent e) { |
| 124 | + if (btnSecond.isVisible()) { |
| 125 | + btnFirst.setEnabled(false); |
| 126 | + frame.remove(btnSecond); |
| 127 | + frame.add(btnThird, BorderLayout.CENTER); |
| 128 | + btnThird.requestFocusInWindow(); |
| 129 | + btnFirst.setEnabled(true); |
| 130 | + frame.validate(); |
| 131 | + frame.repaint(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public static void main(String[] args) throws InterruptedException, |
| 136 | + InvocationTargetException { |
| 137 | + FocusForRemovedComponentTest test = new FocusForRemovedComponentTest(); |
| 138 | + test.start(); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +class MonitoredFocusListener extends FocusAdapter { |
| 143 | + Object monitor; |
| 144 | + |
| 145 | + public MonitoredFocusListener(Object monitor) { |
| 146 | + this.monitor = monitor; |
| 147 | + } |
| 148 | + |
| 149 | + public void focusGained(FocusEvent fe) { |
| 150 | + synchronized (monitor) { |
| 151 | + monitor.notify(); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments