KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > settings > storage > FontColorSettingsImplTest


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.modules.editor.settings.storage;
21
22 import java.awt.Color JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Collection JavaDoc;
25 import javax.swing.text.AttributeSet JavaDoc;
26 import javax.swing.text.StyleConstants JavaDoc;
27 import org.netbeans.api.editor.mimelookup.MimeLookup;
28 import org.netbeans.api.editor.mimelookup.MimePath;
29 import org.netbeans.api.editor.settings.EditorStyleConstants;
30 import org.netbeans.api.editor.settings.FontColorSettings;
31 import org.netbeans.core.startup.Main;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.modules.editor.settings.storage.api.EditorSettings;
34 import org.openide.util.Lookup;
35
36 /** Testing basic functionality of Editor Settings Storage friend API
37  *
38  * @author Martin Roskanin
39  */

40 public class FontColorSettingsImplTest extends NbTestCase {
41
42     public FontColorSettingsImplTest(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     protected void setUp() throws Exception JavaDoc {
47         super.setUp();
48     
49         EditorTestLookup.setLookup(
50             new URL JavaDoc[] {
51                 getClass().getClassLoader().getResource(
52                         "org/netbeans/modules/editor/settings/storage/test-layer.xml"),
53                 getClass().getClassLoader().getResource(
54                         "org/netbeans/modules/editor/settings/storage/layer.xml"),
55             },
56             getWorkDir(),
57             new Object JavaDoc[] {},
58             getClass().getClassLoader()
59         );
60
61         // This is here to initialize Nb URL factory (org.netbeans.core.startup),
62
// which is needed by Nb EntityCatalog (org.netbeans.core).
63
// Also see the test dependencies in project.xml
64
Main.initializeURLFactory();
65     }
66
67     public void testDefaults() {
68         MimePath mimePath = MimePath.parse("text/x-type-B");
69         
70         checkSingleAttribute(mimePath, "test-inheritance-coloring-1", StyleConstants.Background, 0xAA0000);
71         checkSingleAttribute(mimePath, "test-inheritance-coloring-2", StyleConstants.Background, 0xAA0000);
72
73         checkSingleAttribute(mimePath, "test-inheritance-coloring-A", StyleConstants.Background, 0xABCDEF);
74         checkSingleAttribute(mimePath, "test-inheritance-coloring-B", StyleConstants.Background, 0xABCDEF);
75
76         checkSingleAttribute(mimePath, "test-inheritance-coloring-X", StyleConstants.Background, 0xBB0000);
77         checkSingleAttribute(mimePath, "test-inheritance-coloring-Y", StyleConstants.Background, 0xBB0000);
78     }
79
80     public void testAllLanguagesTheCrapWay() {
81         Collection JavaDoc<AttributeSet JavaDoc> colorings = EditorSettings.getDefault().getFontColorSettings(new String JavaDoc[0]).getAllFontColors(EditorSettingsImpl.DEFAULT_PROFILE);
82         assertNotNull("Can't get colorings for all languages", colorings);
83         
84         AttributeSet JavaDoc attribs = null;
85         for(AttributeSet JavaDoc coloring : colorings) {
86             String JavaDoc name = (String JavaDoc) coloring.getAttribute(StyleConstants.NameAttribute);
87             if (name != null && name.equals("test-all-languages-set-all")) {
88                 attribs = coloring;
89                 break;
90             }
91         }
92         
93         assertNotNull("Can't find test-all-languages-set-all coloring", attribs);
94         assertEquals("Wrong color", new Color JavaDoc(0x0A0B0C), attribs.getAttribute(StyleConstants.Background));
95         assertEquals("Wrong color", new Color JavaDoc(0x0D0E0F), attribs.getAttribute(StyleConstants.Foreground));
96         assertEquals("Wrong color", new Color JavaDoc(0x010203), attribs.getAttribute(StyleConstants.Underline));
97         assertEquals("Wrong color", new Color JavaDoc(0x040506), attribs.getAttribute(StyleConstants.StrikeThrough));
98         assertEquals("Wrong color", new Color JavaDoc(0x070809), attribs.getAttribute(EditorStyleConstants.WaveUnderlineColor));
99     }
100
101     public void testAllLanguagesTheStandardWay() {
102         checkSingleAttribute(MimePath.EMPTY, "test-all-languages-set-all", StyleConstants.Background, 0x0A0B0C);
103         checkSingleAttribute(MimePath.EMPTY, "test-all-languages-set-all", StyleConstants.Foreground, 0x0D0E0F);
104         checkSingleAttribute(MimePath.EMPTY, "test-all-languages-set-all", StyleConstants.Underline, 0x010203);
105         checkSingleAttribute(MimePath.EMPTY, "test-all-languages-set-all", StyleConstants.StrikeThrough, 0x040506);
106         checkSingleAttribute(MimePath.EMPTY, "test-all-languages-set-all", EditorStyleConstants.WaveUnderlineColor, 0x070809);
107     }
108     
109     private void checkSingleAttribute(MimePath mimePath, String JavaDoc coloringName, Object JavaDoc attributeKey, int rgb) {
110         Lookup lookup = MimeLookup.getLookup(mimePath);
111         
112         FontColorSettings fcs = lookup.lookup(FontColorSettings.class);
113         assertNotNull("Can't find FontColorSettings", fcs);
114         
115         AttributeSet JavaDoc attribs = fcs.getTokenFontColors(coloringName);
116         assertNotNull("Can't find " + coloringName + " coloring", attribs);
117         assertEquals("Wrong color", new Color JavaDoc(rgb), attribs.getAttribute(attributeKey));
118     }
119     
120 }
121
Popular Tags