1 19 20 package org.netbeans.spi.looks; 21 22 import java.util.*; 23 import java.io.IOException ; 24 import java.beans.BeanInfo ; 25 26 27 28 import org.openide.nodes.*; 29 import org.openide.util.Lookup; 30 31 import org.netbeans.spi.looks.*; 32 33 import org.netbeans.api.nodes2looks.TestUtil; 34 import org.netbeans.junit.*; 35 36 39 40 public class TestBaseValues extends NbTestCase { 41 42 protected Look look; 44 45 protected Node node; 47 48 protected SampleRepObject representedObject; 50 51 protected GoldenValue[] goldenValues; 53 54 protected int expectedCallsCount; 56 57 protected static final String MSSG_UNEXPECTED_VALUE_RETURNED = 59 "Unexpected value returned."; 60 61 protected static final String MSSG_METHOD_NOT_CALLED = 63 "Method not called."; 64 65 protected static final String MSSG_EXCEPTION_THROWN = 67 "Exception thrown."; 68 69 protected static final Lookup NO_LOOKUP = null; 71 72 73 74 76 public TestBaseValues(java.lang.String testName) { 77 super(testName); 78 } 79 80 public static void main(java.lang.String [] args) { 81 junit.textui.TestRunner.run(suite()); 82 } 83 84 public static NbTest suite() { 85 NbTestSuite suite = new NbTestSuite( TestBaseValues.class ); 86 87 return suite; 88 } 89 90 protected void setUp() throws Exception { 91 super.setUp(); 92 } 93 94 protected void tearDown() throws Exception { 95 look = null; 96 super.tearDown(); 97 } 98 99 101 protected void setTarget( Look look, SampleRepObject representedObject, int expectedCallsCount ) { 102 this.look = look; 103 this.node = null; 104 this.representedObject = representedObject; 105 this.expectedCallsCount = expectedCallsCount; 106 org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( look, representedObject, null ); 107 } 108 109 protected void setTarget( Node node, SampleRepObject representedObject, int expectedCallsCount ) { 110 this.look = null; 111 this.node = node; 112 this.representedObject = representedObject; 113 this.expectedCallsCount = expectedCallsCount; 114 } 115 116 protected void setGoldenValues( GoldenValue[] goldenValues ) { 117 this.goldenValues = goldenValues; 118 } 119 120 protected boolean onNode() { 121 return node != null; 122 } 123 124 126 public void testAttachTo() { 127 if ( onNode() ) { 128 assertEquals ( MSSG_METHOD_NOT_CALLED, expectedCallsCount, 129 representedObject.getAttachCalled() ); 130 } 131 else { 132 assertEquals ( MSSG_METHOD_NOT_CALLED, expectedCallsCount, 133 ((SampleRepObject)representedObject).getAttachCalled() ); 134 } 135 } 136 137 139 public void testGetLookupValues() { 140 if ( onNode() ) { 141 Lookup lookup = node.getLookup(); 142 Lookup.Result result = lookup.lookup( new Lookup.Template( Object .class ) ); 143 Collection items = new ArrayList( result.allItems() ); 145 147 Object nodeItself = null; 148 149 for( Iterator it = items.iterator(); it.hasNext(); ) { 150 Lookup.Item item = (Lookup.Item)it.next(); 151 if ( item.getInstance() == node ) { 152 nodeItself = item; 153 break; 154 } 155 } 156 157 assertNotNull( "Lookup should contain the node itself ", nodeItself ); 158 items.remove( nodeItself ); 159 160 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 161 GoldenValue.isOK( 162 ProxyLook.GET_LOOKUP_ITEMS, 163 items, 164 goldenValues ) ); 165 } 166 else { 167 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 168 GoldenValue.isOK( 169 ProxyLook.GET_LOOKUP_ITEMS, 170 look.getLookupItems( representedObject, Lookup.EMPTY ), 171 goldenValues ) ); 172 } 173 } 174 175 177 public void testGetDisplayName() { 178 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 179 GoldenValue.isOK( 180 ProxyLook.GET_DISPLAY_NAME, 181 onNode() ? node.getDisplayName() : 182 look.getDisplayName( representedObject, NO_LOOKUP ), 183 goldenValues ) ); 184 185 } 186 187 public void testGetName() { 188 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 189 GoldenValue.isOK( 190 ProxyLook.GET_NAME, 191 onNode() ? node.getName() : 192 look.getName( representedObject, NO_LOOKUP ), 193 goldenValues ) ); 194 } 195 196 public void testSetName() throws IOException { 197 if ( onNode() ) { 198 node.setName( "New name" ); 199 if ( onNode() ) { 200 assertEquals( MSSG_METHOD_NOT_CALLED, expectedCallsCount, 201 representedObject.getSetNameCalled() ); 202 } 203 } 204 else { 205 look.rename( representedObject, "New name", NO_LOOKUP ); 206 assertEquals ( MSSG_METHOD_NOT_CALLED, expectedCallsCount, 207 ((SampleRepObject)representedObject).getSetNameCalled() ); 208 } 209 210 } 211 212 public void testGetShortDescription() { 213 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 214 GoldenValue.isOK( 215 ProxyLook.GET_SHORT_DESCRIPTION, 216 onNode() ? node.getShortDescription() : 217 look.getShortDescription( representedObject, NO_LOOKUP ), 218 goldenValues ) ); 219 } 220 221 public void testGetIcon() { 222 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 223 GoldenValue.isOK( 224 ProxyLook.GET_ICON, 225 onNode() ? node.getIcon( BeanInfo.ICON_COLOR_16x16 ) : 226 look.getIcon( representedObject, BeanInfo.ICON_COLOR_16x16, NO_LOOKUP ), 227 goldenValues ) ); 228 229 } 230 231 public void testGetOpenedIcon() { 232 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 233 GoldenValue.isOK( 234 ProxyLook.GET_OPENED_ICON, 235 onNode() ? node.getOpenedIcon( BeanInfo.ICON_COLOR_16x16 ) : 236 look.getOpenedIcon( representedObject, BeanInfo.ICON_COLOR_16x16, NO_LOOKUP ), 237 goldenValues ) ); 238 } 239 240 public void testGetHelpCtx() { 241 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 242 GoldenValue.isOK( 243 ProxyLook.GET_HELP_CTX, 244 onNode() ? node.getHelpCtx() : 245 look.getHelpCtx( representedObject, NO_LOOKUP ), 246 goldenValues ) ); 247 } 248 249 251 public void testGetChildObjects() { 252 253 if ( onNode() ) { 254 255 Node[] nodes = node.getChildren().getNodes(); 256 List gv = (List)GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues ); 257 258 if ( gv == null ) { 259 fail( "Golden value is invalid" ); 260 } 261 if ( gv.size() != nodes.length ) { 262 fail( MSSG_UNEXPECTED_VALUE_RETURNED ); 263 } 264 265 266 for( int i = 0; i < nodes.length; i++ ) { 267 268 Node n = nodes[i]; 269 if ( TestUtil.getRepresentedObject( n ) != gv.get(i) && 270 !TestUtil.getRepresentedObject( n ).equals( gv.get(i) ) ) { 271 fail( MSSG_UNEXPECTED_VALUE_RETURNED + "on index : " + i ); 272 } 273 } 274 275 } 276 else { 277 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 278 GoldenValue.isOK( 279 ProxyLook.GET_CHILD_OBJECTS, 280 look.getChildObjects( representedObject, NO_LOOKUP ), 281 goldenValues ) ); 282 } 283 } 284 285 287 public void testGetNewTypes() { 288 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 289 GoldenValue.isOK( 290 ProxyLook.GET_NEW_TYPES, 291 onNode() ? node.getNewTypes() : 292 look.getNewTypes( representedObject, NO_LOOKUP ), 293 goldenValues ) ); 294 } 295 296 public void testGetActions() throws Exception { 297 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 298 GoldenValue.isOK( 299 ProxyLook.GET_ACTIONS, 300 onNode() ? node.getActions() : 301 look.getActions( representedObject, NO_LOOKUP ), 302 goldenValues ) ); 303 } 304 305 public void testGetContextActions() { 306 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 307 GoldenValue.isOK( 308 ProxyLook.GET_CONTEXT_ACTIONS, 309 onNode() ? node.getContextActions() : 310 look.getContextActions( representedObject, NO_LOOKUP ), 311 goldenValues ) ); 312 } 313 314 public void testGetDefaultAction() { 315 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 316 GoldenValue.isOK( 317 ProxyLook.GET_DEFAULT_ACTION, 318 onNode() ? node.getDefaultAction() : 319 look.getDefaultAction( representedObject, NO_LOOKUP ), 320 goldenValues ) ); 321 } 322 323 325 public void testGetPropertySets() { 326 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 327 GoldenValue.isOK( 328 ProxyLook.GET_PROPERTY_SETS, 329 onNode() ? node.getPropertySets() : 330 look.getPropertySets( representedObject, NO_LOOKUP ), 331 goldenValues ) ); 332 } 333 334 public void testGetCustomizer() { 335 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 336 GoldenValue.isOK( 337 ProxyLook.GET_CUSTOMIZER, 338 onNode() ? node.getCustomizer() : 339 look.getCustomizer( representedObject, NO_LOOKUP ), 340 goldenValues ) ); 341 } 342 343 public void testHasCustomizer() { 344 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 345 GoldenValue.isOK( 346 ProxyLook.HAS_CUSTOMIZER, 347 onNode() ? new Boolean ( node.hasCustomizer() ) : 348 new Boolean ( look.hasCustomizer( representedObject, NO_LOOKUP ) ), 349 goldenValues ) ); 350 } 351 352 353 355 public void testCanRename() { 356 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 357 GoldenValue.isOK( 358 ProxyLook.CAN_RENAME, 359 onNode() ? new Boolean ( node.canRename() ) : 360 new Boolean ( look.canRename( representedObject, NO_LOOKUP ) ), 361 goldenValues ) ); 362 } 363 364 public void testCanDestroy() { 365 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 366 GoldenValue.isOK( 367 ProxyLook.CAN_DESTROY, 368 onNode() ? new Boolean ( node.canDestroy() ) : 369 new Boolean ( look.canDestroy( representedObject, NO_LOOKUP ) ), 370 goldenValues ) ); 371 } 372 373 public void testCanCopy() { 374 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 375 GoldenValue.isOK( 376 ProxyLook.CAN_COPY, 377 onNode() ? new Boolean ( node.canCopy() ) : 378 new Boolean ( look.canCopy( representedObject, NO_LOOKUP ) ), 379 goldenValues ) ); 380 } 381 382 public void testCanCut() { 383 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 384 GoldenValue.isOK( 385 ProxyLook.CAN_CUT, 386 onNode() ? new Boolean ( node.canCut() ) : 387 new Boolean ( look.canCut( representedObject, NO_LOOKUP ) ), 388 goldenValues ) ); 389 } 390 391 public void testGetPasteTypes() { 392 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 393 GoldenValue.isOK( 394 ProxyLook.GET_PASTE_TYPES, 395 onNode() ? node.getPasteTypes( null ) : 396 look.getPasteTypes( representedObject, null, NO_LOOKUP ), 397 goldenValues ) ); 398 } 399 400 public void testGetDropType() { 401 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 402 GoldenValue.isOK( 403 ProxyLook.GET_DROP_TYPE, 404 onNode() ? node.getDropType( null, 0, 0 ) : 405 look.getDropType( representedObject, null, 0, 0, NO_LOOKUP ), 406 goldenValues ) ); 407 } 408 409 public void testClipboardCopy() { 410 try { 411 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 412 GoldenValue.isOK( 413 ProxyLook.CLIPBOARD_COPY, 414 onNode() ? node.clipboardCopy() : 415 look.clipboardCopy( representedObject, NO_LOOKUP ), 416 goldenValues ) ); 417 } 418 catch ( java.io.IOException e ) { 419 fail ( MSSG_EXCEPTION_THROWN ); 420 } 421 } 422 423 public void testClipboardCut() { 424 try { 425 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 426 GoldenValue.isOK( 427 ProxyLook.CLIPBOARD_CUT, 428 onNode() ? node.clipboardCut() : 429 look.clipboardCut( representedObject, NO_LOOKUP ), 430 goldenValues ) ); 431 } 432 catch ( java.io.IOException e ) { 433 fail ( MSSG_EXCEPTION_THROWN ); 434 } 435 436 } 437 438 public void testDrag() { 439 try { 440 assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED, 441 GoldenValue.isOK( 442 ProxyLook.DRAG, 443 onNode() ? node.drag() : 444 look.drag( representedObject, NO_LOOKUP ), 445 goldenValues ) ); 446 } 447 catch ( java.io.IOException e ) { 448 fail ( MSSG_EXCEPTION_THROWN ); 449 } 450 } 451 452 public void testDestroy() { 453 try { 454 if ( onNode() ) { 455 node.destroy(); 456 assertEquals ( MSSG_METHOD_NOT_CALLED, expectedCallsCount, 457 representedObject.getDestroyCalled() ); 458 } 459 else { 460 look.destroy( representedObject, NO_LOOKUP ); 461 assertEquals ( MSSG_METHOD_NOT_CALLED, expectedCallsCount, 462 ((SampleRepObject)representedObject).getDestroyCalled() ); 463 } 464 465 } 466 catch ( java.io.IOException e ) { 467 fail ( MSSG_EXCEPTION_THROWN ); 468 } 469 } 470 471 472 473 } 474 | Popular Tags |