changeset 58003:cd9a28621c53

8225126: Test SetBoundsPaintTest.html faild on Windows when desktop is scaled Reviewed-by: jdv
author serb
date Tue, 04 Feb 2020 12:56:19 -0800
parents 2f9416465d1d
children 0017ed5e309e
files src/java.desktop/windows/classes/sun/awt/windows/WChoicePeer.java src/java.desktop/windows/native/libawt/windows/awt_Choice.cpp test/jdk/java/awt/Choice/RemoveAllShrinkTest/RemoveAllShrinkTest.java test/jdk/java/awt/Choice/SetBoundsPaintTest/SetBoundsPaintTest.java
diffstat 4 files changed, 129 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/windows/classes/sun/awt/windows/WChoicePeer.java	Mon Feb 03 14:58:54 2020 +0530
+++ b/src/java.desktop/windows/classes/sun/awt/windows/WChoicePeer.java	Tue Feb 04 12:56:19 2020 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2020, Oracle and/or its affiliates. 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
@@ -22,14 +22,19 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
+
 package sun.awt.windows;
 
-import java.awt.*;
-import java.awt.peer.*;
+import java.awt.Choice;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.FontMetrics;
+import java.awt.Window;
 import java.awt.event.ItemEvent;
+import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.awt.event.WindowListener;
-import java.awt.event.WindowAdapter;
+import java.awt.peer.ChoicePeer;
 
 import sun.awt.AWTAccessor;
 import sun.awt.SunToolkit;
@@ -154,12 +159,5 @@
         });
     }
 
-    int getDropDownHeight() {
-        Choice c = (Choice)target;
-        FontMetrics fm = getFontMetrics(c.getFont());
-        int maxItems = Math.min(c.getItemCount(), 8);
-        return fm.getHeight() * maxItems;
-    }
-
     native void closeList();
 }
--- a/src/java.desktop/windows/native/libawt/windows/awt_Choice.cpp	Mon Feb 03 14:58:54 2020 +0530
+++ b/src/java.desktop/windows/native/libawt/windows/awt_Choice.cpp	Tue Feb 04 12:56:19 2020 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2020, Oracle and/or its affiliates. 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
@@ -177,8 +177,8 @@
              * Fix: Set the Choice to its actual size in the component.
              */
             ::GetClientRect(c->GetHWnd(), &rc);
