|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 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.Button; |
| 25 | +import java.awt.EventQueue; |
| 26 | +import java.awt.FlowLayout; |
| 27 | +import java.awt.Frame; |
| 28 | +import java.awt.Label; |
| 29 | +import java.awt.Point; |
| 30 | +import java.awt.Robot; |
| 31 | +import java.awt.TextField; |
| 32 | +import java.awt.Toolkit; |
| 33 | +import java.awt.datatransfer.Clipboard; |
| 34 | +import java.awt.datatransfer.StringSelection; |
| 35 | +import java.awt.event.ActionEvent; |
| 36 | +import java.awt.event.ActionListener; |
| 37 | +import java.awt.event.InputEvent; |
| 38 | +import java.awt.event.KeyEvent; |
| 39 | + |
| 40 | +import jdk.test.lib.Platform; |
| 41 | + |
| 42 | +/* |
| 43 | + * @test |
| 44 | + * @bug 4124697 |
| 45 | + * @key headful |
| 46 | + * @summary Make sure that after setting and then changing the echo |
| 47 | + * character again, the TextField continues to function as expected. |
| 48 | + * @library /test/lib |
| 49 | + * @build jdk.test.lib.Platform |
| 50 | + * @run main SetEchoCharTest |
| 51 | + */ |
| 52 | + |
| 53 | +public class SetEchoCharTest { |
| 54 | + private static Frame frame; |
| 55 | + private static Robot robot; |
| 56 | + private static TextField tfPassword; |
| 57 | + private static Button btn1; |
| 58 | + private static Button btn2; |
| 59 | + private static volatile Point btn1Loc; |
| 60 | + private static volatile Point btn2Loc; |
| 61 | + |
| 62 | + private static final String CHANGE = "Change echo char"; |
| 63 | + private static final String PRINT = "Print text"; |
| 64 | + private static final String INITIAL_TEXT = "DefaultPwd"; |
| 65 | + private static final String CHANGED_TEXT = "NewPwd"; |
| 66 | + private static final char NEW_ECHO_CHAR = '*'; |
| 67 | + |
| 68 | + public static void main(String[] args) throws Exception { |
| 69 | + try { |
| 70 | + robot = new Robot(); |
| 71 | + robot.setAutoWaitForIdle(true); |
| 72 | + robot.setAutoDelay(50); |
| 73 | + |
| 74 | + EventQueue.invokeAndWait(() -> createAndShowUI()); |
| 75 | + robot.waitForIdle(); |
| 76 | + robot.delay(1000); |
| 77 | + |
| 78 | + testEchoChar(); |
| 79 | + robot.waitForIdle(); |
| 80 | + robot.delay(200); |
| 81 | + |
| 82 | + testNewEchoChar(); |
| 83 | + robot.waitForIdle(); |
| 84 | + robot.delay(200); |
| 85 | + } finally { |
| 86 | + EventQueue.invokeAndWait(() -> { |
| 87 | + if (frame != null) { |
| 88 | + frame.dispose(); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static void createAndShowUI() { |
| 95 | + frame = new Frame("SetEchoCharTest"); |
| 96 | + frame.setLayout(new FlowLayout()); |
| 97 | + |
| 98 | + Label label = new Label("Pwd:"); |
| 99 | + tfPassword = new TextField(INITIAL_TEXT, 10); |
| 100 | + tfPassword.setEchoChar('X'); |
| 101 | + tfPassword.addActionListener((ActionListener) e -> { |
| 102 | + if (e.getActionCommand().equals(CHANGED_TEXT)) { |
| 103 | + //check the 2nd condition only if ActionEvent |
| 104 | + //is triggered by changed text |
| 105 | + if (!(tfPassword.getText().equals(CHANGED_TEXT) |
| 106 | + && tfPassword.getEchoChar() == NEW_ECHO_CHAR)) { |
| 107 | + throw new RuntimeException("Test Failed!!! TextField not working" |
| 108 | + + " as expected after echo char change"); |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + frame.add(label); |
| 113 | + frame.add(tfPassword); |
| 114 | + |
| 115 | + btn1 = new Button(PRINT); |
| 116 | + btn1.addActionListener(new BtnActionListener()); |
| 117 | + frame.add(btn1); |
| 118 | + |
| 119 | + btn2 = new Button(CHANGE); |
| 120 | + btn2.addActionListener(new BtnActionListener()); |
| 121 | + frame.add(btn2); |
| 122 | + frame.setSize(200,200); |
| 123 | + frame.setLocationRelativeTo(null); |
| 124 | + frame.setVisible(true); |
| 125 | + } |
| 126 | + |
| 127 | + private static void testEchoChar() throws Exception { |
| 128 | + EventQueue.invokeAndWait(() -> { |
| 129 | + btn1Loc = btn1.getLocationOnScreen(); |
| 130 | + btn2Loc = btn2.getLocationOnScreen(); |
| 131 | + }); |
| 132 | + |
| 133 | + robot.mouseMove(btn1Loc.x + btn1.getWidth() / 2, |
| 134 | + btn1Loc.y + btn1.getHeight() / 2); |
| 135 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 136 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 137 | + robot.delay(1000); |
| 138 | + |
| 139 | + robot.mouseMove(btn2Loc.x + btn2.getWidth() / 2, |
| 140 | + btn2Loc.y + btn2.getHeight() / 2); |
| 141 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 142 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 143 | + robot.delay(1000); |
| 144 | + } |
| 145 | + |
| 146 | + private static void testNewEchoChar() { |
| 147 | + StringSelection stringSelection = new StringSelection(CHANGED_TEXT); |
| 148 | + Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
| 149 | + clipboard.setContents(stringSelection, stringSelection); |
| 150 | + |
| 151 | + int ctrlKey = Platform.isOSX() ? KeyEvent.VK_META : KeyEvent.VK_CONTROL; |
| 152 | + robot.keyPress(ctrlKey); |
| 153 | + robot.keyPress(KeyEvent.VK_V); |
| 154 | + robot.keyRelease(KeyEvent.VK_V); |
| 155 | + robot.keyRelease(ctrlKey); |
| 156 | + |
| 157 | + robot.keyPress(KeyEvent.VK_ENTER); |
| 158 | + robot.keyRelease(KeyEvent.VK_ENTER); |
| 159 | + } |
| 160 | + |
| 161 | + private static class BtnActionListener implements ActionListener { |
| 162 | + public void actionPerformed(ActionEvent evt) { |
| 163 | + String ac = evt.getActionCommand(); |
| 164 | + if (CHANGE.equals(ac)) { |
| 165 | + tfPassword.setText(""); |
| 166 | + tfPassword.setEchoChar(NEW_ECHO_CHAR); |
| 167 | + tfPassword.requestFocus(); |
| 168 | + } |
| 169 | + if (PRINT.equals(ac)) { |
| 170 | + if (!tfPassword.getText().equals(INITIAL_TEXT)) { |
| 171 | + throw new RuntimeException("Test Failed!!!" |
| 172 | + + " Initial text not as expected"); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
0 commit comments