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