1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   @test
  26   @bug 7154841
  27   @summary JPopupMenu is overlapped by a Dock on Mac OS X
  28   @author Petr Pchelko
  29   @library ../../../../lib/testlibrary
  30   @build ExtendedRobot jdk.testlibrary.OSInfo
  31   @run main bug7154841
  32  */
  33 
  34 import java.awt.*;
  35 import javax.swing.*;
  36 import java.awt.event.MouseEvent;
  37 import java.awt.event.MouseMotionAdapter;
  38 import java.util.concurrent.atomic.AtomicReference;
  39 import jdk.testlibrary.OSInfo;
  40 
  41 public class bug7154841 {
  42 
  43     private static final int STEP = 10;
  44 
  45     private static volatile boolean passed = false;
  46     private static JFrame frame;
  47     private static JPopupMenu popupMenu;
  48     private static AtomicReference<Rectangle> screenBounds = new AtomicReference<>();
  49 
  50     private static void initAndShowUI() {
  51         popupMenu = new JPopupMenu();
  52         for (int i = 0; i < 100; i++) {
  53             JRadioButtonMenuItem item = new JRadioButtonMenuItem(" Test " + i);
  54             item.addMouseMotionListener(new MouseMotionAdapter() {
  55                 @Override
  56                 public void mouseMoved(MouseEvent e) {
  57                     passed = true;
  58                 }
  59             });
  60             popupMenu.add(item);
  61         }
  62 
  63         frame = new JFrame();
  64         screenBounds.set(getScreenBounds());
  65         frame.setBounds(screenBounds.get());
  66         frame.setVisible(true);
  67     }
  68 
  69     public static void main(String[] args) throws Exception {
  70         if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
  71             return; // Test only for Mac OS X
  72         }
  73 
  74         try {
  75             ExtendedRobot r = new ExtendedRobot();
  76             r.setAutoDelay(100);
  77             r.setAutoWaitForIdle(true);
  78             r.mouseMove(0, 0);
  79 
  80             SwingUtilities.invokeAndWait(bug7154841::initAndShowUI);
  81 
  82             r.waitForIdle(200);
  83 
  84             SwingUtilities.invokeAndWait(() -> {
  85                 popupMenu.show(frame, frame.getX() + frame.getWidth() / 2, frame.getY() + frame.getHeight() / 2);
  86             });
  87 
  88             r.waitForIdle(200);
  89 
  90             int y = (int)screenBounds.get().getY() + (int)screenBounds.get().getHeight() - 10;
  91             int center = (int)(screenBounds.get().getX() + screenBounds.get().getWidth() / 2);
  92             for (int x = center - 10 * STEP; x < center + 10 * STEP; x += STEP) {
  93                 r.mouseMove(x, y);
  94             }
  95 
  96             if (!passed) {
  97                 throw new RuntimeException("Failed: no mouse events on the popup menu");
  98             }
  99         } finally {
 100             SwingUtilities.invokeLater(() -> {
 101                 if (frame != null) {
 102                     frame.dispose();
 103                 }
 104             });
 105         }
 106     }
 107 
 108     public static Rectangle getScreenBounds() {
 109         return GraphicsEnvironment
 110                 .getLocalGraphicsEnvironment()
 111                 .getDefaultScreenDevice()
 112                 .getDefaultConfiguration()
 113                 .getBounds();
 114     }
 115 
 116 }