Skip to content

Commit 5754eb1

Browse files
committedNov 7, 2023
8290469: Add new positioning options to PassFailJFrame test framework
Backport-of: 62140e249cf78d8d4acd1e5ab76bac0cc013a1a5
1 parent 2aca53f commit 5754eb1

File tree

7 files changed

+160
-78
lines changed

7 files changed

+160
-78
lines changed
 

‎test/jdk/java/awt/TrayIcon/TrayIconScalingTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public static void main(String[] args)
8181
PassFailJFrame passFailJFrame = new PassFailJFrame("TrayIcon " +
8282
"Test Instructions", INSTRUCTIONS, 8, 18, 85);
8383
createAndShowGUI();
84+
// does not have a test window,
85+
// hence only the instruction frame is positioned
86+
PassFailJFrame.positionTestWindow(null,
87+
PassFailJFrame.Position.HORIZONTAL);
8488
try {
8589
passFailJFrame.awaitAndCheck();
8690
} finally {

‎test/jdk/java/awt/print/PrinterJob/ImagePrinting/ClippedImages.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
public class ClippedImages {
6060

6161
private static ClippedImageCanvas c;
62+
private static Frame frame;
6263

6364
public static void main(String[] args) throws InterruptedException,
6465
InvocationTargetException {
@@ -100,7 +101,7 @@ public static void main(String[] args) throws InterruptedException,
100101
}
101102

102103
public static void createTestUI() {
103-
Frame frame = new Frame("Clipped Src Area Image Printing Test");
104+
frame = new Frame("Clipped Src Area Image Printing Test");
104105
c = new ClippedImageCanvas();
105106
frame.add(c, BorderLayout.CENTER);
106107

@@ -123,10 +124,10 @@ public static void createTestUI() {
123124
frame.add(p, BorderLayout.SOUTH);
124125
frame.setLocationRelativeTo(null);
125126
frame.pack();
126-
frame.setVisible(true);
127127

128128
PassFailJFrame.addTestWindow(frame);
129129
PassFailJFrame.positionTestWindow(frame, PassFailJFrame.Position.HORIZONTAL);
130+
frame.setVisible(true);
130131
}
131132

132133
private static void printOne() {

‎test/jdk/java/awt/print/PrinterJob/PrintGlyphVectorTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@
3434
import java.awt.Button;
3535
import java.awt.Color;
3636
import java.awt.Component;
37-
import java.awt.Dialog;
3837
import java.awt.Dimension;
3938
import java.awt.Font;
4039
import java.awt.Frame;
4140
import java.awt.Graphics;
4241
import java.awt.Graphics2D;
43-
import java.awt.Label;
4442
import java.awt.font.FontRenderContext;
4543
import java.awt.font.GlyphVector;
4644
import java.awt.geom.Point2D;
@@ -51,6 +49,7 @@
5149
import java.awt.print.PrinterJob;
5250

5351
public class PrintGlyphVectorTest extends Component implements Printable {
52+
private static Frame f;
5453

5554
private static final String INSTRUCTIONS =
5655
"Note: You must have a printer installed for this test.\n" +
@@ -127,7 +126,7 @@ private static void createTestUI() {
127126
PassFailJFrame.forcePass();
128127
}
129128

130-
Frame f = new Frame("Test PrintGlyphVector");
129+
f = new Frame("Test PrintGlyphVector");
131130
PrintGlyphVectorTest pvt = new PrintGlyphVectorTest();
132131
f.add(pvt, BorderLayout.CENTER);
133132

@@ -148,13 +147,13 @@ private static void createTestUI() {
148147

149148
f.add(printButton, BorderLayout.SOUTH);
150149
f.pack();
151-
f.setVisible(true);
152150

153151
// add the test frame to dispose
154152
PassFailJFrame.addTestWindow(f);
155153

156154
// Arrange the test instruction frame and test frame side by side
157155
PassFailJFrame.positionTestWindow(f, PassFailJFrame.Position.HORIZONTAL);
156+
f.setVisible(true);
158157
}
159158

160159
public static void main(String[] arg) throws Exception {

‎test/jdk/java/awt/print/PrinterJob/PrintLatinCJKTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,14 @@ public static void showFrame() throws InterruptedException, InvocationTargetExce
7373
});
7474
frame.getContentPane().add(b, BorderLayout.SOUTH);
7575
frame.pack();
76-
frame.setLocationRelativeTo(null);
77-
frame.setVisible(true);
7876

7977
// add the test frame to dispose
8078
PassFailJFrame.addTestWindow(frame);
8179

8280
// Arrange the test instruction frame and test frame side by side
8381
PassFailJFrame.positionTestWindow(frame,
8482
PassFailJFrame.Position.HORIZONTAL);
83+
frame.setVisible(true);
8584
});
8685
}
8786

@@ -99,7 +98,7 @@ public int print(Graphics g, PageFormat pf, int pageIndex)
9998

10099
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
101100
PassFailJFrame passFailJFrame = new PassFailJFrame("Test Instruction" +
102-
"Frame", info, 10, 40, 5);
101+
"Frame", info, 10, 10, 45);
103102
showFrame();
104103
passFailJFrame.awaitAndCheck();
105104
}

