1 /* 2 * Copyright (c) 2011, 2020, 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 5074573 8196100 28 * @summary tests delte-next-word and delete-prev-word actions for all text compnents and all look&feels 29 * @run main bug5074573 30 */ 31 32 import java.awt.Robot; 33 import java.awt.event.KeyEvent; 34 import java.util.Arrays; 35 import java.util.List; 36 37 import javax.swing.JEditorPane; 38 import javax.swing.JFormattedTextField; 39 import javax.swing.JFrame; 40 import javax.swing.JPasswordField; 41 import javax.swing.JTextArea; 42 import javax.swing.JTextField; 43 import javax.swing.JTextPane; 44 import javax.swing.SwingUtilities; 45 import javax.swing.UIManager; 46 import javax.swing.text.Caret; 47 import javax.swing.text.JTextComponent; 48 49 public class bug5074573 { 50 51 private static JTextComponent textComponent; 52 final static String testString = "123 456 789"; 53 final static String resultString = "456 "; 54 final static List<Class<? extends JTextComponent>> textClasses = Arrays.asList( 55 JTextArea.class, JEditorPane.class, JTextPane.class, 56 JTextField.class, JFormattedTextField.class, JPasswordField.class); 57 main(String[] args)58 public static void main(String[] args) throws Exception { 59 for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 60 UIManager.setLookAndFeel(info.getClassName()); 61 System.out.println(info); 62 for (Class<? extends JTextComponent> clazz : textClasses) { 63 boolean res = test(clazz); 64 if (!res && clazz != JPasswordField.class) { 65 throw new RuntimeException("failed"); 66 } 67 } 68 } 69 } 70 test(final Class<? extends JTextComponent> textComponentClass)71 static boolean test(final Class<? extends JTextComponent> textComponentClass) throws Exception { 72 Robot robot = new Robot(); 73 robot.setAutoWaitForIdle(true); 74 robot.setAutoDelay(50); 75 76 77 SwingUtilities.invokeAndWait(new Runnable() { 78 79 @Override 80 public void run() { 81 initialize(textComponentClass); 82 } 83 }); 84 85 robot.waitForIdle(); 86 87 // Remove selection from JTextField components for the Aqua Look & Feel 88 if (textComponent instanceof JTextField && "Aqua".equals(UIManager.getLookAndFeel().getID())) { 89 SwingUtilities.invokeAndWait(new Runnable() { 90 91 @Override 92 public void run() { 93 Caret caret = textComponent.getCaret(); 94 int dot = caret.getDot(); 95 textComponent.select(dot, dot); 96 } 97 }); 98 99 robot.waitForIdle(); 100 } 101 102 robot.keyPress(getCtrlKey()); 103 robot.keyPress(KeyEvent.VK_BACK_SPACE); 104 robot.keyRelease(KeyEvent.VK_BACK_SPACE); 105 robot.keyRelease(getCtrlKey()); 106 robot.waitForIdle(); 107 108 SwingUtilities.invokeAndWait(new Runnable() { 109 110 @Override 111 public void run() { 112 Caret caret = textComponent.getCaret(); 113 caret.setDot(0); 114 } 115 }); 116 robot.waitForIdle(); 117 118 robot.keyPress(getCtrlKey()); 119 robot.keyPress(KeyEvent.VK_DELETE); 120 robot.keyRelease(KeyEvent.VK_DELETE); 121 robot.keyRelease(getCtrlKey()); 122 robot.waitForIdle(); 123 124 return resultString.equals(getText()); 125 } 126 getText()127 private static String getText() throws Exception { 128 final String[] result = new String[1]; 129 130 SwingUtilities.invokeAndWait(new Runnable() { 131 @Override 132 public void run() { 133 result[0] = textComponent.getText(); 134 } 135 }); 136 137 return result[0]; 138 } 139 140 /** 141 * Gets a control key related to the used Look & Feel 142 * Returns VK_ALT for Aqua and VK_CONTROL for others 143 */ getCtrlKey()144 public static int getCtrlKey() { 145 146 if ("Aqua".equals(UIManager.getLookAndFeel().getID())) { 147 return KeyEvent.VK_ALT; 148 } 149 150 return KeyEvent.VK_CONTROL; 151 } 152 initialize(Class<? extends JTextComponent> textComponentClass)153 private static void initialize(Class<? extends JTextComponent> textComponentClass) { 154 try { 155 JFrame frame = new JFrame(); 156 textComponent = textComponentClass.newInstance(); 157 textComponent.setText(testString); 158 frame.add(textComponent); 159 frame.pack(); 160 frame.setLocationRelativeTo(null); 161 frame.setVisible(true); 162 textComponent.requestFocus(); 163 Caret caret = textComponent.getCaret(); 164 caret.setDot(textComponent.getDocument().getLength()); 165 } catch (Exception e) { 166 throw new RuntimeException(e); 167 } 168 } 169 } 170