1 19 20 package org.netbeans.core.windows.services; 21 22 import java.awt.Dialog ; 23 import java.awt.EventQueue ; 24 import javax.swing.JButton ; 25 import javax.swing.JOptionPane ; 26 import javax.swing.SwingUtilities ; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.DialogDescriptor; 29 import org.openide.DialogDisplayer; 30 import org.openide.NotifyDescriptor; 31 32 36 public class DialogDisplayerImplTest extends NbTestCase { 37 private DialogDisplayer dd; 38 private final Object RESULT = "DialogDisplayerImplTestResult"; 39 private JOptionPane pane; 40 private JButton closeOwner; 41 private DialogDescriptor childDD; 42 private JButton openChild; 43 private JButton closeChild; 44 private Dialog child; 45 46 public DialogDisplayerImplTest (String testName) { 47 super (testName); 48 } 49 50 protected void setUp() throws Exception { 51 dd = new DialogDisplayerImpl (RESULT); 52 closeOwner = new JButton ("Close this dialog"); 53 childDD = new DialogDescriptor ("Child", "Child", false, null); 54 openChild = new JButton ("Open child"); 55 closeChild = new JButton ("Close child"); 56 pane = new JOptionPane ("", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object [] {openChild, closeChild}); 57 } 58 59 protected boolean runInEQ () { 60 return false; 61 } 62 63 public void testUnitTestByDefaultReturnsRESULT () throws Exception { 64 NotifyDescriptor nd = new NotifyDescriptor.Confirmation ("AnyQuestion?"); 65 Object r = dd.notify (nd); 66 assertEquals (RESULT, r); 67 } 68 69 public void testWorksFromAWTImmediatelly () throws Exception { 70 class FromAWT implements Runnable { 71 public void run () { 72 NotifyDescriptor nd = new NotifyDescriptor.Confirmation ("HowAreYou?"); 73 Object r = dd.notify (nd); 74 assertEquals ("Returns ok", RESULT, r); 75 } 76 } 77 78 SwingUtilities.invokeAndWait (new FromAWT ()); 79 } 80 81 public void testDeadlock41544IfItIsNotPossibleToAccessAWTReturnAfterTimeout () throws Exception { 82 NotifyDescriptor nd = new NotifyDescriptor.Confirmation ("HowAreYou?"); 83 84 class BlockAWT implements Runnable { 85 public volatile int state; 86 public synchronized void run () { 87 state = 1; 88 try { 89 notify (); 90 long t = System.currentTimeMillis (); 91 wait (15000); 92 if (System.currentTimeMillis () - t > 13000) { 93 state = 3; 95 notify (); 97 return ; 98 } 99 } catch (Exception ex) { 100 } 101 state = 2; 102 notify (); 103 } 104 } 105 106 BlockAWT b = new BlockAWT (); 107 synchronized (b) { 108 SwingUtilities.invokeLater (b); 109 b.wait (); 110 assertEquals ("In state one", 1, b.state); 111 } 112 113 Object res = dd.notify (nd); 114 115 if (b.state == 3) { 116 fail ("This means that the AWT blocked timeouted - e.g. no time out implemented in the dd.notify at all"); 117 } 118 119 assertEquals ("Returns as closed, if cannot access AWT", nd.CLOSED_OPTION, res); 120 121 synchronized (b) { 122 b.notify (); 123 b.wait (); 124 assertEquals ("Exited correctly", 2, b.state); 125 } 126 } 127 128 public void testLeafDialog () throws Exception { 129 boolean leaf = true; 130 DialogDescriptor ownerDD = new DialogDescriptor (pane, "Owner", true, new Object [] {closeOwner}, null, 0, null, null, leaf); 131 final Dialog owner = DialogDisplayer.getDefault ().createDialog (ownerDD); 132 133 postInAwtAndWaitOutsideAwt (new Runnable () { 135 public void run () { 136 owner.setVisible (true); 137 } 138 }); 139 while (!owner.isVisible ()) {} 140 141 child = DialogDisplayer.getDefault ().createDialog (childDD); 142 143 postInAwtAndWaitOutsideAwt (new Runnable () { 145 public void run () { 146 child.setVisible (true); 147 } 148 }); 149 while (!child.isVisible ()) {} 150 151 assertFalse ("No dialog is owned by leaf dialog.", owner.equals (child.getOwner ())); 152 assertEquals ("The leaf dialog has no child.", 0, owner.getOwnedWindows ().length); 153 154 assertTrue ("Leaf is visible", owner.isVisible ()); 155 assertTrue ("Child is visible", child.isVisible ()); 156 157 postInAwtAndWaitOutsideAwt (new Runnable () { 159 public void run () { 160 owner.setVisible (false); 161 } 162 }); 163 while (owner.isVisible ()) {} 164 165 assertFalse ("Leaf is dead", owner.isVisible ()); 166 assertTrue ("Child is visible still", child.isVisible ()); 167 168 postInAwtAndWaitOutsideAwt (new Runnable () { 170 public void run () { 171 child.setVisible (false); 172 } 173 }); 174 while (child.isVisible ()) {} 175 176 assertFalse ("Child is dead too", child.isVisible ()); 177 } 178 179 public void testNonLeafDialog () throws Exception { 180 boolean leaf = false; 181 DialogDescriptor ownerDD = new DialogDescriptor (pane, "Owner", true, new Object [] {closeOwner}, null, 0, null, null, leaf); 182 final Dialog owner = DialogDisplayer.getDefault ().createDialog (ownerDD); 183 184 postInAwtAndWaitOutsideAwt (new Runnable () { 186 public void run () { 187 owner.setVisible (true); 188 } 189 }); 190 while (!owner.isVisible ()) {} 191 192 child = DialogDisplayer.getDefault ().createDialog (childDD); 193 194 postInAwtAndWaitOutsideAwt (new Runnable () { 196 public void run () { 197 child.setVisible (true); 198 } 199 }); 200 while (!child.isVisible ()) {} 201 202 assertTrue ("The child is owned by leaf dialog.", owner.equals (child.getOwner ())); 203 assertEquals ("The leaf dialog has one child.", 1, owner.getOwnedWindows ().length); 204 205 assertTrue ("Leaf is visible", owner.isVisible ()); 206 assertTrue ("Child is visible", child.isVisible ()); 207 208 postInAwtAndWaitOutsideAwt (new Runnable () { 210 public void run () { 211 owner.setVisible (false); 212 } 213 }); 214 while (owner.isVisible ()) {} 215 216 assertFalse ("Leaf is dead", owner.isVisible ()); 217 assertFalse ("Child is dead too", child.isVisible ()); 218 } 219 220 private void postInAwtAndWaitOutsideAwt (final Runnable run) throws Exception { 221 SwingUtilities.invokeLater (run); 223 while (EventQueue.getCurrentEvent () != null) { 225 } 227 } 228 229 private void waitAWT() throws Exception { 230 SwingUtilities.invokeAndWait(new Runnable () { public void run() { } }); 231 } 232 233 public void testIfLasterWhenSplashShownThanWaitTillItFinished() throws Exception { 234 class MyObj extends Object { 235 public int called; 236 237 public String toString() { 238 called = 1; 239 return "Kuk"; 240 } 241 } 242 MyObj obj = new MyObj(); 243 244 NotifyDescriptor ownerDD = new NotifyDescriptor.Message(obj); 245 246 247 248 DialogDisplayer.getDefault ().notifyLater(ownerDD); 249 waitAWT(); 250 assertEquals("No notify yet", 0, obj.called); 251 252 postInAwtAndWaitOutsideAwt(new Runnable () { 253 public void run() { 254 DialogDisplayerImpl.runDelayed(); 255 } 256 }); 257 258 259 waitAWT(); 260 assertEquals("Now it is showing", 1, obj.called); 261 262 SwingUtilities.invokeAndWait(new Runnable () { 263 public void run() { 264 DialogDisplayerImpl.runDelayed(); 265 } 266 }); 267 268 } 269 270 } 271 | Popular Tags |