001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing.plaf.synth;
019:
020: import java.awt.Color;
021: import java.awt.Font;
022: import java.awt.Insets;
023: import java.util.Hashtable;
024:
025: import javax.swing.Icon;
026: import javax.swing.ImageIcon;
027: import javax.swing.JLabel;
028:
029: import junit.framework.TestCase;
030:
031: public class SynthStyleTest extends TestCase {
032:
033: static final String ICON_KEY = "TestIcon"; //$NON-NLS-1$
034:
035: static final String STRING_KEY = "TestString"; //$NON-NLS-1$
036:
037: static final String INT_KEY = "TestInteger"; //$NON-NLS-1$
038:
039: static final String BOOLEAN_KEY = "TestBoolean"; //$NON-NLS-1$
040:
041: static final Icon ICON_VALUE = new ImageIcon("test"); //$NON-NLS-1$
042:
043: static final String STRING_VALUE = "test"; //$NON-NLS-1$
044:
045: @SuppressWarnings("boxing")
046: static final Integer INT_VALUE = Integer.MAX_VALUE; //$NON-NLS-1$
047:
048: @SuppressWarnings("boxing")
049: static final Boolean BOOLEAN_VALUE = true; //$NON-NLS-1$
050:
051: static final Color COLOR_VALUE = Color.GREEN;
052:
053: static final Font FONT_VALUE = new Font("Times", Font.BOLD, 15); //$NON-NLS-1$
054:
055: private static JLabel currentComponent = new JLabel(
056: "Test_component"); //$NON-NLS-1$
057:
058: private static SynthStyle currentStyle = new SynthStyleForTest();
059:
060: private static SynthContext currentContext = new SynthContext(
061: currentComponent, Region.LABEL, currentStyle,
062: SynthConstants.ENABLED);
063:
064: /*
065: * Test method for 'javax.swing.plaf.synth.SynthStyle.get(SynthContext,
066: * Object)'
067: */
068: public void testGet() {
069: assertEquals(currentStyle.get(currentContext, ICON_KEY),
070: ICON_VALUE);
071: assertEquals(currentStyle.get(currentContext, INT_KEY),
072: INT_VALUE);
073: assertEquals(currentStyle.get(currentContext, STRING_KEY),
074: STRING_VALUE);
075: assertEquals(currentStyle.get(currentContext, BOOLEAN_KEY),
076: BOOLEAN_VALUE);
077: }
078:
079: /*
080: * Test method for
081: * 'javax.swing.plaf.synth.SynthStyle.getBoolean(SynthContext, Object,
082: * boolean)'
083: */
084: public void testGetBoolean() {
085: assertTrue(currentStyle.getBoolean(currentContext, BOOLEAN_KEY,
086: true));
087: assertTrue(currentStyle.getBoolean(currentContext, BOOLEAN_KEY,
088: false));
089: assertTrue(currentStyle.getBoolean(currentContext,
090: "Not_found_boolean", true)); //$NON-NLS-1$
091: assertFalse(currentStyle.getBoolean(currentContext,
092: "Not_found_boolean", false)); //$NON-NLS-1$
093: }
094:
095: /*
096: * Test method for 'javax.swing.plaf.synth.SynthStyle.getColor(SynthContext,
097: * ColorType)'
098: */
099: public void testGetColor() {
100: currentComponent.setBackground(Color.RED);
101: assertEquals(currentStyle.getColor(currentContext,
102: ColorType.BACKGROUND), Color.RED);
103: assertEquals(currentStyle.getColor(currentContext,
104: ColorType.FOREGROUND), currentComponent.getForeground());
105: assertEquals(currentStyle.getColor(currentContext,
106: ColorType.FOCUS), COLOR_VALUE);
107:
108: }
109:
110: /*
111: * Test method for 'javax.swing.plaf.synth.SynthStyle.getFont(SynthContext)'
112: */
113: public void testGetFont() {
114: Font newFont = new Font("Dialog", Font.BOLD, 15); //$NON-NLS-1$
115: currentComponent.setFont(newFont);
116: assertEquals(currentStyle.getFont(currentContext), newFont);
117: currentContext.setState(SynthConstants.DISABLED);
118: assertEquals(currentStyle.getFont(currentContext), FONT_VALUE);
119: }
120:
121: /*
122: * Test method for
123: * 'javax.swing.plaf.synth.SynthStyle.getInsets(SynthContext, Insets)'
124: */
125: public void testGetInsets() {
126: Insets insets = new Insets(1, 2, 3, 4);
127: currentStyle.getInsets(currentContext, insets);
128: assertTrue((insets.top == 0) && (insets.left == 0)
129: && (insets.right == 0) && (insets.bottom == 0));
130: }
131:
132: /*
133: * Test method for 'javax.swing.plaf.synth.SynthStyle.getInt(SynthContext,
134: * Object, int)'
135: */
136: @SuppressWarnings("boxing")
137: public void testGetInt() {
138: assertTrue(currentStyle.getInt(currentContext, INT_KEY,
139: INT_VALUE - 1) == INT_VALUE);
140: assertTrue(currentStyle.getInt(currentContext,
141: "KEY_NOT_FOUND", INT_VALUE - 1) == INT_VALUE - 1); //$NON-NLS-1$
142: }
143:
144: /*
145: * Test method for
146: * 'javax.swing.plaf.synth.SynthStyle.getString(SynthContext, Object,
147: * String)'
148: */
149: public void testGetString() {
150: String anotherString = "KEY_NOT_FOUND"; //$NON-NLS-1$
151: assertEquals(currentStyle.getString(currentContext, STRING_KEY,
152: anotherString), STRING_VALUE);
153: assertEquals(currentStyle.getString(currentContext,
154: anotherString, anotherString), anotherString);
155: }
156:
157: /*
158: * Test method for
159: * 'javax.swing.plaf.synth.SynthStyle.isOpaque(SynthContext)'
160: */
161: public void testIsOpaque() {
162: assertTrue(currentStyle.isOpaque(currentContext));
163: }
164:
165: private static class SynthStyleForTest extends SynthStyle {
166:
167: Hashtable propertiesMap = new Hashtable();
168:
169: SynthStyleForTest() {
170: addProperty(ICON_KEY, ICON_VALUE);
171: addProperty(STRING_KEY, STRING_VALUE);
172: addProperty(INT_KEY, INT_VALUE);
173: addProperty(BOOLEAN_KEY, BOOLEAN_VALUE);
174: }
175:
176: @Override
177: public Object get(SynthContext context, Object key) {
178: return propertiesMap.get(key);
179: }
180:
181: @Override
182: @SuppressWarnings("unused")
183: protected Font getFontForState(SynthContext context) {
184: if (context.getComponentState() == SynthConstants.DISABLED) {
185: return FONT_VALUE;
186: }
187: return null;
188: }
189:
190: @Override
191: @SuppressWarnings("unused")
192: protected Color getColorForState(SynthContext context,
193: ColorType type) {
194: if (type == ColorType.FOCUS) {
195: return COLOR_VALUE;
196: }
197: return null;
198: }
199:
200: public void addProperty(String key, Object value) {
201: propertiesMap.put(key, value);
202: }
203: }
204:
205: }
|