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-2007 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.spi.palette;
043:
044: import java.io.IOException;
045: import javax.swing.SwingUtilities;
046: import org.netbeans.core.windows.Constants;
047: import org.netbeans.core.windows.SplitConstraint;
048: import org.netbeans.core.windows.WindowManagerImpl;
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.FileSystem;
051: import org.openide.filesystems.MIMEResolver;
052: import org.openide.filesystems.Repository;
053: import org.openide.loaders.DataObject;
054: import org.openide.loaders.DataObjectNotFoundException;
055: import org.openide.nodes.Node;
056: import org.openide.util.lookup.AbstractLookup;
057: import org.openide.util.lookup.InstanceContent;
058: import org.openide.windows.Mode;
059: import org.openide.windows.TopComponent;
060:
061: /**
062: *
063: * @author S. Aubrecht
064: */
065: public class PaletteSwitchTest extends AbstractPaletteTestHid {
066:
067: private String lookupPaletteRootName;
068: private FileObject lookupPaletteRootFolder;
069:
070: static String mimePaletteRootName;
071: private static FileObject mimePaletteRootFolder;
072: private static final String MIME_TYPE_NAME = "text/x-paletteswitchtest";
073:
074: private FileObject dummyDocumentFile;
075: private final static String DUMMY_DOCUMENT_FILE_EXTENSION = "junitPaletteSwitchTest";
076:
077: static {
078: String[] layers = new String[] { "org/netbeans/spi/palette/mf-layer.xml" };//NOI18N
079: Object[] instances = new Object[] { new MyMimeResolver() };
080: IDEInitializer.setup(layers, instances);
081: }
082:
083: public PaletteSwitchTest(String testName) {
084: super (testName);
085: }
086:
087: protected void setUp() throws Exception {
088: super .setUp();
089: FileSystem fs = Repository.getDefault().getDefaultFileSystem();
090:
091: lookupPaletteRootName = "lookupPalette"
092: + System.currentTimeMillis();
093: lookupPaletteRootFolder = fs.getRoot().createFolder(
094: lookupPaletteRootName);
095: createDefaultPaletteContentInFolder(lookupPaletteRootFolder);
096:
097: if (null == mimePaletteRootName) {
098: mimePaletteRootName = "mimePalette"
099: + System.currentTimeMillis();
100: mimePaletteRootFolder = fs.getRoot().createFolder(
101: mimePaletteRootName);
102: createDefaultPaletteContentInFolder(mimePaletteRootFolder);
103: }
104:
105: dummyDocumentFile = fs.getRoot().createData(
106: "dummyDocumentFile" + System.currentTimeMillis(),
107: DUMMY_DOCUMENT_FILE_EXTENSION);
108: }
109:
110: @Override
111: protected boolean runInEQ() {
112: return true;
113: }
114:
115: public void testNoLookupPalette() throws IOException {
116: TopComponent tc = createTopComponentWithPalette(null);
117: PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
118:
119: PaletteController foundPalette = paletteSwitch
120: .getPaletteFromTopComponent(tc, false);
121:
122: assertNull(foundPalette);
123: }
124:
125: public void testNoMimePalette() throws IOException {
126: PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
127:
128: PaletteController foundPalette = paletteSwitch
129: .getPaletteFromMimeType("text/_unknown");
130:
131: assertNull(foundPalette);
132: }
133:
134: public void testLookupPalette() throws IOException {
135: PaletteController pc = PaletteFactory.createPalette(
136: lookupPaletteRootName, new DummyActions());
137:
138: TopComponent tc = createTopComponentWithPalette(pc);
139: PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
140:
141: PaletteController foundPalette = paletteSwitch
142: .getPaletteFromTopComponent(tc, false);
143:
144: assertNotNull(foundPalette);
145: assertEquals(pc.getModel().getName(), foundPalette.getModel()
146: .getName());
147: }
148:
149: public void testMimePalette() throws IOException {
150: TopComponent tc = createTopComponentWithPalette(null);
151: tc.setActivatedNodes(new Node[] { DataObject.find(
152: dummyDocumentFile).getNodeDelegate() });
153: tc.open();
154:
155: PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
156:
157: PaletteController foundPalette = paletteSwitch
158: .getPaletteFromMimeType(MIME_TYPE_NAME);
159: assertNotNull(foundPalette);
160: assertEquals(mimePaletteRootName, foundPalette.getModel()
161: .getName());
162:
163: foundPalette = paletteSwitch.getPaletteFromTopComponent(tc,
164: false);
165: assertNotNull(foundPalette);
166: assertEquals(mimePaletteRootName, foundPalette.getModel()
167: .getName());
168: }
169:
170: public void testLookupPaletteTakePrecendsOverMimePalette()
171: throws IOException {
172: PaletteController pc = PaletteFactory.createPalette(
173: lookupPaletteRootName, new DummyActions());
174:
175: TopComponent tc = createTopComponentWithPalette(pc);
176: tc.setActivatedNodes(new Node[] { DataObject.find(
177: dummyDocumentFile).getNodeDelegate() });
178:
179: PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
180:
181: PaletteController foundPalette = paletteSwitch
182: .getPaletteFromMimeType(MIME_TYPE_NAME);
183: assertNotNull(foundPalette);
184: assertEquals(mimePaletteRootName, foundPalette.getModel()
185: .getName());
186:
187: foundPalette = paletteSwitch.getPaletteFromTopComponent(tc,
188: false);
189: assertNotNull(foundPalette);
190: assertEquals(pc.getModel().getName(), foundPalette.getModel()
191: .getName());
192: }
193:
194: public void testVisibilityPerDocumentType() throws Exception {
195: final TopComponent palette = PaletteTopComponent.getDefault();
196: assertNotNull(palette);
197:
198: final PaletteController pc = PaletteFactory.createPalette(
199: lookupPaletteRootName, new DummyActions());
200: final MyTopComponent paletteTc1 = createTopComponentWithPalette(pc);
201: paletteTc1.open();
202:
203: final MyTopComponent paletteTc2 = createTopComponentWithPalette(null);
204: paletteTc2.setActivatedNodes(new Node[] { DataObject.find(
205: dummyDocumentFile).getNodeDelegate() });
206: paletteTc2.open();
207:
208: final MyTopComponent noPaletteTc = createTopComponentWithPalette(null);
209: noPaletteTc.open();
210:
211: noPaletteTc.requestActive();
212: final PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
213: paletteSwitch.startListening();
214:
215: assertNull(
216: "No PaletteController for TC which doesn't provide one",
217: paletteSwitch.getCurrentPalette());
218: assertFalse(
219: "Palette window cannot be opened when a document without PaletteController is active",
220: palette.isOpened());
221:
222: paletteTc1.requestActive();
223: SwingUtilities.invokeLater(new Runnable() {
224: public void run() {
225: assertEquals(TopComponent.getRegistry().getActivated(),
226: paletteTc1);
227: assertNotNull("TC with PaletteController is active",
228: paletteSwitch.getCurrentPalette());
229: assertTrue(
230: "Palette window opens by default when a document with PaletteController is active",
231: palette.isOpened());
232:
233: noPaletteTc.requestActive();
234:
235: SwingUtilities.invokeLater(new Runnable() {
236: public void run() {
237: assertFalse(
238: "Palette window closes when document without PaletteController is active",
239: palette.isOpened());
240:
241: paletteTc1.requestActive();
242: SwingUtilities.invokeLater(new Runnable() {
243: public void run() {
244: assertTrue(
245: "Palette window opens by default when a document with PaletteController is active",
246: palette.isOpened());
247:
248: //simulate closing palette window by user
249: palette.close();
250:
251: noPaletteTc.requestActive();
252: paletteTc1.requestActive();
253:
254: SwingUtilities
255: .invokeLater(new Runnable() {
256: public void run() {
257: assertFalse(
258: "Palette window doesn't show up even when document with PaletteController is active because the palette window was closed by the user before.",
259: palette
260: .isOpened());
261:
262: paletteTc2
263: .requestActive();
264:
265: SwingUtilities
266: .invokeLater(new Runnable() {
267: public void run() {
268: assertTrue(
269: "Palette window opens by default when a different document with PaletteController is active",
270: palette
271: .isOpened());
272: paletteSwitch
273: .stopListening();
274: }
275: });
276: }
277: });
278: }
279: });
280: }
281: });
282: }
283: });
284: }
285:
286: private MyTopComponent createTopComponentWithPalette(
287: PaletteController pc) throws IOException {
288: MyTopComponent tc = new MyTopComponent(pc);
289:
290: Mode editorMode = WindowManagerImpl.getInstance().findMode(
291: "unitTestEditorMode");
292: if (null == editorMode) {
293: editorMode = WindowManagerImpl.getInstance().createMode(
294: "unitTestEditorMode", Constants.MODE_KIND_EDITOR,
295: Constants.MODE_STATE_JOINED, false,
296: new SplitConstraint[0]);
297: }
298: editorMode.dockInto(tc);
299: return tc;
300: }
301:
302: private static class MyTopComponent extends TopComponent {
303: private boolean hackIsShowing = false;
304:
305: public MyTopComponent(PaletteController palette)
306: throws DataObjectNotFoundException {
307: this (new InstanceContent(), palette);
308: }
309:
310: private MyTopComponent(InstanceContent ic,
311: PaletteController palette)
312: throws DataObjectNotFoundException {
313: super (new AbstractLookup(ic));
314: if (null != palette)
315: ic.add(palette);
316: }
317:
318: @Override
319: public boolean isShowing() {
320: return hackIsShowing;
321: }
322:
323: @Override
324: protected void componentActivated() {
325: super .componentActivated();
326: hackIsShowing = true;
327: }
328:
329: @Override
330: protected void componentDeactivated() {
331: super .componentDeactivated();
332: hackIsShowing = false;
333: }
334: }
335:
336: public static class MyMimeResolver extends MIMEResolver {
337:
338: public String findMIMEType(FileObject fo) {
339: if (DUMMY_DOCUMENT_FILE_EXTENSION.equals(fo.getExt()))
340: return MIME_TYPE_NAME;
341: return null;
342: }
343: }
344:
345: }
|