2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.editor.settings.storage;
44
import java.awt.Color;
46
import java.util.Collection;
47
import javax.swing.text.AttributeSet;
48
import javax.swing.text.StyleConstants;
49
import org.netbeans.api.editor.mimelookup.MimeLookup;
50
import org.netbeans.api.editor.mimelookup.MimePath;
51
import org.netbeans.api.editor.settings.EditorStyleConstants;
52
import org.netbeans.api.editor.settings.FontColorSettings;
53
import org.netbeans.core.startup.Main;
54
import org.netbeans.junit.NbTestCase;
55
import org.netbeans.modules.editor.settings.storage.api.EditorSettings;
56
import org.openide.util.Lookup;
58
/** Testing basic functionality of Editor Settings Storage friend API
60
* @author Martin Roskanin
62
public class FontColorSettingsImplTest extends NbTestCase {
64
public FontColorSettingsImplTest(String testName) {
68
protected void setUp() throws Exception {
71
EditorTestLookup.setLookup(
73
getClass().getClassLoader().getResource(
74
"org/netbeans/modules/editor/settings/storage/test-layer.xml"),
75
getClass().getClassLoader().getResource(
76
"org/netbeans/modules/editor/settings/storage/layer.xml"),
77
getClass().getClassLoader().getResource(
78
"org/netbeans/core/resources/mf-layer.xml"), // for MIMEResolverImpl to work
82
getClass().getClassLoader()
85
// This is here to initialize Nb URL factory (org.netbeans.core.startup),
86
// which is needed by Nb EntityCatalog (org.netbeans.core).
87
// Also see the test dependencies in project.xml
88
Main.initializeURLFactory();
91
public void testDefaults() {
92
MimePath mimePath = MimePath.parse("text/x-type-B");
94
checkSingleAttribute(mimePath, "test-inheritance-coloring-1", StyleConstants.Background, 0xAA0000);
95
checkSingleAttribute(mimePath, "test-inheritance-coloring-2", StyleConstants.Background, 0xAA0000);
97
checkSingleAttribute(mimePath, "test-inheritance-coloring-A", StyleConstants.Background, 0xABCDEF);
98
checkSingleAttribute(mimePath, "test-inheritance-coloring-B", StyleConstants.Background, 0xABCDEF);
100
checkSingleAttribute(mimePath, "test-inheritance-coloring-X", StyleConstants.Background, 0xBB0000);
101
checkSingleAttribute(mimePath, "test-inheritance-coloring-Y", StyleConstants.Background, 0xBB0000);
104
public void testAllLanguagesTheCrapWay() {
105
Collection<AttributeSet> colorings = EditorSettings.getDefault().getFontColorSettings(new String[0]).getAllFontColors(EditorSettingsImpl.DEFAULT_PROFILE);
106
assertNotNull("Can't get colorings for all languages", colorings);
108
AttributeSet attribs = null;
109
for(AttributeSet coloring : colorings) {
110
String name = (String) coloring.getAttribute(StyleConstants.NameAttribute);
111
if (name != null && name.equals("test-all-languages-set-all")) {
117
assertNotNull("Can't find test-all-languages-set-all coloring", attribs);
118
assertEquals("Wrong color", new Color(0x0A0B0C), attribs.getAttribute(StyleConstants.Background));
119
assertEquals("Wrong color", new Color(0x0D0E0F), attribs.getAttribute(StyleConstants.Foreground));
120
assertEquals("Wrong color", new Color(0x010203), attribs.getAttribute(StyleConstants.Underline));
121
assertEquals("Wrong color", new Color(0x040506), attribs.getAttribute(StyleConstants.StrikeThrough));
122
assertEquals("Wrong color", new Color(0x070809), attribs.getAttribute(EditorStyleConstants.WaveUnderlineColor));
125
public void testAllLanguagesTheStandardWay() {
126
checkSingleAttribute(MimePath.EMPTY, "test-all-languages-set-all", StyleConstants.Background, 0x0A0B0C);
127
checkSingleAttribute(MimePath.EMPTY, "test-all-languages-set-all", StyleConstants.Foreground, 0x0D0E0F);
128
checkSingleAttribute(MimePath.EMPTY, "test-all-languages-set-all", StyleConstants.Underline, 0x010203);
129
checkSingleAttribute(MimePath.EMPTY, "test-all-languages-set-all", StyleConstants.StrikeThrough, 0x040506);
130
checkSingleAttribute(MimePath.EMPTY, "test-all-languages-set-all", EditorStyleConstants.WaveUnderlineColor, 0x070809);
133
public void testUserChangesOverrideDefaults() {
134
MimePath mimePath = MimePath.parse("text/x-type-C");
135
Lookup lookup = MimeLookup.getLookup(mimePath);
136
FontColorSettings fcs = lookup.lookup(FontColorSettings.class);
137
assertNotNull("Can't find FontColorSettings", fcs);
139
AttributeSet attribs = fcs.getTokenFontColors("test-module-and-user");
140
assertNotNull("Can't find 'test-module-and-user' coloring", attribs);
142
assertNull("Wrong foreColor", attribs.getAttribute(StyleConstants.Foreground));
143
assertEquals("Wrong bgColor", new Color(0xCC0000), attribs.getAttribute(StyleConstants.Background));
144
assertNull("Wrong font family", attribs.getAttribute(StyleConstants.FontFamily));
145
assertNull("Wrong font size", attribs.getAttribute(StyleConstants.FontSize));
146
assertNull("Wrong bold", attribs.getAttribute(StyleConstants.Bold));
147
assertNull("Wrong italic", attribs.getAttribute(StyleConstants.Italic));
149
checkSingleAttribute(mimePath, "test-module", StyleConstants.Foreground, 0x000011);
150
checkSingleAttribute(mimePath, "test-module", StyleConstants.Background, 0x000022);
151
checkSingleAttribute(mimePath, "test-module", StyleConstants.Bold, false);
152
checkSingleAttribute(mimePath, "test-module", StyleConstants.Italic, true);
155
private void checkSingleAttribute(MimePath mimePath, String coloringName, Object attributeKey, int rgb) {
156
Lookup lookup = MimeLookup.getLookup(mimePath);
158
FontColorSettings fcs = lookup.lookup(FontColorSettings.class);
159
assertNotNull("Can't find FontColorSettings", fcs);
161
AttributeSet attribs = fcs.getTokenFontColors(coloringName);
162
assertNotNull("Can't find " + coloringName + " coloring", attribs);
163
assertEquals("Wrong color", new Color(rgb), attribs.getAttribute(attributeKey));
166
private void checkSingleAttribute(MimePath mimePath, String coloringName, Object attributeKey, Object attributeValue) {
167
Lookup lookup = MimeLookup.getLookup(mimePath);
169
FontColorSettings fcs = lookup.lookup(FontColorSettings.class);
170
assertNotNull("Can't find FontColorSettings", fcs);
172
AttributeSet attribs = fcs.getTokenFontColors(coloringName);
173
assertNotNull("Can't find " + coloringName + " coloring", attribs);
174
assertEquals("Wrong value of '" + attributeKey + "'", attributeValue, attribs.getAttribute(attributeKey));