Skip to content

Commit d35cb91

Browse files
committed
8346055: javax/swing/text/StyledEditorKit/4506788/bug4506788.java fails in ubuntu22.04
Backport-of: 31ceec7cd55b455cddf0953cc23aaa64612bd6e7
1 parent 91fd105 commit d35cb91

File tree

1 file changed

+60
-73
lines changed

1 file changed

+60
-73
lines changed

test/jdk/javax/swing/text/StyledEditorKit/4506788/bug4506788.java

Lines changed: 60 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,98 +29,85 @@
2929
* @run main bug4506788
3030
*/
3131

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.*;
32+
import java.awt.Dimension;
33+
import java.awt.Point;
34+
import java.awt.Robot;
35+
import java.awt.event.KeyEvent;
36+
import java.awt.event.InputEvent;
37+
import javax.swing.JEditorPane;
38+
import javax.swing.JFrame;
39+
import javax.swing.SwingUtilities;
40+
import javax.swing.event.CaretEvent;
41+
import javax.swing.event.CaretListener;
42+
import javax.swing.text.DefaultStyledDocument;
43+
import javax.swing.text.MutableAttributeSet;
44+
import javax.swing.text.SimpleAttributeSet;
45+
import javax.swing.text.StyleConstants;
46+
import javax.swing.text.StyledEditorKit;
3847

3948
public class bug4506788 {
4049

41-
private volatile boolean passed = false;
42-
private JEditorPane jep;
50+
private static volatile boolean passed;
51+
private static volatile Point p;
52+
private static volatile Dimension dim;
53+
private static JEditorPane jep;
54+
private static JFrame f;
4355

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;
56+
public static void main(final String[] args) throws Exception {
6657
try {
67-
robot = new Robot();
58+
Robot robot = new Robot();
6859
robot.setAutoDelay(100);
69-
} catch (AWTException e) {
70-
throw new RuntimeException("Robot could not be created");
71-
}
72-
73-
robot.waitForIdle();
74-
75-
Point p;
76-
try {
77-
p = getJEPLocOnScreen();
78-
} catch (Exception e) {
79-
throw new RuntimeException("Could not get JEditorPane location on screen");
80-
}
81-
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();
9560

96-
if (!passed) {
97-
throw new RuntimeException("Test failed.");
98-
}
99-
}
61+
SwingUtilities.invokeAndWait(() -> createAndShowGUI());
10062

101-
private Point getJEPLocOnScreen() throws Exception {
63+
robot.waitForIdle();
64+
robot.delay(1000);
10265

103-
final Point[] result = new Point[1];
66+
SwingUtilities.invokeAndWait(() -> {
67+
p = jep.getLocationOnScreen();
68+
dim = jep.getSize();
69+
});
10470

105-
SwingUtilities.invokeAndWait(new Runnable() {
106-
@Override
107-
public void run() {
108-
result[0] = jep.getLocationOnScreen();
71+
robot.mouseMove(p.x + dim.width / 2, p.y + dim.height / 2);
72+
robot.waitForIdle();
73+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
74+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
75+
robot.waitForIdle();
76+
robot.keyPress(KeyEvent.VK_HOME);
77+
robot.keyRelease(KeyEvent.VK_HOME);
78+
robot.waitForIdle();
79+
robot.keyPress(KeyEvent.VK_RIGHT);
80+
robot.keyRelease(KeyEvent.VK_RIGHT);
81+
robot.waitForIdle();
82+
robot.keyPress(KeyEvent.VK_X);
83+
robot.keyRelease(KeyEvent.VK_X);
84+
robot.waitForIdle();
85+
robot.keyPress(KeyEvent.VK_RIGHT);
86+
robot.keyRelease(KeyEvent.VK_RIGHT);
87+
robot.waitForIdle();
88+
89+
if (!passed) {
90+
throw new RuntimeException("Test failed.");
10991
}
110-
});
111-
112-
return result[0];
92+
} finally {
93+
SwingUtilities.invokeAndWait(() -> {
94+
if (f != null) {
95+
f.dispose();
96+
}
97+
});
98+
}
11399
}
114100

115-
private void createAndShowGUI() {
101+
private static void createAndShowGUI() {
116102
jep = new JEditorPane();
117103
String text = "abc";
118-
JFrame f = new JFrame();
104+
f = new JFrame("bug4506788");
119105
jep.setEditorKit(new StyledEditorKit());
120106
jep.setText(text);
121107
jep.addCaretListener(new CaretListener() {
122108
@Override
123109
public void caretUpdate(CaretEvent e) {
110+
System.out.println("getDot " + e.getDot());
124111
passed = (e.getDot() == 3);
125112
}
126113
});

0 commit comments

Comments
 (0)