1 19 package org.openide; 20 21 22 import org.netbeans.junit.*; 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 import org.netbeans.junit.NbTestCase; 26 import org.netbeans.junit.NbTestSuite; 27 28 import java.awt.Component ; 29 import java.util.*; 30 import javax.swing.*; 31 import javax.swing.JLabel ; 32 import javax.swing.event.ChangeListener ; 33 import org.netbeans.junit.NbTestCase; 34 import org.openide.util.*; 35 import org.openide.util.HelpCtx; 36 37 40 public class WizardDescTest extends NbTestCase { 41 42 43 public WizardDescTest (String name) { 44 super(name); 45 } 46 47 public static void main(String [] args) { 48 junit.textui.TestRunner.run (new NbTestSuite (WizardDescTest.class)); 49 System.exit (0); 50 } 51 52 WizardDescriptor wd; 53 String exceptedValue; 54 55 protected final void setUp () { 56 WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2]; 57 panels[0] = new Panel("first panel"); 58 panels[1] = new Panel("second panel"); 59 wd = new WizardDescriptor(panels); 60 wd.addPropertyChangeListener(new Listener ()); 61 java.awt.Dialog d = DialogDisplayer.getDefault().createDialog (wd); 62 } 64 65 public boolean runInEQ () { 66 return true; 67 } 68 69 public void testNextOption () throws Exception { 70 exceptedValue = "NEXT_OPTION"; 71 log ("Do click Next button."); 72 wd.doNextClick (); 73 74 assertEquals ("Closed with next option.", WizardDescriptor.NEXT_OPTION, wd.getValue ()); 75 } 76 77 public void testPreviousOption () throws Exception { 78 exceptedValue = "NEXT_OPTION"; 79 log ("Do click Next button."); 80 wd.doNextClick (); 81 82 exceptedValue = "PREVIOUS_OPTION"; 83 log ("Do click Previous button."); 84 wd.doPreviousClick (); 85 86 assertEquals ("Closed with previous option. \n (failed because PREVIOUS_OPTION is replaced with NEXT_OPTION by WD.updateState())", WizardDescriptor.PREVIOUS_OPTION, wd.getValue ()); 88 } 89 90 public void testFinishOption () throws Exception { 91 exceptedValue = "NEXT_OPTION"; 92 log ("Do click Next button."); 93 wd.doNextClick (); 94 95 exceptedValue = "FINISH_OPTION"; 96 log ("Do click Finish button."); 97 wd.doFinishClick (); 98 99 assertEquals ("Closed with finish option.", WizardDescriptor.FINISH_OPTION, wd.getValue ()); 100 } 101 102 public void testCancelOption () throws Exception { 103 exceptedValue = "NEXT_OPTION"; 104 log ("Do click Next button."); 105 wd.doNextClick (); 106 107 exceptedValue = "CANCEL_OPTION"; 108 log ("Do click Cancel button."); 109 wd.doCancelClick (); 110 111 assertEquals ("Closed with cancel option.", WizardDescriptor.CANCEL_OPTION, wd.getValue ()); 112 } 113 114 public void testNextOptionWhenLazyValidationFails () throws Exception { 115 Panel panels[] = new Panel[3]; 116 117 class MyPanel extends Panel implements WizardDescriptor.ValidatingPanel { 118 public String validateMsg; 119 public String failedMsg; 120 121 public MyPanel () { 122 super ("enhanced panel"); 123 } 124 125 public void validate () throws WizardValidationException { 126 if (validateMsg != null) { 127 failedMsg = validateMsg; 128 throw new WizardValidationException (null, "MyPanel.validate() failed.", validateMsg); 129 } 130 return; 131 } 132 } 133 134 class MyFinishPanel extends MyPanel implements WizardDescriptor.FinishablePanel { 135 public boolean isFinishPanel () { 136 return true; 137 } 138 } 139 140 MyPanel mp = new MyPanel (); 141 MyFinishPanel mfp = new MyFinishPanel (); 142 panels[0] = mp; 143 panels[1] = mfp; 144 panels[2] = new Panel ("Last one"); 145 wd = new WizardDescriptor(panels); 146 147 assertNull ("Component has not been yet initialized", panels[1].component); 148 mp.failedMsg = null; 149 mp.validateMsg = "xtest-fail-without-msg"; 150 wd.doNextClick (); 151 assertEquals ("The lazy validation failed on Next.", mp.validateMsg, mp.failedMsg); 152 assertNull ("The lazy validation failed, still no initialiaation", panels[1].component); 153 assertNull ("The lazy validation failed, still no initialiaation", panels[2].component); 154 mp.failedMsg = null; 155 mp.validateMsg = null; 156 wd.doNextClick (); 157 assertNull ("Validation on Next passes", mp.failedMsg); 158 assertNotNull ("Now we switched to another panel", panels[1].component); 159 assertNull ("The lazy validation failed, still no initialiaation", panels[2].component); 160 161 Object state = wd.getValue(); 163 mfp.validateMsg = "xtest-fail-without-msg"; 164 mfp.failedMsg = null; 165 wd.doFinishClick(); 166 assertEquals ("The lazy validation failed on Finish.", mfp.validateMsg, mfp.failedMsg); 167 assertNull ("The validation failed, still no initialiaation", panels[2].component); 168 assertEquals ("State has not changed", state, wd.getValue ()); 169 170 mfp.validateMsg = null; 171 mfp.failedMsg = null; 172 wd.doFinishClick (); 173 assertNull ("Validation on Finish passes", mfp.failedMsg); 174 assertNull ("Finish was clicked, no initialization either", panels[2].component); 175 assertEquals ("The state is finish", WizardDescriptor.FINISH_OPTION, wd.getValue ()); 176 } 177 178 public void testDynamicallyEnabledFinish () throws Exception { 179 WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel [2]; 180 181 class MaybeFinishPanel implements WizardDescriptor.Panel, WizardDescriptor.FinishablePanel { 182 private JLabel component; 183 private String text; 184 185 public boolean isValid = true; 186 public boolean isFinishPanel = true; 187 188 public MaybeFinishPanel () { 189 text = "maybe finish panel"; 190 } 191 192 public boolean isFinishPanel () { 193 return isFinishPanel; 194 } 195 196 public boolean isValid () { 197 return isValid; 198 } 199 200 public MaybeFinishPanel (String text) { 201 this.text = text; 202 } 203 204 public Component getComponent() { 205 if (component == null) { 206 component = new JLabel (text); 207 } 208 return component; 209 } 210 211 public void addChangeListener(ChangeListener l) { 212 } 213 214 public HelpCtx getHelp() { 215 return null; 216 } 217 218 public void readSettings(Object settings) { 219 } 220 221 public void removeChangeListener(ChangeListener l) { 222 } 223 224 public void storeSettings(Object settings) { 225 } 226 } 227 228 MaybeFinishPanel firstPanel = new MaybeFinishPanel (); 229 Panel normalPanel = new Panel ("normal panel"); 230 panels[0] = firstPanel; 231 panels[1] = normalPanel; 232 wd = new WizardDescriptor(panels); 233 234 firstPanel.isValid = false; 236 firstPanel.isFinishPanel = false; 237 wd.updateState (); 238 assertFalse ("Panel is not valid and Next button is disabled.", wd.isNextEnabled ()); 239 assertFalse ("Panel is not valid and Finish button is disabled as well.", wd.isFinishEnabled ()); 240 241 firstPanel.isValid = true; 244 wd.updateState (); 245 assertTrue ("Panel is valid then Next button is enabled.", wd.isNextEnabled ()); 246 assertFalse ("Panel doesn't implement WD.FinishPanel.", panels[0] instanceof WizardDescriptor.FinishPanel); 247 assertFalse ("Panel is valid but Finish button is disabled because not FinishPanel.", wd.isFinishEnabled ()); 248 249 firstPanel.isValid = true; 253 firstPanel.isFinishPanel = true; 254 wd.updateState (); 255 assertTrue ("Panel is valid then Next button is enabled.", wd.isNextEnabled ()); 256 assertFalse ("Panel doesn't implement WD.FinishPanel.", panels[0] instanceof WizardDescriptor.FinishPanel); 257 assertTrue ("Panel implements WD.FinishablePanel.", panels[0] instanceof WizardDescriptor.FinishablePanel); 258 assertTrue ("Panel is enabled because implements FinishablePanel.", wd.isFinishEnabled ()); 259 } 260 261 public void testGetInstantiatedObjectsWhenFinished () { 262 boolean exceptionCaught = false; 263 try { 264 wd.getInstantiatedObjects (); 265 } catch (IllegalStateException ise) { 266 exceptionCaught = true; 267 } 268 if (!exceptionCaught) { 269 fail ("Call getInstantiatedObjects() only on finished wizard, not on start."); 270 exceptionCaught = false; 271 } 272 273 log ("Do click Next button."); 274 wd.doNextClick (); 275 try { 276 wd.getInstantiatedObjects (); 277 } catch (IllegalStateException ise) { 278 exceptionCaught = true; 279 } 280 if (!exceptionCaught) { 281 fail ("Call getInstantiatedObjects() only on finished wizard, not on next."); 282 exceptionCaught = false; 283 } 284 285 286 log ("Do click Cancel button."); 287 wd.doFinishClick (); 288 try { 289 wd.getInstantiatedObjects (); 290 } catch (IllegalStateException ise) { 291 fail ("Called getInstantiatedObjects() on finished wizard."); 292 } 293 } 294 295 public void testGetInstantiatedObjectsWhenCanceled () { 296 boolean exceptionCaught = false; 297 try { 298 wd.getInstantiatedObjects (); 299 } catch (IllegalStateException ise) { 300 exceptionCaught = true; 301 } 302 if (!exceptionCaught) { 303 fail ("Call getInstantiatedObjects() only on finished wizard, not on start."); 304 exceptionCaught = false; 305 } 306 307 log ("Do click Next button."); 308 wd.doNextClick (); 309 try { 310 wd.getInstantiatedObjects (); 311 } catch (IllegalStateException ise) { 312 exceptionCaught = true; 313 } 314 if (!exceptionCaught) { 315 fail ("Call getInstantiatedObjects() only on finished wizard, not on next."); 316 exceptionCaught = false; 317 } 318 319 320 log ("Do click Cancel button."); 321 wd.doCancelClick (); 322 try { 323 wd.getInstantiatedObjects (); 324 } catch (IllegalStateException ise) { 325 exceptionCaught = true; 326 } 327 if (!exceptionCaught) { 328 fail ("Call getInstantiatedObjects() only on finished wizard, not when wizard canceled."); 329 exceptionCaught = false; 330 } 331 } 332 333 public class Panel implements WizardDescriptor.Panel, WizardDescriptor.FinishPanel { 334 private JLabel component; 335 private String text; 336 public Panel(String text) { 337 this.text = text; 338 } 339 340 public Component getComponent() { 341 if (component == null) { 342 component = new JLabel (text); 343 } 344 return component; 345 } 346 347 public void addChangeListener(ChangeListener l) { 348 } 349 350 public HelpCtx getHelp() { 351 return null; 352 } 353 354 public boolean isValid() { 355 return true; 356 } 357 358 public void readSettings(Object settings) { 359 log ("readSettings of panel: " + text + " [time: " + System.currentTimeMillis () + 360 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 361 } 362 363 public void removeChangeListener(ChangeListener l) { 364 } 365 366 public void storeSettings(Object settings) { 367 log ("storeSettings of panel: " + text + " [time: " + System.currentTimeMillis () + 368 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 369 if (exceptedValue != null) { 370 assertEquals ("WD.getValue() returns excepted value.", exceptedValue, handleValue (wd.getValue ())); 371 } 372 } 373 374 } 375 376 public class Listener implements java.beans.PropertyChangeListener { 377 378 public void propertyChange(java.beans.PropertyChangeEvent propertyChangeEvent) { 379 if (WizardDescriptor.PROP_VALUE.equals(propertyChangeEvent.getPropertyName ())) { 380 log("propertyChange [time: " + System.currentTimeMillis () + 381 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 382 383 } 384 } 385 386 } 387 388 public String handleValue (Object val) { 389 if (val == null) return "NULL"; 390 if (val instanceof String ) return (String ) val; 391 if (WizardDescriptor.FINISH_OPTION.equals (val)) return "FINISH_OPTION"; 392 if (WizardDescriptor.CANCEL_OPTION.equals (val)) return "CANCEL_OPTION"; 393 if (WizardDescriptor.CLOSED_OPTION.equals (val)) return "CLOSED_OPTION"; 394 if (val instanceof JButton) { 395 JButton butt = (JButton) val; 396 ResourceBundle b = NbBundle.getBundle ("org.openide.Bundle"); if (b.getString ("CTL_NEXT").equals (butt.getText ())) return "NEXT_OPTION"; 398 if (b.getString ("CTL_PREVIOUS").equals (butt.getText ())) return "NEXT_PREVIOUS"; 399 if (b.getString ("CTL_FINISH").equals (butt.getText ())) return "FINISH_OPTION"; 400 if (b.getString ("CTL_CANCEL").equals (butt.getText ())) return "CANCEL_OPTION"; 401 } 402 return "UNKNOWN OPTION: " + val; 403 } 404 } 405 | Popular Tags |