‎test/jdk/java/awt/regtesthelpers/PassFailJFrame.java

+106-17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
import java.awt.BorderLayout;
2525
import java.awt.Dimension;
26+
import java.awt.GraphicsConfiguration;
27+
import java.awt.GraphicsEnvironment;
28+
import java.awt.Insets;
29+
import java.awt.Rectangle;
2630
import java.awt.Toolkit;
2731
import java.awt.Window;
2832
import java.awt.event.WindowAdapter;
@@ -41,6 +45,7 @@
4145
import javax.swing.JTextArea;
4246
import javax.swing.Timer;
4347

48+
4449
import static javax.swing.SwingUtilities.invokeAndWait;
4550
import static javax.swing.SwingUtilities.isEventDispatchThread;
4651

@@ -65,7 +70,7 @@ public class PassFailJFrame {
6570
private static volatile String testFailedReason;
6671
private static JFrame frame;
6772

68-
public enum Position {HORIZONTAL, VERTICAL}
73+
public enum Position {HORIZONTAL, VERTICAL, TOP_LEFT_CORNER}
6974

7075
public PassFailJFrame(String instructions) throws InterruptedException,
7176
InvocationTargetException {
@@ -176,7 +181,6 @@ public void windowClosing(WindowEvent e) {
176181
frame.add(buttonsPanel, BorderLayout.SOUTH);
177182
frame.pack();
178183
frame.setLocationRelativeTo(null);
179-
frame.setVisible(true);
180184
windowList.add(frame);
181185
}
182186

@@ -262,31 +266,116 @@ private static void getFailureReason() {
262266
}
263267

264268
/**
265-
* Position the instruction frame with testWindow (testcase created
266-
* window) by the specified position.
267-
* Note: This method should be invoked from the method that creates
268-
* testWindow.
269+
* Approximately positions the instruction frame relative to the test
270+
* window as specified by the {@code position} parameter. If {@code testWindow}
271+
* is {@code null}, only the instruction frame is positioned according to
272+
* {@code position} parameter.
273+
* <p>This method should be called before making the test window visible
274+
* to avoid flickering.</p>
275+
*
276+
* @param testWindow test window that the test created.
277+
* May be {@code null}.
278+
*
279+
* @param position position must be one of:
280+
* <ul>
281+
* <li>{@code HORIZONTAL} - the test instruction frame is positioned
282+
* such that its right edge aligns with screen's horizontal center
283+
* and the test window (if not {@code null}) is placed to the right
284+
* of the instruction frame.</li>
269285
*
270-
* @param testWindow test window that the test is created
271-
* @param position position can be either HORIZONTAL (both test
272-
* instruction frame and test window as arranged
273-
* side by side) or VERTICAL (both test instruction
274-
* frame and test window as arranged up and down)
286+
* <li>{@code VERTICAL} - the test instruction frame is positioned
287+
* such that its bottom edge aligns with the screen's vertical center
288+
* and the test window (if not {@code null}) is placed below the
289+
* instruction frame.</li>
290+
*
291+
* <li>{@code TOP_LEFT_CORNER} - the test instruction frame is positioned
292+
* such that its top left corner is at the top left corner of the screen
293+
* and the test window (if not {@code null}) is placed to the right of
294+
* the instruction frame.</li>
295+
* </ul>
275296
*/
276297
public static void positionTestWindow(Window testWindow, Position position) {
277298
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
299+
300+
// Get the screen insets to position the frame by taking into
301+
// account the location of taskbar/menubars on screen.
302+
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
303+
.getDefaultScreenDevice().getDefaultConfiguration();
304+
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
305+
278306
if (position.equals(Position.HORIZONTAL)) {
279307
int newX = ((screenSize.width / 2) - frame.getWidth());
280-
frame.setLocation(newX, frame.getY());
281-
282-
testWindow.setLocation((frame.getLocation().x + frame.getWidth() + 5), frame.getY());
308+
frame.setLocation((newX + screenInsets.left),
309+
(frame.getY() + screenInsets.top));
310+
syncLocationToWindowManager();
311+
if (testWindow != null) {
312+
testWindow.setLocation((frame.getX() + frame.getWidth() + 5),
313+
frame.getY());
314+
}
283315
} else if (position.equals(Position.VERTICAL)) {
284316
int newY = ((screenSize.height / 2) - frame.getHeight());
285-
frame.setLocation(frame.getX(), newY);
317+
frame.setLocation((frame.getX() + screenInsets.left),
318+
(newY + screenInsets.top));
319+
syncLocationToWindowManager();
320+
if (testWindow != null) {
321+
testWindow.setLocation(frame.getX(),
322+
(frame.getY() + frame.getHeight() + 5));
323+
}
324+
} else if (position.equals(Position.TOP_LEFT_CORNER)) {
325+
frame.setLocation(screenInsets.left, screenInsets.top);
326+
syncLocationToWindowManager();
327+
if (testWindow != null) {
328+
testWindow.setLocation((frame.getX() + frame.getWidth() + 5),
329+
frame.getY());
330+
}
331+
}
332+
// make instruction frame visible after updating
333+
// frame & window positions
334+
frame.setVisible(true);
335+
}
336+
337+
/**
338+
* Ensures the frame location is updated by the window manager
339+
* if it adjusts the frame location after {@code setLocation}.
340+
*
341+
* @see #positionTestWindow
342+
*/
343+
private static void syncLocationToWindowManager() {
344+
Toolkit.getDefaultToolkit().sync();
345+
try {
346+
Thread.sleep(500);
347+
} catch (InterruptedException e) {
348+
e.printStackTrace();
349+
}
350+
}
286351

287-
testWindow.setLocation(frame.getX(),
288-
(frame.getLocation().y + frame.getHeight() + 5));
352+
/**
353+
* Returns the current position and size of the test instruction frame.
354+
* This method can be used in scenarios when custom positioning of
355+
* multiple test windows w.r.t test instruction frame is necessary,
356+
* at test-case level and the desired configuration is not available
357+
* as a {@code Position} option.
358+
*
359+
* @return Rectangle bounds of test instruction frame
360+
* @see #positionTestWindow
361+
*
362+
* @throws InterruptedException exception thrown when thread is
363+
* interrupted
364+
* @throws InvocationTargetException if an exception is thrown while
365+
* obtaining frame bounds on EDT
366+
*/
367+
public static Rectangle getInstructionFrameBounds()
368+
throws InterruptedException, InvocationTargetException {
369+
final Rectangle[] bounds = {null};
370+
371+
if (isEventDispatchThread()) {
372+
bounds[0] = frame != null ? frame.getBounds() : null;
373+
} else {
374+
invokeAndWait(() -> {
375+
bounds[0] = frame != null ? frame.getBounds() : null;
376+
});
289377
}
378+
return bounds[0];
290379
}
291380

292381
/**

‎test/jdk/javax/swing/JRadioButton/bug4380543.java

+18-22
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
* or visit www.oracle.com if you need additional information or have any
2121
* questions.
2222
*/
23-
/* @test
24-
* @bug 4380543
25-
* @key headful
26-
* @library /java/awt/regtesthelpers
27-
* @build PassFailJFrame
28-
* @summary setMargin() does not work for AbstractButton
29-
* @run main/manual bug4380543
30-
*/
31-
3223
import java.awt.BorderLayout;
3324
import java.awt.Color;
3425
import java.awt.event.ActionEvent;
@@ -45,6 +36,14 @@
4536
import javax.swing.UIManager;
4637
import javax.swing.UnsupportedLookAndFeelException;
4738

39+
/* @test
40+
* @bug 4380543
41+
* @key headful
42+
* @library /java/awt/regtesthelpers
43+
* @build PassFailJFrame
44+
* @summary setMargin() does not work for AbstractButton
45+
* @run main/manual bug4380543
46+
*/
4847
public class bug4380543 {
4948
static TestFrame testObj;
5049
static String instructions
@@ -62,17 +61,16 @@ public class bug4380543 {
6261

6362
public static void main(String[] args) throws Exception {
6463

65-
SwingUtilities.invokeAndWait(new Runnable() {
66-
public void run() {
67-
try {
68-
passFailJFrame = new PassFailJFrame(instructions);
69-
testObj = new TestFrame();
70-
//Adding the Test Frame to handle dispose
71-
PassFailJFrame.addTestWindow(testObj);
72-
PassFailJFrame.positionTestWindow(testObj, PassFailJFrame.Position.HORIZONTAL);
73-
} catch (Exception e) {
74-
e.printStackTrace();
75-
}
64+
SwingUtilities.invokeAndWait(() -> {
65+
try {
66+
passFailJFrame = new PassFailJFrame(instructions);
67+
testObj = new TestFrame();
68+
//Adding the Test Frame to handle dispose
69+
PassFailJFrame.addTestWindow(testObj);
70+
PassFailJFrame.positionTestWindow(testObj, PassFailJFrame.Position.HORIZONTAL);
71+
testObj.setVisible(true);
72+
} catch (Exception e) {
73+
e.printStackTrace();
7674
}
7775
});
7876
passFailJFrame.awaitAndCheck();
@@ -111,9 +109,7 @@ public void initComponents() {
111109
}
112110

113111
getContentPane().add(p,BorderLayout.SOUTH);
114-
115112
setSize(500, 300);
116-
setVisible(true);
117113
}
118114

119115
private static void setLookAndFeel(String laf) {

‎test/jdk/javax/swing/JTabbedPane/4209065/bug4209065.java

+24-30
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
* questions.
2222
*/
2323

24+
import java.lang.reflect.InvocationTargetException;
25+
26+
import javax.swing.JFrame;
27+
import javax.swing.JLabel;
28+
import javax.swing.JTabbedPane;
29+
import javax.swing.SwingUtilities;
30+
2431
/*
2532
* @test
2633
* @bug 4209065
@@ -30,14 +37,6 @@
3037
* @run main/manual bug4209065
3138
*/
3239

33-
import java.lang.reflect.InvocationTargetException;
34-
35-
import javax.swing.JFrame;
36-
import javax.swing.JLabel;
37-
import javax.swing.JTabbedPane;
38-
import javax.swing.SwingUtilities;
39-
40-
4140
public final class bug4209065 {
4241

4342
private static JFrame frame;
@@ -47,37 +46,32 @@ public final class bug4209065 {
4746
" text may be larger\nthan the tab height but this is OK" +
4847
" and NOT a failure.";
4948

50-
public static void createAndShowGUI() throws InterruptedException,
51-
InvocationTargetException {
52-
SwingUtilities.invokeAndWait(() -> {
53-
frame = new JFrame("JTabbedPane");
54-
55-
JTabbedPane tp = new JTabbedPane();
49+
public static void createAndShowGUI() {
5650

57-
tp.addTab("<html><center><font size=+3>big</font></center></html>",
58-
new JLabel());
59-
tp.addTab("<html><center><font color=red>red</font></center></html>",
60-
new JLabel());
61-
tp.addTab("<html><center><em><b>Bold Italic!</b></em></center></html>",
62-
new JLabel());
51+
frame = new JFrame("JTabbedPane");
52+
JTabbedPane tp = new JTabbedPane();
6353

64-
frame.getContentPane().add(tp);
65-
frame.setSize(400, 400);
66-
frame.setLocationRelativeTo(null);
67-
frame.setVisible(true);
54+
tp.addTab("<html><center><font size=+3>big</font></center></html>",
55+
new JLabel());
56+
tp.addTab("<html><center><font color=red>red</font></center></html>",
57+
new JLabel());
58+
tp.addTab("<html><center><em><b>Bold Italic!</b></em></center></html>",
59+
new JLabel());
6860

61+
frame.getContentPane().add(tp);
62+
frame.setSize(400, 400);
6963

70-
PassFailJFrame.addTestWindow(frame);
71-
PassFailJFrame.positionTestWindow(frame,
72-
PassFailJFrame.Position.HORIZONTAL);
73-
});
64+
PassFailJFrame.addTestWindow(frame);
65+
PassFailJFrame.positionTestWindow(frame,
66+
PassFailJFrame.Position.HORIZONTAL);
67+
frame.setVisible(true);
7468
}
7569

7670
public static void main(String[] args) throws InterruptedException,
7771
InvocationTargetException {
7872
PassFailJFrame passFailJFrame = new PassFailJFrame("JTabbedPane " +
7973
"Test Instructions", text, 5, 19, 35);
80-
createAndShowGUI();
74+
SwingUtilities.invokeAndWait(bug4209065::createAndShowGUI);
8175
passFailJFrame.awaitAndCheck();
8276
}
83-
}
77+
}

0 commit comments

Comments
 (0)
Please sign in to comment.