CaciocavalloSilano / caciocavallo

headless Swing UI testing
GNU General Public License v2.0
30 stars 15 forks source link

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

Open oldcwj opened 8 months ago

oldcwj commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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