Skip to content

Commit

Permalink
8290469: Add new positioning options to PassFailJFrame test framework
Browse files Browse the repository at this point in the history
Reviewed-by: mbaesken
Backport-of: 568be58e8521e5e87baca1872ba8cc1941607bb7
  • Loading branch information
TheRealMDoerr committed Jun 30, 2023
1 parent a563c45 commit 62140e2
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 78 deletions.
4 changes: 4 additions & 0 deletions test/jdk/java/awt/TrayIcon/TrayIconScalingTest.java
Expand Up @@ -81,6 +81,10 @@ public static void main(String[] args)
PassFailJFrame passFailJFrame = new PassFailJFrame("TrayIcon " +
"Test Instructions", INSTRUCTIONS, 8, 18, 85);
createAndShowGUI();
// does not have a test window,
// hence only the instruction frame is positioned
PassFailJFrame.positionTestWindow(null,
PassFailJFrame.Position.HORIZONTAL);
try {
passFailJFrame.awaitAndCheck();
} finally {
Expand Down
Expand Up @@ -59,6 +59,7 @@
public class ClippedImages {

private static ClippedImageCanvas c;
private static Frame frame;

public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
Expand Down Expand Up @@ -100,7 +101,7 @@ public static void main(String[] args) throws InterruptedException,
}

public static void createTestUI() {
Frame frame = new Frame("Clipped Src Area Image Printing Test");
frame = new Frame("Clipped Src Area Image Printing Test");
c = new ClippedImageCanvas();
frame.add(c, BorderLayout.CENTER);

Expand All @@ -123,10 +124,10 @@ public static void createTestUI() {
frame.add(p, BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);

PassFailJFrame.addTestWindow(frame);
PassFailJFrame.positionTestWindow(frame, PassFailJFrame.Position.HORIZONTAL);
frame.setVisible(true);
}

private static void printOne() {
Expand Down
7 changes: 3 additions & 4 deletions test/jdk/java/awt/print/PrinterJob/PrintGlyphVectorTest.java
Expand Up @@ -34,13 +34,11 @@
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.Point2D;
Expand All @@ -51,6 +49,7 @@
import java.awt.print.PrinterJob;

public class PrintGlyphVectorTest extends Component implements Printable {
private static Frame f;

private static final String INSTRUCTIONS = """
Note: You must have a printer installed for this test.
Expand Down Expand Up @@ -128,7 +127,7 @@ private static void createTestUI() {
PassFailJFrame.forcePass();
}

Frame f = new Frame("Test PrintGlyphVector");
f = new Frame("Test PrintGlyphVector");
PrintGlyphVectorTest pvt = new PrintGlyphVectorTest();
f.add(pvt, BorderLayout.CENTER);

Expand All @@ -149,13 +148,13 @@ private static void createTestUI() {

f.add(printButton, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);

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

// Arrange the test instruction frame and test frame side by side
PassFailJFrame.positionTestWindow(f, PassFailJFrame.Position.HORIZONTAL);
f.setVisible(true);
}

public static void main(String[] arg) throws Exception {
Expand Down
5 changes: 2 additions & 3 deletions test/jdk/java/awt/print/PrinterJob/PrintLatinCJKTest.java
Expand Up @@ -74,15 +74,14 @@ public static void showFrame() throws InterruptedException, InvocationTargetExce
});
frame.getContentPane().add(b, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

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

// Arrange the test instruction frame and test frame side by side
PassFailJFrame.positionTestWindow(frame,
PassFailJFrame.Position.HORIZONTAL);
frame.setVisible(true);
});
}

Expand All @@ -100,7 +99,7 @@ public int print(Graphics g, PageFormat pf, int pageIndex)

public static void main(String[] args) throws InterruptedException, InvocationTargetException {
PassFailJFrame passFailJFrame = new PassFailJFrame("Test Instruction" +
"Frame", info, 10, 40, 5);
"Frame", info, 10, 10, 45);
showFrame();
passFailJFrame.awaitAndCheck();
}
Expand Down
123 changes: 106 additions & 17 deletions test/jdk/java/awt/regtesthelpers/PassFailJFrame.java
Expand Up @@ -23,6 +23,10 @@

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.WindowAdapter;
Expand All @@ -41,6 +45,7 @@
import javax.swing.JTextArea;
import javax.swing.Timer;


import static javax.swing.SwingUtilities.invokeAndWait;
import static javax.swing.SwingUtilities.isEventDispatchThread;

Expand All @@ -60,7 +65,7 @@ public class PassFailJFrame {
private static volatile String testFailedReason;
private static JFrame frame;

public enum Position {HORIZONTAL, VERTICAL}
public enum Position {HORIZONTAL, VERTICAL, TOP_LEFT_CORNER}

public PassFailJFrame(String instructions) throws InterruptedException,
InvocationTargetException {
Expand Down Expand Up @@ -171,7 +176,6 @@ public void windowClosing(WindowEvent e) {
frame.add(buttonsPanel, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
windowList.add(frame);
}

Expand Down Expand Up @@ -257,31 +261,116 @@ private static void getFailureReason() {
}

/**
* Position the instruction frame with testWindow (testcase created
* window) by the specified position.
* Note: This method should be invoked from the method that creates
* testWindow.
* Approximately positions the instruction frame relative to the test
* window as specified by the {@code position} parameter. If {@code testWindow}
* is {@code null}, only the instruction frame is positioned according to
* {@code position} parameter.
* <p>This method should be called before making the test window visible
* to avoid flickering.</p>
*
* @param testWindow test window that the test created.
* May be {@code null}.
*
* @param position position must be one of:
* <ul>
* <li>{@code HORIZONTAL} - the test instruction frame is positioned
* such that its right edge aligns with screen's horizontal center
* and the test window (if not {@code null}) is placed to the right
* of the instruction frame.</li>
*
* @param testWindow test window that the test is created
* @param position position can be either HORIZONTAL (both test
* instruction frame and test window as arranged
* side by side) or VERTICAL (both test instruction
* frame and test window as arranged up and down)
* <li>{@code VERTICAL} - the test instruction frame is positioned
* such that its bottom edge aligns with the screen's vertical center
* and the test window (if not {@code null}) is placed below the
* instruction frame.</li>
*
* <li>{@code TOP_LEFT_CORNER} - the test instruction frame is positioned
* such that its top left corner is at the top left corner of the screen
* and the test window (if not {@code null}) is placed to the right of
* the instruction frame.</li>
* </ul>
*/
public static void positionTestWindow(Window testWindow, Position position) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

// Get the screen insets to position the frame by taking into
// account the location of taskbar/menubars on screen.
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);

if (position.equals(Position.HORIZONTAL)) {
int newX = ((screenSize.width / 2) - frame.getWidth());
frame.setLocation(newX, frame.getY());

testWindow.setLocation((frame.getLocation().x + frame.getWidth() + 5), frame.getY());
frame.setLocation((newX + screenInsets.left),
(frame.getY() + screenInsets.top));
syncLocationToWindowManager();
if (testWindow != null) {
testWindow.setLocation((frame.getX() + frame.getWidth() + 5),
frame.getY());
}
} else if (position.equals(Position.VERTICAL)) {
int newY = ((screenSize.height / 2) - frame.getHeight());
frame.setLocation(frame.getX(), newY);
frame.setLocation((frame.getX() + screenInsets.left),
(newY + screenInsets.top));
syncLocationToWindowManager();
if (testWindow != null) {
testWindow.setLocation(frame.getX(),
(frame.getY() + frame.getHeight() + 5));
}
} else if (position.equals(Position.TOP_LEFT_CORNER)) {
frame.setLocation(screenInsets.left, screenInsets.top);
syncLocationToWindowManager();
if (testWindow != null) {
testWindow.setLocation((frame.getX() + frame.getWidth() + 5),
frame.getY());
}
}
// make instruction frame visible after updating
// frame & window positions
frame.setVisible(true);
}

/**
* Ensures the frame location is updated by the window manager
* if it adjusts the frame location after {@code setLocation}.
*
* @see #positionTestWindow
*/
private static void syncLocationToWindowManager() {
Toolkit.getDefaultToolkit().sync();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

testWindow.setLocation(frame.getX(),
(frame.getLocation().y + frame.getHeight() + 5));
/**
* Returns the current position and size of the test instruction frame.
* This method can be used in scenarios when custom positioning of
* multiple test windows w.r.t test instruction frame is necessary,
* at test-case level and the desired configuration is not available
* as a {@code Position} option.
*
* @return Rectangle bounds of test instruction frame
* @see #positionTestWindow
*
* @throws InterruptedException exception thrown when thread is
* interrupted
* @throws InvocationTargetException if an exception is thrown while
* obtaining frame bounds on EDT
*/
public static Rectangle getInstructionFrameBounds()
throws InterruptedException, InvocationTargetException {
final Rectangle[] bounds = {null};

if (isEventDispatchThread()) {
bounds[0] = frame != null ? frame.getBounds() : null;
} else {
invokeAndWait(() -> {
bounds[0] = frame != null ? frame.getBounds() : null;
});
}
return bounds[0];
}

/**
Expand Down
40 changes: 18 additions & 22 deletions test/jdk/javax/swing/JRadioButton/bug4380543.java
Expand Up @@ -20,15 +20,6 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4380543
* @key headful
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @summary setMargin() does not work for AbstractButton
* @run main/manual bug4380543
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
Expand All @@ -45,6 +36,14 @@
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/* @test
* @bug 4380543
* @key headful
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @summary setMargin() does not work for AbstractButton
* @run main/manual bug4380543
*/
public class bug4380543 {
static TestFrame testObj;
static String instructions
Expand All @@ -62,17 +61,16 @@ and CheckBox (insets set to 20 on all 4 sides).

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

SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
try {
passFailJFrame = new PassFailJFrame(instructions);
testObj = new TestFrame();
//Adding the Test Frame to handle dispose
PassFailJFrame.addTestWindow(testObj);
PassFailJFrame.positionTestWindow(testObj, PassFailJFrame.Position.HORIZONTAL);
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeAndWait(() -> {
try {
passFailJFrame = new PassFailJFrame(instructions);
testObj = new TestFrame();
//Adding the Test Frame to handle dispose
PassFailJFrame.addTestWindow(testObj);
PassFailJFrame.positionTestWindow(testObj, PassFailJFrame.Position.HORIZONTAL);
testObj.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
passFailJFrame.awaitAndCheck();
Expand Down Expand Up @@ -111,9 +109,7 @@ public void initComponents() {
}

getContentPane().add(p,BorderLayout.SOUTH);

setSize(500, 300);
setVisible(true);
}

private static void setLookAndFeel(String laf) {
Expand Down

3 comments on commit 62140e2

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 62140e2 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 62140e2 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin the backport was successfully created on the branch GoeLin-backport-62140e24 in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 62140e24 from the openjdk/jdk17u-dev repository.

The commit being backported was authored by Martin Doerr on 30 Jun 2023 and was reviewed by Matthias Baesken.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev.git GoeLin-backport-62140e24:GoeLin-backport-62140e24
$ git checkout GoeLin-backport-62140e24
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev.git GoeLin-backport-62140e24

Please sign in to comment.