Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JMenu/8072900/WrongSelectionOnMouseOver.java
38854 views
/*1* Copyright (c) 2015, 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 807290026@summary Mouse events are captured by the wrong menu in OS X27@author Anton Nashatyrev28@run main WrongSelectionOnMouseOver29*/3031import javax.swing.*;32import javax.swing.event.MenuEvent;33import javax.swing.event.MenuListener;34import java.awt.*;35import java.awt.event.MouseAdapter;36import java.awt.event.MouseEvent;37import java.util.concurrent.CountDownLatch;38import java.util.concurrent.TimeUnit;3940import static javax.swing.UIManager.getInstalledLookAndFeels;4142public class WrongSelectionOnMouseOver implements Runnable {4344CountDownLatch firstMenuSelected = new CountDownLatch(1);45CountDownLatch secondMenuMouseEntered = new CountDownLatch(1);46CountDownLatch secondMenuSelected = new CountDownLatch(1);4748JMenu m1, m2;4950private UIManager.LookAndFeelInfo laf;51JFrame frame1;52JFrame frame2;5354public WrongSelectionOnMouseOver(UIManager.LookAndFeelInfo laf) throws Exception {55this.laf = laf;56}5758private void createUI() throws Exception {59System.out.println("Testing UI: " + laf);60UIManager.setLookAndFeel(laf.getClassName());6162{63frame1 = new JFrame("Frame1");64JMenuBar mb = new JMenuBar();65m1 = new JMenu("File");66JMenuItem i1 = new JMenuItem("Save");67JMenuItem i2 = new JMenuItem("Load");6869m1.addMenuListener(new MenuListener() {70@Override71public void menuSelected(MenuEvent e) {72firstMenuSelected.countDown();73System.out.println("Menu1: menuSelected");74}7576@Override77public void menuDeselected(MenuEvent e) {78System.out.println("Menu1: menuDeselected");79}8081@Override82public void menuCanceled(MenuEvent e) {83System.out.println("Menu1: menuCanceled");84}85});8687frame1.setJMenuBar(mb);88mb.add(m1);89m1.add(i1);90m1.add(i2);9192frame1.setLayout(new FlowLayout());93frame1.setBounds(200, 200, 200, 200);9495frame1.setVisible(true);96}9798{99frame2 = new JFrame("Frame2");100JMenuBar mb = new JMenuBar();101m2 = new JMenu("File");102JMenuItem i1 = new JMenuItem("Save");103JMenuItem i2 = new JMenuItem("Load");104105m2.addMouseListener(new MouseAdapter() {106@Override107public void mouseEntered(MouseEvent e) {108secondMenuMouseEntered.countDown();109System.out.println("WrongSelectionOnMouseOver.mouseEntered");110}111});112113m2.addMenuListener(new MenuListener() {114@Override115public void menuSelected(MenuEvent e) {116secondMenuSelected.countDown();117System.out.println("Menu2: menuSelected");118}119120@Override121public void menuDeselected(MenuEvent e) {122System.out.println("Menu2: menuDeselected");123}124125@Override126public void menuCanceled(MenuEvent e) {127System.out.println("Menu2: menuCanceled");128}129});130131frame2.setJMenuBar(mb);132mb.add(m2);133m2.add(i1);134m2.add(i2);135136frame2.setLayout(new FlowLayout());137frame2.setBounds(400, 200, 200, 200);138139frame2.setVisible(true);140}141}142143public void disposeUI() {144frame1.dispose();145frame2.dispose();146}147148@Override149public void run() {150try {151if (frame1 == null) {152createUI();153} else {154disposeUI();155}156} catch (Exception e) {157throw new RuntimeException(e);158}159}160161public void test() throws Exception {162Robot robot = new Robot();163robot.setAutoDelay(100);164165robot.waitForIdle();166167robot.mouseMove((int) m1.getLocationOnScreen().getX() + 5,168(int) m1.getLocationOnScreen().getY() + 5);169robot.mousePress(MouseEvent.BUTTON1_MASK);170robot.mouseRelease(MouseEvent.BUTTON1_MASK);171172if (!firstMenuSelected.await(5, TimeUnit.SECONDS)) {173throw new RuntimeException("Menu has not been selected.");174};175176robot.mouseMove((int) m2.getLocationOnScreen().getX() + 5,177(int) m2.getLocationOnScreen().getY() + 5);178179if (!secondMenuMouseEntered.await(5, TimeUnit.SECONDS)) {180throw new RuntimeException("MouseEntered event missed for the second menu");181};182183if (secondMenuSelected.await(1, TimeUnit.SECONDS)) {184throw new RuntimeException("The second menu has been selected");185};186}187188public static void main(final String[] args) throws Exception {189for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {190WrongSelectionOnMouseOver test = new WrongSelectionOnMouseOver(laf);191SwingUtilities.invokeAndWait(test);192test.test();193SwingUtilities.invokeAndWait(test);194}195System.out.println("Test passed");196}197}198199