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

Reviewed-by: kizune, aivanov, tr
  • Loading branch information
Harshitha Onkar authored and aivanov-jdk committed Sep 23, 2022
1 parent 2e20e7e commit eca9749
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
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,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 @@ -1400,39 +1399,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 @@ -1445,7 +1438,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
Original file line number Diff line number Diff line change
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

3 comments on commit eca9749

@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 eca9749 Sep 20, 2023

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on eca9749 Sep 20, 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-eca9749d in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-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 eca9749d from the openjdk/jdk repository.

The commit being backported was authored by Harshitha Onkar on 23 Sep 2022 and was reviewed by Alexander Zuev, Alexey Ivanov and Tejesh R.

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/jdk17u-dev:

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

Please sign in to comment.