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 43 public class WizardDescriptorOrderTest extends NbTestCase { 44 45 46 public WizardDescriptorOrderTest (String name) { 47 super(name); 48 } 49 50 public static void main(String [] args) { 51 junit.textui.TestRunner.run (new NbTestSuite (WizardDescTest.class)); 52 System.exit (0); 53 } 54 55 WizardDescriptor wd; 56 TestPanel[] panels; 57 58 int[] readSettingsCalls; 60 int[] storeSettingsCalls; 61 62 boolean checkStoreBeforeNext; 64 boolean checkStoreOldeBeforeReadNew; 65 boolean checkPCH2FinishOption; 66 67 protected final void setUp () { 68 panels = new TestPanel[] {new TestPanel (0), new TestPanel (1)}; 69 readSettingsCalls = new int[] {0, 0}; 70 storeSettingsCalls = new int[] {0, 0}; 71 wd = new WizardDescriptor (new TestIterator (panels[0], panels[1])); 72 wd.addPropertyChangeListener(new Listener ()); 73 java.awt.Dialog d = DialogDisplayer.getDefault().createDialog (wd); 74 76 checkStoreBeforeNext = false; 77 checkStoreOldeBeforeReadNew = false; 78 checkPCH2FinishOption = false; 79 } 80 81 public void testReadSettingsOnFirstPanel () throws Exception { 82 log ("Wizard has been initialized."); 83 assertTrue ("WD.P.readSettings on the first panel has been called.", readSettingsCalls[0] > 0); 84 } 85 86 public void testOrderNextPanelAndStoreSettings () throws Exception { 87 checkStoreBeforeNext = true; 88 log ("Do click Next button."); 89 wd.doNextClick (); 90 } 91 92 public void testStoreOldBeforeReadNew () throws Exception { 93 checkStoreOldeBeforeReadNew = true; 94 log ("Do click Next button."); 95 wd.doNextClick (); 96 } 97 98 public void testStoreLastBeforePCH2Finish () throws Exception { 99 log ("Do click Next button."); 100 wd.doNextClick (); 101 checkPCH2FinishOption = true; 102 log ("Do click Finish button."); 103 wd.doFinishClick (); 104 } 105 106 public class Listener implements java.beans.PropertyChangeListener { 107 108 public void propertyChange(java.beans.PropertyChangeEvent propertyChangeEvent) { 109 if (WizardDescriptor.PROP_VALUE.equals(propertyChangeEvent.getPropertyName ())) { 110 log("propertyChange [time: " + System.currentTimeMillis () + 111 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 112 113 if (checkPCH2FinishOption && WizardDescriptor.FINISH_OPTION.equals (propertyChangeEvent.getNewValue ())) { 114 assertTrue ("WD.P.storeSettings on the last panel has been called before propertyChangeEvent to FINISH_OPTION was fired.", storeSettingsCalls[1] > 0); 115 } 116 } 117 } 118 119 } 120 121 private final class TestPanel implements WizardDescriptor.Panel { 122 123 private final int x; 124 125 public TestPanel(int x) { 126 this.x = x; 127 } 128 129 public void readSettings(Object settings) { 130 readSettingsCalls[x]++; 131 log ("readSettings of panel: " + x + " [time: " + System.currentTimeMillis () + 132 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 133 } 134 135 public void addChangeListener(ChangeListener l) {} 136 137 public void storeSettings(Object settings) { 138 if (checkStoreOldeBeforeReadNew && (x > 0)) { 139 assertTrue ("WD.P.storeSettings on the previous panel has been called before WD.P.readSettings on next panel.", readSettingsCalls[x - 1] > 0); 140 } 141 storeSettingsCalls[x] ++; 142 log ("storeSettings of panel: " + x + " [time: " + System.currentTimeMillis () + 143 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 144 } 145 146 public void removeChangeListener(ChangeListener l) {} 147 148 public boolean isValid() { 149 return true; 150 } 151 public HelpCtx getHelp() { 152 return null; 153 } 154 155 public Component getComponent() { 156 return new JLabel ("panel #" + x); 157 } 158 } 159 160 private final class TestIterator implements WizardDescriptor.Iterator { 161 private final TestPanel panel1, panel2; 162 private int which = 0; 163 public TestIterator(TestPanel panel1, TestPanel panel2) { 164 this.panel1 = panel1; 165 this.panel2 = panel2; 166 } 167 public boolean hasNext () { 168 return which == 0; 169 } 170 public WizardDescriptor.Panel current() { 171 log ("current: " + name()); 172 TestPanel currentPanel = which == 0 ? panel1 : panel2; 173 return currentPanel; 174 } 175 public void nextPanel() { 176 if (checkStoreBeforeNext) { 177 assertTrue ("WD.P.storeSettings on the previous panel has been called before WD.I.nextPanel.", storeSettingsCalls[which] > 0); 178 } 179 which ++; 180 } 181 public void removeChangeListener(ChangeListener l) {} 182 public boolean hasPrevious() { 183 return which > 0; 184 } 185 public void previousPanel() { 186 which --;; 187 } 188 public String name() { 189 return which == 0 ? "First Panel" : "Second Panel"; 190 } 191 public void addChangeListener(ChangeListener l) {} 192 } 193 194 public String handleValue (Object val) { 195 if (val == null) return "NULL"; 196 if (val instanceof String ) return (String ) val; 197 if (WizardDescriptor.FINISH_OPTION.equals (val)) return "FINISH_OPTION"; 198 if (WizardDescriptor.CANCEL_OPTION.equals (val)) return "CANCEL_OPTION"; 199 if (WizardDescriptor.CLOSED_OPTION.equals (val)) return "CLOSED_OPTION"; 200 if (val instanceof JButton) { 201 JButton butt = (JButton) val; 202 ResourceBundle b = NbBundle.getBundle ("org.openide.Bundle"); if (b.getString ("CTL_NEXT").equals (butt.getText ())) return "NEXT_OPTION"; 204 if (b.getString ("CTL_PREVIOUS").equals (butt.getText ())) return "NEXT_PREVIOUS"; 205 if (b.getString ("CTL_FINISH").equals (butt.getText ())) return "FINISH_OPTION"; 206 if (b.getString ("CTL_CANCEL").equals (butt.getText ())) return "CANCEL_OPTION"; 207 } 208 return "UNKNOWN OPTION: " + val; 209 } 210 211 } 212 | Popular Tags |