benjamin84 / fest

Automatically exported from code.google.com/p/fest
0 stars 0 forks source link

JMenuItemFixture.click() not working #165

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Get a menu item within a menu
2. Invoke the "click()" method.

What is the expected output? What do you see instead?
I'm not totally sure this is a bug, because I don't know if you have to do
something before calling click() to make a click() operation valid. If it's
not valid, then an exception should be immediately thrown saying the menu
item is not visible, or something like that. Otherwise I'd expect the
action to be invoked. Currently, the call to click() eventually times out
with this error:

org.fest.swing.exception.ActionFailedException: Could not obtain position
of component javax.swing.JMenuItem[name='doit', text='Doit',
selected=false, enabled=true, visible=true, showing=false]
    at
org.fest.swing.exception.ActionFailedException.actionFailure(ActionFailedExcepti
on.java:33)
    at org.fest.swing.core.RobotFixture.moveMouse(RobotFixture.java:418)
    at org.fest.swing.core.RobotFixture.moveMouse(RobotFixture.java:412)
    at org.fest.swing.core.RobotFixture.moveMouse(RobotFixture.java:407)
    at org.fest.swing.core.RobotFixture.focus(RobotFixture.java:228)
    at org.fest.swing.core.RobotFixture.focus(RobotFixture.java:215)
    at org.fest.swing.core.RobotFixture.click(RobotFixture.java:342)
    at org.fest.swing.core.RobotFixture.click(RobotFixture.java:332)
    at org.fest.swing.core.RobotFixture.click(RobotFixture.java:322)
    at org.fest.swing.core.RobotFixture.click(RobotFixture.java:312)
    at org.fest.swing.driver.ComponentDriver.click(ComponentDriver.java:73)
    at org.fest.swing.fixture.JMenuItemFixture.click(JMenuItemFixture.java:107)

What version of the product are you using? On what operating system?

fest-swing-1.0a3.jar on MacOS X 10.5 w/Java 1.5

Please provide any additional information below.

Test code:

import static org.junit.Assert.*;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JMenuItemFixture;
import org.junit.*;

public class JMenuItemFESTTest {
    private FrameFixture _window;

    private int _clickCount;

    @BeforeClass
    public static void globalInit() {
        // Keep it simple on MacOS and turn off screen menubar.
        System.setProperty("apple.laf.useScreenMenuBar", "false");
    }

    @Before
    public void setUp() throws Exception {
        _clickCount = 0;
        JFrame f = new JFrame(getClass().getName());
        JMenuBar mb = new JMenuBar();
        f.setJMenuBar(mb);
        JMenu file = new JMenu("File");
        file.setName("file");
        mb.add(file);
        JMenuItem doit = new JMenuItem("Doit");
        doit.setName("doit");
        file.add(doit);
        doit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                _clickCount++;
            }
        });

        f.setPreferredSize(new Dimension(100, 100));

        _window = new FrameFixture(f);
        _window.robot.settings().timeoutToBeVisible(1000);
    }

    @Test 
    public void lookupMenuItemWithNameAndSelect() throws Exception {
        _window.show();
        _window.requireVisible();
        _window.menuItem("doit").select();
        assertEquals("Click count", 1, _clickCount);
    }

    @Test 
    public void lookupMenuItemWithPathAndSelect() throws Exception {
        _window.show();
        _window.requireVisible();
        _window.menuItemWithPath("File", "Doit").select();
        assertEquals("Click count", 1, _clickCount);
    }    

    @Test 
    public void lookupMenuItemWithNameAndClick() throws Exception {
        _window.show();
        _window.requireVisible();
        JMenuItemFixture menu = _window.menuItem("doit");
        menu.click();
        assertEquals("Click count", 1, _clickCount);
    }

    @Test 
    public void lookupMenuItemWithPathAndClick() throws Exception {
        _window.show();
        _window.requireVisible();
        JMenuItemFixture menu = _window.menuItemWithPath("File", "Doit");
        menu.click();
        assertEquals("Click count", 1, _clickCount);
    } 

    @After
    public void tearDown() throws Exception {
        _window.cleanUp();
    }
}

Original issue reported on code.google.com by simeon.f...@gmail.com on 26 Jun 2008 at 9:10

GoogleCodeExporter commented 9 years ago
Hi Simeon,

If I understood the bug description correctly, it is really not a bug. It is a 
case
of bad API design from our part. We have the method 'select' which will make the
parent menu(s) visible before selecting the menu of interest. The method 
'click' only
clicks the menu if it is already visible. I think we should eliminate 'select' 
and
make 'click' behave like 'select' does. I also consider this has to be done 
before we
reach the first release candidate (1.0R1), otherwise we will have to maintain 
two
methods that do exactly the same.

-Alex

Original comment by Alex.Rui...@gmail.com on 26 Jun 2008 at 9:21

GoogleCodeExporter commented 9 years ago
Alex,

Yes, I think the "bug" is more of understanding the semantics of click() vs.
select(). I'm realizing "select" is a very overloaded word in GUI parlance. 
Perhaps
the distinction is that "select" is appropriate when you are choosing something 
from
the underlying data model (e.g. list, combo box, check box, maybe even a table 
cell),
and "click" is more appropriate as a synonym for "invoke" or "perform", when the
underlying model is supported by an Action. But then there's also the definition
where "click" means "mouse press then release". Diagramming all this out, 
showing the
different layer of the architecture (e.g. AWT Robot -> FEST Robot -> Driver ->
Fixture -> User Code) and the "verbs" that apply at each level might help..... 
just
thinking out loud.

My apologies for the title not being more accurate. Feel free to change it if 
you desire.

Simeon

Original comment by simeon.f...@gmail.com on 28 Jun 2008 at 5:13

GoogleCodeExporter commented 9 years ago
Hi Simeon,

I got a little bit confused. Do you agree that it would be better to have 
'click'
perform the behavior of 'select', and we should eliminate 'select'? (I was 
creating a
test that involved JMenuItems and it was very easy to get confused with these 
two
methods) :D

-Alex

Original comment by Alex.Rui...@gmail.com on 8 Jul 2008 at 1:02

GoogleCodeExporter commented 9 years ago
I think in the case of the JMenuItem, it should be "click()". I think select 
should
be reserved for the case where the user is choosing from among an explicit list 
of
things, such as in a JList or JComboBox, and where an argument is required. If 
no
argument is required (i.e. button or menu), I suggest using click().

Original comment by simeon.f...@gmail.com on 8 Jul 2008 at 1:06

GoogleCodeExporter commented 9 years ago
Replaced method 'select(JMenuItem)' with 'click(JMenuItem)' in
org.fest.swing.fixture.JMenuItemFixture.

Removed method 'doubleClick(JMenuItem)' in 
org.fest.swing.fixture.JMenuItemFixture.

Removed method 'rightClick(JMenuItem)' in 
org.fest.swing.fixture.JMenuItemFixture.

Removed method 'click(JMenuItem, MouseButton)' in
org.fest.swing.fixture.JMenuItemFixture.

Original comment by Alex.Rui...@gmail.com on 9 Jul 2008 at 6:12

GoogleCodeExporter commented 9 years ago
Set the module as a label, instead of being part of the title.

Original comment by Alex.Rui...@gmail.com on 1 Dec 2008 at 2:05