|
| 1 | +/* |
| 2 | + * Copyright (c) 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 | + * @key headful |
| 27 | + * @bug 4231444 8354646 |
| 28 | + * @summary Password fields' ActionMap needs to replace |
| 29 | + * DefaultEditorKit.selectWordAction with |
| 30 | + * DefaultEditorKit.selectLineAction. |
| 31 | + * |
| 32 | + * @run main PasswordSelectionWordTest |
| 33 | + */ |
| 34 | + |
| 35 | +import javax.swing.Action; |
| 36 | +import javax.swing.JPasswordField; |
| 37 | +import javax.swing.SwingUtilities; |
| 38 | +import javax.swing.UIManager; |
| 39 | +import javax.swing.UnsupportedLookAndFeelException; |
| 40 | +import javax.swing.plaf.basic.BasicTextUI; |
| 41 | +import javax.swing.text.DefaultEditorKit; |
| 42 | +import java.awt.event.ActionEvent; |
| 43 | + |
| 44 | +public class PasswordSelectionWordTest { |
| 45 | + public static void main(String[] args) throws Exception { |
| 46 | + for (UIManager.LookAndFeelInfo laf : |
| 47 | + UIManager.getInstalledLookAndFeels()) { |
| 48 | + System.out.println("Testing LAF: " + laf.getClassName()); |
| 49 | + SwingUtilities.invokeAndWait(() -> { |
| 50 | + if (setLookAndFeel(laf)) { |
| 51 | + runTest(); |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static boolean setLookAndFeel(UIManager.LookAndFeelInfo laf) { |
| 58 | + try { |
| 59 | + UIManager.setLookAndFeel(laf.getClassName()); |
| 60 | + return true; |
| 61 | + } catch (UnsupportedLookAndFeelException e) { |
| 62 | + System.err.println("Skipping unsupported look and feel:"); |
| 63 | + e.printStackTrace(); |
| 64 | + return false; |
| 65 | + } catch (Exception e) { |
| 66 | + throw new RuntimeException(e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static void runTest() { |
| 71 | + String str = "one two three"; |
| 72 | + JPasswordField field = new JPasswordField(str); |
| 73 | + if (!(field.getUI() instanceof BasicTextUI)) { |
| 74 | + throw new RuntimeException("Unexpected condition: JPasswordField UI was " + field.getUI()); |
| 75 | + } |
| 76 | + System.out.println("Testing " + field.getUI()); |
| 77 | + |
| 78 | + // do something (anything) to initialize the Views: |
| 79 | + field.setSize(100, 100); |
| 80 | + field.addNotify(); |
| 81 | + |
| 82 | + Action action = field.getActionMap().get( |
| 83 | + DefaultEditorKit.selectWordAction); |
| 84 | + action.actionPerformed(new ActionEvent(field, 0, "")); |
| 85 | + int selectionStart = field.getSelectionStart(); |
| 86 | + int selectionEnd = field.getSelectionEnd(); |
| 87 | + System.out.println("selectionStart = " + selectionStart); |
| 88 | + System.out.println("selectionEnd = " + selectionEnd); |
| 89 | + if (selectionStart != 0 || selectionEnd != str.length()) { |
| 90 | + throw new RuntimeException("selectionStart = " + selectionStart + |
| 91 | + " and selectionEnd = " + selectionEnd); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments