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 33 import java.awt.*; 34 import java.awt.event.*; 35 import java.lang.reflect.InvocationTargetException; 36 import javax.swing.*; 37 import javax.swing.event.*; 38 import javax.swing.text.*; 39 import sun.awt.SunToolkit; 40 41 public class bug4506788 extends JApplet { 42 43 private volatile boolean passed = false; 44 private JEditorPane jep; 45 private SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 46 47 @Override 48 public void init() { 49 try { 50 SwingUtilities.invokeAndWait(new Runnable() { 51 @Override 52 public void run() { 53 createAndShowGUI(); 54 } 55 }); 56 } catch (InterruptedException | InvocationTargetException ex) { 57 ex.printStackTrace(); 58 throw new RuntimeException("FAILED: SwingUtilities.invokeAndWait method failed then creating and showing GUI"); 59 } 60 } 61 62 @Override 63 public void start() { 64 Robot robot; 65 try { 66 robot = new Robot(); 67 } catch (AWTException e) { 68 throw new RuntimeException("Robot could not be created"); 69 } 70 71 toolkit.realSync(); 72 73 Point p; 74 try { 75 p = getJEPLocOnScreen(); 76 } catch (Exception e) { 77 throw new RuntimeException("Could not get JEditorPane location on screen"); 78 } 79 80 robot.setAutoDelay(50); 81 robot.mouseMove(p.x, p.y); 82 robot.mousePress(InputEvent.BUTTON1_MASK); 83 robot.mouseRelease(InputEvent.BUTTON1_MASK); 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 toolkit.realSync(); 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 }