1 19 20 package org.netbeans.api.options; 21 27 28 import java.awt.Frame ; 29 import java.awt.Dialog ; 30 import java.awt.event.ActionEvent ; 31 import java.lang.reflect.InvocationTargetException ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import javax.swing.JButton ; 35 import javax.swing.JDialog ; 36 import javax.swing.SwingUtilities ; 37 import org.netbeans.junit.NbTestCase; 38 import org.openide.DialogDescriptor; 39 import org.openide.DialogDisplayer; 40 import org.openide.NotifyDescriptor; 41 42 46 public class OptionsDisplayerOpenTest extends NbTestCase { 47 private static TestDisplayer displayer = new TestDisplayer(); 48 private static final int REPEATER = 10; 49 Logger log; 50 static { 51 String [] layers = new String [] {"org/netbeans/api/options/mf-layer.xml"}; Object [] instances = new Object [] {displayer}; 53 IDEInitializer.setup(layers,instances); 54 } 55 56 public OptionsDisplayerOpenTest(String testName) { 57 super(testName); 58 } 59 60 protected void setUp() throws Exception { 61 log = Logger.getLogger("[Test - " + getName() + "]"); 62 } 63 64 protected void tearDown() throws Exception { 65 } 66 69 public void testGetDefault() { 70 assertNotNull(OptionsDisplayer.getDefault()); 71 } 72 73 public void testOpenFromWorkerThread() { 74 openOpen(null); 75 openOpen("Registered"); 76 openOpen("Unknown"); 77 openCloseOpen(null); 78 openCloseOpen("Registered"); 79 openCloseOpen("Unknown"); 80 } 81 82 public void testOpenFromAWT() throws Exception { 83 SwingUtilities.invokeAndWait(new Runnable () { 84 public void run() { 85 testOpenFromWorkerThread(); 86 } 87 }); 88 } 89 90 public void testOpenFromMixedThreads() throws Exception { 91 testOpenFromWorkerThread(); 92 testOpenFromAWT(); 93 testOpenFromWorkerThread(); 94 testOpenFromAWT(); 95 } 96 97 public void openOpen(String categoryId) { 98 for (int i = 0; i < REPEATER; i++) { 99 if (categoryId == null) { 100 open(true); 101 open(false); 102 close(); 103 open(null, false); 105 close(); 106 } else { 107 if ("Registered".equals(categoryId)) { 108 open(categoryId, true); 109 open(categoryId, false); 110 close(); 111 } else { 112 open(categoryId, false); 113 open(categoryId, false); 114 close(); 115 } 116 } 117 } 118 } 119 120 public void openCloseOpen(String categoryId) { 121 for (int i = 0; i < REPEATER; i++) { 122 if (categoryId == null) { 123 open(true); 124 close(); 125 open(true); 126 close(); 127 } else { 128 if ("Registered".equals(categoryId)) { 129 open(categoryId, true); 130 close(); 131 open(categoryId, true); 132 close(); 133 } else { 134 open(categoryId, false); 135 close(); 136 open(categoryId, false); 137 close(); 138 } 139 } 140 } 141 } 142 143 public void open(boolean expectedResult) { 144 modality(displayer.descriptor); 145 boolean latestResult = OptionsDisplayer.getDefault().open() ; 146 assertEquals(expectedResult, latestResult); 147 } 148 149 public void open(String categoryId, boolean expectedResult) { 150 modality(displayer.descriptor); 151 boolean latestResult = OptionsDisplayer.getDefault().open(categoryId); 152 assertEquals(expectedResult, latestResult); 153 } 154 155 public void modality(DialogDescriptor desc) { 156 if (desc != null) { 157 assertFalse(desc.isModal()); 158 } 159 } 160 161 public void close() { 162 modality(displayer.descriptor); 163 displayer.close(); 164 } 165 166 protected Level logLevel() { 167 return Level.FINE; 168 } 169 170 private static class TestDisplayer extends DialogDisplayer implements Runnable { 171 DialogDescriptor descriptor; 172 private JDialog dialog; 173 public Object notify(NotifyDescriptor descriptor) { 174 throw new UnsupportedOperationException ("Not supported yet."); 175 } 176 177 public Dialog createDialog(DialogDescriptor descriptor) { 178 this.descriptor = descriptor; 179 return dialog = new TestDialog(descriptor); 180 } 181 182 public void close() { 183 try { 184 if (SwingUtilities.isEventDispatchThread()) { 185 run(); 186 } else { 187 SwingUtilities.invokeAndWait(this); 188 } 189 } catch (InterruptedException ex) { 190 } catch (InvocationTargetException ex) { 191 } 192 } 193 194 public void run() { 195 if (descriptor != null) { 196 Object [] oo = descriptor.getClosingOptions(); 197 for (int i = 0; i < oo.length; i++) { 198 String command = ((JButton )oo[i]).getActionCommand(); 199 if (oo[i] instanceof JButton && "OK".equals(command)) { 200 descriptor.getButtonListener().actionPerformed(new ActionEvent (oo[i], 0, command)); 201 return; 202 } 203 } 204 } 205 } 206 } 207 208 private static class TestDialog extends JDialog { 209 TestDialog(DialogDescriptor descriptor) { 210 super((Frame )null, descriptor.getTitle(), descriptor.isModal()); 211 } 212 213 public void setVisible(boolean b) { 214 if (isModal()) { 215 super.setVisible(b); 216 } 217 } 218 } 219 } | Popular Tags |