Path: blob/master/test/jdk/javax/swing/JPopupMenu/4634626/bug4634626.java
66646 views
/*1* Copyright (c) 2003, 2021, 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.JButton;34import javax.swing.JComponent;35import javax.swing.JFrame;36import javax.swing.JPanel;37import javax.swing.JPopupMenu;38import javax.swing.JTextArea;39import javax.swing.SwingUtilities;40import java.awt.BorderLayout;41import java.awt.Component;42import java.awt.event.KeyEvent;43import java.awt.event.MouseAdapter;44import java.awt.event.MouseEvent;45import java.awt.event.WindowAdapter;46import java.awt.event.WindowEvent;47import java.util.concurrent.atomic.AtomicBoolean;4849public class bug4634626 {5051public volatile boolean passed = true;52public volatile boolean done = false;5354public static JFrame mainFrame;55public JPanel contentPane;56public JButton nopButton;57public JTextArea someText;58public JButton popButton;5960public JPopupMenu btnPopup;61public JPopupMenu commonPopup;62static public Error toBeThrown = null;63static int popTrig = MouseEvent.BUTTON3_MASK;64static boolean popt = false;6566public static class MouseWatcher extends MouseAdapter {67public void mousePressed(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 through the component with popup: "75+ e);76}77}7879public void mouseReleased(MouseEvent e) {80if(e.isPopupTrigger()) popt = true;81if(e.getComponent() != null &&82e.getComponent() instanceof JComponent &&83e.isPopupTrigger() &&84((JComponent)e.getComponent()).getComponentPopupMenu() != null) {85toBeThrown =86new Error("The event got through the component with popup: "87+ e);88}89if(toBeThrown != null) {90throw(toBeThrown);91}92}93}9495public static MouseWatcher mouser = new MouseWatcher();9697public static void main(final String[] args) throws Exception {98try {99bug4634626 app = new bug4634626();100app.init();101app.destroy();102} finally {103if (mainFrame != null) SwingUtilities.invokeAndWait(() -> mainFrame.dispose());104}105}106107public void init() throws Exception {108SwingUtilities.invokeLater(() -> {109mainFrame = new JFrame("Bug4634626");110contentPane = new JPanel();111nopButton = new JButton("No popup button");112someText = new JTextArea("Some text here", 20, 10);113popButton = new JButton("Button with the popup");114115btnPopup = new JPopupMenu();116commonPopup = new JPopupMenu();117118try {119popButton.setComponentPopupMenu(null);120popButton.setComponentPopupMenu(null);121popButton.setComponentPopupMenu(btnPopup);122popButton.setComponentPopupMenu(null);123} catch (Exception ex) {124System.err.println("Unexpected exception was thrown by " +125"setComponentPopupMenu() method: " + ex);126}127btnPopup.add("Button 1");128btnPopup.add("Button 2");129btnPopup.add("Button 3");130popButton.setComponentPopupMenu(btnPopup);131popButton.addMouseListener(mouser);132commonPopup.add("One");133commonPopup.add("Two");134commonPopup.add("Three");135136contentPane.setLayout(new BorderLayout());137contentPane.setComponentPopupMenu(commonPopup);138contentPane.addMouseListener(mouser);139contentPane.add(nopButton, BorderLayout.NORTH);140nopButton.addMouseListener(mouser);141contentPane.add(popButton, BorderLayout.SOUTH);142someText.addMouseListener(mouser);143contentPane.add(someText, BorderLayout.CENTER);144mainFrame.setContentPane(contentPane);145146mainFrame.pack();147mainFrame.setLocation(50, 50);148mainFrame.addWindowListener(new TestStateListener());149mainFrame.setLocationRelativeTo(null);150mainFrame.setVisible(true);151});152153while(!done) Thread.yield();154155if(!passed) {156throw new RuntimeException("Test failed");157}158}159160public class TestStateListener extends WindowAdapter {161public void windowOpened(WindowEvent ev) {162try {163ev.getWindow().toFront();164ev.getWindow().requestFocus();165new Thread(new RobotThread()).start();166} catch (Exception ex) {167throw new RuntimeException("Thread Exception");168}169}170}171172class RobotThread implements Runnable {173public void run() {174ExtendedRobot robo;175try {176robo = new ExtendedRobot();177}catch(Exception ex) {178ex.printStackTrace();179throw new RuntimeException("Cannot create Robot");180}181robo.setAutoDelay(100);182robo.waitForIdle();183184// Determine working popup trigger event185clickMouseOn(robo, nopButton, popTrig);186robo.waitForIdle();187robo.delay(1000);188if(!popt) popTrig = MouseEvent.BUTTON2_MASK;189190// Inheritance is OFF by default. Popup should not appear.191clickMouseOn(robo, someText, popTrig);192193// Set inheritance ON watch for popup.194try {195SwingUtilities.invokeAndWait(() -> someText.setInheritsPopupMenu(true));196} catch (Exception ex) {197throw new RuntimeException("Can not assign popup to button", ex);198}199clickMouseOn(robo, someText, popTrig);200robo.waitForIdle();201robo.delay(1000);202AtomicBoolean popupVisible = new AtomicBoolean(false);203try {204SwingUtilities.invokeAndWait(() ->205popupVisible.set(commonPopup.isVisible()));206} catch (Exception ex) {207throw new RuntimeException("Can not get commonPopup status");208}209if(!popupVisible.get()) {210toBeThrown = new Error("Popup should be visible");211passed = false;212}213// Dispose popup.214robo.type(KeyEvent.VK_ESCAPE);215robo.waitForIdle();216try {217SwingUtilities.invokeAndWait(() -> someText.setInheritsPopupMenu(false));218} catch (Exception ex) {219throw new RuntimeException("Can not unassign popup on button", ex);220}221222// Button with popup assigned. Wathch for popup.223clickMouseOn(robo, popButton, popTrig);224robo.waitForIdle();225robo.delay(1000);226try {227SwingUtilities.invokeAndWait(() ->228popupVisible.set(btnPopup.isVisible()));229} catch (Exception ex) {230throw new RuntimeException("Can not get btnPopup status");231}232if(!popupVisible.get()) {233toBeThrown = new Error("Popup should be visible");234passed = false;235}236// Dispose popup.237robo.type(KeyEvent.VK_ESCAPE);238// Test finished.239done = true;240}241}242243public void destroy() {244if(!passed) {245throw(toBeThrown);246}247}248249private void clickMouseOn(ExtendedRobot robot, Component c, int button) {250java.awt.Point p = c.getLocationOnScreen();251java.awt.Dimension size = c.getSize();252p.x += size.width / 2;253p.y += size.height / 2;254robot.mouseMove(p.x, p.y);255robot.delay(100);256robot.click(button);257}258}259260261