Skip to content

Commit

Permalink
8017175: [TESTBUG] javax/swing/JPopupMenu/4634626/bug4634626.java som…
Browse files Browse the repository at this point in the history
…etimes failed on mac

Backport-of: 4c3491bfa5f296b80c56a37cb4fffd6497323ac2
  • Loading branch information
GoeLin committed Jul 25, 2022
1 parent dae14b3 commit c406983
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 61 deletions.
1 change: 0 additions & 1 deletion test/jdk/ProblemList.txt
Expand Up @@ -740,7 +740,6 @@ javax/swing/dnd/8139050/NativeErrorsInTableDnD.java 8202765 macosx-all,linux-al
javax/swing/Popup/TaskbarPositionTest.java 8065097 macosx-all,linux-all
javax/swing/JEditorPane/6917744/bug6917744.java 8213124 macosx-all
javax/swing/JRootPane/4670486/bug4670486.java 8042381 macosx-all
javax/swing/JPopupMenu/4634626/bug4634626.java 8017175 macosx-all

sanity/client/SwingSet/src/ToolTipDemoTest.java 8225012 windows-all,macosx-all
sanity/client/SwingSet/src/ScrollPaneDemoTest.java 8225013 linux-all
Expand Down
160 changes: 100 additions & 60 deletions test/jdk/javax/swing/JPopupMenu/4634626/bug4634626.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -31,24 +31,35 @@
* @run main bug4634626
*/

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.atomic.AtomicBoolean;