-            env->SetIntField(target, AwtComponent::widthID,  (jint) rc.right);
-            env->SetIntField(target, AwtComponent::heightID, (jint) rc.bottom);
+            env->SetIntField(target, AwtComponent::widthID, c->ScaleDownX(rc.right));
+            env->SetIntField(target, AwtComponent::heightID, c->ScaleDownY(rc.bottom));
 
             if (IS_WINXP) {
                 ::SendMessage(c->GetHWnd(), CB_SETMINVISIBLE, (WPARAM) MINIMUM_NUMBER_OF_VISIBLE_ITEMS, 0);
@@ -228,13 +228,11 @@
 {
     int dropHeight = GetDropDownHeight();
     int fieldHeight = GetFieldHeight();
-    int totalHeight;
 
     // border on drop-down portion is always non-3d (so don't use SM_CYEDGE)
-    int borderHeight = ::GetSystemMetrics(SM_CYBORDER);
+    int borderHeight = ScaleDownY(::GetSystemMetrics(SM_CYBORDER));
     // total height = drop down height + field height + top+bottom drop down border lines
-    totalHeight = dropHeight + fieldHeight +borderHeight*2;
-    return totalHeight;
+    return dropHeight + fieldHeight + borderHeight * 2;
 }
 
 // Recalculate and set the drop-down height for the Choice.
@@ -244,7 +242,7 @@
 
     ::GetWindowRect(GetHWnd(), &rcWindow);
     // resize the drop down to accommodate added/removed items
-    int     totalHeight = GetTotalHeight();
+    int totalHeight = ScaleUpY(GetTotalHeight());
     ::SetWindowPos(GetHWnd(), NULL,
                     0, 0, rcWindow.right - rcWindow.left, totalHeight,
                     SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOZORDER);
@@ -305,11 +303,11 @@
     BOOL bReshape = true;
     if (awtParent != NULL) {
         ::GetWindowRect(GetHWnd(), &rc);
-        int oldW = rc.right - rc.left;
+        int oldW = ScaleDownX(rc.right - rc.left);
         RECT parentRc;
         ::GetWindowRect(awtParent->GetHWnd(), &parentRc);
-        int oldX = rc.left - parentRc.left;
-        int oldY = rc.top - parentRc.top;
+        int oldX = ScaleDownX(rc.left - parentRc.left);
+        int oldY = ScaleDownY(rc.top - parentRc.top);
         bReshape = (x != oldX || y != oldY || w != oldW);
     }
 
--- a/test/jdk/java/awt/Choice/RemoveAllShrinkTest/RemoveAllShrinkTest.java	Mon Feb 03 14:58:54 2020 +0530
+++ b/test/jdk/java/awt/Choice/RemoveAllShrinkTest/RemoveAllShrinkTest.java	Tue Feb 04 12:56:19 2020 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2020, Oracle and/or its affiliates. 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
@@ -24,62 +24,72 @@
 /*
   @test
   @key headful
-  @bug 4851798 8041896
+  @bug 4851798 8041896 8225126
   @summary Tests Choice List shrinks after removeAll
   @run main RemoveAllShrinkTest
 */
 
-import java.awt.*;
-import java.awt.event.*;
-
+import java.awt.BorderLayout;
+import java.awt.Choice;
+import java.awt.Color;
+import java.awt.Frame;
+import java.awt.Panel;
+import java.awt.Point;
+import java.awt.Robot;
+import java.awt.event.InputEvent;
 
 public class RemoveAllShrinkTest {
 
     public static void main(String[] args) {
         Frame f = new Frame();
-        Choice choice = new Choice();
+        try {
+            Choice choice = new Choice();
 
-        for (int i = 0; i < 10; ++i) {
-            choice.addItem("Item " + i);
-        }
+            for (int i = 0; i < 10; ++i) {
+                choice.addItem("Item " + i);
+            }
 
-        f.add(choice, BorderLayout.NORTH);
-        Panel panel = new Panel();
-        panel.setBackground(Color.RED);
-        f.add(panel);
+            f.add(choice, BorderLayout.NORTH);
+            Panel panel = new Panel();
+            panel.setBackground(Color.RED);
+            f.add(panel);
 
-        f.setSize(200, 200);
-        f.setVisible(true);
-        f.toFront();
+            f.setSize(200, 200);
+            f.setLocationRelativeTo(null);
+            f.setVisible(true);
+            f.toFront();
 
-        choice.removeAll();
+            choice.removeAll();
 
-        try {
-            Robot robot = new Robot();
-            robot.setAutoWaitForIdle(true);
-            robot.setAutoDelay(50);
+            try {
+                Robot robot = new Robot();
+                robot.setAutoWaitForIdle(true);
+                robot.setAutoDelay(50);
 
-            robot.waitForIdle();
-            Thread.sleep(200);
+                robot.waitForIdle();
+                Thread.sleep(200);
 
-            Point pt = choice.getLocationOnScreen();
-            robot.mouseMove(pt.x + choice.getWidth() - choice.getHeight() / 2,
-                    pt.y + choice.getHeight() / 2);
-            robot.mousePress(InputEvent.BUTTON1_MASK);
-            robot.mouseRelease(InputEvent.BUTTON1_MASK);
+                Point pt = choice.getLocationOnScreen();
+                robot.mouseMove(pt.x + choice.getWidth() - choice.getHeight() / 2,
+                        pt.y + choice.getHeight() / 2);
+                robot.mousePress(InputEvent.BUTTON1_MASK);
+                robot.mouseRelease(InputEvent.BUTTON1_MASK);
 
-            Thread.sleep(400);
+                Thread.sleep(400);
 
-            Point pt1 = panel.getLocationOnScreen();
+                Point pt1 = panel.getLocationOnScreen();
 
-            Color color = robot.getPixelColor(pt1.x + panel.getWidth() / 2,
-                    pt1.y + panel.getHeight() / 2);
+                Color color = robot.getPixelColor(pt1.x + panel.getWidth() / 2,
+                        pt1.y + panel.getHeight() / 2);
 
-            if (!color.equals(Color.RED)) {
-                throw new RuntimeException("RemoveAllShrinkTest failed. " + color);
+                if (!color.equals(Color.RED)) {
+                    throw new RuntimeException("RemoveAllShrinkTest failed. " + color);
+                }
+            } catch (Exception e) {
+                throw new RuntimeException("The test was not completed.\n\n" + e);
             }
-        } catch (Exception e) {
-            throw new RuntimeException("The test was not completed.\n\n" + e);
+        } finally {
+            f.dispose();
         }
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/awt/Choice/SetBoundsPaintTest/SetBoundsPaintTest.java	Tue Feb 04 12:56:19 2020 -0800
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2003, 2020, Oracle and/or its affiliates. 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.Choice;
+import java.awt.Frame;
+import java.awt.Graphics;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @test
+ * @bug 4790927 4783342 4930678 8225126
+ * @key headful
+ * @summary Tests Choice doesn't flash
+ */
+public final class SetBoundsPaintTest {
+
+    public static void main(String[] args) throws Exception {
+        CountDownLatch go = new CountDownLatch(10);
+        Choice choice = new Choice();
+        choice.addItem("first");
+        choice.addItem("second");
+        Frame frame = new Frame() {
+            @Override
+            public void paint(Graphics g) {
+                g.fillRect(0, 0, 200, 200);
+                choice.setBounds(50, 50, 180, 30);
+                go.countDown();
+            }
+        };
+        try {
+            frame.setLayout(null);
+            frame.add(choice);
+            frame.setSize(300, 300);
+            frame.setLocationRelativeTo(null);
+            frame.setVisible(true);
+
+            if (go.await(5, TimeUnit.SECONDS)) {
+                throw new RuntimeException("recursive setBounds paint");
+            }
+        } finally {
+            frame.dispose();
+        }
+    }
+}
\ No newline at end of file