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 import sun.awt.SunToolkit;
  39 
  40 public class bug4278839 extends JFrame {
  41 
  42     private static boolean passed = true;
  43     private static JTextArea area;
  44     private static Robot robo;
  45     private static SunToolkit toolkit;
  46 
  47     public static void main(String[] args) {
  48         try {
  49 
  50             toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  51             robo = new Robot();
  52             robo.setAutoDelay(100);
  53 
  54             SwingUtilities.invokeAndWait(new Runnable() {
  55                 @Override
  56                 public void run() {
  57                     createAndShowGUI();
  58                 }
  59             });
  60 
  61             toolkit.realSync();
  62 
  63             clickMouse();
  64             toolkit.realSync();
  65 
  66 
  67             if ("Aqua".equals(UIManager.getLookAndFeel().getID())) {
  68                 Util.hitKeys(robo, KeyEvent.VK_HOME);
  69             } else {
  70                 Util.hitKeys(robo, KeyEvent.VK_CONTROL, KeyEvent.VK_HOME);
  71             }
  72             toolkit.realSync();
  73 
  74             passed &= moveCaret(true) == 1;
  75             passed &= moveCaret(true) == 5;
  76             passed &= moveCaret(true) == 8;
  77             passed &= moveCaret(true) == 9;
  78             passed &= moveCaret(true) == 13;
  79             passed &= moveCaret(true) == 16;
  80             passed &= moveCaret(true) == 17;
  81             passed &= moveCaret(false) == 16;
  82             passed &= moveCaret(false) == 13;
  83             passed &= moveCaret(false) == 9;
  84             passed &= moveCaret(false) == 8;
  85             passed &= moveCaret(false) == 5;
  86             passed &= moveCaret(false) == 1;
  87             passed &= moveCaret(false) == 0;
  88 
  89         } catch (Exception e) {
  90             throw new RuntimeException("Test failed because of an exception:",
  91                     e);
  92         }
  93 
  94         if (!passed) {
  95             throw new RuntimeException("Test failed.");
  96         }
  97     }
  98 
  99     private static int moveCaret(boolean right) throws Exception {
 100         Util.hitKeys(robo, getCtrlKey(),
 101                 right ? KeyEvent.VK_RIGHT : KeyEvent.VK_LEFT);
 102         toolkit.realSync();
 103 
 104         final int[] result = new int[1];
 105 
 106         SwingUtilities.invokeAndWait(new Runnable() {
 107 
 108             @Override
 109             public void run() {
 110                 result[0] = area.getCaretPosition();
 111             }
 112         });
 113 
 114         int pos = result[0];
 115         return pos;
 116     }
 117 
 118     private static void clickMouse() throws Exception {
 119         final Rectangle result[] = new Rectangle[1];
 120 
 121         SwingUtilities.invokeAndWait(new Runnable() {
 122             @Override
 123             public void run() {
 124                 result[0] = new Rectangle(area.getLocationOnScreen(), area.getSize());
 125             }
 126         });
 127 
 128         Rectangle rect = result[0];
 129 
 130         robo.mouseMove(rect.x + rect.width / 2, rect.y + rect.width / 2);
 131         robo.mousePress(InputEvent.BUTTON1_MASK);
 132     }
 133 
 134     /**
 135      * Gets a control key related to the used Look & Feel
 136      * Returns VK_ALT for Aqua and VK_CONTROL for others
 137      */
 138     public static int getCtrlKey() {
 139 
 140         if ("Aqua".equals(UIManager.getLookAndFeel().getID())) {
 141             return KeyEvent.VK_ALT;
 142         }
 143 
 144         return KeyEvent.VK_CONTROL;
 145     }
 146 
 147     private static void createAndShowGUI() {
 148         JFrame frame = new JFrame();
 149         frame.setTitle("Bug# 4278839");
 150         frame.setSize(200, 200);
 151         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 152         area = new JTextArea("\naaa bbb\nccc ddd\n");
 153         frame.getContentPane().add(new JScrollPane(area));
 154         frame.setVisible(true);
 155     }
 156 }