/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Anton Avtamonov
* @version $Revision$
*/
package javax.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;
import javax.swing.filechooser.FileView;
import javax.swing.plaf.FileChooserUI;
import javax.swing.plaf.basic.BasicFileChooserUI;
public class JFileChooserTest extends SwingTestCase {
private JFileChooser chooser;
public JFileChooserTest(final String name) {
super(name);
setIgnoreNotImplemented(true);
}
@Override
protected void setUp() throws Exception {
super.setUp();
timeoutDelay = 5 * DEFAULT_TIMEOUT_DELAY;
chooser = new JFileChooser();
propertyChangeController = new PropertyChangeController();
chooser.addPropertyChangeListener(propertyChangeController);
}
@Override
protected void tearDown() throws Exception {
chooser = null;
super.tearDown();
}
public void testJFileChooser() throws Exception {
assertNotNull(chooser.getCurrentDirectory());
assertEquals(FileSystemView.getFileSystemView().getDefaultDirectory(), chooser
.getCurrentDirectory());
File testDir = new File("testDir");
testDir.deleteOnExit();
testDir.mkdir();
chooser = new JFileChooser(testDir);
assertEqualsIgnoreCase(testDir.getAbsolutePath(), chooser.getCurrentDirectory()
.getAbsolutePath());
chooser = new JFileChooser(testDir.getAbsolutePath());
assertEqualsIgnoreCase(testDir.getAbsolutePath(), chooser.getCurrentDirectory()
.getAbsolutePath());
testDir.delete();
testDir = new File("anotherTestDir");
chooser = new JFileChooser(testDir);
assertEquals(FileSystemView.getFileSystemView().getDefaultDirectory(), chooser
.getCurrentDirectory());
chooser = new JFileChooser(testDir.getAbsolutePath());
assertEquals(FileSystemView.getFileSystemView().getDefaultDirectory(), chooser
.getCurrentDirectory());
chooser = new JFileChooser((String) null);
assertEquals(FileSystemView.getFileSystemView().getDefaultDirectory(), chooser
.getCurrentDirectory());
final File defaultDir = new File("testDir");
defaultDir.deleteOnExit();
defaultDir.mkdir();
try {
FileSystemView view = new FileSystemView() {
@Override
public File createNewFolder(final File containingDir) throws IOException {
return containingDir;
}
@Override
public File getDefaultDirectory() {
return defaultDir;
}
};
chooser = new JFileChooser(view);
assertEquals(view, chooser.getFileSystemView());
assertEqualsIgnoreCase(defaultDir.getAbsolutePath(), chooser.getCurrentDirectory()
.getAbsolutePath());
} finally {
defaultDir.delete();
}
}
public void testJFileChooser_FSV() throws Exception {
File testDir = new File("testDir");
testDir.deleteOnExit();
testDir.mkdir();
chooser = new JFileChooser();
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser(testDir);
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser((File) null);
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser(testDir.getAbsolutePath());
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser((String) null);
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser(FileSystemView.getFileSystemView());
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser((FileSystemView) null);
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser(testDir, FileSystemView.getFileSystemView());
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser(testDir, null);
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser((File) null, FileSystemView.getFileSystemView());
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser((File) null, null);
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser(testDir.getAbsolutePath(), FileSystemView.getFileSystemView());
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser(testDir.getAbsolutePath(), null);
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser((String) null, FileSystemView.getFileSystemView());
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser = new JFileChooser((String) null, null);
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
testDir.delete();
}
//TODO
public void testSetup() throws Exception {
chooser = new JFileChooser();
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser.setup(FileSystemView.getFileSystemView());
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser.setup(null);
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
final File defaultDir = new File("testDir");
defaultDir.deleteOnExit();
try {
defaultDir.mkdir();
FileSystemView view = new FileSystemView() {
@Override
public File createNewFolder(final File containingDir) throws IOException {
return containingDir;
}
@Override
public File getDefaultDirectory() {
return defaultDir;
}
};
chooser.setup(view);
assertEquals(view, chooser.getFileSystemView());
assertEquals(FileSystemView.getFileSystemView().getDefaultDirectory(), chooser
.getCurrentDirectory());
chooser.setup(FileSystemView.getFileSystemView());
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
chooser.setup(view);
assertEquals(view, chooser.getFileSystemView());
chooser.setup(null);
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
} catch (Throwable t) {
defaultDir.delete();
fail("Detected problem " + t.getMessage());
}
}
public void testGetSetDragEnabled() throws Exception {
assertFalse(chooser.getDragEnabled());
chooser.setDragEnabled(true);
assertTrue(chooser.getDragEnabled());
assertFalse(propertyChangeController.isChanged());
}
public void testGetSetSelectedFile() throws Exception {
assertNull(chooser.getSelectedFile());
File selectedFile = new File("testFile");
chooser.setSelectedFile(selectedFile);
assertEquals(selectedFile, chooser.getSelectedFile());
assertEquals(0, chooser.getSelectedFiles().length);
assertTrue(propertyChangeController.isChanged("SelectedFileChangedProperty"));
selectedFile = new File("testDir");
selectedFile.deleteOnExit();
selectedFile.mkdir();
chooser.setSelectedFile(selectedFile);
assertEquals(selectedFile, chooser.getSelectedFile());
}
public void testGetSetSelectedFiles() throws Exception {
assertEquals(0, chooser.getSelectedFiles().length);
chooser.setSelectedFile(new File("c"));
assertEquals(0, chooser.getSelectedFiles().length);
propertyChangeController.reset();
File selectedFile = new File("a");
File[] files = new File[] { selectedFile, new File("b") };
chooser.setSelectedFiles(files);
assertNotSame(files, chooser.getSelectedFiles());
assertEquals(files.length, chooser.getSelectedFiles().length);
assertEquals(selectedFile, chooser.getSelectedFile());
assertTrue(propertyChangeController.isChanged("SelectedFilesChangedProperty"));
assertTrue(propertyChangeController.isChanged("SelectedFileChangedProperty"));
propertyChangeController.reset();
files = new File[] { new File("b"), selectedFile };
chooser.setSelectedFiles(files);
assertNotSame(files, chooser.getSelectedFiles());
assertEquals(files.length, chooser.getSelectedFiles().length);
assertEquals(new File("b"), chooser.getSelectedFile());
assertTrue(propertyChangeController.isChanged("SelectedFilesChangedProperty"));
assertTrue(propertyChangeController.isChanged("SelectedFileChangedProperty"));
propertyChangeController.reset();
chooser.setSelectedFiles(new File[] { new File("b"), selectedFile });
assertTrue(propertyChangeController.isChanged("SelectedFilesChangedProperty"));
assertFalse(propertyChangeController.isChanged("SelectedFileChangedProperty"));
}
public void testGetSetCurrentDirectory() throws Exception {
assertEquals(FileSystemView.getFileSystemView().getDefaultDirectory(), chooser
.getCurrentDirectory());
File dir = new File("testDir");
dir.deleteOnExit();
dir.mkdir();
File innerDir = new File(dir, "innerDir");
innerDir.deleteOnExit();
innerDir.mkdir();
File file = new File(innerDir, "innerFile");
file.deleteOnExit();
file.createNewFile();
try {
chooser.setCurrentDirectory(dir);
assertEqualsIgnoreCase(dir.getAbsolutePath(), chooser.getCurrentDirectory()
.getAbsolutePath());
chooser.setCurrentDirectory(file);
assertEqualsIgnoreCase(innerDir.getAbsolutePath(), chooser.getCurrentDirectory()
.getAbsolutePath());
} finally {
file.delete();
innerDir.delete();
dir.delete();
}
}
public void testChangeToParentDirectory() throws Exception {
chooser.changeToParentDirectory();
assertEquals(FileSystemView.getFileSystemView().getDefaultDirectory().getParentFile(),
chooser.getCurrentDirectory());
}
public void testGetSetControlButtonsAreShown() throws Exception {
assertTrue(chooser.getControlButtonsAreShown());
chooser.setControlButtonsAreShown(false);
assertFalse(chooser.getControlButtonsAreShown());
assertTrue(propertyChangeController.isChanged("ControlButtonsAreShownChangedProperty"));
}
public void testgetSetDialogType() throws Exception {
assertEquals(JFileChooser.OPEN_DIALOG, chooser.getDialogType());
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
assertEquals(JFileChooser.SAVE_DIALOG, chooser.getDialogType());
assertTrue(propertyChangeController.isChanged("DialogTypeChangedProperty"));
chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
assertEquals(JFileChooser.CUSTOM_DIALOG, chooser.getDialogType());
testExceptionalCase(new IllegalArgumentCase() {
@Override
public void exceptionalAction() throws Exception {
chooser.setDialogType(10);
}
});
}
public void testGetSetDialogTitle() throws Exception {
assertNull(chooser.getDialogTitle());
assertEquals("Open", chooser.getUI().getDialogTitle(chooser));
chooser.setDialogTitle("title");
assertEquals("title", chooser.getDialogTitle());
assertEquals("title", chooser.getUI().getDialogTitle(chooser));
assertTrue(propertyChangeController.isChanged("DialogTitleChangedProperty"));
}
public void testGetSetApproveButtonToolTipText() throws Exception {
assertNull(chooser.getApproveButtonToolTipText());
chooser.setApproveButtonToolTipText("text");
assertEquals("text", chooser.getApproveButtonToolTipText());
assertTrue(propertyChangeController
.isChanged("ApproveButtonToolTipTextChangedProperty"));
chooser.setApproveButtonToolTipText(null);
assertNull(chooser.getApproveButtonToolTipText());
}
public void testGetSetApproveButtonText() throws Exception {
assertNull(chooser.getApproveButtonText());
assertEquals("Open", chooser.getUI().getApproveButtonText(chooser));
chooser.setApproveButtonText("text");
assertEquals(JFileChooser.OPEN_DIALOG, chooser.getDialogType());
assertEquals("text", chooser.getApproveButtonText());
assertEquals("text", chooser.getUI().getApproveButtonText(chooser));
assertTrue(propertyChangeController.isChanged("ApproveButtonTextChangedProperty"));
chooser.setApproveButtonText(null);
assertNull(chooser.getApproveButtonToolTipText());
assertEquals("Open", chooser.getUI().getApproveButtonText(chooser));
}
public void testGetSetApproveButtonMnemonic() throws Exception {
assertEquals(0, chooser.getApproveButtonMnemonic());
chooser.setApproveButtonMnemonic('c');
assertEquals(KeyEvent.VK_C, chooser.getApproveButtonMnemonic());
assertTrue(propertyChangeController.isChanged("ApproveButtonMnemonicChangedProperty"));
chooser.setApproveButtonMnemonic(KeyEvent.VK_X);
assertEquals(KeyEvent.VK_X, chooser.getApproveButtonMnemonic());
}
public void testGetAddRemoveResetChoosableFileFilters() throws Exception {
assertEquals(1, chooser.getChoosableFileFilters().length);
FileFilter fileFilter = new FileFilter() {
@Override
public boolean accept(final File file) {
return false;
}
@Override
public String getDescription() {
return "additional";
}
};
chooser.addChoosableFileFilter(fileFilter);
assertEquals(2, chooser.getChoosableFileFilters().length);
assertEquals("additional", chooser.getChoosableFileFilters()[1].getDescription());
assertTrue(propertyChangeController.isChanged("ChoosableFileFilterChangedProperty"));
assertTrue(propertyChangeController.isChanged("fileFilterChanged"));
propertyChangeController.reset();
chooser.removeChoosableFileFilter(fileFilter);
assertEquals(1, chooser.getChoosableFileFilters().length);
assertTrue(propertyChangeController.isChanged("ChoosableFileFilterChangedProperty"));
chooser.addChoosableFileFilter(fileFilter);
assertEquals(2, chooser.getChoosableFileFilters().length);
propertyChangeController.reset();
chooser.resetChoosableFileFilters();
assertEquals(1, chooser.getChoosableFileFilters().length);
assertTrue(propertyChangeController.isChanged("ChoosableFileFilterChangedProperty"));
chooser.resetChoosableFileFilters();
assertTrue(propertyChangeController.isChanged("ChoosableFileFilterChangedProperty"));
}
public void testGetAcceptAllFileFilter() throws Exception {
FileFilter acceptAllFilter = chooser.getAcceptAllFileFilter();
assertNotNull(acceptAllFilter);
assertEquals(acceptAllFilter, chooser.getUI().getAcceptAllFileFilter(chooser));
chooser.removeChoosableFileFilter(acceptAllFilter);
assertEquals(0, chooser.getChoosableFileFilters().length);
assertTrue(acceptAllFilter == chooser.getAcceptAllFileFilter());
assertEquals(acceptAllFilter, chooser.getUI().getAcceptAllFileFilter(chooser));
}
public void testIsSetAcceptAllFilterUsed() throws Exception {
assertTrue(chooser.isAcceptAllFileFilterUsed());
chooser.setAcceptAllFileFilterUsed(false);
assertFalse(chooser.isAcceptAllFileFilterUsed());
assertEquals(0, chooser.getChoosableFileFilters().length);
assertTrue(propertyChangeController.isChanged("acceptAllFileFilterUsedChanged"));
chooser.setAcceptAllFileFilterUsed(true);
assertTrue(chooser.isAcceptAllFileFilterUsed());
assertEquals(1, chooser.getChoosableFileFilters().length);
}
public void testGetSetAccessory() throws Exception {
assertNull(chooser.getAccessory());
JComponent accessory = new JButton();
chooser.setAccessory(accessory);
assertEquals(accessory, chooser.getAccessory());
assertTrue(propertyChangeController.isChanged("AccessoryChangedProperty"));
}
public void testGetSetFileSelectionMode_isSelectionEnabled() throws Exception {
assertEquals(JFileChooser.FILES_ONLY, chooser.getFileSelectionMode());
assertTrue(chooser.isFileSelectionEnabled());
assertFalse(chooser.isDirectorySelectionEnabled());
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
assertEquals(JFileChooser.DIRECTORIES_ONLY, chooser.getFileSelectionMode());
assertFalse(chooser.isFileSelectionEnabled());
assertTrue(chooser.isDirectorySelectionEnabled());
assertTrue(propertyChangeController.isChanged("fileSelectionChanged"));
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
assertEquals(JFileChooser.FILES_AND_DIRECTORIES, chooser.getFileSelectionMode());
assertTrue(chooser.isFileSelectionEnabled());
assertTrue(chooser.isDirectorySelectionEnabled());
testExceptionalCase(new IllegalArgumentCase() {
@Override
public void exceptionalAction() throws Exception {
chooser.setFileSelectionMode(10);
}
});
}
public void testIsSetMultiSelectionEnabled() throws Exception {
assertFalse(chooser.isMultiSelectionEnabled());
chooser.setMultiSelectionEnabled(true);
assertTrue(chooser.isMultiSelectionEnabled());
assertTrue(propertyChangeController.isChanged("MultiSelectionEnabledChangedProperty"));
}
public void testIsSetFileHidingEnabled() throws Exception {
assertTrue(chooser.isFileHidingEnabled());
chooser.setFileHidingEnabled(false);
assertFalse(chooser.isFileHidingEnabled());
assertTrue(propertyChangeController.isChanged("FileHidingChanged"));
}
public void testGetSetFileFilter() throws Exception {
assertEquals(chooser.getAcceptAllFileFilter(), chooser.getFileFilter());
FileFilter fileFilter = new FileFilter() {
@Override
public boolean accept(final File file) {
return false;
}
@Override
public String getDescription() {
return "description";
}
};
chooser.setFileFilter(fileFilter);
assertEquals(fileFilter, chooser.getFileFilter());
assertTrue(propertyChangeController.isChanged("fileFilterChanged"));
}
public void testGetSetFileView() throws Exception {
assertNull(chooser.getFileView());
assertNotNull(chooser.getUI().getFileView(chooser));
FileView view = new FileView() {
};
chooser.setFileView(view);
assertTrue(propertyChangeController.isChanged("fileViewChanged"));
assertEquals(view, chooser.getFileView());
assertNotSame(view, chooser.getUI().getFileView(chooser));
}
public void testGetName_Description_TypeDescription_Icon_Traversable() throws Exception {
File f = new File(".");
assertEquals(chooser.getUI().getFileView(chooser).getName(f), chooser.getName(f));
assertEquals(chooser.getUI().getFileView(chooser).getDescription(f), chooser
.getDescription(f));
assertEquals(chooser.getUI().getFileView(chooser).getTypeDescription(f), chooser
.getTypeDescription(f));
assertEquals(chooser.getUI().getFileView(chooser).getIcon(f), chooser.getIcon(f));
assertTrue(chooser.isTraversable(f));
final String description = "description";
final Icon icon = new ImageIcon();
final String name = "name";
final String typeDescription = "typeDescription";
FileView view = new FileView() {
@Override
public String getDescription(final File f) {
return description;
}
@Override
public Icon getIcon(final File f) {
return icon;
}
@Override
public String getName(final File f) {
return name;
}
@Override
public String getTypeDescription(final File f) {
return typeDescription;
}
@Override
public Boolean isTraversable(final File f) {
return Boolean.FALSE;
}
};
chooser.setFileView(view);
assertEquals(name, chooser.getName(f));
assertEquals(description, chooser.getDescription(f));
assertEquals(typeDescription, chooser.getTypeDescription(f));
assertEquals(icon, chooser.getIcon(f));
assertFalse(chooser.isTraversable(f));
}
public void testAccept() throws Exception {
File f = new File(".");
assertTrue(chooser.accept(f));
chooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(final File file) {
return false;
}
@Override
public String getDescription() {
return null;
}
});
assertFalse(chooser.accept(f));
chooser.setFileFilter(null);
assertTrue(chooser.accept(f));
}
public void testGetSetFileSystemView() throws Exception {
assertEquals(FileSystemView.getFileSystemView(), chooser.getFileSystemView());
FileSystemView fileSystemView = new FileSystemView() {
@Override
public File createNewFolder(final File containingDir) throws IOException {
return null;
}
};
chooser.setFileSystemView(fileSystemView);
assertEquals(fileSystemView, chooser.getFileSystemView());
assertTrue(propertyChangeController.isChanged("FileSystemViewChanged"));
}
public void testApproveSelection() throws Exception {
TestActionListener listener = new TestActionListener();
chooser.addActionListener(listener);
chooser.approveSelection();
assertNotNull(listener.getEvent());
assertEquals(JFileChooser.APPROVE_SELECTION, listener.getEvent().getActionCommand());
assertEquals(chooser, listener.getEvent().getSource());
}
public void testCancelSelection() throws Exception {
TestActionListener listener = new TestActionListener();
chooser.addActionListener(listener);
chooser.cancelSelection();
assertNotNull(listener.getEvent());
assertEquals(JFileChooser.CANCEL_SELECTION, listener.getEvent().getActionCommand());
assertEquals(chooser, listener.getEvent().getSource());
}
public void testGetAddRemoveFireActionListeners() throws Exception {
assertEquals(0, chooser.getActionListeners().length);
TestActionListener listener = new TestActionListener();
chooser.addActionListener(listener);
chooser.addActionListener(new TestActionListener());
chooser.fireActionPerformed("command");
assertEquals("command", listener.getEvent().getActionCommand());
assertEquals(2, chooser.getActionListeners().length);
chooser.removeActionListener(listener);
assertEquals(1, chooser.getActionListeners().length);
}
public void testGetUpdateUI() throws Exception {
FileChooserUI ui = chooser.getUI();
assertNotNull(ui);
FileChooserUI customUI = new BasicFileChooserUI(chooser);
chooser.setUI(customUI);
assertEquals(customUI, chooser.getUI());
assertNotSame(ui, chooser.getUI());
chooser.updateUI();
assertNotSame(customUI, chooser.getUI());
}
public void testGetUIClassID() throws Exception {
assertEquals("FileChooserUI", chooser.getUIClassID());
}
public void testgetAccessibleContext() throws Exception {
assertTrue(chooser.getAccessibleContext() instanceof JFileChooser.AccessibleJFileChooser);
}
public void testGetName() {
JFileChooser fc = new JFileChooser();
assertNull(fc.getName());
}
private class TestActionListener implements ActionListener {
private ActionEvent event;
public void actionPerformed(final ActionEvent e) {
event = e;
}
public ActionEvent getEvent() {
return event;
}
}
private static void assertEqualsIgnoreCase(final String expected, final String actual) {
assertTrue(expected.compareToIgnoreCase(actual) == 0);
}
}