KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > services > DialogDisplayerImplTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.services;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.EventQueue JavaDoc;
24 import javax.swing.JButton JavaDoc;
25 import javax.swing.JOptionPane JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.DialogDescriptor;
29 import org.openide.DialogDisplayer;
30 import org.openide.NotifyDescriptor;
31
32 /**
33  *
34  * @author Jaroslav Tulach, Jiri Rechtacek
35  */

36 public class DialogDisplayerImplTest extends NbTestCase {
37     private DialogDisplayer dd;
38     private final Object JavaDoc RESULT = "DialogDisplayerImplTestResult";
39     private JOptionPane JavaDoc pane;
40     private JButton JavaDoc closeOwner;
41     private DialogDescriptor childDD;
42     private JButton JavaDoc openChild;
43     private JButton JavaDoc closeChild;
44     private Dialog JavaDoc child;
45     
46     public DialogDisplayerImplTest (String JavaDoc testName) {
47         super (testName);
48     }
49     
50     protected void setUp() throws Exception JavaDoc {
51         dd = new DialogDisplayerImpl (RESULT);
52         closeOwner = new JButton JavaDoc ("Close this dialog");
53         childDD = new DialogDescriptor ("Child", "Child", false, null);
54         openChild = new JButton JavaDoc ("Open child");
55         closeChild = new JButton JavaDoc ("Close child");
56         pane = new JOptionPane JavaDoc ("", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object JavaDoc[] {openChild, closeChild});
57     }
58
59     protected boolean runInEQ () {
60         return false;
61     }
62     
63     public void testUnitTestByDefaultReturnsRESULT () throws Exception JavaDoc {
64         NotifyDescriptor nd = new NotifyDescriptor.Confirmation ("AnyQuestion?");
65         Object JavaDoc r = dd.notify (nd);
66         assertEquals (RESULT, r);
67     }
68
69     public void testWorksFromAWTImmediatelly () throws Exception JavaDoc {
70         class FromAWT implements Runnable JavaDoc {
71             public void run () {
72                 NotifyDescriptor nd = new NotifyDescriptor.Confirmation ("HowAreYou?");
73                 Object JavaDoc 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 JavaDoc {
82         NotifyDescriptor nd = new NotifyDescriptor.Confirmation ("HowAreYou?");
83         
84         class BlockAWT implements Runnable JavaDoc {
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                         // this is wrong
94
state = 3;
95                         // wait for the dialog to finish
96
notify ();
97                         return ;
98                     }
99                 } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc {
129         boolean leaf = true;
130         DialogDescriptor ownerDD = new DialogDescriptor (pane, "Owner", true, new Object JavaDoc[] {closeOwner}, null, 0, null, null, leaf);
131         final Dialog JavaDoc owner = DialogDisplayer.getDefault ().createDialog (ownerDD);
132         
133         // make leaf visible
134
postInAwtAndWaitOutsideAwt (new Runnable JavaDoc () {
135             public void run () {
136                 owner.setVisible (true);
137             }
138         });
139         while (!owner.isVisible ()) {}
140         
141         child = DialogDisplayer.getDefault ().createDialog (childDD);
142
143         // make the child visible
144
postInAwtAndWaitOutsideAwt (new Runnable JavaDoc () {
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         // close the leaf window
158
postInAwtAndWaitOutsideAwt (new Runnable JavaDoc () {
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         // close the child dialog
169
postInAwtAndWaitOutsideAwt (new Runnable JavaDoc () {
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 JavaDoc {
180         boolean leaf = false;
181         DialogDescriptor ownerDD = new DialogDescriptor (pane, "Owner", true, new Object JavaDoc[] {closeOwner}, null, 0, null, null, leaf);
182         final Dialog JavaDoc owner = DialogDisplayer.getDefault ().createDialog (ownerDD);
183         
184         // make leaf visible
185
postInAwtAndWaitOutsideAwt (new Runnable JavaDoc () {
186             public void run () {
187                 owner.setVisible (true);
188             }
189         });
190         while (!owner.isVisible ()) {}
191         
192         child = DialogDisplayer.getDefault ().createDialog (childDD);
193
194         // make the child visible
195
postInAwtAndWaitOutsideAwt (new Runnable JavaDoc () {
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         // close the leaf window
209
postInAwtAndWaitOutsideAwt (new Runnable JavaDoc () {
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 JavaDoc run) throws Exception JavaDoc {
221         // pendig to better implementation
222
SwingUtilities.invokeLater (run);
223 // Thread.sleep (10);
224
while (EventQueue.getCurrentEvent () != null) {
225 // Thread.sleep (10);
226
}
227     }
228     
229     private void waitAWT() throws Exception JavaDoc {
230         SwingUtilities.invokeAndWait(new Runnable JavaDoc() { public void run() { } });
231     }
232
233     public void testIfLasterWhenSplashShownThanWaitTillItFinished() throws Exception JavaDoc {
234         class MyObj extends Object JavaDoc {
235             public int called;
236             
237             public String JavaDoc 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 JavaDoc () {
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 JavaDoc () {
263             public void run() {
264                 DialogDisplayerImpl.runDelayed();
265             }
266         });
267         
268     }
269     
270 }
271
Popular Tags