Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
oldcwj opened this issue Mar 13, 2024 · 2 comments

Comments

@oldcwj
Copy link

oldcwj commented Mar 13, 2024

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

@gschrader
Copy link
Member

Hi sorry for the late response. Nothing obvious is jumping out at me as to why the mouse event isn't triggering the popup to close. Did this work in a previous version? This library has mainly been concerned with Swing so I'm not sure how much effort I want to put into AWT support.

@oldcwj
Copy link
Author

oldcwj commented Apr 3, 2024

I use cacio-1.11 tag with jdk11 has the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants