|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 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 | + @test |
| 25 | + @bug 4423838 |
| 26 | + @summary KEY_TYPED and KEY_PRESSED generated by the same key are notified to |
| 27 | + different TextFields |
| 28 | + @key headful |
| 29 | + @run main QuickTypeTest |
| 30 | +*/ |
| 31 | + |
| 32 | +import java.awt.AWTException; |
| 33 | +import java.awt.BorderLayout; |
| 34 | +import java.awt.Dialog; |
| 35 | +import java.awt.Dimension; |
| 36 | +import java.awt.Frame; |
| 37 | +import java.awt.Panel; |
| 38 | +import java.awt.Point; |
| 39 | +import java.awt.Robot; |
| 40 | +import java.awt.TextArea; |
| 41 | +import java.awt.EventQueue; |
| 42 | +import java.awt.event.FocusAdapter; |
| 43 | +import java.awt.event.FocusEvent; |
| 44 | +import java.awt.event.InputEvent; |
| 45 | +import java.awt.event.KeyAdapter; |
| 46 | +import java.awt.event.KeyEvent; |
| 47 | +import java.awt.event.KeyListener; |
| 48 | + |
| 49 | +import java.util.Properties; |
| 50 | + |
| 51 | +import javax.swing.JFrame; |
| 52 | +import javax.swing.JTextField; |
| 53 | + |
| 54 | +public class QuickTypeTest { |
| 55 | + static final int TEST_TIMEOUT=10000; |
| 56 | + static JFrame frame1; |
| 57 | + static JFrame frame2; |
| 58 | + static JTextField tf1; |
| 59 | + static JTextField tf2; |
| 60 | + static SmartKeyAdapter ska; |
| 61 | + static Object keyMonitor; |
| 62 | + |
| 63 | + public static void main(String[] args) throws Exception { |
| 64 | + try { |
| 65 | + EventQueue.invokeAndWait(() -> { |
| 66 | + frame1 = new JFrame("First Frame"); |
| 67 | + frame2 = new JFrame("Second Frame"); |
| 68 | + tf1 = new JTextField("", 10); |
| 69 | + tf2 = new JTextField("", 10); |
| 70 | + frame1.getContentPane().add(tf1); |
| 71 | + frame2.getContentPane().add(tf2); |
| 72 | + frame1.setLocation(200,220); |
| 73 | + frame2.setLocation(220,300); |
| 74 | + frame1.pack(); |
| 75 | + frame2.pack(); |
| 76 | + keyMonitor = new Object(); |
| 77 | + ska = new SmartKeyAdapter(frame2, keyMonitor); |
| 78 | + tf1.addKeyListener(ska); |
| 79 | + frame1.setVisible(true); |
| 80 | + }); |
| 81 | + |
| 82 | + Robot robot = new Robot(); |
| 83 | + robot.setAutoWaitForIdle(true); |
| 84 | + robot.setAutoDelay(100); |
| 85 | + robot.waitForIdle(); |
| 86 | + robot.delay(1000); |
| 87 | + Object tf1Monitor = new Object(); |
| 88 | + MonitoredFocusListener monitorer = new MonitoredFocusListener(tf1Monitor); |
| 89 | + tf1.addFocusListener(monitorer); |
| 90 | + Point origin = tf1.getLocationOnScreen(); |
| 91 | + Dimension dim = tf1.getSize(); |
| 92 | + robot.mouseMove((int)origin.getX() + (int)dim.getWidth()/2, |
| 93 | + (int)origin.getY() + (int)dim.getHeight()/2); |
| 94 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 95 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 96 | + |
| 97 | + if (!tf1.isFocusOwner()) { |
| 98 | + synchronized (tf1Monitor) { |
| 99 | + tf1Monitor.wait(TEST_TIMEOUT); |
| 100 | + } |
| 101 | + } |
| 102 | + if (!tf1.isFocusOwner()) { |
| 103 | + throw new RuntimeException("TEST FAILED. tf1 doesn't receive focus."); |
| 104 | + } |
| 105 | + |
| 106 | + robot.keyPress(KeyEvent.VK_A); |
| 107 | + robot.keyRelease(KeyEvent.VK_A); |
| 108 | + robot.keyPress(KeyEvent.VK_B); |
| 109 | + robot.keyRelease(KeyEvent.VK_B); |
| 110 | + if (!ska.isFrameShown) { |
| 111 | + synchronized (keyMonitor) { |
| 112 | + keyMonitor.wait(TEST_TIMEOUT); |
| 113 | + } |
| 114 | + } |
| 115 | + if (!ska.isFrameShown) { |
| 116 | + throw new RuntimeException("TEST FAILED. Second frame is not shown."); |
| 117 | + } |
| 118 | + |
| 119 | + Object waitMonitor = new Object(); |
| 120 | + ReleaseWaiter waiter = new ReleaseWaiter(waitMonitor, KeyEvent.VK_C); |
| 121 | + tf1.addKeyListener(waiter); |
| 122 | + tf2.addKeyListener(waiter); |
| 123 | + robot.keyPress(KeyEvent.VK_C); |
| 124 | + robot.keyRelease(KeyEvent.VK_C); |
| 125 | + |
| 126 | + synchronized (waitMonitor) { |
| 127 | + waitMonitor.wait(2000); |
| 128 | + } |
| 129 | + |
| 130 | + if ((tf1.getText().length() > 2) || (tf2.getText().length() < 1)) { |
| 131 | + System.out.println("tf1's text = \"" + tf1.getText() + "\""); |
| 132 | + System.out.println("tf2's text = \"" + tf2.getText() + "\""); |
| 133 | + System.out.println("l1 = " + tf1.getText().length()); |
| 134 | + System.out.println("l2 = " + tf2.getText().length()); |
| 135 | + throw new RuntimeException("TEST FAILED."); |
| 136 | + } |
| 137 | + } finally { |
| 138 | + EventQueue.invokeAndWait(() -> { |
| 139 | + if (frame1 != null) { |
| 140 | + frame1.dispose(); |
| 141 | + } |
| 142 | + if (frame2 != null) { |
| 143 | + frame2.dispose(); |
| 144 | + } |
| 145 | + }); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + }// class QuickTypeTest |
| 150 | + |
| 151 | +class ReleaseWaiter extends KeyAdapter { |
| 152 | + Object monitor; |
| 153 | + int keycode; |
| 154 | + public ReleaseWaiter(Object monitor, int keycode) { |
| 155 | + this.monitor = monitor; |
| 156 | + this.keycode = keycode; |
| 157 | + } |
| 158 | + |
| 159 | + public void keyReleased(KeyEvent ke) { |
| 160 | + System.out.println("keyReleased " + ke.getKeyCode()); |
| 161 | + if (ke.getKeyCode() == keycode) { |
| 162 | + synchronized (monitor) { |
| 163 | + monitor.notify(); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +class SmartKeyAdapter implements KeyListener { |
| 170 | + JFrame frame; |
| 171 | + int charCounter = 0; |
| 172 | + boolean isFrameShown = false; |
| 173 | + Object monitor; |
| 174 | + |
| 175 | + public SmartKeyAdapter(JFrame frame, Object monitor) { |
| 176 | + this.frame = frame; |
| 177 | + this.monitor = monitor; |
| 178 | + } |
| 179 | + |
| 180 | + public void keyReleased(KeyEvent ke) { |
| 181 | + System.out.println(ke.toString()); |
| 182 | + } |
| 183 | + public void keyPressed(KeyEvent ke) { |
| 184 | + System.out.println(ke.toString()); |
| 185 | + charCounter++; |
| 186 | + if (charCounter == 2) { |
| 187 | + frame.setVisible(true); |
| 188 | + isFrameShown = true; |
| 189 | + synchronized (monitor) { |
| 190 | + monitor.notify(); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + public void keyTyped(KeyEvent ke) { |
| 195 | + System.out.println(ke.toString()); |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +class MonitoredFocusListener extends FocusAdapter { |
| 200 | + Object monitor; |
| 201 | + |
| 202 | + public MonitoredFocusListener(Object monitor) { |
| 203 | + this.monitor = monitor; |
| 204 | + } |
| 205 | + |
| 206 | + public void focusLost(FocusEvent fe) { |
| 207 | + System.out.println(fe.toString()); |
| 208 | + } |
| 209 | + public void focusGained(FocusEvent fe) { |
| 210 | + System.out.println(fe.toString()); |
| 211 | + synchronized (monitor) { |
| 212 | + monitor.notify(); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments