4533N/A/*
4533N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4533N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4533N/A *
4533N/A * This code is free software; you can redistribute it and/or modify it
4533N/A * under the terms of the GNU General Public License version 2 only, as
4533N/A * published by the Free Software Foundation.
4533N/A *
4533N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4533N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4533N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4533N/A * version 2 for more details (a copy is included in the LICENSE file that
4533N/A * accompanied this code).
4533N/A *
4533N/A * You should have received a copy of the GNU General Public License version
4533N/A * 2 along with this work; if not, write to the Free Software Foundation,
4533N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4533N/A *
4533N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4533N/A * or visit www.oracle.com if you need additional information or have any
4533N/A * questions.
4533N/A */
4533N/A
4533N/A/* @test Jan 16, 2003
4533N/A * @bug 4278839
4533N/A * @summary Incorrect cursor movement between words at the end of line
4533N/A * @author Anton Nashatyrev
4533N/A * @library ../../../regtesthelpers
4533N/A * @build Util
4533N/A * @run main bug4278839
4533N/A */
4533N/A
4533N/Aimport java.awt.*;
4533N/Aimport java.awt.event.*;
4533N/Aimport javax.swing.*;
4533N/Aimport sun.awt.SunToolkit;
4533N/A
4533N/Apublic class bug4278839 extends JFrame {
4533N/A
4533N/A private static boolean passed = true;
4533N/A private static JTextArea area;
4533N/A private static Robot robo;
4533N/A private static SunToolkit toolkit;
4533N/A
4533N/A public static void main(String[] args) {
4533N/A try {
4533N/A
4533N/A toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
4533N/A robo = new Robot();
4533N/A robo.setAutoDelay(100);
4533N/A
4533N/A SwingUtilities.invokeAndWait(new Runnable() {
4533N/A @Override
4533N/A public void run() {
4533N/A createAndShowGUI();
4533N/A }
4533N/A });
4533N/A
4533N/A toolkit.realSync();
4533N/A
4533N/A clickMouse();
4533N/A toolkit.realSync();
4533N/A
4533N/A
4533N/A if ("Aqua".equals(UIManager.getLookAndFeel().getID())) {
4533N/A Util.hitKeys(robo, KeyEvent.VK_HOME);
4533N/A } else {
4533N/A Util.hitKeys(robo, KeyEvent.VK_CONTROL, KeyEvent.VK_HOME);
4533N/A }
4533N/A toolkit.realSync();
4533N/A
4533N/A passed &= moveCaret(true) == 1;
4533N/A passed &= moveCaret(true) == 5;
4533N/A passed &= moveCaret(true) == 8;
4533N/A passed &= moveCaret(true) == 9;
4533N/A passed &= moveCaret(true) == 13;
4533N/A passed &= moveCaret(true) == 16;
4533N/A passed &= moveCaret(true) == 17;
4533N/A passed &= moveCaret(false) == 16;
4533N/A passed &= moveCaret(false) == 13;
4533N/A passed &= moveCaret(false) == 9;
4533N/A passed &= moveCaret(false) == 8;
4533N/A passed &= moveCaret(false) == 5;
4533N/A passed &= moveCaret(false) == 1;
4533N/A passed &= moveCaret(false) == 0;
4533N/A
4533N/A } catch (Exception e) {
4533N/A throw new RuntimeException("Test failed because of an exception:",
4533N/A e);
4533N/A }
4533N/A
4533N/A if (!passed) {
4533N/A throw new RuntimeException("Test failed.");
4533N/A }
4533N/A }
4533N/A
4533N/A private static int moveCaret(boolean right) throws Exception {
4533N/A Util.hitKeys(robo, getCtrlKey(),
4533N/A right ? KeyEvent.VK_RIGHT : KeyEvent.VK_LEFT);
4533N/A toolkit.realSync();
4533N/A
4533N/A final int[] result = new int[1];
4533N/A
4533N/A SwingUtilities.invokeAndWait(new Runnable() {
4533N/A
4533N/A @Override
4533N/A public void run() {
4533N/A result[0] = area.getCaretPosition();
4533N/A }
4533N/A });
4533N/A
4533N/A int pos = result[0];
4533N/A return pos;
4533N/A }
4533N/A
4533N/A private static void clickMouse() throws Exception {
4533N/A final Rectangle result[] = new Rectangle[1];
4533N/A
4533N/A SwingUtilities.invokeAndWait(new Runnable() {
4533N/A @Override
4533N/A public void run() {
4533N/A result[0] = new Rectangle(area.getLocationOnScreen(), area.getSize());
4533N/A }
4533N/A });
4533N/A
4533N/A Rectangle rect = result[0];
4533N/A
4533N/A robo.mouseMove(rect.x + rect.width / 2, rect.y + rect.width / 2);
4533N/A robo.mousePress(InputEvent.BUTTON1_MASK);
4533N/A }
4533N/A
4533N/A /**
4533N/A * Gets a control key related to the used Look & Feel
4533N/A * Returns VK_ALT for Aqua and VK_CONTROL for others
4533N/A */
4533N/A public static int getCtrlKey() {
4533N/A
4533N/A if ("Aqua".equals(UIManager.getLookAndFeel().getID())) {
4533N/A return KeyEvent.VK_ALT;
4533N/A }
4533N/A
4533N/A return KeyEvent.VK_CONTROL;
4533N/A }
4533N/A
4533N/A private static void createAndShowGUI() {
4533N/A JFrame frame = new JFrame();
4533N/A frame.setTitle("Bug# 4278839");
4533N/A frame.setSize(200, 200);
4533N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
4533N/A area = new JTextArea("\naaa bbb\nccc ddd\n");
4533N/A frame.getContentPane().add(new JScrollPane(area));
4533N/A frame.setVisible(true);
4533N/A }
4533N/A}