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