测试将文件拖放到应用程序上
我正在寻找一种方法,从单元测试中将文件/多个文件拖放到我的应用程序上。例如,在Windows资源管理器中选择一些文件,将它们拖放到我的应用程序上。
我能够测试应用程序中两个组件之间的拖放行为(见下文 - 如果您知道更好的方法,请随时指出),但是当数据必须来自我的应用程序外部时,我不知道如何做同样的事情。
当我手动执行拖放操作时,我想过使用调试器检查“文件”,但是必须有更好的方法可以对完整的进行硬编码。Transferable
Transferable
组件之间的拖放测试示例
import org.junit.Test;
import javax.swing.Action;
import javax.swing.JTextField;
import javax.swing.TransferHandler;
import java.awt.event.ActionEvent;
import static org.junit.Assert.assertEquals;
public class DragAndDropTest {
@Test
public void dragAndDropBetweenTwoTextFields() {
JTextField firstField = new JTextField();
JTextField secondField = new JTextField();
String testText = "Test text";
firstField.setText( testText );
firstField.selectAll();
Action copyAction = TransferHandler.getCopyAction();
copyAction.actionPerformed( new ActionEvent( firstField, ActionEvent.ACTION_PERFORMED, "Copy" ) );
Action pasteAction = TransferHandler.getPasteAction();
pasteAction.actionPerformed( new ActionEvent( secondField, ActionEvent.ACTION_PERFORMED, "Paste" ) );
assertEquals( "Text is not copied", testText, secondField.getText() );
}
}
编辑
根据此问题下的评论,我已更新了我的代码片段,通过提供硬编码来“伪造”拖放。该代码还包含一个小的主程序,它只是创建一个空帧,您可以在其中放置文件。然后,该路径将打印在控制台上。Transferable
在我的电脑上,拖放文件不使用 URI 风格。以前的经验(参见此问题)已经告诉我,从 Java 应用程序外部拖动某些内容时,组件接收的组件可能会以细微的方式有所不同。javaFileListFlavor
Transferable
因此,要完全清楚:我想测试从接收到的信息中提取信息的代码部分。“信息处理”代码(例如,当应用程序收到文件时会发生什么)可以很容易地进行测试,而无需打扰D&D。我只需要确保我从中提取正确的信息,并且用硬编码对此进行测试是愚蠢的。Transferable
Transferable
Transferable
import org.junit.Test;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.TransferHandler;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class DragAndDropTest {
private static DataFlavor URI_LIST_FLAVOR = null;
static {
try {
URI_LIST_FLAVOR = new DataFlavor( "text/uri-list;class=java.lang.String" );
}
catch ( ClassNotFoundException ignore ) {
}
}
@Test
public void testFileDragAndDrop() throws IOException, UnsupportedFlavorException {
JComponent testComponent = new JPanel();
TestingTransferHandler transferHandler = new TestingTransferHandler();
testComponent.setTransferHandler( transferHandler );
Clipboard clipBoard = new JLabel( ).getToolkit().getSystemClipboard();
Transferable transferable = new Transferable() {
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{URI_LIST_FLAVOR};
}
@Override
public boolean isDataFlavorSupported( DataFlavor flavor ) {
return flavor == URI_LIST_FLAVOR;
}
@Override
public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException, IOException {
if ( flavor == URI_LIST_FLAVOR ) {
return new String( "file:///home/robins/Desktop/swingx-1.0-javadoc.jar" );
}
throw new UnsupportedFlavorException( flavor );
}
};
clipBoard.setContents( transferable, null );
Action pasteAction = TransferHandler.getPasteAction();
pasteAction.actionPerformed( new ActionEvent( testComponent, ActionEvent.ACTION_PERFORMED, "Paste" ) );
assertEquals( transferable.getTransferData( URI_LIST_FLAVOR ), transferHandler.transferable.getTransferData( URI_LIST_FLAVOR ) );
}
private static class TestingTransferHandler extends TransferHandler{
public Transferable transferable;
@Override
public boolean canImport( TransferSupport support ) {
return true;
}
@Override
public boolean importData( TransferSupport support ) {
transferable = support.getTransferable();
try{
if ( transferable.isDataFlavorSupported( DataFlavor.javaFileListFlavor ) ) {
System.out.println("File list flavor");
List<File> fileList = ( List<File> ) transferable.getTransferData( DataFlavor.javaFileListFlavor );
System.out.println( "fileList = " + fileList );
}
if ( transferable.isDataFlavorSupported( URI_LIST_FLAVOR )){
System.out.println("URI list flavor");
String uriList = ( String ) transferable.getTransferData( URI_LIST_FLAVOR );
System.out.println( "uriList = " + uriList );
}
return true;
} catch ( UnsupportedFlavorException e ) {
return false;
} catch ( IOException e ) {
return false;
}
}
}
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame( "TestFrame" );
JPanel contentPane = new JPanel( new BorderLayout( ) );
contentPane.setTransferHandler( new TestingTransferHandler() );
frame.setContentPane( contentPane );
frame.setSize( 200,200 );
frame.setVisible( true );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} );
}
}