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