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.modules.options.editor;
043:
044: import java.net.URL;
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048: import javax.swing.JComponent;
049: import org.netbeans.core.startup.Main;
050: import org.netbeans.junit.NbTestCase;
051: import org.netbeans.modules.editor.settings.storage.EditorTestLookup;
052: import org.netbeans.spi.options.OptionsCategory;
053: import org.netbeans.spi.options.OptionsPanelController;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.Repository;
056: import org.openide.loaders.DataFolder;
057: import org.openide.loaders.FolderLookup;
058: import org.openide.util.Lookup;
059:
060: import org.openide.util.lookup.ProxyLookup;
061:
062: /**
063: *
064: * @author Jan Jancura
065: */
066: public class EditorOptionsTest extends NbTestCase {
067:
068: protected @Override
069: void setUp() throws Exception {
070: super .setUp();
071:
072: EditorTestLookup
073: .setLookup(
074: new URL[] {
075: getClass()
076: .getClassLoader()
077: .getResource(
078: "org/netbeans/modules/options/editor/mf-layer.xml"),
079: getClass()
080: .getClassLoader()
081: .getResource(
082: "org/netbeans/modules/options/keymap/mf-layer.xml"), },
083: getWorkDir(), new Object[] {}, getClass()
084: .getClassLoader());
085:
086: // This is here to initialize Nb URL factory (org.netbeans.core.startup),
087: // which is needed by Nb EntityCatalog (org.netbeans.core).
088: // Also see the test dependencies in project.xml
089: Main.initializeURLFactory();
090: }
091:
092: public EditorOptionsTest(String testName) {
093: super (testName);
094: }
095:
096: public void testPanelsRegistration() {
097: // there are two panels registered from this module - Editor, Fonts & Colors
098: // and one panel from core/options/keymap
099: assertEquals(3, getCategories().size());
100: }
101:
102: public void testOptionsCategories() {
103: Iterator it = getCategories().iterator();
104: while (it.hasNext()) {
105: OptionsCategory oc = (OptionsCategory) it.next();
106: assertNotNull(oc.getCategoryName());
107: assertNotNull(oc.getIcon());
108: assertNotNull(oc.getTitle());
109: }
110: }
111:
112: public void testUpdateOk() {
113: List controllers = new ArrayList();
114: List lookups = new ArrayList();
115: Iterator it = getCategories().iterator();
116: while (it.hasNext()) {
117: OptionsCategory oc = (OptionsCategory) it.next();
118: OptionsPanelController pc = oc.create();
119: controllers.add(pc);
120: lookups.add(pc.getLookup());
121: }
122: Lookup masterLookup = new ProxyLookup((Lookup[]) lookups
123: .toArray(new Lookup[lookups.size()]));
124: it = controllers.iterator();
125: while (it.hasNext()) {
126: OptionsPanelController pc = (OptionsPanelController) it
127: .next();
128: JComponent c = pc.getComponent(masterLookup);
129: pc.update();
130: pc.applyChanges();
131: }
132: }
133:
134: public void testUpdateCancel() {
135: List controllers = new ArrayList();
136: List lookups = new ArrayList();
137: Iterator it = getCategories().iterator();
138: while (it.hasNext()) {
139: OptionsCategory oc = (OptionsCategory) it.next();
140: OptionsPanelController pc = oc.create();
141: controllers.add(pc);
142: lookups.add(pc.getLookup());
143: }
144: Lookup masterLookup = new ProxyLookup((Lookup[]) lookups
145: .toArray(new Lookup[lookups.size()]));
146: it = controllers.iterator();
147: while (it.hasNext()) {
148: OptionsPanelController pc = (OptionsPanelController) it
149: .next();
150: JComponent c = pc.getComponent(masterLookup);
151: pc.update();
152: pc.cancel();
153: }
154: }
155:
156: public void testOk() {
157: List controllers = new ArrayList();
158: List lookups = new ArrayList();
159: Iterator it = getCategories().iterator();
160: while (it.hasNext()) {
161: OptionsCategory oc = (OptionsCategory) it.next();
162: OptionsPanelController pc = oc.create();
163: controllers.add(pc);
164: lookups.add(pc.getLookup());
165: }
166: Lookup masterLookup = new ProxyLookup((Lookup[]) lookups
167: .toArray(new Lookup[lookups.size()]));
168: it = controllers.iterator();
169: while (it.hasNext()) {
170: OptionsPanelController pc = (OptionsPanelController) it
171: .next();
172: JComponent c = pc.getComponent(masterLookup);
173: pc.applyChanges();
174: }
175: }
176:
177: public void testCancel() {
178:
179: // 1) load PanelControllers and init master lookup
180: List controllers = new ArrayList();
181: List lookups = new ArrayList();
182: Iterator it = getCategories().iterator();
183: while (it.hasNext()) {
184: OptionsCategory oc = (OptionsCategory) it.next();
185: OptionsPanelController pc = oc.create();
186: controllers.add(pc);
187: lookups.add(pc.getLookup());
188: }
189: Lookup masterLookup = new ProxyLookup((Lookup[]) lookups
190: .toArray(new Lookup[lookups.size()]));
191:
192: // 2) create panels & call cancel on all PanelControllers
193: it = controllers.iterator();
194: while (it.hasNext()) {
195: OptionsPanelController pc = (OptionsPanelController) it
196: .next();
197: JComponent c = pc.getComponent(masterLookup);
198: pc.cancel();
199: }
200: }
201:
202: public void testChangedAndValid() {
203:
204: // 1) load PanelControllers and init master lookup
205: List controllers = new ArrayList();
206: List lookups = new ArrayList();
207: Iterator it = getCategories().iterator();
208: while (it.hasNext()) {
209: OptionsCategory oc = (OptionsCategory) it.next();
210: OptionsPanelController pc = oc.create();
211: controllers.add(pc);
212: lookups.add(pc.getLookup());
213: }
214: Lookup masterLookup = new ProxyLookup((Lookup[]) lookups
215: .toArray(new Lookup[lookups.size()]));
216:
217: // 2) create panels & call cancel on all PanelControllers
218: it = controllers.iterator();
219: while (it.hasNext()) {
220: OptionsPanelController pc = (OptionsPanelController) it
221: .next();
222: assertFalse(
223: "isChanged should be false if there is no change! (controller = "
224: + pc + ")", pc.isChanged());
225: assertTrue(
226: "isvalid should be true if there is no change! (controller = "
227: + pc + ")", pc.isValid());
228: JComponent c = pc.getComponent(masterLookup);
229: assertFalse(
230: "isChanged should be false if there is no change! (controller = "
231: + pc + ")", pc.isChanged());
232: assertTrue(
233: "isvalid should be true if there is no change! (controller = "
234: + pc + ")", pc.isValid());
235: pc.update();
236: assertFalse(
237: "isChanged should be false if there is no change! (controller = "
238: + pc + ")", pc.isChanged());
239: assertTrue(
240: "isvalid should be true if there is no change! (controller = "
241: + pc + ")", pc.isValid());
242: pc.update();
243: assertFalse(
244: "isChanged should be false if there is no change! (controller = "
245: + pc + ")", pc.isChanged());
246: assertTrue(
247: "isvalid should be true if there is no change! (controller = "
248: + pc + ")", pc.isValid());
249: pc.cancel();
250: assertFalse(
251: "isChanged should be false if there is no change! (controller = "
252: + pc + ")", pc.isChanged());
253: assertTrue(
254: "isvalid should be true if there is no change! (controller = "
255: + pc + ")", pc.isValid());
256: pc.update();
257: assertFalse(
258: "isChanged should be false if there is no change! (controller = "
259: + pc + ")", pc.isChanged());
260: assertTrue(
261: "isvalid should be true if there is no change! (controller = "
262: + pc + ")", pc.isValid());
263: pc.applyChanges();
264: assertFalse(
265: "isChanged should be false if there is no change! (controller = "
266: + pc + ")", pc.isChanged());
267: assertTrue(
268: "isvalid should be true if there is no change! (controller = "
269: + pc + ")", pc.isValid());
270: }
271:
272: it = controllers.iterator();
273: while (it.hasNext()) {
274: OptionsPanelController pc = (OptionsPanelController) it
275: .next();
276: JComponent c = pc.getComponent(masterLookup);
277: pc.update();
278: assertFalse(
279: "isChanged should be false if there is no change! (controller = "
280: + pc + ")", pc.isChanged());
281: assertTrue(
282: "isvalid should be true if there is no change! (controller = "
283: + pc + ")", pc.isValid());
284: }
285: }
286:
287: private List getCategories() {
288: FileObject fo = Repository.getDefault().getDefaultFileSystem()
289: .findResource("OptionsDialog");
290: Lookup lookup = new FolderLookup(DataFolder.findFolder(fo))
291: .getLookup();
292: return new ArrayList(lookup.lookup(
293: new Lookup.Template(OptionsCategory.class))
294: .allInstances());
295: }
296: }
|