New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to select directories with JFileChooserFixture #196

Open
angeloBerlin opened this Issue Dec 17, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@angeloBerlin

angeloBerlin commented Dec 17, 2016

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

@croesch

This comment has been minimized.

Show comment
Hide comment
@croesch

croesch May 16, 2017

Collaborator

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

Collaborator

croesch commented May 16, 2017

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

@croesch croesch assigned croesch and angeloBerlin and unassigned croesch May 16, 2017

@angeloBerlin

This comment has been minimized.

Show comment
Hide comment
@angeloBerlin

angeloBerlin May 16, 2017

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 commented May 16, 2017

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...

@croesch

This comment has been minimized.

Show comment
Hide comment
@croesch

croesch May 16, 2017

Collaborator

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

Collaborator

croesch commented May 16, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment