Skip to content

Commit e75bb0b

Browse files
author
duke
committed
Backport 8d73fe91bccd1da53424b9f8a52d9efafabeb243
1 parent f1d867d commit e75bb0b

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/java.desktop/macosx/classes/com/apple/laf/AquaKeyBindings.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ LateBoundInputMap getPasswordFieldInputMap() {
157157
"shift alt KP_LEFT", null,
158158
"shift alt RIGHT", null,
159159
"shift alt KP_RIGHT", null,
160+
"alt BACK_SPACE", null,
161+
"ctrl W", null,
162+
"alt DELETE", null,
160163
}));
161164
}
162165

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 8358813
28+
* @summary Password fields' InputMap should not include any word-related action.
29+
*
30+
* @run main PasswordFieldInputMapWordTest
31+
*/
32+
33+
import java.util.Collection;
34+
import java.util.Set;
35+
36+
import javax.swing.InputMap;
37+
import javax.swing.JComponent;
38+
import javax.swing.JPasswordField;
39+
import javax.swing.KeyStroke;
40+
import javax.swing.SwingUtilities;
41+
import javax.swing.UIManager;
42+
import javax.swing.UnsupportedLookAndFeelException;
43+
import javax.swing.text.DefaultEditorKit;
44+
45+
public class PasswordFieldInputMapWordTest {
46+
public static void main(String[] args) throws Exception {
47+
for (UIManager.LookAndFeelInfo laf :
48+
UIManager.getInstalledLookAndFeels()) {
49+
System.out.println("Testing LAF: " + laf.getClassName());
50+
SwingUtilities.invokeAndWait(() -> {
51+
if (setLookAndFeel(laf)) {
52+
runTest();
53+
}
54+
});
55+
}
56+
}
57+
58+
private static boolean setLookAndFeel(UIManager.LookAndFeelInfo laf) {
59+
try {
60+
UIManager.setLookAndFeel(laf.getClassName());
61+
return true;
62+
} catch (UnsupportedLookAndFeelException e) {
63+
System.err.println("Skipping unsupported look and feel:");
64+
e.printStackTrace();
65+
return false;
66+
} catch (Exception e) {
67+
throw new RuntimeException(e);
68+
}
69+
}
70+
71+
static int[] inputMapConditions = new int[] {
72+
JComponent.WHEN_IN_FOCUSED_WINDOW,
73+
JComponent.WHEN_FOCUSED,
74+
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
75+
};
76+
77+
/**
78+
* These are all the actions with "word" in their field name.
79+
*/
80+
static Collection<String> wordActions = Set.of(
81+
DefaultEditorKit.deleteNextWordAction,
82+
DefaultEditorKit.deletePrevWordAction,
83+
DefaultEditorKit.beginWordAction,
84+
DefaultEditorKit.endWordAction,
85+
DefaultEditorKit.selectionBeginWordAction,
86+
DefaultEditorKit.selectionEndWordAction,
87+
DefaultEditorKit.previousWordAction,
88+
DefaultEditorKit.nextWordAction,
89+
DefaultEditorKit.selectionPreviousWordAction,
90+
DefaultEditorKit.selectionNextWordAction
91+
);
92+
93+
private static void runTest() {
94+
JPasswordField field = new JPasswordField();
95+
96+
boolean testPassed = true;
97+
for (int condition : inputMapConditions) {
98+
InputMap inputMap = field.getInputMap(condition);
99+
if (inputMap.allKeys() == null) {
100+
continue;
101+
}
102+
for (KeyStroke keyStroke : inputMap.allKeys()) {
103+
Object actionBinding = inputMap.get(keyStroke);
104+
if (wordActions.contains(actionBinding)) {
105+
if (testPassed) {
106+
System.err.println("The following inputs/actions should not be available in a JPasswordField:");
107+
}
108+
System.err.println(inputMap.get(keyStroke) + " (try typing " + keyStroke + ")");
109+
testPassed = false;
110+
}
111+
}
112+
}
113+
114+
if (!testPassed) {
115+
throw new RuntimeException("One or more input/action binding was observed for a JPasswordField.");
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)