assertj / assertj-swing

Fluent assertions for Swing apps
Other
108 stars 52 forks source link

JFileChooserFixture.selectFile() fails with IllegalArgumentException on choosing .app directories on OSX #192

Closed angeloBerlin closed 7 years ago

angeloBerlin commented 8 years ago

Hi,

The following test fails with an exception though it you can redo the same steps manually without a problem. There seems to be a different handling of .app containers in OSX.

java.lang.IllegalArgumentException: Unabled to select file /Applications/TextEdit.app: the file chooser can only open files at org.assertj.swing.driver.JFileChooserSelectFileTask.cannotSelectFile(JFileChooserSelectFileTask.java:81) at org.assertj.swing.driver.JFileChooserSelectFileTask.checkSelectionMode(JFileChooserSelectFileTask.java:72) at org.assertj.swing.driver.JFileChooserSelectFileTask.access$000(JFileChooserSelectFileTask.java:37) at org.assertj.swing.driver.JFileChooserSelectFileTask$1.executeInEDT(JFileChooserSelectFileTask.java:44) at org.assertj.swing.edt.GuiTask.run(GuiTask.java:36) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:726) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109) at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:184) at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:229) at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:227) at java.security.AccessController.doPrivileged(Native Method) at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:227) at java.awt.Dialog.show(Dialog.java:1084) at javax.swing.JFileChooser.showDialog(JFileChooser.java:758) at javax.swing.JFileChooser.showOpenDialog(JFileChooser.java:656) at FileChooserFrame$1.actionPerformed(FileChooserFrame.java:30) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6535) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6300) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4891) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) at org.assertj.swing.edt.GuiActionRunner.execute(GuiActionRunner.java:102) at org.assertj.swing.driver.JFileChooserSelectFileTask.setSelectedFile(JFileChooserSelectFileTask.java:40) at org.assertj.swing.driver.JFileChooserDriver.selectFile(JFileChooserDriver.java:75) at org.assertj.swing.fixture.JFileChooserFixture.selectFile(JFileChooserFixture.java:142) at FileChooserTest.selectAppWithFileChooser(FileChooserTest.java:53) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 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)

import java.io.File;

import org.assertj.swing.core.Robot;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.edt.GuiQuery;
import org.assertj.swing.finder.JFileChooserFinder;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.fixture.JFileChooserFixture;
import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
import org.junit.Test;

public class FileChooserTest extends AssertJSwingJUnitTestCase {

    protected FrameFixture window;
    protected Robot robot;

    @Override
    protected void onSetUp() {

        FileChooserFrame mainFrame = GuiActionRunner
                .execute(new GuiQuery<FileChooserFrame>() {

                    protected FileChooserFrame executeInEDT() {

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

    @Test
    public void selectAppWithFileChooser() {
        String directory = "/Applications";
        String filePath = directory + "/TextEdit.app";

        window.button().click();
        JFileChooserFixture fileChooser = JFileChooserFinder.findFileChooser()
                .using(robot);

        fileChooser.setCurrentDirectory(new File(directory));
        fileChooser.selectFile(new File(filePath));
        fileChooser.approve();

        window.textBox().requireText(filePath);
    }
}
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class FileChooserFrame extends JFrame {

    public FileChooserFrame() {

        setSize(200, 100);
        setTitle(getClass().getCanonicalName());
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new FlowLayout());

        JTextField textField = new JTextField();
        textField.setPreferredSize(new Dimension(200, 20));
        JButton button = new JButton("FileChooser");
        JFrame parentFrame = this;
        button.addActionListener(new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnVal = fileChooser.showOpenDialog(parentFrame);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    textField.setText(file.getAbsolutePath());
                }
            }

        });

        add(textField);
        add(button);
        setVisible(true);
    }

    public static void main(String[] args) {
        new FileChooserFrame();
    }
}
<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>AssertJSwingTest</groupId>
  <artifactId>AssertJSwingTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-swing-junit</artifactId>
        <version>3.1.1</version>
    </dependency>
  </dependencies>
</project>
croesch commented 7 years ago

Thanks for providing the problem and the test!

Did you check if it works if you call #setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES) on the FileChoose? As the default is JFilesChooser.FILES_ONLY and AssertJ Swing checks for that setting.

angeloBerlin commented 7 years ago

Hi Christian,

thank you for the tip, that solved the exception. Now I am not able to open a directory with the fileChooser but I will open another Issue for that.

koujun2012 commented 6 years ago

setFileSelectionMode(JFileChooser.FILES_ONLY) OrderFile3.ttf.csv (normal csv file)

throws exception : the file chooser can only open files

if (mode == FILES_ONLY && !file.isFile())

how about change to if (mode == FILES_ONLY && file.isDirectory)