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