|
| 1 | +/* |
| 2 | + * Copyright (c) 2004, 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 | +import java.awt.FlowLayout; |
| 25 | +import java.awt.event.KeyEvent; |
| 26 | +import java.lang.reflect.InvocationTargetException; |
| 27 | +import javax.swing.InputVerifier; |
| 28 | +import javax.swing.JComponent; |
| 29 | +import javax.swing.JDialog; |
| 30 | +import javax.swing.JFrame; |
| 31 | +import javax.swing.JPanel; |
| 32 | +import javax.swing.JTextField; |
| 33 | +import javax.swing.JWindow; |
| 34 | +import javax.swing.SwingUtilities; |
| 35 | + |
| 36 | +import static java.awt.event.InputEvent.BUTTON1_DOWN_MASK; |
| 37 | + |
| 38 | +/* |
| 39 | + * @test |
| 40 | + * @bug 4774166 |
| 41 | + * @key headful |
| 42 | + * @summary InputVerifier should be called after a window loses and then regains focus |
| 43 | + * @library /javax/swing/regtesthelpers |
| 44 | + * @build JRobot |
| 45 | + * @run main bug4774166 |
| 46 | + */ |
| 47 | + |
| 48 | +class TestPanel extends JPanel { |
| 49 | + JTextField tf1 = null; |
| 50 | + JTextField tf2 = null; |
| 51 | + volatile boolean verifierCalled; |
| 52 | + |
| 53 | + public void init() { |
| 54 | + tf1 = new JTextField(10); |
| 55 | + tf2 = new JTextField(10); |
| 56 | + |
| 57 | + InputVerifier verifier = new InputVerifier() { |
| 58 | + public boolean verify(JComponent input) { |
| 59 | + verifierCalled = true; |
| 60 | + return false; |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + setLayout(new FlowLayout()); |
| 65 | + tf1.setInputVerifier(verifier); |
| 66 | + add(tf1); |
| 67 | + add(tf2); |
| 68 | + validate(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +public class bug4774166 { |
| 73 | + private static JRobot robot = JRobot.getRobot(); |
| 74 | + |
| 75 | + JFrame testframe; |
| 76 | + JFrame testwindowframe; |
| 77 | + JFrame customframe; |
| 78 | + JWindow testwindow; |
| 79 | + JDialog testdialog; |
| 80 | + JTextField frametf1, frametf2, windowtf1, windowtf2, dialogtf1, dialogtf2; |
| 81 | + TestPanel testpanel; |
| 82 | + |
| 83 | + volatile boolean isFocused; |
| 84 | + volatile boolean verifierCalled; |
| 85 | + |
| 86 | + public void setupGUI() { |
| 87 | + testframe = new JFrame("Test 4774166"); |
| 88 | + testframe.setLayout(new FlowLayout()); |
| 89 | + testframe.setBounds(100, 100, 200, 100); |
| 90 | + |
| 91 | + testwindowframe = new JFrame("Owner of JWindow"); |
| 92 | + testwindowframe.setBounds(0, 0, 0, 0); |
| 93 | + |
| 94 | + testwindow = new JWindow(testwindowframe); |
| 95 | + testwindow.setBounds(175, 325, 200, 100); |
| 96 | + testwindow.setLayout(new FlowLayout()); |
| 97 | + |
| 98 | + testdialog = new JDialog((JFrame)null, "Test dialog"); |
| 99 | + testdialog.setBounds(420, 100, 200, 100); |
| 100 | + testdialog.setLayout(new FlowLayout()); |
| 101 | + |
| 102 | + InputVerifier verifier = new InputVerifier() { |
| 103 | + public boolean verify(JComponent input) { |
| 104 | + verifierCalled = true; |
| 105 | + return false; |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + frametf1 = new JTextField(10); |
| 110 | + frametf2 = new JTextField(10); |
| 111 | + frametf1.setInputVerifier(verifier); |
| 112 | + testframe.add(frametf1); |
| 113 | + testframe.add(frametf2); |
| 114 | + testframe.setVisible(true); |
| 115 | + |
| 116 | + windowtf1 = new JTextField(10); |
| 117 | + windowtf2 = new JTextField(10); |
| 118 | + windowtf1.setInputVerifier(verifier); |
| 119 | + testwindow.add(windowtf1); |
| 120 | + testwindow.add(windowtf2); |
| 121 | + testwindowframe.setVisible(true); |
| 122 | + testwindow.setVisible(true); |
| 123 | + |
| 124 | + dialogtf1 = new JTextField(10); |
| 125 | + dialogtf2 = new JTextField(10); |
| 126 | + dialogtf1.setInputVerifier(verifier); |
| 127 | + testdialog.add(dialogtf1); |
| 128 | + testdialog.add(dialogtf2); |
| 129 | + testdialog.setVisible(true); |
| 130 | + |
| 131 | + customframe = new JFrame("Frame with custom panel"); |
| 132 | + customframe.setLayout(new FlowLayout()); |
| 133 | + testpanel = new TestPanel(); |
| 134 | + testpanel.init(); |
| 135 | + |
| 136 | + customframe.add(testpanel); |
| 137 | + customframe.setBounds(420, 250, 200, 100); |
| 138 | + customframe.pack(); |
| 139 | + customframe.setVisible(true); |
| 140 | + } |
| 141 | + |
| 142 | + public void performTest() throws InterruptedException, InvocationTargetException { |
| 143 | + robot.setAutoDelay(100); |
| 144 | + robot.delay(2000); |
| 145 | + |
| 146 | + robot.clickMouseOn(frametf1, BUTTON1_DOWN_MASK); |
| 147 | + robot.hitKey(KeyEvent.VK_A); |
| 148 | + robot.hitKey(KeyEvent.VK_B); |
| 149 | + robot.hitKey(KeyEvent.VK_C); |
| 150 | + robot.hitKey(KeyEvent.VK_D); |
| 151 | + robot.hitKey(KeyEvent.VK_E); |
| 152 | + |
| 153 | + robot.clickMouseOn(windowtf1, BUTTON1_DOWN_MASK); |
| 154 | + robot.hitKey(KeyEvent.VK_F); |
| 155 | + robot.hitKey(KeyEvent.VK_G); |
| 156 | + robot.hitKey(KeyEvent.VK_H); |
| 157 | + robot.hitKey(KeyEvent.VK_I); |
| 158 | + robot.hitKey(KeyEvent.VK_J); |
| 159 | + |
| 160 | + robot.clickMouseOn(dialogtf1, BUTTON1_DOWN_MASK); |
| 161 | + robot.hitKey(KeyEvent.VK_K); |
| 162 | + robot.hitKey(KeyEvent.VK_L); |
| 163 | + robot.hitKey(KeyEvent.VK_M); |
| 164 | + robot.hitKey(KeyEvent.VK_N); |
| 165 | + robot.hitKey(KeyEvent.VK_O); |
| 166 | + |
| 167 | + robot.clickMouseOn(testpanel.tf1, BUTTON1_DOWN_MASK); |
| 168 | + robot.hitKey(KeyEvent.VK_P); |
| 169 | + robot.hitKey(KeyEvent.VK_Q); |
| 170 | + robot.hitKey(KeyEvent.VK_R); |
| 171 | + robot.hitKey(KeyEvent.VK_S); |
| 172 | + robot.hitKey(KeyEvent.VK_T); |
| 173 | + |
| 174 | + verifierCalled = false; |
| 175 | + robot.clickMouseOn(frametf2, BUTTON1_DOWN_MASK); |
| 176 | + robot.delay(2000); |
| 177 | + SwingUtilities.invokeAndWait(() -> { |
| 178 | + isFocused = frametf1.isFocusOwner(); |
| 179 | + }); |
| 180 | + if (!isFocused) { |
| 181 | + throw new RuntimeException("Focus error. Test failed!"); |
| 182 | + } |
| 183 | + if (!verifierCalled) { |
| 184 | + throw new RuntimeException("Verifier was not called upon regaining focus"); |
| 185 | + } |
| 186 | + |
| 187 | + verifierCalled = false; |
| 188 | + robot.clickMouseOn(windowtf2, BUTTON1_DOWN_MASK); |
| 189 | + robot.delay(2000); |
| 190 | + SwingUtilities.invokeAndWait(() -> { |
| 191 | + isFocused = windowtf1.isFocusOwner(); |
| 192 | + }); |
| 193 | + if (!isFocused) { |
| 194 | + throw new RuntimeException("Focus error. Test failed!"); |
| 195 | + } |
| 196 | + if (!verifierCalled) { |
| 197 | + throw new RuntimeException("Verifier was not called upon regaining focus"); |
| 198 | + } |
| 199 | + |
| 200 | + testpanel.verifierCalled = false; |
| 201 | + robot.clickMouseOn(testpanel.tf2, BUTTON1_DOWN_MASK); |
| 202 | + robot.delay(2000); |
| 203 | + SwingUtilities.invokeAndWait(() -> { |
| 204 | + isFocused = testpanel.tf1.isFocusOwner(); |
| 205 | + }); |
| 206 | + if (!isFocused) { |
| 207 | + throw new RuntimeException("Focus error. Test failed!"); |
| 208 | + } |
| 209 | + if (!testpanel.verifierCalled) { |
| 210 | + throw new RuntimeException("Verifier was not called upon regaining focus"); |
| 211 | + } |
| 212 | + |
| 213 | + verifierCalled = false; |
| 214 | + robot.clickMouseOn(dialogtf2, BUTTON1_DOWN_MASK); |
| 215 | + robot.delay(2000); |
| 216 | + SwingUtilities.invokeAndWait(() -> { |
| 217 | + isFocused = dialogtf1.isFocusOwner(); |
| 218 | + }); |
| 219 | + if (!isFocused) { |
| 220 | + throw new RuntimeException("Focus error. Test failed!"); |
| 221 | + } |
| 222 | + if (!verifierCalled) { |
| 223 | + throw new RuntimeException("Verifier was not called upon regaining focus"); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + public void cleanupGUI() { |
| 228 | + if (testframe != null) { |
| 229 | + testframe.dispose(); |
| 230 | + } |
| 231 | + if (testwindowframe != null) { |
| 232 | + testwindowframe.dispose(); |
| 233 | + } |
| 234 | + if (testwindow != null) { |
| 235 | + testwindow.dispose(); |
| 236 | + } |
| 237 | + if (customframe != null) { |
| 238 | + customframe.dispose(); |
| 239 | + } |
| 240 | + if (testdialog != null) { |
| 241 | + testdialog.dispose(); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + public static void main(String[] args) throws InterruptedException, |
| 246 | + InvocationTargetException { |
| 247 | + bug4774166 b = new bug4774166(); |
| 248 | + SwingUtilities.invokeAndWait(b::setupGUI); |
| 249 | + try { |
| 250 | + b.performTest(); |
| 251 | + } finally { |
| 252 | + SwingUtilities.invokeAndWait(b::cleanupGUI); |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments