7109085: Test use hotkeys not intended for Mac
authorrupashka
Mon, 21 Nov 2011 18:22:30 +0400
changeset 11096 a39b7342177f
parent 11095 51b42f922950
child 11097 5ecd0d351c92
7109085: Test use hotkeys not intended for Mac Reviewed-by: rupashka Contributed-by: alexandr.scherbatiy@oracle.com
jdk/test/javax/swing/regtesthelpers/Util.java
jdk/test/javax/swing/text/DefaultEditorKit/4278839/bug4278839.java
jdk/test/javax/swing/text/JTextComponent/5074573/bug5074573.java
jdk/test/javax/swing/text/html/HTMLEditorKit/5043626/bug5043626.java
--- a/jdk/test/javax/swing/regtesthelpers/Util.java	Fri Nov 18 15:49:09 2011 -0800
+++ b/jdk/test/javax/swing/regtesthelpers/Util.java	Mon Nov 21 18:22:30 2011 +0400
@@ -140,4 +140,17 @@
 
         return null;
     }
+
+     /**
+     * Hits keys by robot.
+     */
+    public static void hitKeys(Robot robot, int... keys) {
+        for (int i = 0; i < keys.length; i++) {
+            robot.keyPress(keys[i]);
+        }
+
+        for (int i = keys.length - 1; i >= 0; i--) {
+            robot.keyRelease(keys[i]);
+        }
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/text/DefaultEditorKit/4278839/bug4278839.java	Mon Nov 21 18:22:30 2011 +0400
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test Jan 16, 2003
+ * @bug 4278839
+ * @summary Incorrect cursor movement between words at the end of line
+ * @author Anton Nashatyrev
+ * @library ../../../regtesthelpers
+ * @build Util
+ * @run main bug4278839
+ */
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import sun.awt.SunToolkit;
+
+public class bug4278839 extends JFrame {
+
+    private static boolean passed = true;
+    private static JTextArea area;
+    private static Robot robo;
+    private static SunToolkit toolkit;
+
+    public static void main(String[] args) {
+        try {
+
+            toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
+            robo = new Robot();
+            robo.setAutoDelay(100);
+
+            SwingUtilities.invokeAndWait(new Runnable() {
+                @Override
+                public void run() {
+                    createAndShowGUI();
+                }
+            });
+
+            toolkit.realSync();
+
+            clickMouse();
+            toolkit.realSync();
+
+
+            if ("Aqua".equals(UIManager.getLookAndFeel().getID())) {
+                Util.hitKeys(robo, KeyEvent.VK_HOME);
+            } else {
+                Util.hitKeys(robo, KeyEvent.VK_CONTROL, KeyEvent.VK_HOME);
+            }
+            toolkit.realSync();
+
+            passed &= moveCaret(true) == 1;
+            passed &= moveCaret(true) == 5;
+            passed &= moveCaret(true) == 8;
+            passed &= moveCaret(true) == 9;
+            passed &= moveCaret(true) == 13;
+            passed &= moveCaret(true) == 16;
+            passed &= moveCaret(true) == 17;
+            passed &= moveCaret(false) == 16;
+            passed &= moveCaret(false) == 13;
+            passed &= moveCaret(false) == 9;
+            passed &= moveCaret(false) == 8;
+            passed &= moveCaret(false) == 5;
+            passed &= moveCaret(false) == 1;
+            passed &= moveCaret(false) == 0;
+
+        } catch (Exception e) {
+            throw new RuntimeException("Test failed because of an exception:",
+                    e);
+        }
+
+        if (!passed) {
+            throw new RuntimeException("Test failed.");
+        }
+    }
+
+    private static int moveCaret(boolean right) throws Exception {
+        Util.hitKeys(robo, getCtrlKey(),
+                right ? KeyEvent.VK_RIGHT : KeyEvent.VK_LEFT);
+        toolkit.realSync();
+
+        final int[] result = new int[1];
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+
+            @Override
+            public void run() {
+                result[0] = area.getCaretPosition();
+            }
+        });
+
+        int pos = result[0];
+        return pos;
+    }
+
+    private static void clickMouse() throws Exception {
+        final Rectangle result[] = new Rectangle[1];
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                result[0] = new Rectangle(area.getLocationOnScreen(), area.getSize());
+            }
+        });
+
+        Rectangle rect = result[0];
+
+        robo.mouseMove(rect.x + rect.width / 2, rect.y + rect.width / 2);
+        robo.mousePress(InputEvent.BUTTON1_MASK);
+    }
+
+    /**
+     * Gets a control key related to the used Look & Feel
+     * Returns VK_ALT for Aqua and VK_CONTROL for others
+     */
+    public static int getCtrlKey() {
+
+        if ("Aqua".equals(UIManager.getLookAndFeel().getID())) {
+            return KeyEvent.VK_ALT;
+        }
+
+        return KeyEvent.VK_CONTROL;
+    }
+
+    private static void createAndShowGUI() {
+        JFrame frame = new JFrame();
+        frame.setTitle("Bug# 4278839");
+        frame.setSize(200, 200);
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        area = new JTextArea("\naaa bbb\nccc ddd\n");
+        frame.getContentPane().add(new JScrollPane(area));
+        frame.setVisible(true);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/text/JTextComponent/5074573/bug5074573.java	Mon Nov 21 18:22:30 2011 +0400
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 5074573
+ * @summary tests delte-next-word and delete-prev-word actions for all text compnents and all look&feels
+ * @author Igor Kushnirskiy
+ * @run main bug5074573
+ */
+
+import java.util.*;
+import java.awt.Robot;
+import java.awt.Toolkit;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.text.*;
+import sun.awt.SunToolkit;
+
+public class bug5074573 {
+
+    private static JTextComponent textComponent;
+    final static String testString = "123 456 789";
+    final static String resultString = "456 ";
+    final static List<Class<? extends JTextComponent>> textClasses = Arrays.asList(
+            JTextArea.class, JEditorPane.class, JTextPane.class,
+            JTextField.class, JFormattedTextField.class, JPasswordField.class);
+
+    public static void main(String[] args) throws Exception {
+        for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
+            UIManager.setLookAndFeel(info.getClassName());
+            System.out.println(info);
+            for (Class<? extends JTextComponent> clazz : textClasses) {
+                boolean res = test(clazz);
+                if (!res && clazz != JPasswordField.class) {
+                    throw new RuntimeException("failed");
+                }
+            }
+        }
+    }
+
+    static boolean test(final Class<? extends JTextComponent> textComponentClass) throws Exception {
+        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
+        Robot robot = new Robot();
+        robot.setAutoWaitForIdle(true);
+        robot.setAutoDelay(50);
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+
+            @Override
+            public void run() {
+                initialize(textComponentClass);
+            }
+        });
+
+        toolkit.realSync();
+
+        // Remove selection from JTextField components for the Aqua Look & Feel
+        if (textComponent instanceof JTextField && "Aqua".equals(UIManager.getLookAndFeel().getID())) {
+            SwingUtilities.invokeAndWait(new Runnable() {
+
+                @Override
+                public void run() {
+                    Caret caret = textComponent.getCaret();
+                    int dot = caret.getDot();
+                    textComponent.select(dot, dot);
+                }
+            });
+
+            toolkit.realSync();
+        }
+
+        robot.keyPress(getCtrlKey());
+        robot.keyPress(KeyEvent.VK_BACK_SPACE);
+        robot.keyRelease(KeyEvent.VK_BACK_SPACE);
+        robot.keyRelease(getCtrlKey());
+        toolkit.realSync();
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+
+            @Override
+            public void run() {
+                Caret caret = textComponent.getCaret();
+                caret.setDot(0);
+            }
+        });
+        toolkit.realSync();
+
+        robot.keyPress(getCtrlKey());
+        robot.keyPress(KeyEvent.VK_DELETE);
+        robot.keyRelease(KeyEvent.VK_DELETE);
+        robot.keyRelease(getCtrlKey());
+        toolkit.realSync();
+
+        return resultString.equals(getText());
+    }
+
+    private static String getText() throws Exception {
+        final String[] result = new String[1];
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                result[0] = textComponent.getText();
+            }
+        });
+
+        return result[0];
+    }
+
+    /**
+     * Gets a control key related to the used Look & Feel
+     * Returns VK_ALT for Aqua and VK_CONTROL for others
+     */
+    public static int getCtrlKey() {
+
+        if ("Aqua".equals(UIManager.getLookAndFeel().getID())) {
+            return KeyEvent.VK_ALT;
+        }
+
+        return KeyEvent.VK_CONTROL;
+    }
+
+    private static void initialize(Class<? extends JTextComponent> textComponentClass) {
+        try {
+            JFrame frame = new JFrame();
+            textComponent = textComponentClass.newInstance();
+            textComponent.setText(testString);
+            frame.add(textComponent);
+            frame.pack();
+            frame.setVisible(true);
+            textComponent.requestFocus();
+            Caret caret = textComponent.getCaret();
+            caret.setDot(textComponent.getDocument().getLength());
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/text/html/HTMLEditorKit/5043626/bug5043626.java	Mon Nov 21 18:22:30 2011 +0400
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 5043626
+ * @summary  Tests pressing Home or Ctrl+Home set cursor to invisible element <head>
+ * @author Alexander Potochkin
+ * @library ../../../../regtesthelpers
+ * @build Util
+ * @run main bug5043626
+ */
+
+import java.awt.Robot;
+import java.awt.Toolkit;
+import javax.swing.*;
+import javax.swing.text.Document;
+import javax.swing.text.BadLocationException;
+import java.awt.event.KeyEvent;
+import sun.awt.SunToolkit;
+
+public class bug5043626 {
+
+    private static Document doc;
+    private static Robot robot;
+
+    public static void main(String[] args) throws Exception {
+        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
+        robot = new Robot();
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                createAndShowGUI();
+            }
+        });
+
+        toolkit.realSync();
+
+        Util.hitKeys(robot, KeyEvent.VK_HOME);
+        Util.hitKeys(robot, KeyEvent.VK_1);
+
+        toolkit.realSync();
+
+        String test = getText();
+
+        if (!"1test".equals(test)) {
+            throw new RuntimeException("Begin line action set cursor inside <head> tag");
+        }
+
+        Util.hitKeys(robot, KeyEvent.VK_HOME);
+        Util.hitKeys(robot, KeyEvent.VK_2);
+
+        toolkit.realSync();
+
+        test = getText();
+
+        if (!"21test".equals(test)) {
+            throw new RuntimeException("Begin action set cursor inside <head> tag");
+        }
+    }
+
+    private static String getText() throws Exception {
+        final String[] result = new String[1];
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                try {
+                    result[0] = doc.getText(0, doc.getLength()).trim();
+                } catch (BadLocationException ex) {
+                    ex.printStackTrace();
+                }
+            }
+        });
+
+        return result[0];
+    }
+
+    private static void createAndShowGUI() {
+        JFrame frame = new JFrame();
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+        JEditorPane editorPane = new JEditorPane();
+        editorPane.setContentType("text/html");
+        editorPane.setText("test");
+        editorPane.setEditable(true);
+        frame.add(editorPane);
+        frame.pack();
+        frame.setVisible(true);
+        doc = editorPane.getDocument();
+        editorPane.setCaretPosition(doc.getLength());
+    }
+}