Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking on disabled Swing components

I have a disabled JTable that provides a popup menu:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

public class DisabledTableFrame extends JFrame {

    public DisabledTableFrame() {

        setSize(200, 100);
        setTitle(getClass().getCanonicalName());
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JTable table = new JTable();
        add(table);

        table.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                mouseReleased(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                new PopupMenu();
            }

        });

        table.setEnabled(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new DisabledTableFrame();

    }

    private class PopupMenu extends JPopupMenu {

        public PopupMenu() {

            JMenuItem menuItem = new JMenuItem("TEST");
            add(menuItem);
            setVisible(true);
        }
    }
}

So when testing this function with AssertJ Swing using:

import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.edt.GuiQuery;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
import org.junit.Test;

public class PopupTestCase extends AssertJSwingJUnitTestCase {

    protected FrameFixture window;

    @Override
    protected void onSetUp() {
        DisabledTableFrame mainFrame = GuiActionRunner
                .execute(new GuiQuery<DisabledTableFrame>() {
                    protected DisabledTableFrame executeInEDT() {

                        return new DisabledTableFrame();
                    }
                });

        window = new FrameFixture(robot(), mainFrame);
    }

    @Test
    public void popupShouldBeOpened() {

        window.table().showPopupMenu();
    }
}

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>AssertJSwingInactiveTest</groupId>
    <artifactId>AssertJSwingInactiveTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-swing-junit-4.5</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

It works fine when the table is enabled. But the popupShoudBeOpened() test on the disabled table throws the following exception:

   java.lang.IllegalStateException: Expecting component javax.swing.JTable[name=null, rowCount=0, columnCount=0, enabled=false, visible=true, showing=true] to be enabled
at org.assertj.swing.driver.ComponentPreconditions.checkEnabled(ComponentPreconditions.java:68)
at org.assertj.swing.driver.ComponentPreconditions.checkEnabledAndShowing(ComponentPreconditions.java:48)
at org.assertj.swing.driver.ComponentDriver$2.executeInEDT(ComponentDriver.java:555)
at org.assertj.swing.edt.GuiTask.run(GuiTask.java:38)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
at org.assertj.swing.edt.GuiActionRunner.execute(GuiActionRunner.java:103)
at org.assertj.swing.driver.ComponentDriver.checkInEdtEnabledAndShowing(ComponentDriver.java:552)
at org.assertj.swing.driver.ComponentDriver.invokePopupMenu(ComponentDriver.java:519)
at org.assertj.swing.fixture.AbstractJPopupMenuInvokerFixture.showPopupMenu(AbstractJPopupMenuInvokerFixture.java:95)
at GUITestCase.popupShouldBeOpened(GUITestCase.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

As the popup menu obviously works even on the disabled table, how can I get AssertJ to "right click" the disabled table?

like image 888
Angelo Berlin Avatar asked Nov 22 '15 17:11

Angelo Berlin


People also ask

What are the components of swing?

For example, a button, check-box, radio-button, text-field, etc. These together form the components in Swing. So, to summarise, Swing components are the interactive elements in a Java application.

What is the function of the swing button?

It enables the middle button and the left button, and disables itself. As the ButtonDemo example shows, a Swing button can display both text and an image. In ButtonDemo, each button has its text in a different place, relative to its image. The underlined letter in each button's text shows the mnemonic —...

How do I add a radio button to a Swing application?

The Swing release supports radio buttons with the JRadioButton and ButtonGroup classes. To put a radio button in a menu, use the JRadioButtonMenuItem class. Other ways of displaying one-of-many choices are combo boxes and lists.

What is swing in Java?

We know that Swing is a GUI widget toolkit for Java. Every application has some basic interactive interface for the user. For example, a button, check-box, radio-button, text-field, etc. These together form the components in Swing.


1 Answers

AsserJ expected the tested component to be enabled.
Once it's disabled, It will throw an IllegalStateException.
Check AbstractComponentFixture API here.

One way to fix this issue is to divide for 2 tests and contain the exception when the table is expected to be disabled:

@Test
public void popupShouldBeOpenedIfTableIsDisabled() {
    try {
        window.table().showPopupMenu();
    } catch (IllegalStateException e) {
        // Continue normally if IllegalStateException was thrown since the table is disabled on purpose.
    }
}

@Test
public void popupShouldBeOpenedIfTableIsEnabled() {
    window.table().showPopupMenu();
    // IllegalStateException will fail the test since the table is enabled.
}
like image 70
Leet-Falcon Avatar answered Oct 21 '22 09:10

Leet-Falcon