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