assertj-swing icon indicating copy to clipboard operation
assertj-swing copied to clipboard

How to select directories with JFileChooserFixture

Open angeloBerlin opened this issue 8 years ago • 3 comments

It seems as if JFileChooserFixture.selectFile() is only able to select files but no directories. How can I choose a directory with a FileChooser?

Many thanks in advance, Angelo

angeloBerlin avatar Dec 17 '16 14:12 angeloBerlin

Without having a deeper look it should rely on the return value of fileChooser.getFileSelectionMode(). Which selection mode does your file chooser return?

croesch avatar May 16 '17 13:05 croesch

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();
				fileChooser
						.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				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();
	}
}
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;

/**
 * Test for <a
 * href="https://github.com/joel-costigliola/assertj-swing/issues/196"
 * >github.com - assertj-swing #196</a>
 */
public class GitHub_196_OpenAppdirectoryFileChooser_Test 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();

		try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		window.textBox().requireText(filePath);
	}
}

This might be an OSX issue again, as apps are directories...

angeloBerlin avatar May 16 '17 15:05 angeloBerlin

Runs fine under Linux, too. Created a directory /tmp/TextEdit.app and pointed the test to it. :/

croesch avatar May 16 '17 16:05 croesch