2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.editor.colors.impl;
18 import com.intellij.openapi.editor.markup.EffectType;
19 import com.intellij.openapi.editor.markup.TextAttributes;
20 import com.intellij.testFramework.LightPlatformTestCase;
21 import org.jdom.Element;
22 import org.jetbrains.annotations.NotNull;
24 import java.awt.Color;
26 import java.lang.reflect.Field;
29 * @author Sergey.Malenkov
31 public final class TextAttributesReaderTest extends LightPlatformTestCase {
32 private final TextAttributesReader myReader = new TextAttributesReader();
34 public void testBackgroundColor() throws Exception {
35 assertNull(readBackgroundColor(null));
36 assertNull(readBackgroundColor(""));
37 assertNull(readBackgroundColor("WRONG"));
38 String hex = Integer.toHexString(0xFFFFFF & Color.WHITE.getRGB());
39 assertEquals(Color.WHITE, readBackgroundColor(hex));
40 assertEquals(Color.WHITE, readBackgroundColor('#' + hex));
41 assertEquals(Color.WHITE, readBackgroundColor("0x" + hex));
44 private Color readBackgroundColor(Object value) throws Exception {
45 return read(value("BACKGROUND", value)).getBackgroundColor();
48 public void testForegroundColor() throws Exception {
49 assertNull(readForegroundColor(null));
50 assertNull(readForegroundColor(""));
51 assertNull(readForegroundColor("WRONG"));
52 String hex = Integer.toHexString(0xFFFFFF & Color.BLACK.getRGB());
53 assertEquals(Color.BLACK, readForegroundColor(hex));
54 assertEquals(Color.BLACK, readForegroundColor('#' + hex));
55 assertEquals(Color.BLACK, readForegroundColor("0x" + hex));
58 private Color readForegroundColor(Object value) throws Exception {
59 return read(value("FOREGROUND", value)).getForegroundColor();
62 public void testErrorStripeColor() throws Exception {
63 assertNull(readErrorStripeColor(null));
64 assertNull(readErrorStripeColor(""));
65 assertNull(readErrorStripeColor("WRONG"));
66 String hex = Integer.toHexString(0xFFFFFF & Color.RED.getRGB());
67 assertEquals(Color.RED, readErrorStripeColor(hex));
68 assertEquals(Color.RED, readErrorStripeColor('#' + hex));
69 assertEquals(Color.RED, readErrorStripeColor("0x" + hex));
72 private Color readErrorStripeColor(Object value) throws Exception {
73 return read(value("ERROR_STRIPE_COLOR", value)).getErrorStripeColor();
76 public void testEffectColor() throws Exception {
77 assertNull(readEffectColor(null));
78 assertNull(readEffectColor(""));
79 assertNull(readEffectColor("WRONG"));
80 String hex = Integer.toHexString(0xFFFFFF & Color.YELLOW.getRGB());
81 assertEquals(Color.YELLOW, readEffectColor(hex));
82 assertEquals(Color.YELLOW, readEffectColor('#' + hex));
83 assertEquals(Color.YELLOW, readEffectColor("0x" + hex));
86 private Color readEffectColor(Object value) throws Exception {
87 return read(value("EFFECT_COLOR", value)).getEffectColor();
90 public void testEffectType() throws Exception {
91 assertEquals(EffectType.BOXED, readEffectType(null));
92 assertEquals(EffectType.BOXED, readEffectType(""));
93 assertEquals(EffectType.BOXED, readEffectType("WRONG"));
94 assertEquals(EffectType.BOXED, readEffectType("0"));
95 assertEquals(EffectType.BOXED, readEffectType("BORDER"));
96 assertEquals(EffectType.LINE_UNDERSCORE, readEffectType("1"));
97 assertEquals(EffectType.LINE_UNDERSCORE, readEffectType("LINE"));
98 assertEquals(EffectType.WAVE_UNDERSCORE, readEffectType("2"));
99 assertEquals(EffectType.WAVE_UNDERSCORE, readEffectType("WAVE"));
100 assertEquals(EffectType.STRIKEOUT, readEffectType("3"));
101 assertEquals(EffectType.STRIKEOUT, readEffectType("STRIKEOUT"));
102 assertEquals(EffectType.BOLD_LINE_UNDERSCORE, readEffectType("4"));
103 assertEquals(EffectType.BOLD_LINE_UNDERSCORE, readEffectType("BOLD_LINE"));
104 assertEquals(EffectType.BOLD_DOTTED_LINE, readEffectType("5"));
105 assertEquals(EffectType.BOLD_DOTTED_LINE, readEffectType("BOLD_DOTTED_LINE"));
106 assertEquals(EffectType.BOXED, readEffectType("6"));
109 private EffectType readEffectType(Object value) throws Exception {
110 return read(value("EFFECT_TYPE", value)).getEffectType();
113 public void testFontType() throws Exception {
114 assertEquals(Font.PLAIN, readFontType(null));
115 assertEquals(Font.PLAIN, readFontType(""));
116 assertEquals(Font.PLAIN, readFontType("WRONG"));
117 assertEquals(Font.PLAIN, readFontType("0"));
118 assertEquals(Font.PLAIN, readFontType("PLAIN"));
119 assertEquals(Font.PLAIN, readFontType(Font.PLAIN));
120 assertEquals(Font.BOLD, readFontType("1"));
121 assertEquals(Font.BOLD, readFontType("BOLD"));
122 assertEquals(Font.BOLD, readFontType(Font.BOLD));
123 assertEquals(Font.ITALIC, readFontType("2"));
124 assertEquals(Font.ITALIC, readFontType("ITALIC"));
125 assertEquals(Font.ITALIC, readFontType(Font.ITALIC));
126 assertEquals(Font.BOLD | Font.ITALIC, readFontType("3"));
127 assertEquals(Font.BOLD | Font.ITALIC, readFontType("BOLD_ITALIC"));
128 assertEquals(Font.BOLD | Font.ITALIC, readFontType(Font.BOLD | Font.ITALIC));
129 assertEquals(Font.PLAIN, readFontType("4"));
132 private int readFontType(Object value) throws Exception {
133 return read(value("FONT_TYPE", value)).getFontType();
136 public void testTextAttributesCompatibility() throws Exception {
139 compare(value("UNSUPPORTED", null));
140 compare(value("UNSUPPORTED", "OPTION"));
142 new Option("BACKGROUND", "000000"),
143 new Option("BACKGROUND", "FFFFFF")
146 new Option("BACKGROUND", "000000"),
147 new Option("FOREGROUND", "111111"),
148 new Option("ERROR_STRIPE_COLOR", "222222"),
149 new Option("EFFECT_COLOR", "333333"),
150 new Option("EFFECT_TYPE", "1"),
151 new Option("FONT_TYPE", "2")
153 compare(value("EFFECT_TYPE", null));
154 compare(value("FONT_TYPE", null));
155 for (int i = 0; i < 6; i++) {
156 compare(value("EFFECT_TYPE", i));
157 compare(value("FONT_TYPE", i));
161 private TextAttributes read(String value) throws Exception {
162 return read(Option.element(value));
165 private TextAttributes read(Element element) throws Exception {
166 return myReader.read(TextAttributes.class, element);
169 private void compare(String value) throws Exception {
170 Element element = Option.element(value);
171 TextAttributes expected = element == null ? new TextAttributes() : new TextAttributes(element);
172 TextAttributes actual = read(element);
173 assertEquals(expected, actual);
174 // EditorColorsSchemeImplTest.testWriteInheritedFromDefault
175 // EditorColorsSchemeImplTest.testWriteInheritedFromDarcula
176 Field field = TextAttributes.class.getDeclaredField("myEnforcedDefaults");
177 field.setAccessible(true);
178 assertEquals(field.get(expected), field.get(actual));
182 private static String value(String name, Object value) {
183 return value(new Option(name, value == null ? null : value.toString()));
187 private static String value(Option... options) {
188 StringBuilder sb = new StringBuilder("<value>");
189 for (Option option : options) {
190 sb.append("\n\t").append(option);
192 return sb.append("\n</value>").toString();