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 /* @test Jan 16, 2003
  25  * @bug 4278839
  26  * @summary Incorrect cursor movement between words at the end of line
  27  * @author Anton Nashatyrev
  28  * @library ../../../regtesthelpers
  29  * @build Util
  30  * @run main bug4278839
  31  */
  32 
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import javax.swing.*;
  36 import sun.awt.SunToolkit;
  37 
  38 public class bug4278839 extends JFrame {
  39 
  40     private static boolean passed = true;
  41     private static JTextArea area;
  42     private static Robot robo;
  43     private static SunToolkit toolkit;
  44 
  45     public static void main(String[] args) {
  46         try {
  47 
  48             toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  49             robo = new Robot();
  50             robo.setAutoDelay(100);
  51 
  52             SwingUtilities.invokeAndWait(new Runnable() {
  53                 @Override
  54                 public void run() {
  55                     createAndShowGUI();
  56                 }
  57             });
  58 
  59             toolkit.realSync();
  60 
  61             clickMouse();
  62             toolkit.realSync();
  63 
  64 
  65             if ("Aqua".equals(UIManager.getLookAndFeel().getID())) {
  66                 Util.hitKeys(robo, KeyEvent.VK_HOME);
  67             } else {
  68                 Util.hitKeys(robo, KeyEvent.VK_CONTROL, KeyEvent.VK_HOME);
  69             }
  70             toolkit.realSync();
  71 
  72             passed &= moveCaret(true) == 1;
  73             passed &= moveCaret(true) == 5;
  74             passed &= moveCaret(true) == 8;
  75             passed &= moveCaret(true) == 9;
  76             passed &= moveCaret(true) == 13;
  77             passed &= moveCaret(true) == 16;
  78             passed &= moveCaret(true) == 17;
  79             passed &= moveCaret(false) == 16;
  80             passed &= moveCaret(false) == 13;
  81             passed &= moveCaret(false) == 9;
  82             passed &= moveCaret(false) == 8;
  83             passed &= moveCaret(false) == 5;
  84             passed &= moveCaret(false) == 1;
  85             passed &= moveCaret(false) == 0;
  86 
  87         } catch (Exception e) {
  88             throw new RuntimeException("Test failed because of an exception:",
  89                     e);
  90         }
  91 
  92         if (!passed) {
  93             throw new RuntimeException("Test failed.");
  94         }
  95     }
  96 
  97     private static int moveCaret(boolean right) throws Exception {
  98         Util.hitKeys(robo, getCtrlKey(),
  99                 right ? KeyEvent.VK_RIGHT : KeyEvent.VK_LEFT);
 100         toolkit.realSync();
 101 
 102         final int[] result = new int[1];
 103 
 104         SwingUtilities.invokeAndWait(new Runnable() {
 105 
 106             @Override
 107             public void run() {
 108                 result[0] = area.getCaretPosition();
 109             }
 110         });
 111 
 112         int pos = result[0];
 113         return pos;
 114     }
 115 
 116     private static void clickMouse() throws Exception {
 117         final Rectangle result[] = new Rectangle[1];
 118 
 119         SwingUtilities.invokeAndWait(new Runnable() {
 120             @Override
 121             public void run() {
 122                 result[0] = new Rectangle(area.getLocationOnScreen(), area.getSize());
 123             }
 124         });
 125 
 126         Rectangle rect = result[0];
 127 
 128         robo.mouseMove(rect.x + rect.width / 2, rect.y + rect.width / 2);
 129         robo.mousePress(InputEvent.BUTTON1_MASK);
 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 createAndShowGUI() {
 146         JFrame frame = new JFrame();
 147         frame.setTitle("Bug# 4278839");
 148         frame.setSize(200, 200);
 149         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 150         area = new JTextArea("\naaa bbb\nccc ddd\n");
 151         frame.getContentPane().add(new JScrollPane(area));
 152         frame.setVisible(true);
 153     }
 154 }