001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.options;
043:
044: /*
045: * OptionsDisplayerTest.java
046: * JUnit based test
047: *
048: * Created on December 12, 2006, 11:13 AM
049: */
050:
051: import java.awt.Frame;
052: import java.awt.Dialog;
053: import java.awt.event.ActionEvent;
054: import java.lang.reflect.InvocationTargetException;
055: import java.util.Collection;
056: import java.util.logging.Level;
057: import java.util.logging.Logger;
058: import javax.swing.JButton;
059: import javax.swing.JDialog;
060: import javax.swing.SwingUtilities;
061: import org.netbeans.junit.NbTestCase;
062: import org.openide.DialogDescriptor;
063: import org.openide.DialogDisplayer;
064: import org.openide.NotifyDescriptor;
065: import org.openide.util.Lookup;
066: import org.openide.util.lookup.Lookups;
067:
068: /**
069: *
070: * @author Radek Matous
071: */
072: public class OptionsDisplayerOpenTest extends NbTestCase {
073: private static TestDisplayer displayer = new TestDisplayer();
074: private static final int REPEATER = 10;
075: private Collection<? extends RegisteredCategory> all;
076: Logger log;
077: static {
078: String[] layers = new String[] { "org/netbeans/api/options/mf-layer.xml" };//NOI18N
079: Object[] instances = new Object[] { displayer };
080: IDEInitializer.setup(layers, instances);
081: }
082:
083: public OptionsDisplayerOpenTest(String testName) {
084: super (testName);
085: }
086:
087: protected void setUp() throws Exception {
088: log = Logger.getLogger("[Test - " + getName() + "]");
089: Lookup lookup = Lookups.forPath("OptionsDialog"); // NOI18N
090: Lookup.Result<RegisteredCategory> result = lookup
091: .lookup(new Lookup.Template<RegisteredCategory>(
092: RegisteredCategory.class));
093: all = result.allInstances();
094: assertTrue(all.size() > 0);
095: }
096:
097: protected void tearDown() throws Exception {
098: }
099:
100: /**
101: * Test of getDefault method, of class org.netbeans.api.options.OptionsDisplayer.
102: */
103: public void testGetDefault() {
104: assertNotNull(OptionsDisplayer.getDefault());
105: }
106:
107: public void testSimulatesAllNecessaryCallAndCheckThreading()
108: throws Exception {
109: RegisteredCategory rcTemp = null;
110: for (RegisteredCategory rc : all) {
111: rcTemp = rc;
112: break;
113: }
114: final RegisteredCategory registeredCategory = rcTemp;
115: assertNotNull(registeredCategory);
116: //cals getComponent,update, getLookup
117: open("Registered", true);
118: try {
119: SwingUtilities.invokeAndWait(new Runnable() {
120: public void run() {
121: //calls isValid
122: registeredCategory.setInvalid();
123: //calls getHelpCtx
124: registeredCategory.helpChanged();
125: }
126: });
127: } finally {
128: //calls cancel
129: close(false);
130:
131: try {
132: open("Registered", true);
133: } finally {
134: //calls applyChanges
135: close();
136: }
137: registeredCategory.assertThreadingForAllCallsWereTested();
138: }
139: }
140:
141: public void testOpenFromWorkerThread() {
142: openOpen(null);
143: openOpen("Registered");
144: openOpen("Unknown");
145: openCloseOpen(null);
146: openCloseOpen("Registered");
147: openCloseOpen("Unknown");
148: }
149:
150: public void testOpenFromAWT() throws Exception {
151: SwingUtilities.invokeAndWait(new Runnable() {
152: public void run() {
153: testOpenFromWorkerThread();
154: }
155: });
156: }
157:
158: public void testOpenFromMixedThreads() throws Exception {
159: testOpenFromWorkerThread();
160: testOpenFromAWT();
161: testOpenFromWorkerThread();
162: testOpenFromAWT();
163: }
164:
165: public void openOpen(String categoryId) {
166: for (int i = 0; i < REPEATER; i++) {
167: if (categoryId == null) {
168: open(true);
169: open(false);
170: close();
171: //don't call: OptionsDisplayer.getDefault().open(null) but OptionsDisplayer.getDefault().open()
172: open(null, false);
173: close();
174: } else {
175: if ("Registered".equals(categoryId)) {
176: open(categoryId, true);
177: open(categoryId, false);
178: close();
179: } else {
180: open(categoryId, false);
181: open(categoryId, false);
182: close();
183: }
184: }
185: }
186: }
187:
188: public void openCloseOpen(String categoryId) {
189: for (int i = 0; i < REPEATER; i++) {
190: if (categoryId == null) {
191: open(true);
192: close();
193: open(true);
194: close();
195: } else {
196: if ("Registered".equals(categoryId)) {
197: open(categoryId, true);
198: close();
199: open(categoryId, true);
200: close();
201: } else {
202: open(categoryId, false);
203: close();
204: open(categoryId, false);
205: close();
206: }
207: }
208: }
209: }
210:
211: public void open(boolean expectedResult) {
212: modality(displayer.descriptor);
213: boolean latestResult = OptionsDisplayer.getDefault().open();
214: assertEquals(expectedResult, latestResult);
215: }
216:
217: public void open(String categoryId, boolean expectedResult) {
218: modality(displayer.descriptor);
219: boolean latestResult = OptionsDisplayer.getDefault().open(
220: categoryId);
221: assertEquals(expectedResult, latestResult);
222: }
223:
224: public void modality(DialogDescriptor desc) {
225: if (desc != null) {
226: assertFalse(desc.isModal());
227: }
228: }
229:
230: public void close() {
231: close(true);
232: }
233:
234: public void close(boolean okButtonForClose) {
235: displayer.okButtonForClose = okButtonForClose;
236: modality(displayer.descriptor);
237: displayer.close();
238: }
239:
240: protected Level logLevel() {
241: return Level.FINE;
242: }
243:
244: public static class TestDisplayer extends DialogDisplayer implements
245: Runnable {
246: DialogDescriptor descriptor;
247: private JDialog dialog;
248: boolean okButtonForClose = true;
249:
250: public Object notify(NotifyDescriptor descriptor) {
251: throw new UnsupportedOperationException(
252: "Not supported yet.");
253: }
254:
255: public Dialog createDialog(DialogDescriptor descriptor) {
256: this .descriptor = descriptor;
257: return dialog = new TestDialog(descriptor);
258: }
259:
260: public void close() {
261: try {
262: if (SwingUtilities.isEventDispatchThread()) {
263: run();
264: } else {
265: SwingUtilities.invokeAndWait(this );
266: }
267: } catch (InterruptedException ex) {
268: } catch (InvocationTargetException ex) {
269: }
270: }
271:
272: public void run() {
273: if (descriptor != null) {
274: Object[] oo = descriptor.getClosingOptions();
275: for (int i = 0; i < oo.length; i++) {
276: if (okButtonForClose
277: && oo[i] instanceof JButton
278: && ((JButton) oo[i]).getActionCommand()
279: .equals("OK")) {
280: descriptor.getButtonListener().actionPerformed(
281: new ActionEvent(oo[i], 0,
282: ((JButton) oo[i])
283: .getActionCommand()));
284: return;
285: } else if (!okButtonForClose
286: && !(oo[i] instanceof JButton)) {
287: descriptor.getButtonListener().actionPerformed(
288: new ActionEvent(
289: DialogDescriptor.CANCEL_OPTION,
290: 0, ""));
291: return;
292: }
293: }
294: }
295: }
296: }
297:
298: private static class TestDialog extends JDialog {
299: TestDialog(DialogDescriptor descriptor) {
300: super ((Frame) null, descriptor.getTitle(), descriptor
301: .isModal());
302: }
303:
304: public void setVisible(boolean b) {
305: if (isModal()) {
306: super.setVisible(b);
307: }
308: }
309: }
310: }
|