1 19 20 package org.openide.explorer.view; 21 22 import java.awt.datatransfer.DataFlavor ; 23 import java.awt.datatransfer.StringSelection ; 24 import java.awt.datatransfer.Transferable ; 25 import java.io.IOException ; 26 import org.netbeans.junit.MockServices; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.nodes.AbstractNode; 29 import org.openide.nodes.Children; 30 import org.openide.nodes.NodeTransfer; 31 import org.openide.util.datatransfer.ExClipboard; 32 import org.openide.util.datatransfer.ExTransferable; 33 import org.openide.util.datatransfer.MultiTransferObject; 34 35 39 public class DragDropUtilitiesTest extends NbTestCase { 40 41 public DragDropUtilitiesTest(String testName) { 42 super(testName); 43 } 44 45 protected void setUp() throws Exception { 46 MockServices.setServices(new Class [] {MyClipboard.class}); 47 last = null; 48 } 49 50 public void testGetNodeTransferableForSingleNodeCopy() throws Exception { 51 N node = new N(); 52 53 Transferable t = DragDropUtilities.getNodeTransferable(node, NodeTransfer.DND_COPY); 54 55 assertEquals("One call to copy", 1, node.copy); 56 assertEquals("Also one call to drag which delegates to copy", 1, node.drag); 57 assertEquals("No call to cut", 0, node.cut); 58 assertNotNull("Call to convertor", last); 59 assertTrue("StringSelection got to ExClipboard convertor", last instanceof StringSelection ); 60 } 61 62 public void testGetNodeTransferableForSingleNodeCut() throws Exception { 63 N node = new N(); 64 65 Transferable t = DragDropUtilities.getNodeTransferable(node, NodeTransfer.DND_MOVE); 66 67 assertEquals("One call to cut", 1, node.cut); 68 assertEquals("No call to drag", 0, node.drag); 69 assertEquals("No call to copy", 0, node.copy); 70 assertNotNull("Call to convertor", last); 71 assertTrue("StringSelection got to ExClipboard convertor", last instanceof StringSelection ); 72 } 73 74 public void testMultiTransferableForCopy() throws Exception { 75 N node = new N(); 76 N n2 = new N(); 77 N[] arr = { node, n2 }; 78 79 Transferable t = DragDropUtilities.getNodeTransferable(arr, NodeTransfer.DND_COPY); 80 81 assertEquals("One call to copy", 1, node.copy); 82 assertEquals("One call to copy on n2", 1, n2.copy); 83 assertEquals("Also one call to drag which delegates to copy", 1, node.drag); 84 assertEquals("Also one call to drag which delegates to copy on n2", 1, n2.drag); 85 assertEquals("No call to cut", 0, node.cut); 86 assertEquals("No call to cut", 0, n2.cut); 87 88 assertNotNull("Call to convertor", last); 89 assertTrue("multi flavor supported", last.isDataFlavorSupported(ExTransferable.multiFlavor)); 90 Object obj = last.getTransferData(ExTransferable.multiFlavor); 91 if (!( obj instanceof MultiTransferObject)) { 92 fail("It should be MultiTransferObject: " + obj); 93 } 94 MultiTransferObject m = (MultiTransferObject)obj; 95 96 assertEquals("Two in multi", 2, m.getCount()); 97 assertTrue("Is string", m.getTransferData(0, DataFlavor.stringFlavor) instanceof String ); 98 assertTrue("Is string2", m.getTransferData(1, DataFlavor.stringFlavor) instanceof String ); 99 } 100 101 public void testMultiTransferableForCut() throws Exception { 102 N node = new N(); 103 N n2 = new N(); 104 N[] arr = { node, n2 }; 105 106 Transferable t = DragDropUtilities.getNodeTransferable(arr, NodeTransfer.DND_MOVE); 107 108 assertEquals("One call to cut ", 1, node.cut); 109 assertEquals("One call to cut on n2", 1, n2.cut); 110 assertEquals("No to drag", 0, node.drag); 111 assertEquals("No to drag on n2", 0, n2.drag); 112 assertEquals("No call to copy", 0, node.copy); 113 assertEquals("No call to copy on n2", 0, n2.copy); 114 115 assertNotNull("Call to convertor", last); 116 assertTrue("multi flavor supported", last.isDataFlavorSupported(ExTransferable.multiFlavor)); 117 Object obj = last.getTransferData(ExTransferable.multiFlavor); 118 if (!( obj instanceof MultiTransferObject)) { 119 fail("It should be MultiTransferObject: " + obj); 120 } 121 MultiTransferObject m = (MultiTransferObject)obj; 122 123 assertEquals("Two in multi", 2, m.getCount()); 124 assertTrue("Is string", m.getTransferData(0, DataFlavor.stringFlavor) instanceof String ); 125 assertTrue("Is string2", m.getTransferData(1, DataFlavor.stringFlavor) instanceof String ); 126 } 127 128 129 130 private static class N extends AbstractNode { 131 public int copy; 132 public int cut; 133 public int drag; 134 public Transferable ret = new StringSelection ("A text"); 135 136 public N() { 137 super(Children.LEAF); 138 } 139 140 public Transferable clipboardCut() throws IOException { 141 cut++; 142 return ret; 143 } 144 145 public Transferable clipboardCopy() throws IOException { 146 copy++; 147 return ret; 148 } 149 150 public Transferable drag() throws IOException { 151 drag++; 152 return super.drag(); 153 } 154 } 155 156 public static Transferable last; 157 158 public static final class MyClipboard extends ExClipboard { 159 160 public MyClipboard() { 161 super("Empty"); 162 } 163 164 public ExClipboard.Convertor[] getConvertors() { 165 return new ExClipboard.Convertor[] {new ExClipboard.Convertor() { 166 public Transferable convert(Transferable t) { 167 last = t; 168 return t; 169 } 170 }}; 171 } 172 } 173 } 174 | Popular Tags |