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
Backport-of: 62140e249cf78d8d4acd1e5ab76bac0cc013a1a5
  • Loading branch information
GoeLin committed Nov 7, 2023
1 parent 2aca53f commit 5754eb1
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.\n" +
Expand Down Expand Up @@ -127,7 +126,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 @@ -148,13 +147,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 @@ -73,15 +73,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 @@ -99,7 +98,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 @@ -65,7 +70,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 @@ -176,7 +181,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 @@ -262,31 +266,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 @@ public class bug4380543 {

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

1 comment on commit 5754eb1

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.