Skip to content

After the java.awt.PopupMenu pops up, it does not hide when clicking elsewhere with the mouse. #17

Open
@oldcwj

Description

JPopupMenu is fine, but PopupMenu is not fine:it does not hide when clicking elsewhere with the mouse

JPopupMenu test code:

package com.github.caciocavallosilano.cacio.ctc;

import java.awt.*;
import java.awt.event.InputEvent;

import javax.swing.*;

import org.assertj.swing.annotation.GUITest;
import com.github.caciocavallosilano.cacio.ctc.junit.CacioAssertJRunner;

import org.junit.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

@RunWith(CacioAssertJRunner.class)
//@RunWith(GUITestRunner.class)
@DisabledOnOs(OS.LINUX)
public class MouseInfoTest {

    @Test
    @GUITest
    public void testSimpleMousePosition() throws AWTException {
        JFrame f = new JFrame();
        f.setSize(100, 100);
        f.setLocation(100, 100);
        f.setVisible(true);

        JPopupMenu m = new JPopupMenu();
        m.add(new JMenuItem("Item 1"));
        f.add(m);
        m.show(f, 40, 30);

        Robot r = new Robot();
        r.mouseMove(150, 150);

        // Simulate mouse click
        r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

        // Delay to allow the PopupMenu to disappear
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Assert that PopupMenu is no longer the active window
        assertFalse(isPopupMenuVisible(m));

        Point p = f.getMousePosition();
        assertEquals(50, p.x);
        assertEquals(50, p.y);
    }

    private boolean isPopupMenuVisible(JPopupMenu popupMenu) {
        return popupMenu.getParent() != null;
    }

}

PopupMenu test code:

package com.github.caciocavallosilano.cacio.ctc;

import java.awt.*;
import java.awt.event.InputEvent;

import javax.swing.*;

import org.assertj.swing.annotation.GUITest;
import com.github.caciocavallosilano.cacio.ctc.junit.CacioAssertJRunner;

import org.junit.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

@RunWith(CacioAssertJRunner.class)
//@RunWith(GUITestRunner.class)
@DisabledOnOs(OS.LINUX)
public class MouseInfoTest {

    @Test
    @GUITest
    public void testSimpleMousePosition() throws AWTException {
        Frame f = new Frame();
        f.setSize(100, 100);
        f.setLocation(100, 100);
        f.setVisible(true);

        PopupMenu m = new PopupMenu();
        m.add(new MenuItem("Item 1"));
        f.add(m);
        m.show(f, 40, 30);

        Robot r = new Robot();
        r.mouseMove(150, 150);

        // Simulate mouse click
        r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

        // Delay to allow the PopupMenu to disappear
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Check if the PopupMenu is still showing
        assertFalse(isPopupMenuVisible(m));

        Point p = f.getMousePosition();
        assertEquals(50, p.x);
        assertEquals(50, p.y);
    }

    private boolean isPopupMenuVisible(PopupMenu popupMenu) {
        return popupMenu.getParent() != null;
    }

}

isPopupMenuVisible method maybe not right,but failed-gui-tests image show the popupmenu not hide

com github caciocavallosilano cacio ctc MouseInfoTest testSimpleMousePosition

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions