Path: blob/master/test/jdk/javax/swing/JMenu/8072900/WrongSelectionOnMouseOver.java
41153 views
/*1* Copyright (c) 2015, 2017, 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 807290027* @summary Mouse events are captured by the wrong menu in OS X28* @author Anton Nashatyrev29* @run main WrongSelectionOnMouseOver30*/3132import javax.swing.*;33import javax.swing.event.MenuEvent;34import javax.swing.event.MenuListener;35import java.awt.*;36import java.awt.event.MouseAdapter;37import java.awt.event.MouseEvent;38import java.util.concurrent.CountDownLatch;39import java.util.concurrent.TimeUnit;4041import static javax.swing.UIManager.getInstalledLookAndFeels;4243public class WrongSelectionOnMouseOver implements Runnable {4445CountDownLatch firstMenuSelected = new CountDownLatch(1);46CountDownLatch secondMenuMouseEntered = new CountDownLatch(1);47CountDownLatch secondMenuSelected = new CountDownLatch(1);4849JMenu m1, m2;5051private UIManager.LookAndFeelInfo laf;52JFrame frame1;53JFrame frame2;54private Point menu1location;55private Point menu2location;5657public WrongSelectionOnMouseOver(UIManager.LookAndFeelInfo laf) throws Exception {58this.laf = laf;59}6061private void createUI() throws Exception {62System.out.println("Testing UI: " + laf);63UIManager.setLookAndFeel(laf.getClassName());6465{66frame1 = new JFrame("Frame1");67JMenuBar mb = new JMenuBar();68m1 = new JMenu("File");69JMenuItem i1 = new JMenuItem("Save");70JMenuItem i2 = new JMenuItem("Load");7172m1.addMenuListener(new MenuListener() {73@Override74public void menuSelected(MenuEvent e) {75firstMenuSelected.countDown();76System.out.println("Menu1: menuSelected");77}7879@Override80public void menuDeselected(MenuEvent e) {81System.out.println("Menu1: menuDeselected");82}8384@Override85public void menuCanceled(MenuEvent e) {86System.out.println("Menu1: menuCanceled");87}88});8990frame1.setJMenuBar(mb);91mb.add(m1);92m1.add(i1);93m1.add(i2);9495frame1.setLayout(new FlowLayout());96frame1.setBounds(200, 200, 200, 200);9798frame1.setVisible(true);99}100101{102frame2 = new JFrame("Frame2");103JMenuBar mb = new JMenuBar();104m2 = new JMenu("File");105JMenuItem i1 = new JMenuItem("Save");106JMenuItem i2 = new JMenuItem("Load");107108m2.addMouseListener(new MouseAdapter() {109@Override110public void mouseEntered(MouseEvent e) {111secondMenuMouseEntered.countDown();112System.out.println("WrongSelectionOnMouseOver.mouseEntered");113}114});115116m2.addMenuListener(new MenuListener() {117@Override118public void menuSelected(MenuEvent e) {119secondMenuSelected.countDown();120System.out.println("Menu2: menuSelected");121}122123@Override124public void menuDeselected(MenuEvent e) {125System.out.println("Menu2: menuDeselected");126}127128@Override129public void menuCanceled(MenuEvent e) {130System.out.println("Menu2: menuCanceled");131}132});133134frame2.setJMenuBar(mb);135mb.add(m2);136m2.add(i1);137m2.add(i2);138139frame2.setLayout(new FlowLayout());140frame2.setBounds(450, 200, 200, 200);141142frame2.setVisible(true);143}144}145146public void disposeUI() {147frame1.dispose();148frame2.dispose();149}150151@Override152public void run() {153try {154if (frame1 == null) {155createUI();156} else {157disposeUI();158}159} catch (Exception e) {160throw new RuntimeException(e);161}162}163164public void test() throws Exception {165Robot robot = new Robot();166robot.setAutoDelay(100);167168robot.waitForIdle();169170SwingUtilities.invokeAndWait(() -> {171menu1location = m1.getLocationOnScreen();172menu2location = m2.getLocationOnScreen();173});174175robot.mouseMove((int) menu1location.getX() + 5,176(int) menu1location.getY() + 5);177robot.mousePress(MouseEvent.BUTTON1_MASK);178robot.mouseRelease(MouseEvent.BUTTON1_MASK);179180if (!firstMenuSelected.await(5, TimeUnit.SECONDS)) {181throw new RuntimeException("Menu has not been selected.");182};183184robot.mouseMove((int) menu2location.getX() + 5,185(int) menu2location.getY() + 5);186187if (!secondMenuMouseEntered.await(5, TimeUnit.SECONDS)) {188throw new RuntimeException("MouseEntered event missed for the second menu");189};190191if (secondMenuSelected.await(1, TimeUnit.SECONDS)) {192throw new RuntimeException("The second menu has been selected");193};194}195196public static void main(final String[] args) throws Exception {197for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {198WrongSelectionOnMouseOver test = new WrongSelectionOnMouseOver(laf);199SwingUtilities.invokeAndWait(test);200test.test();201SwingUtilities.invokeAndWait(test);202}203System.out.println("Test passed");204}205}206207208