1/*
2 * Copyright (c) 2012, 2016, 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 4506788 7147408
28 * @summary  Tests if cursor gets stuck after insertion a character
29 * @author Denis Sharypov
30 * @run applet bug4506788.html
31 */
32
33import java.awt.*;
34import java.awt.event.*;
35import java.lang.reflect.InvocationTargetException;
36import javax.swing.*;
37import javax.swing.event.*;
38import javax.swing.text.*;
39
40public class bug4506788 extends JApplet {
41
42    private volatile boolean passed = false;
43    private JEditorPane jep;
44
45    @Override
46    public void init() {
47        try {
48            SwingUtilities.invokeAndWait(new Runnable() {
49                @Override
50                public void run() {
51                    createAndShowGUI();
52                }
53            });
54        } catch (InterruptedException | InvocationTargetException ex) {
55            ex.printStackTrace();
56            throw new RuntimeException("FAILED: SwingUtilities.invokeAndWait method failed then creating and showing GUI");
57        }
58    }
59
60    @Override
61    public void start() {
62        Robot robot;
63        try {
64            robot = new Robot();
65        } catch (AWTException e) {
66            throw new RuntimeException("Robot could not be created");
67        }
68
69        robot.waitForIdle();
70
71        Point p;
72        try {
73            p = getJEPLocOnScreen();
74        } catch (Exception e) {
75            throw new RuntimeException("Could not get JEditorPane location on screen");
76        }
77
78        robot.setAutoDelay(50);
79        robot.mouseMove(p.x, p.y);
80        robot.mousePress(InputEvent.BUTTON1_MASK);
81        robot.mouseRelease(InputEvent.BUTTON1_MASK);
82        robot.keyPress(KeyEvent.VK_HOME);
83        robot.keyRelease(KeyEvent.VK_HOME);
84        robot.keyPress(KeyEvent.VK_RIGHT);
85        robot.keyRelease(KeyEvent.VK_RIGHT);
86        robot.keyPress(KeyEvent.VK_X);
87        robot.keyRelease(KeyEvent.VK_X);
88        robot.keyPress(KeyEvent.VK_RIGHT);
89        robot.keyRelease(KeyEvent.VK_RIGHT);
90
91        robot.waitForIdle();
92
93        if (!passed) {
94            throw new RuntimeException("Test failed.");
95        }
96    }
97
98    private Point getJEPLocOnScreen() throws Exception {
99
100        final Point[] result = new Point[1];
101
102        SwingUtilities.invokeAndWait(new Runnable() {
103            @Override
104            public void run() {
105                result[0] = jep.getLocationOnScreen();
106            }
107        });
108
109        return result[0];
110    }
111
112    private void createAndShowGUI() {
113        jep = new JEditorPane();
114        String text = "abc";
115        JFrame f = new JFrame();
116        jep.setEditorKit(new StyledEditorKit());
117        jep.setText(text);
118        jep.addCaretListener(new CaretListener() {
119            @Override
120            public void caretUpdate(CaretEvent e) {
121                passed = (e.getDot() == 3);
122            }
123        });
124
125        DefaultStyledDocument doc = (DefaultStyledDocument) jep.getDocument();
126        MutableAttributeSet atr = new SimpleAttributeSet();
127        StyleConstants.setBold(atr, true);
128        doc.setCharacterAttributes(1, 1, atr, false);
129
130        f.getContentPane().add(jep);
131        f.setSize(100, 100);
132        f.setVisible(true);
133    }
134}
135