1 /*
   2  * Copyright (c) 2012, 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
  25    @bug 4506788
  26    @summary  Tests if cursor gets stuck after insertion a character
  27    @author Denis Sharypov
  28    @library ../../../regtesthelpers
  29    @build Util
  30    @run applet bug4506788.html
  31 */
  32 
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 
  37 import javax.swing.*;
  38 import javax.swing.text.*;
  39 import javax.swing.event.*;
  40 
  41 import java.util.Timer;
  42 import java.util.TimerTask;
  43 
  44 public class bug4506788 extends JApplet {
  45 
  46     private Timer timer;
  47 
  48     private boolean passed = false;
  49     private boolean finished = false;
  50 
  51     private JEditorPane jep = new JEditorPane();
  52 
  53     public void init() {
  54         String text = "abc";
  55         JFrame f = new JFrame();
  56         jep.setEditorKit(new StyledEditorKit());
  57         jep.setText(text);
  58         jep.addCaretListener(new CaretListener() {
  59             public void caretUpdate(CaretEvent e) {
  60                 passed = (e.getDot() == 3);
  61                 if (passed) {
  62                     synchronized (bug4506788.this) {
  63                         finished = true;
  64                         bug4506788.this.notifyAll();
  65                     }
  66                 }
  67             }
  68         });
  69 
  70         DefaultStyledDocument doc = (DefaultStyledDocument)jep.getDocument();
  71         MutableAttributeSet atr = new SimpleAttributeSet();
  72         StyleConstants.setBold(atr, true);
  73         doc.setCharacterAttributes(1, 1, atr, false);
  74 
  75         f.getContentPane().add(jep);
  76         f.setSize(100, 100);
  77         f.setVisible(true);
  78     }
  79 
  80     public void start() {
  81         Util.blockTillDisplayed(jep);
  82         timer = new Timer();
  83         timer.schedule(new RobotTask(), 3000);
  84         synchronized (this) {
  85             if (!finished) {
  86                 try {
  87                     wait(20000);
  88                 } catch (Exception e) {
  89                 }
  90             }
  91         }
  92     }
  93 
  94     public void destroy() {
  95         if(!passed) {
  96             throw new RuntimeException("Test failed.");
  97         }
  98     }
  99 
 100 
 101     class RobotTask extends TimerTask {
 102         public void run() {
 103             Robot robot;
 104             try {
 105                 robot = new Robot();
 106             } catch (AWTException e) {
 107                 throw new RuntimeException("Robot could not be created");
 108             }
 109             Point p = jep.getLocationOnScreen();
 110             robot.setAutoDelay(50);
 111             robot.mouseMove(p.x ,p.y);
 112             robot.mousePress(InputEvent.BUTTON1_MASK);
 113             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 114             robot.keyPress(KeyEvent.VK_RIGHT);
 115             robot.keyRelease(KeyEvent.VK_RIGHT);
 116             robot.keyPress(KeyEvent.VK_X);
 117             robot.keyRelease(KeyEvent.VK_X);
 118             robot.keyPress(KeyEvent.VK_RIGHT);
 119             robot.keyRelease(KeyEvent.VK_RIGHT);
 120         }
 121     }
 122 }