1 19 20 package org.netbeans.spi.looks; 21 22 import java.awt.image.BufferedImage ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import java.util.Collection ; 26 27 import org.openide.nodes.*; 28 29 import org.netbeans.junit.*; 30 31 import org.netbeans.spi.looks.*; 32 33 public class TestBaseEvents extends NbTestCase { 34 35 protected SampleLook sampleLook; 37 38 protected Node node; 40 41 protected SampleRepObject representedObject; 43 44 protected GoldenEvent.Listener testNodeListener; 46 47 protected GoldenEvent.Listener testPcl; 49 50 protected GoldenValue[] goldenValues; 52 53 55 public TestBaseEvents(java.lang.String testName) { 56 super(testName); 57 } 58 59 public static void main(java.lang.String [] args) { 60 junit.textui.TestRunner.run(suite()); 61 } 62 63 public static NbTest suite() { 64 NbTestSuite suite = new NbTestSuite(TestBaseEvents.class); 65 return suite; 66 } 67 68 protected void setUp() throws Exception { 69 super.setUp(); 70 } 71 72 protected void tearDown() throws Exception { 73 node = null; 74 representedObject = null; 75 76 super.tearDown(); 77 } 78 79 80 82 protected void setTarget( Node node, SampleRepObject representedObject ) { 83 this.node = node; 84 this.representedObject = representedObject; 85 86 testNodeListener = new GoldenEvent.Listener(); 87 node.addNodeListener( testNodeListener ); 88 testPcl = new GoldenEvent.Listener(); 89 node.addPropertyChangeListener( testPcl ); 90 91 } 92 93 protected void setGoldenValues( GoldenValue[] goldenValues ) { 94 this.goldenValues = goldenValues; 95 } 96 97 99 100 public void testFirePropertyChange() { 101 102 representedObject.setProperty( "MY_PROP", "something" ); 103 representedObject.setProperty( "MY_PROP", "something else" ); 104 105 GoldenEvent[] goldenEvents = new GoldenEvent[] { 106 new GoldenEvent( node, 107 "MY_PROP", 108 null, null ), 109 110 new GoldenEvent( node, 111 "MY_PROP", 112 null, null ) 113 }; 114 115 assertTrue( GoldenEvent.compare( testPcl.getEvents(), 116 goldenEvents, 117 null ) ); 118 119 assertTrue( "Unexpected events in NodeListsner: " + testNodeListener.getEvents().size(), 120 testNodeListener.getEvents().size() == 0 ); 121 } 122 123 124 public void testFireNameChange() { 125 126 String oldValue = (String )GoldenValue.get( ProxyLook.GET_NAME, goldenValues ); 127 128 representedObject.setValue( ProxyLook.GET_NAME, "New name" ); 129 130 GoldenEvent[] goldenEvents = new GoldenEvent[] { 131 new GoldenEvent( node, 132 Node.PROP_NAME, 133 null, null ), 134 }; 135 136 assertTrue( GoldenEvent.compare( testNodeListener.getEvents(), 137 goldenEvents, 138 null ) ); 139 } 140 141 public void testFireDisplayNameChange() { 142 String oldValue = (String )GoldenValue.get( ProxyLook.GET_DISPLAY_NAME, goldenValues ); 143 144 representedObject.setValue( ProxyLook.GET_DISPLAY_NAME, "New display name" ); 145 146 GoldenEvent[] goldenEvents = new GoldenEvent[] { 147 new GoldenEvent( node, 148 Node.PROP_DISPLAY_NAME, 149 null, null ), 150 }; 151 152 assertTrue( GoldenEvent.compare( testNodeListener.getEvents(), 153 goldenEvents, 154 null ) ); 155 } 156 157 public void testFireShortDescriptionChange() { 158 String oldValue = (String )GoldenValue.get( ProxyLook.GET_SHORT_DESCRIPTION, goldenValues ); 159 160 representedObject.setValue( ProxyLook.GET_SHORT_DESCRIPTION, "New short description" ); 161 162 GoldenEvent[] goldenEvents = new GoldenEvent[] { 163 new GoldenEvent( node, 164 Node.PROP_SHORT_DESCRIPTION, 165 null, null ), 166 }; 167 168 assertTrue( GoldenEvent.compare( testNodeListener.getEvents(), 169 goldenEvents, 170 null ) ); 171 } 172 173 174 public void testFireIconChange() { 175 representedObject.setValue( ProxyLook.GET_ICON, 176 new BufferedImage ( 16, 16, BufferedImage.TYPE_INT_RGB ) ); 177 178 GoldenEvent[] goldenEvents = new GoldenEvent[] { 179 new GoldenEvent( node, 180 Node.PROP_ICON, 181 null, null ), 182 }; 183 184 assertTrue( GoldenEvent.compare( testNodeListener.getEvents(), 185 goldenEvents, 186 null ) ); 187 188 } 189 190 public void testFireOpenedIconChange() { 191 representedObject.setValue( ProxyLook.GET_OPENED_ICON, 192 new BufferedImage ( 16, 16, BufferedImage.TYPE_INT_RGB ) ); 193 194 GoldenEvent[] goldenEvents = new GoldenEvent[] { 195 new GoldenEvent( node, 196 Node.PROP_OPENED_ICON, 197 null, null ), 198 }; 199 200 assertTrue( GoldenEvent.compare( testNodeListener.getEvents(), 201 goldenEvents, 202 null ) ); 203 } 204 205 public void testFirePropertySetsChange() { 206 Node.PropertySet[] oldValue = (Node.PropertySet[])GoldenValue.get( ProxyLook.GET_PROPERTY_SETS, goldenValues ); 207 Node.PropertySet[] newValue = new Node.PropertySet[] { 208 new Sheet.Set(), 209 new Sheet.Set() 210 }; 211 212 representedObject.setValue( ProxyLook.GET_PROPERTY_SETS, newValue ); 213 214 GoldenEvent[] goldenEvents = new GoldenEvent[] { 215 new GoldenEvent( node, 216 Node.PROP_PROPERTY_SETS, 217 null, null ), 218 }; 219 220 assertTrue( GoldenEvent.compare( testNodeListener.getEvents(), 221 goldenEvents, 222 null ) ); 223 224 } 225 226 227 public void testFireCookieChange() { 228 229 Collection oldItems = (Collection )GoldenValue.get( ProxyLook.GET_LOOKUP_ITEMS, goldenValues ); 230 231 node.getCookie( org.openide.cookies.CloseCookie.class ); 233 234 Collection items = new ArrayList ( oldItems ); 236 items.add( 237 new GoldenValue.TestLookupItem ( 238 new org.openide.cookies.ViewCookie() { 239 public void view() {} 240 } 241 ) 242 ); 243 items.add( new GoldenValue.TestLookupItem ( new javax.swing.JPanel () ) ); 244 245 representedObject.setValue( ProxyLook.GET_LOOKUP_ITEMS, items ); 246 247 GoldenEvent[] goldenEvents = new GoldenEvent[] { 248 new GoldenEvent( node, 249 Node.PROP_COOKIE, 250 null, null ), 251 }; 252 253 assertTrue( GoldenEvent.compare( testNodeListener.getEvents(), 254 goldenEvents, 255 null ) ); 256 257 } 258 259 260 public void testFireNodeDestroyed() { 261 representedObject.setProperty( SampleRepObject.DESTROY, "kill" ); 262 263 assertTrue( "Bad number of events: " + testNodeListener.getEvents().size(), 264 testNodeListener.getEvents().size() == 1 ); 265 266 } 267 268 269 public void testAddChildren() { 270 List oldValue = (List )GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues );; 271 List newValue = new ArrayList ( oldValue ); 272 newValue.add( "Additional child" ); 273 274 node.removeNodeListener( testNodeListener ); 276 Node oldNodes[] = node.getChildren().getNodes(); 277 node.addNodeListener( testNodeListener ); 278 279 representedObject.setValue( ProxyLook.GET_CHILD_OBJECTS, newValue ); 280 Node newNodes[] = node.getChildren().getNodes(); 281 282 assertEquals(3, newNodes.length); 283 GoldenEvent[] goldenEvents = new GoldenEvent[] { 284 new GoldenEvent( node, 285 true, 286 new Node[] { newNodes[2] }, 287 new int[] { 2 } ), 288 }; 289 290 assertTrue( GoldenEvent.compare( testNodeListener.getEvents(), 291 goldenEvents, 292 null ) ); 293 } 294 295 public void testRemoveChildren() { 296 List oldValue = (List )GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues ); 297 List newValue = new ArrayList ( oldValue ); 298 newValue.remove( 0 ); 299 300 node.removeNodeListener( testNodeListener ); 302 Node oldNodes[] = node.getChildren().getNodes(); 303 node.addNodeListener( testNodeListener ); 304 305 representedObject.setValue( ProxyLook.GET_CHILD_OBJECTS, newValue ); 306 Node newNodes[] = node.getChildren().getNodes(); 307 308 GoldenEvent[] goldenEvents = new GoldenEvent[] { 309 new GoldenEvent( node, 310 false, 311 new Node[] { oldNodes[0] }, 312 new int[] { 0 } ), 313 }; 314 315 assertTrue( GoldenEvent.compare( testNodeListener.getEvents(), 316 goldenEvents, 317 null ) ); 318 319 } 320 321 322 public void testReorderChildren() { 323 List oldValue = (List )GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues ); 324 List newValue = new ArrayList ( oldValue ); 325 Object o0 = newValue.get( 0 ); 326 Object o1 = newValue.get( 1 ); 327 newValue.set( 0, o1 ); 328 newValue.set( 1, o0 ); 329 330 node.removeNodeListener( testNodeListener ); 332 Node oldNodes[] = node.getChildren().getNodes(); 333 node.addNodeListener( testNodeListener ); 334 335 representedObject.setValue( ProxyLook.GET_CHILD_OBJECTS, newValue ); 336 Node newNodes[] = node.getChildren().getNodes(); 337 338 GoldenEvent[] goldenEvents = new GoldenEvent[] { 339 new GoldenEvent( node, 340 new int[] { 1, 0 } ) 341 }; 342 343 assertTrue( GoldenEvent.compare( testNodeListener.getEvents(), 344 goldenEvents, 345 null ) ); 346 } 347 348 349 public void testNoChildrenChange() { 350 List oldValue = (List )GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues ); 351 List newValue = new ArrayList ( oldValue ); 352 353 node.removeNodeListener( testNodeListener ); 355 node.addNodeListener( testNodeListener ); 356 357 representedObject.setValue( ProxyLook.GET_CHILD_OBJECTS, newValue ); 358 359 360 assertTrue( "Bad number of events : " + testNodeListener.getEvents().size(), 361 testNodeListener.getEvents().size() == 0 ); 362 363 364 } 365 366 367 } 368 369 | Popular Tags |