1 29 30 package nextapp.echo2.app.test; 31 32 import java.util.Locale ; 33 34 import nextapp.echo2.app.ApplicationInstance; 35 import nextapp.echo2.app.Color; 36 import nextapp.echo2.app.Component; 37 import nextapp.echo2.app.Extent; 38 import nextapp.echo2.app.Font; 39 import nextapp.echo2.app.layout.GridLayoutData; 40 import junit.framework.TestCase; 41 42 45 public class ComponentTest extends TestCase { 46 47 50 public void testBackground() { 51 NullComponent c = new NullComponent(); 52 PropertyChangeEvaluator pce = new PropertyChangeEvaluator(); 53 c.addPropertyChangeListener(pce); 54 c.setBackground(new Color(0x12, 0x34, 0x56)); 55 assertEquals(new Color(0x12, 0x34, 0x56), c.getBackground()); 56 assertEquals(Component.PROPERTY_BACKGROUND, pce.lastEvent.getPropertyName()); 57 } 58 59 62 public void testEnabled() { 63 NullComponent c = new NullComponent(); 64 PropertyChangeEvaluator pce = new PropertyChangeEvaluator(); 65 c.addPropertyChangeListener(pce); 66 assertTrue(c.isEnabled()); 67 c.setEnabled(false); 68 assertFalse(c.isEnabled()); 69 assertEquals(Component.ENABLED_CHANGED_PROPERTY, pce.lastEvent.getPropertyName()); 70 c.setEnabled(false); 71 assertFalse(c.isEnabled()); 72 c.setEnabled(true); 73 assertTrue(c.isEnabled()); 74 c.setEnabled(true); 75 assertTrue(c.isEnabled()); 76 } 77 78 81 public void testFocusTraversalIndex() { 82 Component c = new NullComponent(); 83 PropertyChangeEvaluator pce = new PropertyChangeEvaluator(); 84 c.addPropertyChangeListener(pce); 85 assertEquals(0, c.getFocusTraversalIndex()); 86 c.setFocusTraversalIndex(5); 87 assertEquals(Component.FOCUS_TRAVERSAL_INDEX_CHANGED_PROPERTY, pce.lastEvent.getPropertyName()); 88 assertEquals(5, c.getFocusTraversalIndex()); 89 c.setVisible(false); 90 assertEquals(false, c.isVisible()); 91 assertEquals(5, c.getFocusTraversalIndex()); 92 c.setFocusTraversalIndex(70); 93 assertEquals(false, c.isVisible()); 94 assertEquals(70, c.getFocusTraversalIndex()); 95 } 96 97 100 public void testFont() { 101 NullComponent c = new NullComponent(); 102 PropertyChangeEvaluator pce = new PropertyChangeEvaluator(); 103 c.addPropertyChangeListener(pce); 104 c.setFont(new Font(Font.COURIER, Font.BOLD, new Extent(12, Extent.PT))); 105 assertEquals(new Font(Font.COURIER, Font.BOLD, new Extent(12, Extent.PT)), c.getFont()); 106 assertEquals(Component.PROPERTY_FONT, pce.lastEvent.getPropertyName()); 107 } 108 109 112 public void testForeground() { 113 NullComponent c = new NullComponent(); 114 PropertyChangeEvaluator pce = new PropertyChangeEvaluator(); 115 c.addPropertyChangeListener(pce); 116 c.setForeground(new Color(0x12, 0x34, 0x56)); 117 assertEquals(new Color(0x12, 0x34, 0x56), c.getForeground()); 118 assertEquals(Component.PROPERTY_FOREGROUND, pce.lastEvent.getPropertyName()); 119 } 120 121 125 public void testGetComponent() { 126 NullComponent c = new NullComponent(); 127 for (int i = 0; i < 5; ++ i) { 128 c.add(new NullComponent()); 129 } 130 NullComponent sixthComponent = new NullComponent(); 131 c.add(sixthComponent); 132 for (int i = 0; i < 5; ++ i) { 133 c.add(new NullComponent()); 134 } 135 assertTrue(sixthComponent == c.getComponent(5)); 136 } 137 138 142 public void testGetComponentById() { 143 NullComponent c1 = new NullComponent(); 144 c1.setId("c1"); 145 NullComponent c2 = new NullComponent(); 146 c2.setId("c2"); 147 c1.add(c2); 148 NullComponent c3 = new NullComponent(); 149 c3.setId("c3"); 150 c1.add(c3); 151 NullComponent c4 = new NullComponent(); 152 c4.setId("c4"); 153 c2.add(c4); 154 NullComponent c5 = new NullComponent(); 155 c5.setId("c5"); 156 c2.add(c5); 157 NullComponent c6 = new NullComponent(); 158 c6.setId("c6"); 159 c5.add(c6); 160 161 assertEquals("c1", c1.getId()); 162 assertEquals("c2", c2.getId()); 163 assertEquals("c3", c3.getId()); 164 assertEquals("c4", c4.getId()); 165 assertEquals("c5", c5.getId()); 166 assertEquals("c6", c6.getId()); 167 168 assertEquals(c1, c1.getComponent("c1")); 169 assertEquals(c2, c1.getComponent("c2")); 170 assertEquals(c3, c1.getComponent("c3")); 171 assertEquals(c4, c1.getComponent("c4")); 172 assertEquals(c5, c1.getComponent("c5")); 173 assertEquals(c6, c1.getComponent("c6")); 174 175 assertEquals(null, c2.getComponent("c1")); 176 assertEquals(c2, c2.getComponent("c2")); 177 assertEquals(null, c2.getComponent("c3")); 178 assertEquals(c4, c2.getComponent("c4")); 179 assertEquals(c5, c2.getComponent("c5")); 180 assertEquals(c6, c2.getComponent("c6")); 181 182 assertEquals(null, c3.getComponent("c1")); 183 assertEquals(null, c3.getComponent("c2")); 184 assertEquals(c3, c3.getComponent("c3")); 185 assertEquals(null, c3.getComponent("c4")); 186 assertEquals(null, c3.getComponent("c5")); 187 assertEquals(null, c3.getComponent("c6")); 188 } 189 190 193 public void testGetComponentCount() { 194 NullComponent c = new NullComponent(); 195 for (int i = 0; i < 5; ++ i) { 196 c.add(new NullComponent()); 197 } 198 assertEquals(5, c.getComponentCount()); 199 } 200 201 204 public void testGetComponents() { 205 NullComponent parent = new NullComponent(); 206 NullComponent child1 = new NullComponent(); 207 NullComponent child2 = new NullComponent(); 208 parent.add(child1); 209 parent.add(child2); 210 Component[] children = parent.getComponents(); 211 assertSame(child1, children[0]); 212 assertSame(child2, children[1]); 213 } 214 215 218 public void testGetVisibleComponent() { 219 IndexOutOfBoundsException exception; 220 NullComponent parent = new NullComponent(); 221 NullComponent child1 = new NullComponent(); 222 NullComponent child2 = new NullComponent(); 223 parent.add(child1); 224 parent.add(child2); 225 226 assertSame(child1, parent.getVisibleComponent(0)); 227 assertSame(child2, parent.getVisibleComponent(1)); 228 exception = null; 229 try { 230 parent.getVisibleComponent(2); 231 } catch (IndexOutOfBoundsException ex) { 232 exception = ex; 233 } 234 assertNotNull(exception); 235 236 child1.setVisible(false); 237 assertSame(child2, parent.getVisibleComponent(0)); 238 exception = null; 239 try { 240 parent.getVisibleComponent(1); 241 } catch (IndexOutOfBoundsException ex) { 242 exception = ex; 243 } 244 assertNotNull(exception); 245 246 child2.setVisible(false); 247 exception = null; 248 try { 249 parent.getVisibleComponent(0); 250 } catch (IndexOutOfBoundsException ex) { 251 exception = ex; 252 } 253 assertNotNull(exception); 254 255 child1.setVisible(true); 256 assertSame(child1, parent.getVisibleComponent(0)); 257 exception = null; 258 try { 259 parent.getVisibleComponent(1); 260 } catch (IndexOutOfBoundsException ex) { 261 exception = ex; 262 } 263 assertNotNull(exception); 264 } 265 266 269 public void testGetVisibleComponent2() { 270 NullComponent c = new NullComponent(); 271 Exception exception = null; 272 try { 273 c.getVisibleComponent(0); 274 } catch (IndexOutOfBoundsException ex) { 275 exception = ex; 276 } 277 assertNotNull(exception); 278 } 279 280 283 public void testGetVisibleComponentCount() { 284 NullComponent c = new NullComponent(); 285 for (int i = 0; i < 5; ++ i) { 286 c.add(new NullComponent()); 287 } 288 assertEquals(5, c.getVisibleComponentCount()); 289 c.getComponent(1).setVisible(false); 290 assertEquals(4, c.getVisibleComponentCount()); 291 c.getComponent(2).setVisible(false); 292 assertEquals(3, c.getVisibleComponentCount()); 293 } 294 295 298 public void testGetVisibleComponents() { 299 NullComponent parent = new NullComponent(); 300 NullComponent child1 = new NullComponent(); 301 NullComponent child2 = new NullComponent(); 302 parent.add(child1); 303 parent.add(child2); 304 Component[] children = parent.getVisibleComponents(); 305 assertEquals(2, children.length); 306 assertSame(child1, children[0]); 307 assertSame(child2, children[1]); 308 309 child1.setVisible(false); 310 children = parent.getVisibleComponents(); 311 assertEquals(1, children.length); 312 assertSame(child2, children[0]); 313 314 child2.setVisible(false); 315 children = parent.getVisibleComponents(); 316 assertEquals(0, children.length); 317 318 child1.setVisible(true); 319 children = parent.getVisibleComponents(); 320 assertEquals(1, children.length); 321 assertSame(child1, children[0]); 322 } 323 324 327 public void testIndexOf() { 328 NullComponent parent = new NullComponent(); 329 NullComponent a = new NullComponent(); 330 NullComponent b = new NullComponent(); 331 NullComponent c = new NullComponent(); 332 NullComponent d = new NullComponent(); 333 parent.add(a); 334 parent.add(b); 335 parent.add(c); 336 assertEquals(0, parent.indexOf(a)); 337 assertEquals(1, parent.indexOf(b)); 338 assertEquals(2, parent.indexOf(c)); 339 assertEquals(-1, parent.indexOf(d)); 340 } 341 342 346 public void testLifecycleSingleComponent() { 347 ColumnApp app = new ColumnApp(); 348 ApplicationInstance.setActive(app); 349 app.doInit(); 350 351 LifecycleTestComponent ltc1 = new LifecycleTestComponent(); 352 assertEquals(0, ltc1.getInitCount()); 353 assertEquals(0, ltc1.getDisposeCount()); 354 app.getColumn().add(ltc1); 355 assertEquals(1, ltc1.getInitCount()); 356 assertEquals(0, ltc1.getDisposeCount()); 357 app.getColumn().remove(ltc1); 358 assertEquals(1, ltc1.getInitCount()); 359 assertEquals(1, ltc1.getDisposeCount()); 360 app.getColumn().add(ltc1); 361 assertEquals(2, ltc1.getInitCount()); 362 assertEquals(1, ltc1.getDisposeCount()); 363 app.getColumn().add(ltc1); 364 assertEquals(3, ltc1.getInitCount()); 365 assertEquals(2, ltc1.getDisposeCount()); 366 app.getColumn().remove(ltc1); 367 assertEquals(3, ltc1.getInitCount()); 368 assertEquals(3, ltc1.getDisposeCount()); 369 app.getColumn().remove(ltc1); 370 assertEquals(3, ltc1.getInitCount()); 371 assertEquals(3, ltc1.getDisposeCount()); 372 373 ApplicationInstance.setActive(null); 374 } 375 376 380 public void testLifecycleComponentHierarchy() { 381 ColumnApp app = new ColumnApp(); 382 ApplicationInstance.setActive(app); 383 app.doInit(); 384 385 LifecycleTestComponent ltc1 = new LifecycleTestComponent(); 386 LifecycleTestComponent ltc2 = new LifecycleTestComponent(); 387 388 assertEquals(0, ltc1.getInitCount()); 389 assertEquals(0, ltc1.getDisposeCount()); 390 assertEquals(0, ltc2.getInitCount()); 391 assertEquals(0, ltc2.getDisposeCount()); 392 393 ltc1.add(ltc2); 394 395 assertEquals(0, ltc1.getInitCount()); 396 assertEquals(0, ltc1.getDisposeCount()); 397 assertEquals(0, ltc2.getInitCount()); 398 assertEquals(0, ltc2.getDisposeCount()); 399 400 app.getColumn().add(ltc1); 401 402 assertEquals(1, ltc1.getInitCount()); 403 assertEquals(0, ltc1.getDisposeCount()); 404 assertEquals(1, ltc2.getInitCount()); 405 assertEquals(0, ltc2.getDisposeCount()); 406 407 app.getColumn().remove(ltc1); 408 409 assertEquals(1, ltc1.getInitCount()); 410 assertEquals(1, ltc1.getDisposeCount()); 411 assertEquals(1, ltc2.getInitCount()); 412 assertEquals(1, ltc2.getDisposeCount()); 413 414 app.getColumn().add(ltc1); 415 416 assertEquals(2, ltc1.getInitCount()); 417 assertEquals(1, ltc1.getDisposeCount()); 418 assertEquals(2, ltc2.getInitCount()); 419 assertEquals(1, ltc2.getDisposeCount()); 420 421 app.getColumn().add(ltc1); 422 423 assertEquals(3, ltc1.getInitCount()); 424 assertEquals(2, ltc1.getDisposeCount()); 425 assertEquals(3, ltc2.getInitCount()); 426 assertEquals(2, ltc2.getDisposeCount()); 427 428 app.getColumn().remove(ltc1); 429 430 assertEquals(3, ltc1.getInitCount()); 431 assertEquals(3, ltc1.getDisposeCount()); 432 assertEquals(3, ltc2.getInitCount()); 433 assertEquals(3, ltc2.getDisposeCount()); 434 435 app.getColumn().remove(ltc1); 436 437 assertEquals(3, ltc1.getInitCount()); 438 assertEquals(3, ltc1.getDisposeCount()); 439 assertEquals(3, ltc2.getInitCount()); 440 assertEquals(3, ltc2.getDisposeCount()); 441 442 ApplicationInstance.setActive(null); 443 } 444 445 450 public void testLifecycleRemoveDuringInit() { 451 ColumnApp app = new ColumnApp(); 452 ApplicationInstance.setActive(app); 453 app.doInit(); 454 455 LifecycleTestComponent special = new LifecycleTestComponent(){ 456 457 public void init() { 458 super.init(); 459 getParent().remove(this); 460 } 461 }; 462 463 try { 464 app.getColumn().add(special); 465 fail("Did not throw IllegalStateException as expected."); 466 } catch (IllegalStateException ex) { 467 } 469 } 470 471 476 public void testLifecycleAddDuringDispose() { 477 ColumnApp app = new ColumnApp(); 478 ApplicationInstance.setActive(app); 479 app.doInit(); 480 481 LifecycleTestComponent special = new LifecycleTestComponent(){ 482 483 public void dispose() { 484 super.init(); 485 getParent().add(this); 486 } 487 }; 488 489 app.getColumn().add(special); 490 491 try { 492 app.getColumn().remove(special); 493 fail("Did not throw IllegalStateException as expected."); 494 } catch (IllegalStateException ex) { 495 } 497 } 498 499 502 public void testLayoutData() { 503 NullComponent c = new NullComponent(); 504 assertNull(c.getLayoutData()); 505 PropertyChangeEvaluator pce = new PropertyChangeEvaluator(); 506 c.addPropertyChangeListener(pce); 507 GridLayoutData data = new GridLayoutData(); 508 data.setColumnSpan(2); 509 c.setLayoutData(data); 510 assertEquals(2, ((GridLayoutData) c.getLayoutData()).getColumnSpan()); 511 assertEquals(Component.PROPERTY_LAYOUT_DATA, pce.lastEvent.getPropertyName()); 512 } 513 514 518 public void testRenderLocaleWithoutApplication() { 519 NullComponent c = new NullComponent(); 520 assertNull(c.getRenderLocale()); 521 c.setLocale(Locale.TRADITIONAL_CHINESE); 522 assertEquals(Locale.TRADITIONAL_CHINESE, c.getRenderLocale()); 523 } 524 525 528 public void testPropertyChangeListeners() { 529 PropertyChangeEvaluator pce = new PropertyChangeEvaluator(); 530 NullComponent c = new NullComponent(); 531 c.addPropertyChangeListener(pce); 532 c.setBackground(new Color(0xabcdef)); 533 assertEquals(null, pce.lastEvent.getOldValue()); 534 assertEquals(new Color(0xabcdef), pce.lastEvent.getNewValue()); 535 assertEquals(c, pce.lastEvent.getSource()); 536 assertEquals(Component.PROPERTY_BACKGROUND, pce.lastEvent.getPropertyName()); 537 c.setBackground(new Color(0xfedcba)); 538 assertEquals(new Color(0xabcdef), pce.lastEvent.getOldValue()); 539 assertEquals(new Color(0xfedcba), pce.lastEvent.getNewValue()); 540 c.setBackground(null); 541 assertEquals(new Color(0xfedcba), pce.lastEvent.getOldValue()); 542 assertEquals(null, pce.lastEvent.getNewValue()); 543 } 544 545 548 public void testRemoveAll() { 549 NullComponent c = new NullComponent(); 550 c.add(new NullComponent()); 551 c.add(new NullComponent()); 552 c.add(new NullComponent()); 553 assertEquals(3, c.getComponentCount()); 554 c.removeAll(); 555 assertEquals(0, c.getComponentCount()); 556 } 557 558 561 public void testRemoveByIndex() { 562 NullComponent parent = new NullComponent(); 563 NullComponent a = new NullComponent(); 564 NullComponent b = new NullComponent(); 565 NullComponent c = new NullComponent(); 566 NullComponent d = new NullComponent(); 567 parent.add(a); 568 parent.add(b); 569 parent.add(c); 570 parent.add(d); 571 parent.remove(2); 572 assertEquals(0, parent.indexOf(a)); 573 assertEquals(1, parent.indexOf(b)); 574 assertEquals(2, parent.indexOf(d)); 575 assertEquals(-1, parent.indexOf(c)); 576 } 577 578 581 public void testRenderId() { 582 ColumnApp app1 = new ColumnApp(); 583 ApplicationInstance.setActive(app1); 584 app1.doInit(); 585 NullComponent component1 = new NullComponent(); 586 assertNull(component1.getRenderId()); 587 app1.getColumn().add(component1); 588 assertNotNull(component1.getApplicationInstance()); 589 assertNotNull(component1.getRenderId()); 590 ApplicationInstance.setActive(null); 591 592 ColumnApp app2 = new ColumnApp(); 593 ApplicationInstance.setActive(app2); 594 app2.doInit(); 595 NullComponent component2 = new NullComponent(); 596 assertNull(component2.getRenderId()); 597 app2.getColumn().add(component2); 598 assertNotNull(component2.getApplicationInstance()); 599 assertNotNull(component2.getRenderId()); 600 ApplicationInstance.setActive(null); 601 602 assertTrue(component1.getRenderId().equals(component2.getRenderId())); 604 605 ApplicationInstance.setActive(app1); 606 app1.getColumn().remove(component1); 607 ApplicationInstance.setActive(null); 608 609 ApplicationInstance.setActive(app2); 610 app2.getColumn().add(component1); 611 ApplicationInstance.setActive(null); 612 613 assertFalse(component1.getRenderId().equals(component2.getRenderId())); 614 } 615 616 619 public void testRenderEnabled() { 620 ColumnApp app = new ColumnApp(); 621 ApplicationInstance.setActive(app); 622 app.doInit(); 623 assertTrue(app.getContentPane().isRenderEnabled()); 624 assertTrue(app.getColumn().isRenderEnabled()); 625 assertTrue(app.getLabel().isRenderEnabled()); 626 app.getColumn().setEnabled(false); 627 assertTrue(app.getContentPane().isRenderEnabled()); 628 assertFalse(app.getColumn().isRenderEnabled()); 629 assertFalse(app.getLabel().isRenderEnabled()); 630 app.getLabel().setEnabled(false); 631 assertTrue(app.getContentPane().isRenderEnabled()); 632 assertFalse(app.getColumn().isRenderEnabled()); 633 assertFalse(app.getLabel().isRenderEnabled()); 634 app.getColumn().setEnabled(true); 635 assertTrue(app.getContentPane().isRenderEnabled()); 636 assertTrue(app.getColumn().isRenderEnabled()); 637 assertFalse(app.getLabel().isRenderEnabled()); 638 app.getLabel().setEnabled(true); 639 assertTrue(app.getContentPane().isRenderEnabled()); 640 assertTrue(app.getColumn().isRenderEnabled()); 641 assertTrue(app.getLabel().isRenderEnabled()); 642 } 643 644 647 public void testRenderVisible() { 648 ColumnApp app = new ColumnApp(); 649 ApplicationInstance.setActive(app); 650 app.doInit(); 651 assertTrue(app.getContentPane().isRenderVisible()); 652 assertTrue(app.getColumn().isRenderVisible()); 653 assertTrue(app.getLabel().isRenderVisible()); 654 app.getColumn().setVisible(false); 655 assertTrue(app.getContentPane().isRenderVisible()); 656 assertFalse(app.getColumn().isRenderVisible()); 657 assertFalse(app.getLabel().isRenderVisible()); 658 app.getLabel().setVisible(false); 659 assertTrue(app.getContentPane().isRenderVisible()); 660 assertFalse(app.getColumn().isRenderVisible()); 661 assertFalse(app.getLabel().isRenderVisible()); 662 app.getColumn().setVisible(true); 663 assertTrue(app.getContentPane().isRenderVisible()); 664 assertTrue(app.getColumn().isRenderVisible()); 665 assertFalse(app.getLabel().isRenderVisible()); 666 app.getLabel().setVisible(true); 667 assertTrue(app.getContentPane().isRenderVisible()); 668 assertTrue(app.getColumn().isRenderVisible()); 669 assertTrue(app.getLabel().isRenderVisible()); 670 } 671 672 675 public void testVisible() { 676 NullComponent c = new NullComponent(); 677 PropertyChangeEvaluator pce = new PropertyChangeEvaluator(); 678 c.addPropertyChangeListener(pce); 679 assertTrue(c.isVisible()); 680 c.setVisible(false); 681 assertFalse(c.isVisible()); 682 assertEquals(Component.VISIBLE_CHANGED_PROPERTY, pce.lastEvent.getPropertyName()); 683 c.setVisible(false); 684 assertFalse(c.isVisible()); 685 c.setVisible(true); 686 assertTrue(c.isVisible()); 687 c.setVisible(true); 688 assertTrue(c.isVisible()); 689 } 690 691 694 public void testVisibleIndexOf() { 695 NullComponent parent = new NullComponent(); 696 NullComponent a = new NullComponent(); 697 NullComponent b = new NullComponent(); 698 NullComponent c = new NullComponent(); 699 NullComponent d = new NullComponent(); 700 parent.add(a); 701 parent.add(b); 702 parent.add(c); 703 assertEquals(0, parent.visibleIndexOf(a)); 704 assertEquals(1, parent.visibleIndexOf(b)); 705 assertEquals(2, parent.visibleIndexOf(c)); 706 assertEquals(-1, parent.visibleIndexOf(d)); 707 b.setVisible(false); 708 assertEquals(0, parent.visibleIndexOf(a)); 709 assertEquals(-1, parent.visibleIndexOf(b)); 710 assertEquals(1, parent.visibleIndexOf(c)); 711 assertEquals(-1, parent.visibleIndexOf(d)); 712 a.setVisible(false); 713 assertEquals(-1, parent.visibleIndexOf(a)); 714 assertEquals(-1, parent.visibleIndexOf(b)); 715 assertEquals(0, parent.visibleIndexOf(c)); 716 assertEquals(-1, parent.visibleIndexOf(d)); 717 c.setVisible(false); 718 assertEquals(-1, parent.visibleIndexOf(a)); 719 assertEquals(-1, parent.visibleIndexOf(b)); 720 assertEquals(-1, parent.visibleIndexOf(c)); 721 assertEquals(-1, parent.visibleIndexOf(d)); 722 } 723 } 724 | Popular Tags |