1 19 20 package org.netbeans.spi.palette; 21 import java.awt.datatransfer.DataFlavor ; 22 import java.awt.dnd.DnDConstants ; 23 import java.io.IOException ; 24 import junit.framework.*; 25 import org.netbeans.modules.palette.Category; 26 import org.netbeans.modules.palette.Item; 27 import org.netbeans.modules.palette.Model; 28 import org.openide.nodes.AbstractNode; 29 import org.openide.nodes.Children; 30 import org.openide.nodes.Index; 31 import org.openide.util.datatransfer.ExTransferable; 32 33 37 public class DragAndDropHandlerTest extends AbstractPaletteTestHid { 38 39 public DragAndDropHandlerTest(String testName) { 40 super(testName); 41 } 42 43 public static Test suite() { 44 TestSuite suite = new TestSuite(DragAndDropHandlerTest.class); 45 46 return suite; 47 } 48 49 public void testCustomize() throws Exception { 50 PaletteActions actions = new DummyActions(); 51 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 52 Model model = pc.getModel(); 53 54 Item item = model.getCategories()[0].getItems()[0]; 55 56 DragAndDropHandler handler = DragAndDropHandler.getDefault(); 57 58 ExTransferable t = ExTransferable.create( item.cut() ); 59 DataFlavor [] flavorsBefore = t.getTransferDataFlavors(); 60 61 handler.customize( t, item.getLookup() ); 62 63 DataFlavor [] flavorsAfter = t.getTransferDataFlavors(); 64 assertEquals( "Default implementation does nothing", flavorsBefore.length, flavorsAfter.length ); 65 for( int i=0; i<flavorsBefore.length; i++ ) { 66 assertEquals( "Default implementation does nothing", flavorsBefore[i], flavorsAfter[i] ); 67 } 68 } 69 70 public void testCanDrop() throws Exception { 71 PaletteActions actions = new DummyActions(); 72 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 73 Model model = pc.getModel(); 74 75 Category cat = model.getCategories()[0]; 76 77 DragAndDropHandler handler = DragAndDropHandler.getDefault(); 78 79 DataFlavor [] flavors = new DataFlavor [] { PaletteController.ITEM_DATA_FLAVOR }; 80 assertTrue( handler.canDrop( cat.getLookup(), flavors, DnDConstants.ACTION_COPY_OR_MOVE ) ); 81 82 flavors = new DataFlavor [] { new DataFlavor ( "text/xml" ) }; 83 assertFalse( handler.canDrop( cat.getLookup(), flavors, DnDConstants.ACTION_COPY_OR_MOVE ) ); 84 } 85 86 public void testDoDropReorderItemWithinCategory() throws Exception { 87 PaletteActions actions = new DummyActions(); 88 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 89 Model model = pc.getModel(); 90 91 Category[] categories = model.getCategories(); 92 93 Category cat = categories[0]; 94 Item[] itemsBeforeMove = cat.getItems(); 95 96 Item source = itemsBeforeMove[0]; 97 Item target = itemsBeforeMove[itemsBeforeMove.length-1]; 98 99 DragAndDropHandler handler = DragAndDropHandler.getDefault(); 100 101 assertTrue( handler.doDrop( cat.getLookup(), source.cut(), DnDConstants.ACTION_COPY_OR_MOVE, itemsBeforeMove.length ) ); 102 103 pc.refresh(); 104 105 Item[] itemsAfterMove = pc.getModel().getCategories()[0].getItems(); 106 107 assertEquals( itemsBeforeMove.length, itemsAfterMove.length ); 108 assertEquals( source.getName(), itemsAfterMove[itemsAfterMove.length-1].getName() ); 109 assertEquals( itemsBeforeMove[1].getName(), itemsAfterMove[0].getName() ); 110 assertEquals( target.getName(), itemsAfterMove[itemsAfterMove.length-1-1].getName() ); 111 } 112 113 public void testDoDropItemToOtherCategory() throws Exception { 114 PaletteActions actions = new DummyActions(); 115 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 116 Model model = pc.getModel(); 117 118 Category[] categories = model.getCategories(); 119 120 Category srcCat = categories[0]; 121 Item[] srcItemsBefore = srcCat.getItems(); 122 Item dropItem = srcItemsBefore[0]; 123 124 Category tgtCat = categories[1]; 125 Item[] tgtItemsBefore = tgtCat.getItems(); 126 Item target = tgtItemsBefore[5]; 127 128 DragAndDropHandler handler = DragAndDropHandler.getDefault(); 129 130 assertTrue( handler.doDrop( tgtCat.getLookup(), dropItem.cut(), DnDConstants.ACTION_COPY_OR_MOVE, 5 ) ); 131 132 pc.refresh(); 134 categories = model.getCategories(); 135 srcCat = categories[0]; 136 tgtCat = categories[1]; 137 138 Item[] srcItemsAfter = srcCat.getItems(); 139 Item[] tgtItemsAfter = tgtCat.getItems(); 140 141 assertEquals( srcItemsBefore.length, srcItemsAfter.length+1 ); 142 for( int i=0; i<srcItemsAfter.length; i++ ) { 143 assertEquals( srcItemsBefore[i+1].getName(), srcItemsAfter[i].getName() ); 144 } 145 146 assertEquals( tgtItemsBefore.length, tgtItemsAfter.length-1 ); 147 assertEquals( target.getName(), tgtItemsAfter[5+1].getName() ); 148 assertEquals( dropItem.getName(), tgtItemsAfter[5].getName() ); 149 } 150 151 public void testCanReorderCategories() throws Exception { 152 PaletteActions actions = new DummyActions(); 153 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 154 Model model = pc.getModel(); 155 156 DragAndDropHandler handler = DragAndDropHandler.getDefault(); 157 158 assertTrue( handler.canReorderCategories( model.getRoot() ) ); 159 160 assertFalse( handler.canReorderCategories( new NoIndexCookieNode().getLookup() ) ); 161 } 162 163 public void testMoveCategory() throws Exception { 164 PaletteActions actions = new DummyActions(); 165 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 166 Model model = pc.getModel(); 167 168 Category[] catBefore = model.getCategories(); 169 170 Category srcCat = catBefore[0]; 171 Category tgtCat = catBefore[5]; 172 173 DragAndDropHandler handler = DragAndDropHandler.getDefault(); 174 175 assertTrue( handler.moveCategory( srcCat.getLookup(), 5 ) ); 176 177 pc.refresh(); 178 179 Category[] catAfter = model.getCategories(); 180 181 assertEquals( catBefore.length, catAfter.length ); 182 assertEquals( srcCat.getName(), catAfter[4].getName() ); 183 assertEquals( catBefore[1].getName(), catAfter[0].getName() ); 184 assertEquals( tgtCat.getName(), catAfter[5].getName() ); 185 } 186 187 private static class NoIndexCookieNode extends AbstractNode { 188 public NoIndexCookieNode() { 189 super( Children.LEAF ); 190 } 191 192 public org.openide.nodes.Node.Cookie getCookie(Class type) { 193 if( Index.class.equals( type ) ) 194 return null; 195 return super.getCookie(type); 196 } 197 } 198 } 199 | Popular Tags |