Path: blob/master/test/jdk/javax/swing/JPopupMenu/7154841/bug7154841.java
41155 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 715484127* @requires (os.family == "mac")28* @summary JPopupMenu is overlapped by a Dock on Mac OS X29* @library /test/lib30* /lib/client31* @build ExtendedRobot jdk.test.lib.Platform32* @run main bug715484133*/3435import java.awt.*;36import javax.swing.*;37import java.awt.event.MouseEvent;38import java.awt.event.MouseMotionAdapter;39import java.util.concurrent.atomic.AtomicReference;4041import jdk.test.lib.Platform;4243public class bug7154841 {4445private static final int STEP = 10;4647private static volatile boolean passed = false;48private static JFrame frame;49private static JPopupMenu popupMenu;50private static AtomicReference<Rectangle> screenBounds = new AtomicReference<>();5152private static void initAndShowUI() {53popupMenu = new JPopupMenu();54for (int i = 0; i < 400; i++) {55JRadioButtonMenuItem item = new JRadioButtonMenuItem(" Test " + i);56item.addMouseMotionListener(new MouseMotionAdapter() {57@Override58public void mouseMoved(MouseEvent e) {59passed = true;60}61});62popupMenu.add(item);63}6465frame = new JFrame();66screenBounds.set(getScreenBounds());67frame.setBounds(screenBounds.get());68frame.setVisible(true);69}7071public static void main(String[] args) throws Exception {72if (!Platform.isOSX()) {73return; // Test only for Mac OS X74}7576try {77ExtendedRobot r = new ExtendedRobot();78r.setAutoDelay(100);79r.setAutoWaitForIdle(true);80r.mouseMove(0, 0);8182SwingUtilities.invokeAndWait(bug7154841::initAndShowUI);8384r.waitForIdle(200);8586SwingUtilities.invokeAndWait(() -> {87popupMenu.show(frame, frame.getX() + frame.getWidth() / 2, frame.getY() + frame.getHeight() / 2);88});8990r.waitForIdle(200);9192int y = (int)screenBounds.get().getY() + (int)screenBounds.get().getHeight() - 10;93int center = (int)(screenBounds.get().getX() + screenBounds.get().getWidth() / 2);94for (int x = center - 10 * STEP; x < center + 10 * STEP; x += STEP) {95r.mouseMove(x, y);96}9798if (!passed) {99throw new RuntimeException("Failed: no mouse events on the popup menu");100}101} finally {102SwingUtilities.invokeLater(() -> {103if (frame != null) {104frame.dispose();105}106});107}108}109110public static Rectangle getScreenBounds() {111return GraphicsEnvironment112.getLocalGraphicsEnvironment()113.getDefaultScreenDevice()114.getDefaultConfiguration()115.getBounds();116}117118}119120121