Blame SOURCES/1182694_javaApplicationMenuMisbehave.patch

9e49de
changeset:   10519:e1ca700aaa1f
9e49de
tag:         tip
9e49de
user:        neugens
9e49de
date:        Fri Feb 27 15:50:03 2015 +0100
9e49de
summary:     8071705: Java application menu misbehaves when running multiple screen stacked vertically
9e49de
9e49de
diff --git jdk8/jdk/src/share/classes/javax/swing/JMenu.java jdk8/jdk/src/share/classes/javax/swing/JMenu.java
9e49de
--- jdk8/jdk/src/share/classes/javax/swing/JMenu.java
9e49de
+++ jdk8/jdk/src/share/classes/javax/swing/JMenu.java
9e49de
@@ -475,7 +475,8 @@
9e49de
             }
9e49de
             // Then the y:
9e49de
             y = s.height + yOffset;    // Prefer dropping down
9e49de
-            if (position.y + y + pmSize.height >= screenBounds.height &&
9e49de
+            if (position.y + y + pmSize.height >= screenBounds.height
9e49de
+                                                  + screenBounds.y &&
9e49de
                 // popup doesn't fit - place it wherever there's more room
9e49de
                 screenBounds.height - s.height < 2*(position.y
9e49de
                                                   - screenBounds.y)) {
9e49de
diff --git jdk8/jdk/test/javax/swing/JMenu/8071705/bug8071705.java jdk8/jdk/test/javax/swing/JMenu/8071705/bug8071705.java
9e49de
new file mode 100644
9e49de
--- /dev/null
9e49de
+++ jdk8/jdk/test/javax/swing/JMenu/8071705/bug8071705.java
9e49de
@@ -0,0 +1,207 @@
9e49de
+/*
9e49de
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9e49de
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9e49de
+ *
9e49de
+ * This code is free software; you can redistribute it and/or modify it
9e49de
+ * under the terms of the GNU General Public License version 2 only, as
9e49de
+ * published by the Free Software Foundation.
9e49de
+ *
9e49de
+ * This code is distributed in the hope that it will be useful, but WITHOUT
9e49de
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9e49de
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9e49de
+ * version 2 for more details (a copy is included in the LICENSE file that
9e49de
+ * accompanied this code).
9e49de
+ *
9e49de
+ * You should have received a copy of the GNU General Public License version
9e49de
+ * 2 along with this work; if not, write to the Free Software Foundation,
9e49de
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9e49de
+ *
9e49de
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9e49de
+ * or visit www.oracle.com if you need additional information or have any
9e49de
+ * questions.
9e49de
+ */
9e49de
+
9e49de
+/*
9e49de
+ * @test
9e49de
+ * @bug 8071705
9e49de
+ * @summary  Java application menu misbehaves when running multiple screen stacked vertically
9e49de
+ * @build bug8071705
9e49de
+ * @run main/othervm bug8071705
9e49de
+ */
9e49de
+
9e49de
+import java.awt.Dimension;
9e49de
+import java.awt.GraphicsConfiguration;
9e49de
+import java.awt.GraphicsDevice;
9e49de
+import java.awt.GraphicsEnvironment;
9e49de
+import java.awt.Point;
9e49de
+import java.awt.Rectangle;
9e49de
+import java.awt.Toolkit;
9e49de
+import java.awt.event.ComponentAdapter;
9e49de
+import java.awt.event.ComponentEvent;
9e49de
+import java.awt.event.KeyEvent;
9e49de
+import java.util.concurrent.CountDownLatch;
9e49de
+
9e49de
+import javax.swing.JFrame;
9e49de
+import javax.swing.JMenu;
9e49de
+import javax.swing.JMenuBar;
9e49de
+import javax.swing.JMenuItem;
9e49de
+import javax.swing.JPopupMenu;
9e49de
+import javax.swing.SwingUtilities;
9e49de
+import javax.swing.UIManager;
9e49de
+
9e49de
+public class bug8071705 {
9e49de
+
9e49de
+    public static void main(String[] args) throws Exception {
9e49de
+
9e49de
+        final CountDownLatch latch = new CountDownLatch(1);
9e49de
+        final boolean [] result = new boolean[1];
9e49de
+
9e49de
+        SwingUtilities.invokeLater(new Runnable() {
9e49de
+            @Override
9e49de
+            public void run() {
9e49de
+                JFrame frame = createGUI();
9e49de
+                GraphicsDevice[] devices = checkScreens();
9e49de
+
9e49de
+                // check if we have more than one and if they are stacked
9e49de
+                // vertically
9e49de
+                GraphicsDevice device = checkConfigs(devices);
9e49de
+                if (device == null) {
9e49de
+                    // just pass the test
9e49de
+                    frame.dispose();
9e49de
+                    result[0] = true;
9e49de
+                    latch.countDown();
9e49de
+                } else {
9e49de
+                    FrameListener listener =
9e49de
+                            new FrameListener(device, latch, result);
9e49de
+                    frame.addComponentListener(listener);
9e49de
+                    frame.setVisible(true);
9e49de
+                }
9e49de
+            }
9e49de
+        });
9e49de
+
9e49de
+        latch.await();
9e49de
+
9e49de
+        if (result[0] == false) {
9e49de
+            throw new RuntimeException("popup menu rendered in wrong position");
9e49de
+        }
9e49de
+
9e49de
+        System.out.println("OK");
9e49de
+    }
9e49de
+
9e49de
+    private static GraphicsDevice[] checkScreens() {
9e49de
+        GraphicsEnvironment ge =
9e49de
+            GraphicsEnvironment.getLocalGraphicsEnvironment();
9e49de
+        return ge.getScreenDevices();
9e49de
+    }
9e49de
+
9e49de
+    private static JFrame createGUI() {
9e49de
+        JMenuBar menuBar = new JMenuBar();
9e49de
+        JMenu menu = new JMenu("Some menu");
9e49de
+        menuBar.add(menu);
9e49de
+
9e49de
+        for (int i = 0; i < 10; i++) {
9e49de
+            menu.add(new JMenuItem("Some menu #" + i));
9e49de
+        }
9e49de
+
9e49de
+        JFrame frame = new JFrame();
9e49de
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9e49de
+        frame.setMinimumSize(new Dimension(200, 200));
9e49de
+        frame.setJMenuBar(menuBar);
9e49de
+        return frame;
9e49de
+    }
9e49de
+
9e49de
+    private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
9e49de
+
9e49de
+        GraphicsDevice correctDevice = null;
9e49de
+        if (devices.length < 2) {
9e49de
+            return correctDevice;
9e49de
+        }
9e49de
+
9e49de
+        Toolkit toolkit = Toolkit.getDefaultToolkit();
9e49de
+        Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
9e49de
+        int halfScreen = screenBounds.height/2;
9e49de
+
9e49de
+        for(int i = 0; i < devices.length; i++) {
9e49de
+            if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
9e49de
+                GraphicsConfiguration conf =
9e49de
+                        devices[i].getDefaultConfiguration();
9e49de
+                Rectangle bounds = conf.getBounds();
9e49de
+                if (bounds.y >= halfScreen) {
9e49de
+                    // found
9e49de
+                    correctDevice = devices[i];
9e49de
+                    break;
9e49de
+                }
9e49de
+            }
9e49de
+        }
9e49de
+        return correctDevice;
9e49de
+    }
9e49de
+
9e49de
+    private static class FrameListener extends ComponentAdapter {
9e49de
+
9e49de
+        private GraphicsDevice device;
9e49de
+        private CountDownLatch latch;
9e49de
+        private boolean [] result;
9e49de
+        public FrameListener(GraphicsDevice device,
9e49de
+                             CountDownLatch latch,
9e49de
+                             boolean [] result)
9e49de
+        {
9e49de
+            this.device = device;
9e49de
+            this.latch = latch;
9e49de
+            this.result = result;
9e49de
+        }
9e49de
+
9e49de
+        @Override
9e49de
+        public void componentShown(ComponentEvent e) {
9e49de
+            JFrame frame = (JFrame) e.getComponent();
9e49de
+
9e49de
+            runActualTest(device, latch, frame, result);
9e49de
+
9e49de
+            frame.setVisible(false);
9e49de
+            frame.dispose();
9e49de
+            latch.countDown();
9e49de
+        }
9e49de
+    }
9e49de
+
9e49de
+    private static Rectangle setLocation(JFrame frame, GraphicsDevice device) {
9e49de
+        GraphicsConfiguration conf = device.getDefaultConfiguration();
9e49de
+        Rectangle bounds = conf.getBounds();
9e49de
+
9e49de
+        // put just below half screen
9e49de
+        int x = bounds.x + bounds.width/2;
9e49de
+        int y = bounds.y + bounds.height/2;
9e49de
+        frame.setLocation(x, y);
9e49de
+
9e49de
+        return bounds;
9e49de
+    }
9e49de
+
9e49de
+    private static void runActualTest(GraphicsDevice device,
9e49de
+                                      CountDownLatch latch,
9e49de
+                                      JFrame frame,
9e49de
+                                      boolean [] result)
9e49de
+    {
9e49de
+        Rectangle screenBounds = setLocation(frame, device);
9e49de
+        JMenu menu = frame.getJMenuBar().getMenu(0);
9e49de
+        menu.doClick();
9e49de
+
9e49de
+        Point location = menu.getLocationOnScreen();
9e49de
+        JPopupMenu pm = menu.getPopupMenu();
9e49de
+        Dimension pmSize = pm.getSize();
9e49de
+
9e49de
+        int yOffset = UIManager.getInt("Menu.submenuPopupOffsetY");
9e49de
+        int height = location.y + yOffset + pmSize.height + menu.getHeight();
9e49de
+        int available = screenBounds.y + screenBounds.height - height;
9e49de
+        if (available > 0) {
9e49de
+            Point origin = pm.getLocationOnScreen();
9e49de
+            if (origin.y < location.y) {
9e49de
+                // growing upward, wrong!
9e49de
+                result[0] = false;
9e49de
+            } else {
9e49de
+                // growing downward, ok!
9e49de
+                result[0] = true;
9e49de
+            }
9e49de
+        } else {
9e49de
+            // there is no space, growing upward would be ok, so we pass
9e49de
+            result[0] = true;
9e49de
+        }
9e49de
+    }
9e49de
+}
9e49de