KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > options > OptionsDisplayerOpenTest


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.api.options;
21 /*
22  * OptionsDisplayerTest.java
23  * JUnit based test
24  *
25  * Created on December 12, 2006, 11:13 AM
26  */

27
28 import java.awt.Frame JavaDoc;
29 import java.awt.Dialog JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.JDialog JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import org.netbeans.junit.NbTestCase;
38 import org.openide.DialogDescriptor;
39 import org.openide.DialogDisplayer;
40 import org.openide.NotifyDescriptor;
41
42 /**
43  *
44  * @author Radek Matous
45  */

46 public class OptionsDisplayerOpenTest extends NbTestCase {
47     private static TestDisplayer displayer = new TestDisplayer();
48     private static final int REPEATER = 10;
49     Logger JavaDoc log;
50     static {
51         String JavaDoc[] layers = new String JavaDoc[] {"org/netbeans/api/options/mf-layer.xml"};//NOI18N
52
Object JavaDoc[] instances = new Object JavaDoc[] {displayer};
53         IDEInitializer.setup(layers,instances);
54     }
55     
56     public OptionsDisplayerOpenTest(String JavaDoc testName) {
57         super(testName);
58     }
59         
60     protected void setUp() throws Exception JavaDoc {
61         log = Logger.getLogger("[Test - " + getName() + "]");
62     }
63     
64     protected void tearDown() throws Exception JavaDoc {
65     }
66     /**
67      * Test of getDefault method, of class org.netbeans.api.options.OptionsDisplayer.
68      */

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 JavaDoc {
83         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
84             public void run() {
85                 testOpenFromWorkerThread();
86             }
87         });
88     }
89     
90     public void testOpenFromMixedThreads() throws Exception JavaDoc {
91         testOpenFromWorkerThread();
92         testOpenFromAWT();
93         testOpenFromWorkerThread();
94         testOpenFromAWT();
95     }
96     
97     public void openOpen(String JavaDoc categoryId) {
98         for (int i = 0; i < REPEATER; i++) {
99             if (categoryId == null) {
100                 open(true);
101                 open(false);
102                 close();
103                 //don't call: OptionsDisplayer.getDefault().open(null) but OptionsDisplayer.getDefault().open()
104
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 JavaDoc 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 JavaDoc 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 JavaDoc logLevel() {
167         return Level.FINE;
168     }
169         
170     private static class TestDisplayer extends DialogDisplayer implements Runnable JavaDoc {
171         DialogDescriptor descriptor;
172         private JDialog JavaDoc dialog;
173         public Object JavaDoc notify(NotifyDescriptor descriptor) {
174             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
175         }
176         
177         public Dialog JavaDoc 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 JavaDoc ex) {
190             } catch (InvocationTargetException JavaDoc ex) {
191             }
192         }
193         
194         public void run() {
195             if (descriptor != null) {
196                 Object JavaDoc[] oo = descriptor.getClosingOptions();
197                 for (int i = 0; i < oo.length; i++) {
198                     String JavaDoc command = ((JButton JavaDoc)oo[i]).getActionCommand();
199                     if (oo[i] instanceof JButton JavaDoc && "OK".equals(command)) {
200                         descriptor.getButtonListener().actionPerformed(new ActionEvent JavaDoc(oo[i], 0, command));
201                         return;
202                     }
203                 }
204             }
205         }
206     }
207     
208     private static class TestDialog extends JDialog JavaDoc {
209         TestDialog(DialogDescriptor descriptor) {
210             super((Frame JavaDoc)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