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