6848424: java/awt/Frame/FrameSize/TestFrameSize.java needs improvement
authoranthony
Thu, 23 Jul 2009 13:46:23 +0400
changeset 3336 16117c295e3f
parent 3335 891866221955
child 3337 45c02b7c01b6
6848424: java/awt/Frame/FrameSize/TestFrameSize.java needs improvement Summary: The test now thoroughly verifies the pack() method Reviewed-by: art, dcherepanov
jdk/test/java/awt/Frame/FrameSize/TestFrameSize.java
--- a/jdk/test/java/awt/Frame/FrameSize/TestFrameSize.java	Wed Jul 22 13:00:11 2009 +0400
+++ b/jdk/test/java/awt/Frame/FrameSize/TestFrameSize.java	Thu Jul 23 13:46:23 2009 +0400
@@ -1,5 +1,6 @@
 /*
  * Copyright 2009 Red Hat, Inc.  All Rights Reserved.
+ * Portions Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -37,35 +38,62 @@
  * Test fails if size of window is wrong
  */
 
-import java.awt.Dimension;
-import java.awt.Frame;
+import java.awt.*;
 
 public class TestFrameSize {
 
-        static Dimension desiredDimensions = new Dimension(200, 200);
-        static int ERROR_MARGIN = 15;
-        static Frame mainWindow;
+    static Dimension desiredDimensions = new Dimension(200, 200);
+    static Frame mainWindow;
+
+    private static Dimension getClientSize(Frame window) {
+        Dimension size = window.getSize();
+        Insets insets = window.getInsets();
+
+        System.out.println("getClientSize() for " + window);
+        System.out.println("   size: " + size);
+        System.out.println("   insets: " + insets);
 
-        public static void drawGui() {
-                mainWindow = new Frame("");
-                mainWindow.setPreferredSize(desiredDimensions);
-                mainWindow.pack();
+        return new Dimension(
+                size.width - insets.left - insets.right,
+                size.height - insets.top - insets.bottom);
+    }
 
-                Dimension actualDimensions = mainWindow.getSize();
-                System.out.println("Desired dimensions: " + desiredDimensions.toString());
-                System.out.println("Actual dimensions:  " + actualDimensions.toString());
-                if (Math.abs(actualDimensions.height - desiredDimensions.height) > ERROR_MARGIN) {
-                        throw new RuntimeException("Incorrect widow size");
-                }
+    public static void drawGui() {
+        mainWindow = new Frame("");
+        mainWindow.setPreferredSize(desiredDimensions);
+        mainWindow.pack();
+
+        Dimension actualDimensions = mainWindow.getSize();
+        System.out.println("Desired dimensions: " + desiredDimensions.toString());
+        System.out.println("Actual dimensions:  " + actualDimensions.toString());
+        if (!actualDimensions.equals(desiredDimensions)) {
+            throw new RuntimeException("Incorrect widow size");
         }
 
-        public static void main(String[] args) {
-                try {
-                        drawGui();
-                } finally {
-                        if (mainWindow != null) {
-                                mainWindow.dispose();
-                        }
-                }
+        // pack() guarantees to preserve the size of the client area after
+        // showing the window.
+        Dimension clientSize1 = getClientSize(mainWindow);
+        System.out.println("Client size before showing: " + clientSize1);
+
+        mainWindow.setVisible(true);
+
+        ((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync();
+
+        Dimension clientSize2 = getClientSize(mainWindow);
+        System.out.println("Client size after showing: " + clientSize2);
+
+        if (!clientSize2.equals(clientSize1)) {
+            throw new RuntimeException("Incorrect client area size.");
         }
+    }
+
+    public static void main(String[] args) {
+        try {
+            drawGui();
+        } finally {
+            if (mainWindow != null) {
+                mainWindow.dispose();
+            }
+        }
+    }
 }