1 19 20 package org.netbeans.spi.palette; 21 22 import java.awt.event.ActionEvent ; 23 import java.io.IOException ; 24 import javax.swing.AbstractAction ; 25 import javax.swing.Action ; 26 import org.netbeans.modules.palette.Category; 27 import org.netbeans.modules.palette.Item; 28 import org.netbeans.modules.palette.Model; 29 import org.openide.filesystems.FileObject; 30 import org.openide.loaders.DataFolder; 31 import org.openide.util.Lookup; 32 33 37 public class ModelTest extends AbstractPaletteTestHid { 38 39 public ModelTest(String testName) { 40 super(testName); 41 } 42 43 46 public void testGetName() throws IOException { 47 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 48 Model model = pc.getModel(); 49 assertEquals( getRootFolderName(), model.getName() ); 50 } 51 52 55 public void testGetCategories() throws IOException { 56 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 57 Model model = pc.getModel(); 58 Category[] categories = model.getCategories(); 59 assertEquals( categoryNames.length, categories.length ); 60 for( int i=0; i<categories.length; i++ ) { 61 assertEquals( categoryNames[i], categories[i].getName() ); 62 } 63 } 64 65 68 public void testGetActions() throws IOException { 69 PaletteActions actions = new DummyActions(); 70 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 71 Model model = pc.getModel(); 72 Action [] modelActions = model.getActions(); 73 Action [] rootActions = actions.getCustomPaletteActions(); 74 for( int i=0; i<rootActions.length; i++ ) { 75 if( null == rootActions[i] ) 76 continue; 77 boolean found = false; 78 for( int j=0; j<modelActions.length; j++ ) { 79 if( null == modelActions[j] ) 80 continue; 81 if( modelActions[j].equals( rootActions[i] ) ) { 82 found = true; 83 break; 84 } 85 } 86 assertTrue( "Action " + rootActions[i].getValue( Action.NAME ) + " not found in palette actions.", found ); 87 } 88 } 89 90 93 public void testSelectedItemAndCategory() throws IOException { 94 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 95 Model model = pc.getModel(); 96 97 assertNull( "No item is selected by default", model.getSelectedItem() ); 98 assertNull( "No category is selected by default", model.getSelectedCategory() ); 99 100 Category[] categories = model.getCategories(); 101 Category catToSelect = categories[3]; 102 Item itemToSelect = catToSelect.getItems()[4]; 103 104 model.setSelectedItem( catToSelect.getLookup(), itemToSelect.getLookup() ); 105 106 assertEquals( catToSelect, model.getSelectedCategory() ); 107 assertEquals( itemToSelect, model.getSelectedItem() ); 108 109 model.clearSelection(); 110 111 assertNull( "No item is selected after clearSelection()", model.getSelectedItem() ); 112 assertNull( "No category is selected after clearSelection()", model.getSelectedCategory() ); 113 } 114 115 118 public void testGetRoot() throws IOException { 119 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 120 Model model = pc.getModel(); 121 Lookup rootLookup = model.getRoot(); 122 123 DataFolder df = (DataFolder)rootLookup.lookup( DataFolder.class ); 124 assertNotNull( df ); 125 126 FileObject fo = df.getPrimaryFile(); 127 assertNotNull( fo ); 128 129 assertEquals( getRootFolderName(), fo.getName() ); 130 } 131 132 135 public void testMoveCategoryBefore() throws IOException { 136 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 137 Model model = pc.getModel(); 138 139 Category[] categories = model.getCategories(); 140 Category source = categories[0]; 141 Category target = categories[categories.length-1]; 142 143 model.moveCategory( source, target, true ); 144 145 pc.refresh(); 146 147 Category[] movedCategories = model.getCategories(); 148 assertEquals( categories.length, movedCategories.length ); 149 assertEquals( target.getName(), movedCategories[movedCategories.length-1].getName() ); 150 assertEquals( source.getName(), movedCategories[movedCategories.length-1-1].getName() ); 151 } 152 153 156 public void testMoveCategoryAfter() throws IOException { 157 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 158 Model model = pc.getModel(); 159 160 Category[] categories = model.getCategories(); 161 Category source = categories[0]; 162 Category target = categories[categories.length-1]; 163 164 model.moveCategory( source, target, false ); 165 166 pc.refresh(); 167 168 Category[] movedCategories = model.getCategories(); 169 assertEquals( categories.length, movedCategories.length ); 170 assertEquals( target.getName(), movedCategories[movedCategories.length-1-1].getName() ); 171 assertEquals( source.getName(), movedCategories[movedCategories.length-1].getName() ); 172 } 173 174 177 public void testMoveCategorySamePosition() throws IOException { 178 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 179 Model model = pc.getModel(); 180 181 Category[] categories = model.getCategories(); 182 Category source = categories[0]; 183 Category target = categories[0]; 184 185 model.moveCategory( source, target, false ); 186 187 Category[] movedCategories = model.getCategories(); 188 assertEquals( categories.length, movedCategories.length ); 189 assertEquals( target, movedCategories[0] ); 190 assertEquals( source, movedCategories[0] ); 191 } 192 193 196 public void testCustomRefresh() throws IOException { 197 CustomRefresh refresh = new CustomRefresh(); 198 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new CustomRefreshActions( refresh ) ); 199 pc.refresh(); 200 assertTrue( refresh.actionInvoked ); 201 } 202 203 private static class CustomRefreshActions extends DummyActions { 204 private Action refresh; 205 public CustomRefreshActions( Action refresh ) { 206 this.refresh = refresh; 207 } 208 209 @Override 210 public Action getRefreshAction() { 211 return refresh; 212 } 213 } 214 215 private static class CustomRefresh extends AbstractAction { 216 private boolean actionInvoked = false; 217 218 public CustomRefresh() { 219 } 220 221 public void actionPerformed(ActionEvent arg0) { 222 actionInvoked = true; 223 } 224 } 225 } 226 | Popular Tags |