public class bug4634626 {

public boolean passed = true;
public boolean done = false;
public volatile boolean passed = true;
public volatile boolean done = false;

public static JFrame mainFrame = new JFrame("Bug4634626");
public JRootPane rootPane = mainFrame.getRootPane();
public JPanel contentPane = new JPanel();
public JButton nopButton = new JButton("No popup button");
public JTextArea someText = new JTextArea("Some text here", 20, 10);
public JButton popButton = new JButton("Button with the popup");
public static JFrame mainFrame;
public JPanel contentPane;
public JButton nopButton;
public JTextArea someText;
public JButton popButton;

public JPopupMenu btnPopup = new JPopupMenu();
public JPopupMenu commonPopup = new JPopupMenu();
public JPopupMenu btnPopup;
public JPopupMenu commonPopup;
static public Error toBeThrown = null;
static int popTrig = MouseEvent.BUTTON3_MASK;
static boolean popt = false;
Expand All @@ -61,18 +72,19 @@ public void mousePressed(MouseEvent e) {
e.isPopupTrigger() &&
((JComponent)e.getComponent()).getComponentPopupMenu() != null) {
toBeThrown =
new Error("The event got thru the component with popup: "
new Error("The event got through the component with popup: "
+ e);
}
}

public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger()) popt = true;
if(e.getComponent() != null &&
e.getComponent() instanceof JComponent &&
e.isPopupTrigger() &&
((JComponent)e.getComponent()).getComponentPopupMenu() != null) {
toBeThrown =
new Error("The event got thru the component with popup: "
new Error("The event got through the component with popup: "
+ e);
}
if(toBeThrown != null) {
Expand All @@ -93,49 +105,57 @@ public static void main(final String[] args) throws Exception {
}
}

public void init() {
public void init() throws Exception {
SwingUtilities.invokeLater(() -> {
mainFrame = new JFrame("Bug4634626");
contentPane = new JPanel();
nopButton = new JButton("No popup button");
someText = new JTextArea("Some text here", 20, 10);
popButton = new JButton("Button with the popup");

try {
popButton.setComponentPopupMenu(null);
popButton.setComponentPopupMenu(null);
btnPopup = new JPopupMenu();
commonPopup = new JPopupMenu();

try {
popButton.setComponentPopupMenu(null);
popButton.setComponentPopupMenu(null);
popButton.setComponentPopupMenu(btnPopup);
popButton.setComponentPopupMenu(null);
} catch (Exception ex) {
System.err.println("Unexpected exception was thrown by " +
"setComponentPopupMenu() method: " + ex);
}
btnPopup.add("Button 1");
btnPopup.add("Button 2");
btnPopup.add("Button 3");
popButton.setComponentPopupMenu(btnPopup);
popButton.setComponentPopupMenu(null);
} catch(Exception ex) {
System.err.println("Unexpected exception was thrown by " +
"setComponentPopupMenu() method: " + ex);
}
btnPopup.add("Button 1");
btnPopup.add("Button 2");
btnPopup.add("Button 3");
popButton.setComponentPopupMenu(btnPopup);
popButton.addMouseListener(mouser);
commonPopup.add("One");
commonPopup.add("Two");
commonPopup.add("Three");

contentPane.setLayout(new BorderLayout());
contentPane.setComponentPopupMenu(commonPopup);
contentPane.addMouseListener(mouser);
contentPane.add(nopButton, BorderLayout.NORTH);
nopButton.addMouseListener(mouser);
contentPane.add(popButton, BorderLayout.SOUTH);
someText.addMouseListener(mouser);
contentPane.add(someText, BorderLayout.CENTER);
mainFrame.setContentPane(contentPane);

mainFrame.pack();
mainFrame.setLocation(50, 50);

mainFrame.addWindowListener(new TestStateListener());
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
popButton.addMouseListener(mouser);
commonPopup.add("One");
commonPopup.add("Two");
commonPopup.add("Three");

contentPane.setLayout(new BorderLayout());
contentPane.setComponentPopupMenu(commonPopup);
contentPane.addMouseListener(mouser);
contentPane.add(nopButton, BorderLayout.NORTH);
nopButton.addMouseListener(mouser);
contentPane.add(popButton, BorderLayout.SOUTH);
someText.addMouseListener(mouser);
contentPane.add(someText, BorderLayout.CENTER);
mainFrame.setContentPane(contentPane);

mainFrame.pack();
mainFrame.setLocation(50, 50);
mainFrame.addWindowListener(new TestStateListener());
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
});

while(!done) Thread.yield();

if(!passed) {
throw new RuntimeException("Test failed");
}

}

public class TestStateListener extends WindowAdapter {
Expand Down Expand Up @@ -165,31 +185,52 @@ public void run() {
// Determine working popup trigger event
clickMouseOn(robo, nopButton, popTrig);
robo.waitForIdle();
robo.delay(500);
robo.delay(1000);
if(!popt) popTrig = MouseEvent.BUTTON2_MASK;

// Inheritance is OFF by default. Popup should not appear.
clickMouseOn(robo, someText, popTrig);

// Set inheritance ON watch for popup.
someText.setInheritsPopupMenu(true);
try {
SwingUtilities.invokeAndWait(() -> someText.setInheritsPopupMenu(true));
} catch (Exception ex) {
throw new RuntimeException("Can not assign popup to button", ex);
}
clickMouseOn(robo, someText, popTrig);
robo.waitForIdle();
robo.delay(500);
if(!commonPopup.isVisible()) {
robo.delay(1000);
AtomicBoolean popupVisible = new AtomicBoolean(false);
try {
SwingUtilities.invokeAndWait(() ->
popupVisible.set(commonPopup.isVisible()));
} catch (Exception ex) {
throw new RuntimeException("Can not get commonPopup status");
}
if(!popupVisible.get()) {
toBeThrown = new Error("Popup should be visible");
passed = false;
}
// Dispose popup.
robo.type(KeyEvent.VK_ESCAPE);
robo.waitForIdle();
someText.setInheritsPopupMenu(false);
try {
SwingUtilities.invokeAndWait(() -> someText.setInheritsPopupMenu(false));
} catch (Exception ex) {
throw new RuntimeException("Can not unassign popup on button", ex);
}

// Button with popup assigned. Wathch for popup.
clickMouseOn(robo, popButton, popTrig);
robo.waitForIdle();
robo.delay(500);
if(!btnPopup.isVisible()) {
robo.delay(1000);
try {
SwingUtilities.invokeAndWait(() ->
popupVisible.set(btnPopup.isVisible()));
} catch (Exception ex) {
throw new RuntimeException("Can not get btnPopup status");
}
if(!popupVisible.get()) {
toBeThrown = new Error("Popup should be visible");
passed = false;
}
Expand All @@ -200,13 +241,12 @@ public void run() {
}
}



public void destroy() {
if(!passed) {
throw(toBeThrown);
}
}

private void clickMouseOn(ExtendedRobot robot, Component c, int button) {
java.awt.Point p = c.getLocationOnScreen();
java.awt.Dimension size = c.getSize();
Expand Down

1 comment on commit c406983

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.