1 29 30 package nextapp.echo2.app.test; 31 32 import nextapp.echo2.app.ApplicationInstance; 33 import nextapp.echo2.app.Component; 34 import nextapp.echo2.app.Label; 35 import nextapp.echo2.app.Column; 36 import nextapp.echo2.app.Window; 37 import junit.framework.TestCase; 38 39 42 public class ApplicationInstanceTest extends TestCase { 43 44 private class RegistrationTestComponent extends Component { 45 int initCount = 0; 46 int disposeCount = 0; 47 48 public void dispose() { 49 super.dispose(); 50 ++disposeCount; 51 } 52 53 public void init() { 54 super.init(); 55 ++initCount; 56 } 57 } 58 59 private class ValidatingLabel extends Label { 60 61 boolean valid = false; 62 63 public void invalidate() { 64 valid = false; 65 } 66 67 public void validate() { 68 super.validate(); 69 valid = true; 70 } 71 } 72 73 76 public void testActivation() { 77 assertNull(ApplicationInstance.getActive()); 78 HelloWorldApp app = new HelloWorldApp(); 79 assertNull(ApplicationInstance.getActive()); 80 ApplicationInstance.setActive(app); 81 assertTrue(app == ApplicationInstance.getActive()); 82 ApplicationInstance.setActive(null); 83 assertNull(ApplicationInstance.getActive()); 84 } 85 86 89 public void testContext() { 90 HelloWorldApp app = new HelloWorldApp(); 91 assertNull(app.getContextProperty("alpha")); 92 app.setContextProperty("alpha", "bravo"); 93 assertEquals("bravo", app.getContextProperty("alpha")); 94 app.setContextProperty("alpha", null); 95 assertNull(app.getContextProperty("alpha")); 96 } 97 98 102 public void testRegistration() { 103 ColumnApp columnApp = new ColumnApp(); 104 ApplicationInstance.setActive(columnApp); 105 106 Window window = columnApp.doInit(); 107 assertTrue(window.isRegistered()); 108 assertTrue(columnApp.getColumn().isRegistered()); 109 Label label = new Label(); 110 assertFalse(label.isRegistered()); 111 columnApp.getColumn().add(label); 112 assertTrue(label.isRegistered()); 113 columnApp.getColumn().remove(label); 114 assertFalse(label.isRegistered()); 115 columnApp.getColumn().add(label); 116 assertTrue(label.isRegistered()); 117 columnApp.getContentPane().remove(columnApp.getColumn()); 118 assertFalse(label.isRegistered()); 119 120 ApplicationInstance.setActive(null); 121 } 122 123 127 public void testRegistrationLifecycle() { 128 ColumnApp columnApp = new ColumnApp(); 129 ApplicationInstance.setActive(columnApp); 130 columnApp.doInit(); 131 Column column = columnApp.getColumn(); 132 133 RegistrationTestComponent rtc = new RegistrationTestComponent(); 134 135 assertEquals(0, rtc.initCount); 136 assertEquals(0, rtc.disposeCount); 137 138 column.add(rtc); 139 140 assertEquals(1, rtc.initCount); 141 assertEquals(0, rtc.disposeCount); 142 143 column.remove(rtc); 144 145 assertEquals(1, rtc.initCount); 146 assertEquals(1, rtc.disposeCount); 147 148 ApplicationInstance.setActive(null); 149 } 150 151 156 public void testRegistrationLifecycleInitialHierarchy() { 157 final RegistrationTestComponent rtc = new RegistrationTestComponent(); 158 159 assertEquals(0, rtc.initCount); 160 assertEquals(0, rtc.disposeCount); 161 162 ColumnApp columnApp = new ColumnApp(){ 163 164 public Window init() { 165 Window window = super.init(); 166 getColumn().add(rtc); 167 return window; 168 } 169 }; 170 ApplicationInstance.setActive(columnApp); 171 columnApp.doInit(); 172 173 assertEquals(1, rtc.initCount); 174 assertEquals(0, rtc.disposeCount); 175 176 ApplicationInstance.setActive(null); 177 } 178 179 183 public void testValidation() { 184 final ValidatingLabel validatingLabel = new ValidatingLabel(); 185 ColumnApp app = new ColumnApp() { 186 public Window init() { 187 Window window = super.init(); 188 getColumn().add(validatingLabel); 189 return window; 190 } 191 }; 192 193 assertFalse(validatingLabel.valid); 194 195 ApplicationInstance.setActive(app); 196 197 app.doInit(); 198 199 assertTrue(validatingLabel.valid); 201 202 validatingLabel.invalidate(); 203 assertFalse(validatingLabel.valid); 204 205 app.getUpdateManager().processClientUpdates(); 207 assertTrue(validatingLabel.valid); 208 209 ApplicationInstance.setActive(null); 210 } 211 } 212 | Popular Tags |