Skip to content

Commit

Permalink
Backport 31ceec7cd55b455cddf0953cc23aaa64612bd6e7
Browse files Browse the repository at this point in the history
  • Loading branch information
duke committed Jan 17, 2025
1 parent 97119a9 commit 8ee9671
Showing 1 changed file with 60 additions and 73 deletions.
133 changes: 60 additions & 73 deletions test/jdk/javax/swing/text/StyledEditorKit/4506788/bug4506788.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,98 +29,85 @@
* @run main bug4506788
*/

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;

public class bug4506788 {

private volatile boolean passed = false;
private JEditorPane jep;
private static volatile boolean passed;
private static volatile Point p;
private static volatile Dimension dim;
private static JEditorPane jep;
private static JFrame f;

public static void main(final String[] args) {
bug4506788 app = new bug4506788();
app.init();
app.start();
}

public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
} catch (InterruptedException | InvocationTargetException ex) {
ex.printStackTrace();
throw new RuntimeException("FAILED: SwingUtilities.invokeAndWait method failed then creating and showing GUI");
}
}

public void start() {
Robot robot;
public static void main(final String[] args) throws Exception {
try {
robot = new Robot();
Robot robot = new Robot();
robot.setAutoDelay(100);
} catch (AWTException e) {
throw new RuntimeException("Robot could not be created");
}

robot.waitForIdle();

Point p;
try {
p = getJEPLocOnScreen();
} catch (Exception e) {
throw new RuntimeException("Could not get JEditorPane location on screen");
}

robot.mouseMove(p.x, p.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.keyPress(KeyEvent.VK_HOME);
robot.keyRelease(KeyEvent.VK_HOME);
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);

robot.waitForIdle();

if (!passed) {
throw new RuntimeException("Test failed.");
}
}
SwingUtilities.invokeAndWait(() -> createAndShowGUI());

private Point getJEPLocOnScreen() throws Exception {
robot.waitForIdle();
robot.delay(1000);

final Point[] result = new Point[1];
SwingUtilities.invokeAndWait(() -> {
p = jep.getLocationOnScreen();
dim = jep.getSize();
});

SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
result[0] = jep.getLocationOnScreen();
robot.mouseMove(p.x + dim.width / 2, p.y + dim.height / 2);
robot.waitForIdle();
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_HOME);
robot.keyRelease(KeyEvent.VK_HOME);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);
robot.waitForIdle();

if (!passed) {
throw new RuntimeException("Test failed.");
}
});

return result[0];
} finally {
SwingUtilities.invokeAndWait(() -> {
if (f != null) {
f.dispose();
}
});
}
}

private void createAndShowGUI() {
private static void createAndShowGUI() {
jep = new JEditorPane();
String text = "abc";
JFrame f = new JFrame();
f = new JFrame("bug4506788");
jep.setEditorKit(new StyledEditorKit());
jep.setText(text);
jep.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
System.out.println("getDot " + e.getDot());
passed = (e.getDot() == 3);
}
});
Expand Down

0 comments on commit 8ee9671

Please sign in to comment.