Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/7154841/bug7154841.java
38854 views
/*1* Copyright (c) 2013, 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@bug 715484126@summary JPopupMenu is overlapped by a Dock on Mac OS X27@author Petr Pchelko28@library ../../../../lib/testlibrary29@build ExtendedRobot jdk.testlibrary.OSInfo30@run main bug715484131*/3233import java.awt.*;34import javax.swing.*;35import java.awt.event.MouseEvent;36import java.awt.event.MouseMotionAdapter;37import java.util.concurrent.atomic.AtomicReference;38import jdk.testlibrary.OSInfo;3940public class bug7154841 {4142private static final int STEP = 10;4344private static volatile boolean passed = false;45private static JFrame frame;46private static JPopupMenu popupMenu;47private static AtomicReference<Rectangle> screenBounds = new AtomicReference<>();4849private static void initAndShowUI() {50popupMenu = new JPopupMenu();51for (int i = 0; i < 100; i++) {52JRadioButtonMenuItem item = new JRadioButtonMenuItem(" Test " + i);53item.addMouseMotionListener(new MouseMotionAdapter() {54@Override55public void mouseMoved(MouseEvent e) {56passed = true;57}58});59popupMenu.add(item);60}6162frame = new JFrame();63screenBounds.set(getScreenBounds());64frame.setBounds(screenBounds.get());65frame.setVisible(true);66}6768public static void main(String[] args) throws Exception {69if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {70return; // Test only for Mac OS X71}7273try {74ExtendedRobot r = new ExtendedRobot();75r.setAutoDelay(100);76r.setAutoWaitForIdle(true);77r.mouseMove(0, 0);7879SwingUtilities.invokeAndWait(bug7154841::initAndShowUI);8081r.waitForIdle(200);8283SwingUtilities.invokeAndWait(() -> {84popupMenu.show(frame, frame.getX() + frame.getWidth() / 2, frame.getY() + frame.getHeight() / 2);85});8687r.waitForIdle(200);8889int y = (int)screenBounds.get().getY() + (int)screenBounds.get().getHeight() - 10;90int center = (int)(screenBounds.get().getX() + screenBounds.get().getWidth() / 2);91for (int x = center - 10 * STEP; x < center + 10 * STEP; x += STEP) {92r.mouseMove(x, y);93}9495if (!passed) {96throw new RuntimeException("Failed: no mouse events on the popup menu");97}98} finally {99SwingUtilities.invokeLater(() -> {100if (frame != null) {101frame.dispose();102}103});104}105}106107public static Rectangle getScreenBounds() {108return GraphicsEnvironment109.getLocalGraphicsEnvironment()110.getDefaultScreenDevice()111.getDefaultConfiguration()112.getBounds();113}114115}116117118