Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/4634626/bug4634626.java
38918 views
/*1* Copyright (c) 2003, 2014, 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*/22/* @test23@bug 463462624@summary Implement context popup menus for components25@author Alexander Zuev26@library ../../../../lib/testlibrary27@build ExtendedRobot28@run applet bug4634626.html29*/30import javax.swing.*;31import java.awt.event.*;32import java.awt.*;3334public class bug4634626 extends JApplet {3536public boolean passed = true;37public boolean done = false;3839public JFrame mainFrame = new JFrame("Bug4634626");40public JRootPane rootPane = mainFrame.getRootPane();41public JPanel contentPane = new JPanel();42public JButton nopButton = new JButton("No popup button");43public JTextArea someText = new JTextArea("Some text here", 20, 10);44public JButton popButton = new JButton("Button with the popup");4546public JPopupMenu btnPopup = new JPopupMenu();47public JPopupMenu commonPopup = new JPopupMenu();48static public Error toBeThrown = null;49static int popTrig = MouseEvent.BUTTON3_MASK;50static boolean popt = false;5152public static class MouseWatcher extends MouseAdapter {53public void mousePressed(MouseEvent e) {54if(e.isPopupTrigger()) popt = true;55if(e.getComponent() != null &&56e.getComponent() instanceof JComponent &&57e.isPopupTrigger() &&58((JComponent)e.getComponent()).getComponentPopupMenu() != null) {59toBeThrown =60new Error("The event got thru the component with popup: "61+ e);62}63}64public void mouseReleased(MouseEvent e) {65if(e.isPopupTrigger()) popt = true;66if(e.getComponent() != null &&67e.getComponent() instanceof JComponent &&68e.isPopupTrigger() &&69((JComponent)e.getComponent()).getComponentPopupMenu() != null) {70toBeThrown =71new Error("The event got thru the component with popup: "72+ e);73}74if(toBeThrown != null) {75throw(toBeThrown);76}77}78}7980public static MouseWatcher mouser = new MouseWatcher();8182public void init() {8384try {85popButton.setComponentPopupMenu(null);86popButton.setComponentPopupMenu(null);87popButton.setComponentPopupMenu(btnPopup);88popButton.setComponentPopupMenu(null);89} catch(Exception ex) {90System.err.println("Unexpected exception was thrown by " +91"setComponentPopupMenu() method: " + ex);92}93btnPopup.add("Button 1");94btnPopup.add("Button 2");95btnPopup.add("Button 3");96popButton.setComponentPopupMenu(btnPopup);97popButton.addMouseListener(mouser);98commonPopup.add("One");99commonPopup.add("Two");100commonPopup.add("Three");101102contentPane.setLayout(new BorderLayout());103contentPane.setComponentPopupMenu(commonPopup);104contentPane.addMouseListener(mouser);105contentPane.add(nopButton, BorderLayout.NORTH);106nopButton.addMouseListener(mouser);107contentPane.add(popButton, BorderLayout.SOUTH);108someText.addMouseListener(mouser);109contentPane.add(someText, BorderLayout.CENTER);110mainFrame.setContentPane(contentPane);111112mainFrame.pack();113mainFrame.setLocation(50, 50);114115mainFrame.addWindowListener(new TestStateListener());116mainFrame.setVisible(true);117118while(!done) Thread.yield();119120if(!passed) {121throw new RuntimeException("Test failed");122}123124}125126public class TestStateListener extends WindowAdapter {127public void windowOpened(WindowEvent ev) {128try {129ev.getWindow().toFront();130ev.getWindow().requestFocus();131new Thread(new RobotThread()).start();132} catch (Exception ex) {133throw new RuntimeException("Thread Exception");134}135}136}137138class RobotThread implements Runnable {139public void run() {140ExtendedRobot robo;141try {142robo = new ExtendedRobot();143}catch(Exception ex) {144ex.printStackTrace();145throw new RuntimeException("Cannot create Robot");146}147robo.setAutoDelay(100);148robo.waitForIdle();149150// Determine working popup trigger event151clickMouseOn(robo, nopButton, popTrig);152robo.waitForIdle();153robo.delay(500);154if(!popt) popTrig = MouseEvent.BUTTON2_MASK;155156// Inheritance is OFF by default. Popup should not appear.157clickMouseOn(robo, someText, popTrig);158159// Set inheritance ON watch for popup.160someText.setInheritsPopupMenu(true);161clickMouseOn(robo, someText, popTrig);162robo.waitForIdle();163robo.delay(500);164if(!commonPopup.isVisible()) {165toBeThrown = new Error("Popup should be visible");166passed = false;167}168// Dispose popup.169robo.type(KeyEvent.VK_ESCAPE);170robo.waitForIdle();171someText.setInheritsPopupMenu(false);172173// Button with popup assigned. Wathch for popup.174clickMouseOn(robo, popButton, popTrig);175robo.waitForIdle();176robo.delay(500);177if(!btnPopup.isVisible()) {178toBeThrown = new Error("Popup should be visible");179passed = false;180}181// Dispose popup.182robo.type(KeyEvent.VK_ESCAPE);183// Test finished.184done = true;185}186}187188189190public void destroy() {191if(!passed) {192throw(toBeThrown);193}194}195private void clickMouseOn(ExtendedRobot robot, Component c, int button) {196java.awt.Point p = c.getLocationOnScreen();197java.awt.Dimension size = c.getSize();198p.x += size.width / 2;199p.y += size.height / 2;200robot.mouseMove(p.x, p.y);201robot.delay(100);202robot.click(button);203}204}205206207