1 29 30 package nextapp.echo2.app.test; 31 32 import java.util.Arrays ; 33 import java.util.List ; 34 35 import nextapp.echo2.app.Alignment; 36 import nextapp.echo2.app.ApplicationInstance; 37 import nextapp.echo2.app.Color; 38 import nextapp.echo2.app.Command; 39 import nextapp.echo2.app.Component; 40 import nextapp.echo2.app.Label; 41 import nextapp.echo2.app.Column; 42 import nextapp.echo2.app.TextField; 43 import nextapp.echo2.app.layout.ColumnLayoutData; 44 import nextapp.echo2.app.update.PropertyUpdate; 45 import nextapp.echo2.app.update.ServerComponentUpdate; 46 import nextapp.echo2.app.update.UpdateManager; 47 import junit.framework.TestCase; 48 49 52 public class UpdateManagerTest extends TestCase { 53 54 57 private static class ExampleCommand implements Command { } 58 59 62 private ColumnApp columnApp; 63 64 67 private UpdateManager manager; 68 69 72 public void setUp() { 73 columnApp = new ColumnApp(); 74 ApplicationInstance.setActive(columnApp); 75 columnApp.doInit(); 76 manager = columnApp.getUpdateManager(); 77 } 78 79 82 public void tearDown() { 83 ApplicationInstance.setActive(null); 84 } 85 86 90 public void testAddChlidToAddedParent() { 91 ServerComponentUpdate[] componentUpdates; 92 Component[] addedChildren; 93 manager.purge(); 94 95 Column column1 = new Column(); 97 columnApp.getColumn().add(column1); 98 99 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 101 assertEquals(1, componentUpdates.length); 102 addedChildren = componentUpdates[0].getAddedChildren(); 103 assertEquals(1, addedChildren.length); 104 assertEquals(0, componentUpdates[0].getRemovedChildren().length); 105 assertEquals(column1, addedChildren[0]); 106 107 column1.add(new Label("A")); 109 110 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 112 assertEquals(1, componentUpdates.length); 113 addedChildren = componentUpdates[0].getAddedChildren(); 114 assertEquals(1, addedChildren.length); 115 assertEquals(0, componentUpdates[0].getRemovedChildren().length); 116 assertEquals(column1, addedChildren[0]); 117 118 Column column2 = new Column(); 120 Label labelB = new Label("B"); 121 column2.add(labelB); 122 columnApp.getColumn().add(column2); 123 124 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 126 assertEquals(1, componentUpdates.length); 127 addedChildren = componentUpdates[0].getAddedChildren(); 128 assertEquals(2, addedChildren.length); 129 assertEquals(0, componentUpdates[0].getRemovedChildren().length); 130 131 List addedChildrenList = Arrays.asList(addedChildren); 132 assertTrue(addedChildrenList.contains(column1)); 133 assertTrue(addedChildrenList.contains(column2)); 134 assertFalse(addedChildrenList.contains(labelB)); 135 } 136 137 140 public void testAddInvisibleComponent() { 141 ServerComponentUpdate[] componentUpdates; 142 143 manager.purge(); 144 145 Label label = new Label("Label1"); 146 label.setVisible(false); 147 columnApp.getColumn().add(label); 148 149 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 150 assertEquals(0, componentUpdates.length); 151 } 152 153 156 public void testApplicationPropertyUpdate() { 157 manager.purge(); 158 columnApp.setFocusedComponent(columnApp.getLabel()); 159 PropertyUpdate propertyUpdate = manager.getServerUpdateManager().getApplicationPropertyUpdate( 160 ApplicationInstance.FOCUSED_COMPONENT_CHANGED_PROPERTY); 161 assertNotNull(propertyUpdate); 162 assertNull(propertyUpdate.getOldValue()); 163 assertEquals(columnApp.getLabel(), propertyUpdate.getNewValue()); 164 } 165 166 172 public void testApplicationPropertyUpdateWithDifferentClientUpdate() { 173 manager.purge(); 174 manager.getClientUpdateManager().setApplicationProperty(ApplicationInstance.FOCUSED_COMPONENT_CHANGED_PROPERTY, 175 columnApp.getColumn()); 176 manager.processClientUpdates(); 177 assertEquals(columnApp.getColumn(), columnApp.getFocusedComponent()); 178 179 columnApp.setFocusedComponent(columnApp.getLabel()); 180 PropertyUpdate propertyUpdate = manager.getServerUpdateManager().getApplicationPropertyUpdate( 181 ApplicationInstance.FOCUSED_COMPONENT_CHANGED_PROPERTY); 182 assertNotNull(propertyUpdate); 183 assertEquals(columnApp.getColumn(), propertyUpdate.getOldValue()); 184 assertEquals(columnApp.getLabel(), propertyUpdate.getNewValue()); 185 } 186 187 192 public void testApplicationPropertyUpdateWithEquivalentClientUpdate() { 193 manager.purge(); 194 manager.getClientUpdateManager().setApplicationProperty(ApplicationInstance.FOCUSED_COMPONENT_CHANGED_PROPERTY, 195 columnApp.getLabel()); 196 manager.processClientUpdates(); 197 assertEquals(columnApp.getLabel(), columnApp.getFocusedComponent()); 198 PropertyUpdate propertyUpdate = manager.getServerUpdateManager().getApplicationPropertyUpdate( 199 ApplicationInstance.FOCUSED_COMPONENT_CHANGED_PROPERTY); 200 assertNull(propertyUpdate); 201 } 202 203 206 public void testCommand() { 207 Command command = new ExampleCommand(); 208 209 manager.purge(); 210 columnApp.enqueueCommand(command); 211 212 assertEquals(1, manager.getServerUpdateManager().getCommands().length); 214 assertEquals(command, manager.getServerUpdateManager().getCommands()[0]); 215 216 manager.getServerUpdateManager().processFullRefresh(); 217 218 assertEquals(1, manager.getServerUpdateManager().getCommands().length); 220 assertEquals(command, manager.getServerUpdateManager().getCommands()[0]); 221 222 manager.purge(); 223 224 assertEquals(0, manager.getServerUpdateManager().getCommands().length); 226 227 manager.getServerUpdateManager().processFullRefresh(); 228 columnApp.enqueueCommand(command); 229 230 assertEquals(1, manager.getServerUpdateManager().getCommands().length); 232 assertEquals(command, manager.getServerUpdateManager().getCommands()[0]); 233 } 234 235 238 public void testInvisibleHierarchyUpdate() { 239 ServerComponentUpdate[] componentUpdates; 240 columnApp.getColumn().setVisible(false); 241 242 manager.purge(); 243 244 Column column = new Column(); 245 columnApp.getColumn().add(column); 246 Label label1 = new Label("Label1"); 247 column.add(label1); 248 Label label2 = new Label("Label2"); 249 label2.setVisible(false); 250 column.add(label2); 251 252 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 253 assertEquals(0, componentUpdates.length); 254 } 255 256 261 public void testLayoutDataUpdate() { 262 ServerComponentUpdate[] componentUpdates; 263 ColumnLayoutData columnLayoutData; 264 265 manager.purge(); 266 267 Column column = new Column(); 269 columnApp.getColumn().add(column); 270 Label label1 = new Label("Label1"); 271 column.add(label1); 272 Label label2 = new Label("Label2"); 273 column.add(label2); 274 label2.setLayoutData(new ColumnLayoutData()); 275 276 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 277 assertEquals(1, componentUpdates.length); 278 assertEquals(false, componentUpdates[0].hasUpdatedLayoutDataChildren()); 279 280 manager.purge(); 281 282 columnLayoutData = new ColumnLayoutData(); 283 columnLayoutData.setAlignment(new Alignment(Alignment.CENTER, Alignment.DEFAULT)); 284 label1.setLayoutData(columnLayoutData); 285 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 286 assertEquals(1, componentUpdates.length); 287 assertEquals(column, componentUpdates[0].getParent()); 288 assertFalse(componentUpdates[0].hasAddedChildren()); 289 assertFalse(componentUpdates[0].hasRemovedChildren()); 290 assertFalse(componentUpdates[0].hasUpdatedProperties()); 291 assertTrue(componentUpdates[0].hasUpdatedLayoutDataChildren()); 292 293 Component[] components = componentUpdates[0].getUpdatedLayoutDataChildren(); 294 assertEquals(1, components.length); 295 assertEquals(label1, components[0]); 296 } 297 298 299 304 public void testPropertyUpdate() { 305 ServerComponentUpdate[] componentUpdates; 306 manager.purge(); 308 309 columnApp.getLabel().setText("Hi there"); 311 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 312 assertEquals(columnApp.getLabel(), componentUpdates[0].getParent()); 313 assertEquals(1, componentUpdates.length); 314 assertEquals(0, componentUpdates[0].getAddedChildren().length); 315 assertEquals(0, componentUpdates[0].getRemovedChildren().length); 316 317 String [] updatedPropertyNames = componentUpdates[0].getUpdatedPropertyNames(); 318 assertEquals(1, updatedPropertyNames.length); 319 assertEquals(Label.PROPERTY_TEXT, updatedPropertyNames[0]); 320 PropertyUpdate propertyUpdate = componentUpdates[0].getUpdatedProperty(Label.PROPERTY_TEXT); 321 assertEquals("Label", propertyUpdate.getOldValue()); 322 assertEquals("Hi there", propertyUpdate.getNewValue()); 323 324 columnApp.getColumn().remove(columnApp.getLabel()); 326 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 327 assertEquals(1, componentUpdates.length); 328 assertEquals(0, componentUpdates[0].getUpdatedPropertyNames().length); 329 assertEquals(0, componentUpdates[0].getAddedChildren().length); 330 } 331 332 337 public void testPropertyUpdateCancellation1() { 338 TextField textField = new TextField(); 339 columnApp.getColumn().add(textField); 340 341 manager.purge(); 342 manager.getClientUpdateManager().setComponentProperty(textField, TextField.TEXT_CHANGED_PROPERTY, "a user typed this."); 343 manager.processClientUpdates(); 344 345 ServerComponentUpdate[] componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 346 assertEquals(0, componentUpdates.length); 347 } 348 349 356 public void testPropertyUpdateCancellation2() { 357 TextField textField = new TextField(); 358 columnApp.getColumn().add(textField); 359 360 manager.purge(); 361 textField.setBackground(Color.BLUE); 362 manager.getClientUpdateManager().setComponentProperty(textField, TextField.TEXT_CHANGED_PROPERTY, "a user typed this."); 363 manager.processClientUpdates(); 364 365 ServerComponentUpdate[] componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 366 assertEquals(1, componentUpdates.length); 367 PropertyUpdate backgroundUpdate = 368 componentUpdates[0].getUpdatedProperty(TextField.PROPERTY_BACKGROUND); 369 assertNotNull(backgroundUpdate); 370 assertEquals(Color.BLUE, backgroundUpdate.getNewValue()); 371 assertNull(componentUpdates[0].getUpdatedProperty(TextField.TEXT_CHANGED_PROPERTY)); 372 } 373 374 381 public void testPropertyUpdateCancellation3() { 382 TextField textField = new TextField(); 383 columnApp.getColumn().add(textField); 384 385 manager.purge(); 386 textField.setText("first the application set it to this."); 387 manager.getClientUpdateManager().setComponentProperty(textField, TextField.TEXT_CHANGED_PROPERTY, "a user typed this."); 388 manager.processClientUpdates(); 389 390 ServerComponentUpdate[] componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 391 assertEquals(1, componentUpdates.length); 392 assertFalse(componentUpdates[0].hasUpdatedProperties()); 393 } 394 395 398 public void testPurge() { 399 assertFalse(manager.getServerUpdateManager().getComponentUpdates().length == 0); 400 manager.purge(); 401 assertTrue(manager.getServerUpdateManager().getComponentUpdates().length == 0); 402 } 403 404 407 public void testRemove1() { 408 Column column = new Column(); 409 Label label = new Label(); 410 column.add(label); 411 columnApp.getColumn().add(column); 412 manager.purge(); 413 414 columnApp.getColumn().remove(column); 415 416 ServerComponentUpdate[] componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 417 assertEquals(1, componentUpdates.length); 418 419 assertEquals(true, componentUpdates[0].hasRemovedChildren()); 420 assertEquals(true, componentUpdates[0].hasRemovedDescendants()); 421 422 Component[] removedChildren = componentUpdates[0].getRemovedChildren(); 423 assertEquals(1, removedChildren.length); 424 assertEquals(column, removedChildren[0]); 425 426 Component[] removedDescendants = componentUpdates[0].getRemovedDescendants(); 427 assertEquals(1, removedDescendants.length); 428 assertEquals(label, removedDescendants[0]); 429 } 430 431 453 public void testRemove2() { 454 Column column1 = new Column(); 455 Column column2 = new Column(); 456 column1.add(column2); 457 Label label = new Label(); 458 column2.add(label); 459 columnApp.getColumn().add(column1); 460 manager.purge(); 461 462 column1.remove(column2); 463 columnApp.getColumn().remove(column1); 464 465 ServerComponentUpdate[] componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 466 assertEquals(1, componentUpdates.length); 467 468 assertEquals(true, componentUpdates[0].hasRemovedChildren()); 469 assertEquals(true, componentUpdates[0].hasRemovedDescendants()); 470 471 Component[] removedChildren = componentUpdates[0].getRemovedChildren(); 472 assertEquals(1, removedChildren.length); 473 assertEquals(column1, removedChildren[0]); 474 475 Component[] removedDescendants = componentUpdates[0].getRemovedDescendants(); 476 assertEquals(2, removedDescendants.length); 477 assertTrue(removedDescendants[0].equals(column2) || removedDescendants[1].equals(column2)); 478 assertTrue(removedDescendants[0].equals(label) || removedDescendants[1].equals(label)); 479 } 480 481 484 public void testUpdateSorting1() { 485 ServerComponentUpdate[] componentUpdates; 486 manager.purge(); 487 488 columnApp.getLabel().setBackground(Color.BLUE); 489 columnApp.getContentPane().setBackground(Color.RED); 490 columnApp.getColumn().setBackground(Color.GREEN); 491 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 492 493 assertEquals(3, componentUpdates.length); 494 assertEquals(columnApp.getContentPane(), componentUpdates[0].getParent()); 495 assertEquals(columnApp.getColumn(), componentUpdates[1].getParent()); 496 assertEquals(columnApp.getLabel(), componentUpdates[2].getParent()); 497 } 498 499 502 public void testUpdateSorting2() { 503 ServerComponentUpdate[] componentUpdates; 504 Column column2 = new Column(); 505 columnApp.getColumn().add(column2); 506 Label label2 = new Label(); 507 column2.add(label2); 508 manager.purge(); 509 510 columnApp.getLabel().setBackground(Color.BLUE); 511 columnApp.getContentPane().setBackground(Color.RED); 512 columnApp.getColumn().setBackground(Color.GREEN); 513 label2.setBackground(Color.YELLOW); 514 column2.setBackground(Color.ORANGE); 515 516 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 517 assertEquals(5, componentUpdates.length); 518 assertEquals(columnApp.getContentPane(), componentUpdates[0].getParent()); 519 assertEquals(columnApp.getColumn(), componentUpdates[1].getParent()); 520 assertTrue(columnApp.getLabel().equals(componentUpdates[2].getParent()) 521 || columnApp.getLabel().equals(componentUpdates[3].getParent())); 522 assertTrue(column2.equals(componentUpdates[2].getParent()) 523 || column2.equals(componentUpdates[3].getParent())); 524 assertEquals(label2, componentUpdates[4].getParent()); 525 } 526 527 530 public void testVisibleUpdate() { 531 ServerComponentUpdate[] componentUpdates; 532 533 manager.purge(); 534 535 Column column = new Column(); 537 columnApp.getColumn().add(column); 538 Label label1 = new Label("Label1"); 539 column.add(label1); 540 Label label2 = new Label("Label2"); 541 label2.setVisible(false); 542 column.add(label2); 543 label2.setLayoutData(new ColumnLayoutData()); 544 545 manager.purge(); 546 547 label1.setVisible(false); 548 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 549 assertEquals(1, componentUpdates.length); 550 assertEquals(column, componentUpdates[0].getParent()); 551 assertFalse(componentUpdates[0].hasAddedChildren()); 552 assertTrue(componentUpdates[0].hasRemovedChildren()); 553 assertFalse(componentUpdates[0].hasUpdatedProperties()); 554 assertFalse(componentUpdates[0].hasUpdatedLayoutDataChildren()); 555 556 Component[] components = componentUpdates[0].getRemovedChildren(); 557 assertEquals(1, components.length); 558 assertEquals(label1, components[0]); 559 560 manager.purge(); 561 562 label2.setVisible(true); 563 componentUpdates = manager.getServerUpdateManager().getComponentUpdates(); 564 assertEquals(1, componentUpdates.length); 565 assertEquals(column, componentUpdates[0].getParent()); 566 assertTrue(componentUpdates[0].hasAddedChildren()); 567 assertFalse(componentUpdates[0].hasRemovedChildren()); 568 assertFalse(componentUpdates[0].hasUpdatedProperties()); 569 assertFalse(componentUpdates[0].hasUpdatedLayoutDataChildren()); 570 571 components = componentUpdates[0].getAddedChildren(); 572 assertEquals(1, components.length); 573 assertEquals(label2, components[0]); 574 575 label1.setVisible(true); 576 } 577 } 578 | Popular Tags |