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  */
  30 
  31 import sun.awt.OSInfo;
  32 import sun.awt.SunToolkit;
  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 
  40 public class bug7154841 {
  41 
  42     private static final int STEP = 10;
  43 
  44     private static volatile boolean passed = false;
  45     private static JFrame frame;
  46     private static JPopupMenu popupMenu;
  47     private static AtomicReference<Rectangle> screenBounds = new AtomicReference<>();
  48 
  49     private static void initAndShowUI() {
  50         popupMenu = new JPopupMenu();
  51         for (int i = 0; i < 100; i++) {
  52             JRadioButtonMenuItem item = new JRadioButtonMenuItem(" Test " + i);
  53             item.addMouseMotionListener(new MouseMotionAdapter() {
  54                 @Override
  55                 public void mouseMoved(MouseEvent e) {
  56                     passed = true;
  57                 }
  58             });
  59             popupMenu.add(item);
  60         }
  61 
  62         frame = new JFrame();
  63         screenBounds.set(getScreenBounds());
  64         frame.setBounds(screenBounds.get());
  65         frame.setVisible(true);
  66     }
  67 
  68     public static void main(String[] args) throws Exception {
  69         if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
  70             return; // Test only for Mac OS X
  71         }
  72 
  73         try {
  74             Robot r = new Robot();
  75             r.setAutoDelay(100);
  76             r.setAutoWaitForIdle(true);
  77             r.mouseMove(0, 0);
  78 
  79             SwingUtilities.invokeAndWait(bug7154841::initAndShowUI);
  80 
  81             sleep();
  82 
  83             SwingUtilities.invokeAndWait(() -> {
  84                 popupMenu.show(frame, frame.getX() + frame.getWidth() / 2, frame.getY() + frame.getHeight() / 2);
  85             });
  86 
  87             sleep();
  88 
  89             int y = (int)screenBounds.get().getY() + (int)screenBounds.get().getHeight() - 10;
  90             int center = (int)(screenBounds.get().getX() + screenBounds.get().getWidth() / 2);
  91             for (int x = center - 10 * STEP; x < center + 10 * STEP; x += STEP) {
  92                 r.mouseMove(x, y);
  93             }
  94 
  95             if (!passed) {
  96                 throw new RuntimeException("Failed: no mouse events on the popup menu");
  97             }
  98         } finally {
  99             SwingUtilities.invokeLater(() -> {
 100                 if (frame != null) {
 101                     frame.dispose();
 102                 }
 103             });
 104         }
 105     }
 106 
 107     public static Rectangle getScreenBounds() {
 108         return GraphicsEnvironment
 109                 .getLocalGraphicsEnvironment()
 110                 .getDefaultScreenDevice()
 111                 .getDefaultConfiguration()
 112                 .getBounds();
 113     }
 114 
 115     private static void sleep() {
 116         ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
 117         try {
 118             Thread.sleep(200);
 119         } catch (InterruptedException ignored) { }
 120     }
 121 }