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 general;
043:
044: import javax.swing.JEditorPane;
045: import javax.swing.SwingUtilities;
046: import javax.swing.text.DefaultEditorKit;
047: import javax.swing.text.EditorKit;
048: import javax.swing.text.html.HTMLEditorKit;
049: import javax.swing.text.rtf.RTFEditorKit;
050: import org.netbeans.junit.NbTestCase;
051: import org.openide.text.CloneableEditorSupport;
052:
053: /**
054: *
055: * @author Vita Stejskal
056: */
057: public class EditorKitsRegistryTest extends NbTestCase {
058:
059: /** Creates a new instance of EditorKitsRegistryTest */
060: public EditorKitsRegistryTest(String name) {
061: super (name);
062: }
063:
064: public void testHTMLEditorKits() {
065: JEditorPane pane = new JEditorPane();
066: setContentTypeInAwt(pane, "text/html");
067:
068: // Test JDK kit
069: EditorKit kitFromJdk = pane.getEditorKit();
070: assertNotNull("Can't find JDK kit for text/html", kitFromJdk);
071: assertTrue("Wrong JDK kit for text/html",
072: kitFromJdk instanceof HTMLEditorKit);
073:
074: // Test Netbeans kit
075: EditorKit kitFromNb = CloneableEditorSupport
076: .getEditorKit("text/html");
077: assertNotNull("Can't find Nb kit for text/html", kitFromNb);
078: assertEquals("Wrong Nb kit for text/html",
079: "org.netbeans.modules.editor.html.HTMLKit", kitFromNb
080: .getClass().getName());
081: }
082:
083: public void testPlainEditorKits() {
084: // VIS: JEditorPane when constructed contains javax.swing.JEditorPane$PlainEditorKit
085: // and calling JEP.setContenetType("text/plain") has no effect. IMO this is probably
086: // a defect in JDK, becuase JEP should always honour its EditorKit registry.
087: JEditorPane pane = new JEditorPane();
088: pane.setEditorKit(new DefaultEditorKit() {
089: public String getContentType() {
090: return "text/whatever";
091: }
092: });
093: setContentTypeInAwt(pane, "text/plain");
094:
095: // Test JDK kit
096: EditorKit kitFromJdk = pane.getEditorKit();
097: assertNotNull("Can't find JDK kit for text/plain", kitFromJdk);
098: assertEquals("The kit for text/plain should not be from JDK",
099: "org.netbeans.modules.editor.plain.PlainKit",
100: kitFromJdk.getClass().getName());
101:
102: // Test Netbeans kit
103: EditorKit kitFromNb = CloneableEditorSupport
104: .getEditorKit("text/plain");
105: assertNotNull("Can't find Nb kit for text/plain", kitFromNb);
106: assertEquals("Wrong Nb kit for text/plain",
107: "org.netbeans.modules.editor.plain.PlainKit", kitFromNb
108: .getClass().getName());
109: }
110:
111: public void testTextRtfEditorKits() {
112: JEditorPane pane = new JEditorPane();
113: setContentTypeInAwt(pane, "text/rtf");
114:
115: // Test JDK kit
116: EditorKit kitFromJdk = pane.getEditorKit();
117: assertNotNull("Can't find JDK kit for text/rtf", kitFromJdk);
118: assertTrue("Wrong JDK kit for application/rtf",
119: kitFromJdk instanceof RTFEditorKit);
120: }
121:
122: public void testApplicationRtfEditorKits() {
123: JEditorPane pane = new JEditorPane();
124: setContentTypeInAwt(pane, "application/rtf");
125:
126: // Test JDK kit
127: EditorKit kitFromJdk = pane.getEditorKit();
128: assertNotNull("Can't find JDK kit for application/rtf",
129: kitFromJdk);
130: assertTrue("Wrong JDK kit for application/rtf",
131: kitFromJdk instanceof RTFEditorKit);
132: }
133:
134: private void setContentTypeInAwt(final JEditorPane pane,
135: final String mimeType) {
136: try {
137: SwingUtilities.invokeAndWait(new Runnable() {
138: public void run() {
139: pane.setContentType(mimeType);
140: }
141: });
142: } catch (Exception e) {
143: e.printStackTrace();
144: fail("Can't set content type in AWT: " + e.getMessage());
145: }
146: }
147: }
|