1 19 20 package org.netbeans.spi.palette; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.IOException ; 25 import junit.framework.TestCase; 26 import org.netbeans.modules.palette.Category; 27 import org.netbeans.modules.palette.Item; 28 import org.netbeans.modules.palette.Model; 29 import org.netbeans.modules.palette.Settings; 30 import org.openide.nodes.Node; 31 import org.openide.util.Lookup; 32 33 37 public class PaletteControllerTest extends TestCase { 38 39 private PaletteController controller; 40 private Node rootNode; 41 private DummyActions actions; 42 private Model model; 43 private Settings settings; 44 45 public PaletteControllerTest(String testName) { 46 super(testName); 47 } 48 49 protected void setUp() throws Exception { 50 actions = new DummyActions(); 51 rootNode = DummyPalette.createPaletteRoot(); 52 controller = PaletteFactory.createPalette( rootNode, actions ); 53 model = controller.getModel(); 54 settings = controller.getSettings(); 55 } 56 57 60 public void testAddPropertyChangeListener() { 61 model.clearSelection(); 63 64 MyPropertyChangeListener myListener = new MyPropertyChangeListener(); 65 controller.addPropertyChangeListener( myListener ); 66 67 Category cat = model.getCategories()[0]; 68 Item item = cat.getItems()[0]; 69 70 model.setSelectedItem( cat.getLookup(), item.getLookup() ); 71 72 assertEquals( PaletteController.PROP_SELECTED_ITEM, myListener.getPropertyName() ); 73 assertEquals( item.getLookup(), myListener.getValue() ); 74 75 myListener.clear(); 76 model.clearSelection(); 77 78 assertEquals( PaletteController.PROP_SELECTED_ITEM, myListener.getPropertyName() ); 79 assertEquals( Lookup.EMPTY, myListener.getValue() ); 80 } 81 82 85 public void testRemovePropertyChangeListener() { 86 model.clearSelection(); 88 89 MyPropertyChangeListener myListener = new MyPropertyChangeListener(); 90 controller.addPropertyChangeListener( myListener ); 91 92 Category cat = model.getCategories()[0]; 93 Item item = cat.getItems()[0]; 94 95 model.setSelectedItem( cat.getLookup(), item.getLookup() ); 96 97 assertEquals( PaletteController.PROP_SELECTED_ITEM, myListener.getPropertyName() ); 98 assertEquals( item.getLookup(), myListener.getValue() ); 99 100 controller.removePropertyChangeListener( myListener ); 101 myListener.clear(); 102 model.clearSelection(); 103 104 assertEquals( null, myListener.getPropertyName() ); 105 assertEquals( null, myListener.getValue() ); 106 } 107 108 111 public void testGetSelectedItem() { 112 model.clearSelection(); 114 115 assertEquals( Lookup.EMPTY, controller.getSelectedItem() ); 116 117 Category cat = model.getCategories()[0]; 118 Item item = cat.getItems()[0]; 119 model.setSelectedItem( cat.getLookup(), item.getLookup() ); 120 121 assertEquals( item.getLookup(), controller.getSelectedItem() ); 122 123 cat = model.getCategories()[3]; 124 item = cat.getItems()[5]; 125 model.setSelectedItem( cat.getLookup(), item.getLookup() ); 126 127 assertEquals( item.getLookup(), controller.getSelectedItem() ); 128 129 model.clearSelection(); 130 131 assertEquals( Lookup.EMPTY, controller.getSelectedItem() ); 132 } 133 134 137 public void testGetSelectedCategory() { 138 model.clearSelection(); 140 141 assertEquals( Lookup.EMPTY, controller.getSelectedItem() ); 142 143 Category cat = model.getCategories()[0]; 144 Item item = cat.getItems()[0]; 145 model.setSelectedItem( cat.getLookup(), item.getLookup() ); 146 147 assertEquals( cat.getLookup(), controller.getSelectedCategory() ); 148 149 cat = model.getCategories()[0]; 150 item = cat.getItems()[5]; 151 model.setSelectedItem( cat.getLookup(), item.getLookup() ); 152 153 assertEquals( cat.getLookup(), controller.getSelectedCategory() ); 154 155 cat = model.getCategories()[4]; 156 item = cat.getItems()[6]; 157 model.setSelectedItem( cat.getLookup(), item.getLookup() ); 158 159 assertEquals( cat.getLookup(), controller.getSelectedCategory() ); 160 161 model.clearSelection(); 162 163 assertEquals( Lookup.EMPTY, controller.getSelectedCategory() ); 164 } 165 166 169 public void testClearSelection() { 170 model.clearSelection(); 172 173 assertEquals( Lookup.EMPTY, controller.getSelectedItem() ); 174 175 Category cat = model.getCategories()[0]; 176 Item item = cat.getItems()[0]; 177 model.setSelectedItem( cat.getLookup(), item.getLookup() ); 178 179 assertEquals( cat.getLookup(), controller.getSelectedCategory() ); 180 181 controller.clearSelection(); 182 assertEquals( Lookup.EMPTY, controller.getSelectedCategory() ); 183 184 controller.clearSelection(); 185 assertEquals( Lookup.EMPTY, controller.getSelectedCategory() ); 186 } 187 188 191 public void testResetPalette() { 192 } 201 202 205 public void testRefresh() throws IOException { 206 MyPaletteFilter filter = new MyPaletteFilter( false ); 207 208 PaletteController myController = PaletteFactory.createPalette( DummyPalette.createPaletteRoot(), new DummyActions(), filter, null ); 209 210 Model myModel = myController.getModel(); 211 212 213 Category[] categories = myModel.getCategories(); 214 assertEquals( 9, categories.length ); 215 for( int i=0; i<categories.length; i++ ) { 216 assertEquals( 9, categories[i].getItems().length ); 217 } 218 219 filter.setEnabled( true ); 220 myController.refresh(); 221 222 categories = myModel.getCategories(); 223 for( int i=0; i<categories.length; i++ ) { 224 assertTrue( filter.isValidName( categories[i].getName() ) ); 226 227 Item[] items = categories[i].getItems(); 228 for( int j=0; j<items.length; j++ ) { 229 assertTrue( filter.isValidName( items[j].getName() ) ); 231 } 232 } 233 234 filter.setEnabled( false ); 235 myController.refresh(); 236 237 categories = myModel.getCategories(); 238 assertEquals( 9, categories.length ); 239 for( int i=0; i<categories.length; i++ ) { 240 assertEquals( 9, categories[i].getItems().length ); 241 } 242 } 243 244 247 public void testShowCustomizer() { 248 ProxyModel myModel = new ProxyModel( model ); 249 controller.setModel( myModel ); 250 251 controller.showCustomizer(); 252 253 assertTrue( myModel.showCustomizerCalled ); 254 } 255 256 259 public void testGetRoot() { 260 assertEquals( rootNode.getName(), controller.getRoot().lookup( Node.class ).getName() ); 261 } 262 263 private static class MyPropertyChangeListener implements PropertyChangeListener { 264 private String propertyName; 265 private Object newValue; 266 267 public void propertyChange( PropertyChangeEvent evt ) { 268 propertyName = evt.getPropertyName(); 269 newValue = evt.getNewValue(); 270 } 271 272 public String getPropertyName() { 273 return propertyName; 274 } 275 276 public Object getValue() { 277 return newValue; 278 } 279 280 public void clear() { 281 propertyName = null; 282 newValue = null; 283 } 284 } 285 286 private static class MyPaletteFilter extends PaletteFilter { 287 288 private boolean isEnabled; 289 290 public MyPaletteFilter( boolean enabled ) { 291 this.isEnabled = enabled; 292 } 293 294 public boolean isValidItem(Lookup lkp) { 295 if( !isEnabled ) 296 return true; 297 298 Node node = (Node)lkp.lookup( Node.class ); 299 300 return nodeNameEndsWith1or2or3( node ); 301 } 302 303 public boolean isValidCategory(Lookup lkp) { 304 if( !isEnabled ) 305 return true; 306 307 Node node = (Node)lkp.lookup( Node.class ); 308 309 return nodeNameEndsWith1or2or3( node ); 310 } 311 312 private boolean nodeNameEndsWith1or2or3( Node node ) { 313 if( null == node ) 314 return false; 315 316 return isValidName( node.getName() ); 317 } 318 319 public boolean isValidName( String name ) { 320 if( null == name ) 321 return false; 322 323 return name.endsWith( "1" ) || name.endsWith( "2" ) || name.endsWith( "3" ); 324 } 325 326 public void setEnabled( boolean enable ) { 327 this.isEnabled = enable; 328 } 329 } 330 } 331 | Popular Tags |