Path: blob/master/test/jdk/javax/swing/JPopupMenu/4634626/bug4634626.java
41153 views
/*1* Copyright (c) 2003, 2018, 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 463462627* @summary Implement context popup menus for components28* @library /lib/client29* @build ExtendedRobot30* @run main bug463462631*/3233import javax.swing.*;34import java.awt.event.*;35import java.awt.*;3637public class bug4634626 {3839public boolean passed = true;40public boolean done = false;4142public static JFrame mainFrame = new JFrame("Bug4634626");43public JRootPane rootPane = mainFrame.getRootPane();44public JPanel contentPane = new JPanel();45public JButton nopButton = new JButton("No popup button");46public JTextArea someText = new JTextArea("Some text here", 20, 10);47public JButton popButton = new JButton("Button with the popup");4849public JPopupMenu btnPopup = new JPopupMenu();50public JPopupMenu commonPopup = new JPopupMenu();51static public Error toBeThrown = null;52static int popTrig = MouseEvent.BUTTON3_MASK;53static boolean popt = false;5455public static class MouseWatcher extends MouseAdapter {56public void mousePressed(MouseEvent e) {57if(e.isPopupTrigger()) popt = true;58if(e.getComponent() != null &&59e.getComponent() instanceof JComponent &&60e.isPopupTrigger() &&61((JComponent)e.getComponent()).getComponentPopupMenu() != null) {62toBeThrown =63new Error("The event got thru the component with popup: "64+ e);65}66}67public void mouseReleased(MouseEvent e) {68if(e.isPopupTrigger()) popt = true;69if(e.getComponent() != null &&70e.getComponent() instanceof JComponent &&71e.isPopupTrigger() &&72((JComponent)e.getComponent()).getComponentPopupMenu() != null) {73toBeThrown =74new Error("The event got thru the component with popup: "75+ e);76}77if(toBeThrown != null) {78throw(toBeThrown);79}80}81}8283public static MouseWatcher mouser = new MouseWatcher();8485public static void main(final String[] args) throws Exception {86try {87bug4634626 app = new bug4634626();88app.init();89app.destroy();90} finally {91if (mainFrame != null) SwingUtilities.invokeAndWait(() -> mainFrame.dispose());92}93}9495public void init() {9697try {98popButton.setComponentPopupMenu(null);99popButton.setComponentPopupMenu(null);100popButton.setComponentPopupMenu(btnPopup);101popButton.setComponentPopupMenu(null);102} catch(Exception ex) {103System.err.println("Unexpected exception was thrown by " +104"setComponentPopupMenu() method: " + ex);105}106btnPopup.add("Button 1");107btnPopup.add("Button 2");108btnPopup.add("Button 3");109popButton.setComponentPopupMenu(btnPopup);110popButton.addMouseListener(mouser);111commonPopup.add("One");112commonPopup.add("Two");113commonPopup.add("Three");114115contentPane.setLayout(new BorderLayout());116contentPane.setComponentPopupMenu(commonPopup);117contentPane.addMouseListener(mouser);118contentPane.add(nopButton, BorderLayout.NORTH);119nopButton.addMouseListener(mouser);120contentPane.add(popButton, BorderLayout.SOUTH);121someText.addMouseListener(mouser);122contentPane.add(someText, BorderLayout.CENTER);123mainFrame.setContentPane(contentPane);124125mainFrame.pack();126mainFrame.setLocation(50, 50);127128mainFrame.addWindowListener(new TestStateListener());129mainFrame.setLocationRelativeTo(null);130mainFrame.setVisible(true);131132while(!done) Thread.yield();133134if(!passed) {135throw new RuntimeException("Test failed");136}137138}139140public class TestStateListener extends WindowAdapter {141public void windowOpened(WindowEvent ev) {142try {143ev.getWindow().toFront();144ev.getWindow().requestFocus();145new Thread(new RobotThread()).start();146} catch (Exception ex) {147throw new RuntimeException("Thread Exception");148}149}150}151152class RobotThread implements Runnable {153public void run() {154ExtendedRobot robo;155try {156robo = new ExtendedRobot();157}catch(Exception ex) {158ex.printStackTrace();159throw new RuntimeException("Cannot create Robot");160}161robo.setAutoDelay(100);162robo.waitForIdle();163164// Determine working popup trigger event165clickMouseOn(robo, nopButton, popTrig);166robo.waitForIdle();167robo.delay(500);168if(!popt) popTrig = MouseEvent.BUTTON2_MASK;169170// Inheritance is OFF by default. Popup should not appear.171clickMouseOn(robo, someText, popTrig);172173// Set inheritance ON watch for popup.174someText.setInheritsPopupMenu(true);175clickMouseOn(robo, someText, popTrig);176robo.waitForIdle();177robo.delay(500);178if(!commonPopup.isVisible()) {179toBeThrown = new Error("Popup should be visible");180passed = false;181}182// Dispose popup.183robo.type(KeyEvent.VK_ESCAPE);184robo.waitForIdle();185someText.setInheritsPopupMenu(false);186187// Button with popup assigned. Wathch for popup.188clickMouseOn(robo, popButton, popTrig);189robo.waitForIdle();190robo.delay(500);191if(!btnPopup.isVisible()) {192toBeThrown = new Error("Popup should be visible");193passed = false;194}195// Dispose popup.196robo.type(KeyEvent.VK_ESCAPE);197// Test finished.198done = true;199}200}201202203204public void destroy() {205if(!passed) {206throw(toBeThrown);207}208}209private void clickMouseOn(ExtendedRobot robot, Component c, int button) {210java.awt.Point p = c.getLocationOnScreen();211java.awt.Dimension size = c.getSize();212p.x += size.width / 2;213p.y += size.height / 2;214robot.mouseMove(p.x, p.y);215robot.delay(100);216robot.click(button);217}218}219220221