Skip to content

Commit

Permalink
8288325: [windows] Actual and Preferred Size of AWT Non-resizable fra…
Browse files Browse the repository at this point in the history
…me are different

Backport-of: eca9749da01d732033c07f2bbb38800a9d80f18d
  • Loading branch information
GoeLin committed Sep 22, 2023
1 parent 6398e46 commit 06756c4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
24 changes: 8 additions & 16 deletions src/java.desktop/windows/native/libawt/windows/awt_Window.cpp
Expand Up @@ -1393,7 +1393,6 @@ BOOL AwtWindow::UpdateInsets(jobject insets)
*/
RECT outside;
RECT inside;
int extraBottomInsets = 0;

// extra padded border for captioned windows
int extraPaddedBorderInsets = ::GetSystemMetrics(SM_CXPADDEDBORDER);
Expand All @@ -1405,39 +1404,33 @@ BOOL AwtWindow::UpdateInsets(jobject insets)
if (outside.right - outside.left > 0 && outside.bottom - outside.top > 0) {
::MapWindowPoints(GetHWnd(), 0, (LPPOINT)&inside, 2);
m_insets.top = inside.top - outside.top;
m_insets.bottom = outside.bottom - inside.bottom + extraBottomInsets;
m_insets.bottom = outside.bottom - inside.bottom;
m_insets.left = inside.left - outside.left;
m_insets.right = outside.right - inside.right;
} else {
m_insets.top = -1;
}

if (m_insets.left < 0 || m_insets.top < 0 ||
m_insets.right < 0 || m_insets.bottom < 0)
{
/* This window hasn't been sized yet -- use system metrics. */
jobject target = GetTarget(env);
if (IsUndecorated() == FALSE) {
/* Get outer frame sizes. */
LONG style = GetStyle();
if (style & WS_THICKFRAME) {
m_insets.left = m_insets.right =
::GetSystemMetrics(SM_CXSIZEFRAME) + extraPaddedBorderInsets;
m_insets.top = m_insets.bottom =
::GetSystemMetrics(SM_CYSIZEFRAME) + extraPaddedBorderInsets;
} else {
m_insets.left = m_insets.right =
::GetSystemMetrics(SM_CXDLGFRAME) + extraPaddedBorderInsets;
m_insets.top = m_insets.bottom =
::GetSystemMetrics(SM_CYDLGFRAME) + extraPaddedBorderInsets;
}
// System metrics are same for resizable & non-resizable frame.
m_insets.left = m_insets.right =
::GetSystemMetrics(SM_CXFRAME) + extraPaddedBorderInsets;
m_insets.top = m_insets.bottom =
::GetSystemMetrics(SM_CYFRAME) + extraPaddedBorderInsets;
/* Add in title. */
m_insets.top += ::GetSystemMetrics(SM_CYCAPTION);
}
else {
/* fix for 4418125: Undecorated frames are off by one */
/* undo the -1 set above */
/* Additional fix for 5059656 */
/* Also, 5089312: Window insets should be 0. */
/* Also, 5089312: Window insets should be 0. */
::memset(&m_insets, 0, sizeof(m_insets));
}

Expand All @@ -1450,7 +1443,6 @@ BOOL AwtWindow::UpdateInsets(jobject insets)
env->DeleteLocalRef(target);
return FALSE;
}
m_insets.bottom += extraBottomInsets;
env->DeleteLocalRef(target);
}

Expand Down
52 changes: 36 additions & 16 deletions test/jdk/java/awt/Frame/AwtFramePackTest.java
Expand Up @@ -39,21 +39,37 @@
* @test
* @bug 8265586
* @key headful
* @summary Tests whether insets are calculated correctly on Windows
* for AWT Frame by checking the actual and expected/preferred frame sizes.
* @summary Tests whether correct native frame insets are obtained
* for Resizable & Non-Resizable AWT Frame by checking the actual
* and expected/preferred frame sizes.
* @run main AwtFramePackTest
*/

public class AwtFramePackTest {

private static Frame frame;
private static Robot robot;
private static StringBuffer errorLog = new StringBuffer();

public static void main(String[] args) throws AWTException {
try {
robot = new Robot();
robot.setAutoDelay(300);
robot = new Robot();
robot.setAutoDelay(300);

// Resizable frame
createAWTFrame(true);

robot.waitForIdle();
robot.delay(500);

// Non-Resizable frame
createAWTFrame(false);

if (!errorLog.isEmpty()) {
throw new RuntimeException("Test failed due to the following" +
" one or more errors: \n" + errorLog);
}
}

private static void createAWTFrame(boolean isResizable) {
try {
frame = new Frame();
frame.setLayout(new BorderLayout());

Expand All @@ -67,32 +83,36 @@ public static void main(String[] args) throws AWTException {
mb.add(m);
frame.setMenuBar(mb);

frame.setResizable(isResizable);
frame.pack();
frame.setVisible(true);

robot.delay(500);
robot.waitForIdle();
robot.delay(500);

Dimension actualFrameSize = frame.getSize();
Dimension expectedFrameSize = frame.getPreferredSize();

if (!actualFrameSize.equals(expectedFrameSize)) {
System.out.println("Expected frame size: "+ expectedFrameSize);
System.out.println("Actual frame size: "+ actualFrameSize);
saveScreenCapture();
throw new RuntimeException("Expected and Actual frame size" +
" are different. frame.pack() does not work!!");
String frameType = isResizable ? "ResizableFrame" : "NonResizableFrame";
System.out.println("Expected frame size: " + expectedFrameSize);
System.out.println("Actual frame size: " + actualFrameSize);
saveScreenCapture(frameType + ".png");
errorLog.append(frameType + ": Expected and Actual frame size" +
" are different. frame.pack() does not work!! \n");
}
} finally {
frame.dispose();
if (frame != null) {
frame.dispose();
}
}
}

// for debugging purpose, saves screen capture when test fails.
private static void saveScreenCapture() {
private static void saveScreenCapture(String filename) {
BufferedImage image = robot.createScreenCapture(frame.getBounds());
try {
ImageIO.write(image,"png", new File("Frame.png"));
ImageIO.write(image,"png", new File(filename));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down

1 comment on commit 06756c4

@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.