|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2025, 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 4257434 |
| 27 | + * @summary Ensures that the right results are produced by the |
| 28 | + * carriage return keys. |
| 29 | + * @library /java/awt/regtesthelpers |
| 30 | + * @build PassFailJFrame |
| 31 | + * @run main/manual CRTest |
| 32 | + */ |
| 33 | + |
| 34 | +import java.awt.BorderLayout; |
| 35 | +import java.awt.Button; |
| 36 | +import java.awt.Frame; |
| 37 | +import java.awt.TextField; |
| 38 | +import java.awt.event.ActionEvent; |
| 39 | +import java.awt.event.ActionListener; |
| 40 | +import java.awt.event.KeyEvent; |
| 41 | +import java.awt.event.KeyListener; |
| 42 | +import java.lang.reflect.InvocationTargetException; |
| 43 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 44 | + |
| 45 | +public class CRTest extends Frame implements KeyListener, ActionListener { |
| 46 | + StringBuilder error = new StringBuilder(); |
| 47 | + AtomicBoolean actionCompleted = new AtomicBoolean(false); |
| 48 | + static String INSTRUCTIONS = """ |
| 49 | + This test requires keyboard with the numeric keypad (numpad). |
| 50 | + If your keyboard does not have numpad press "Pass" to skip testing. |
| 51 | + Click on the text field in window named "Check KeyChar values". |
| 52 | + Press Enter on keypad. Then press Return key on a standard keyboard. |
| 53 | + Then click on "Done" button. Test will pass or fail automatically. |
| 54 | + """; |
| 55 | + |
| 56 | + public CRTest() { |
| 57 | + super("Check KeyChar values"); |
| 58 | + setLayout(new BorderLayout()); |
| 59 | + TextField tf = new TextField(30); |
| 60 | + |
| 61 | + tf.addKeyListener(this); |
| 62 | + tf.addActionListener(this); |
| 63 | + |
| 64 | + add(tf, BorderLayout.CENTER); |
| 65 | + |
| 66 | + Button done = new Button("Done"); |
| 67 | + done.addActionListener((event) -> { |
| 68 | + checkAndComplete(); |
| 69 | + }); |
| 70 | + add(done, BorderLayout.SOUTH); |
| 71 | + pack(); |
| 72 | + } |
| 73 | + |
| 74 | + public void checkAndComplete() { |
| 75 | + if (!actionCompleted.get()) { |
| 76 | + error.append("\nNo action received!"); |
| 77 | + } |
| 78 | + |
| 79 | + if (!error.isEmpty()) { |
| 80 | + PassFailJFrame.forceFail(error.toString()); |
| 81 | + } else { |
| 82 | + PassFailJFrame.forcePass(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public void keyPressed(KeyEvent evt) { |
| 87 | + if ((evt.getKeyChar() != '\n') || (evt.getKeyCode() != KeyEvent.VK_ENTER)) { |
| 88 | + error.append("\nKeyPressed: Unexpected code " + evt.getKeyCode()); |
| 89 | + } else { |
| 90 | + PassFailJFrame.log("KeyPressed Test PASSED"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public void keyTyped(KeyEvent evt) { |
| 95 | + if ((evt.getKeyChar() != '\n') || (evt.getKeyCode() != KeyEvent.VK_UNDEFINED)) { |
| 96 | + error.append("\nKeyTyped: Unexpected code " + evt.getKeyCode()); |
| 97 | + } else { |
| 98 | + PassFailJFrame.log("KeyTyped Test PASSED"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public void keyReleased(KeyEvent evt) { |
| 103 | + if ((evt.getKeyChar() != '\n') || (evt.getKeyCode() != KeyEvent.VK_ENTER)) { |
| 104 | + error.append("\nKeyReleased: Unexpected code " + evt.getKeyCode()); |
| 105 | + } else { |
| 106 | + PassFailJFrame.log("KeyReleased Test PASSED"); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public void actionPerformed(ActionEvent evt) { |
| 111 | + PassFailJFrame.log("ActionPerformed Test PASSED"); |
| 112 | + actionCompleted.set(true); |
| 113 | + } |
| 114 | + |
| 115 | + public static void main(String[] args) throws InterruptedException, |
| 116 | + InvocationTargetException { |
| 117 | + PassFailJFrame.builder() |
| 118 | + .instructions(INSTRUCTIONS) |
| 119 | + .logArea(10) |
| 120 | + .testUI(CRTest::new) |
| 121 | + .build() |
| 122 | + .awaitAndCheck(); |
| 123 | + } |
| 124 | +} |
0 commit comments