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: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Color;
023: import java.awt.Font;
024: import junit.framework.TestCase;
025:
026: /**
027: * Tests for getBackground, getFont, getFontMetrics, getForeground
028: * methods.
029: *
030: */
031: public class StyleContext_PredefinedAttrTest extends TestCase {
032: private static final String FONT_FAMILY = "Arial";
033:
034: private StyleContext sc;
035:
036: public void testGetBackground() {
037: Color color = sc.getBackground(sc.getEmptySet());
038: assertSame(Color.black, color);
039: Color colorAttrValue = new Color(0xFAF0E6);
040: AttributeSet as = sc.addAttribute(sc.getEmptySet(),
041: StyleConstants.Background, colorAttrValue);
042: color = sc.getBackground(as);
043: assertSame(colorAttrValue, color);
044: Color newAttrValue = new Color(0xFAF0E6);
045: as = sc.addAttribute(as, StyleConstants.Background,
046: newAttrValue);
047: color = sc.getBackground(as);
048: assertSame(colorAttrValue, color);
049: assertNotSame(colorAttrValue, newAttrValue);
050: }
051:
052: /*
053: * Font getFont(AttributeSet)
054: */
055: public void testGetFontAttributeSet() {
056: Font fontEmpty = sc.getFont(sc.getEmptySet());
057: Font fontEmpty2 = sc.getFont(sc.getEmptySet());
058: assertSame(fontEmpty, fontEmpty2);
059: AttributeSet as = sc.addAttribute(sc.getEmptySet(),
060: StyleConstants.FontFamily, FONT_FAMILY);
061: Font fontArial = sc.getFont(as);
062: assertEquals(FONT_FAMILY, fontArial.getName());
063: assertFalse(fontArial.getName().equals(fontEmpty.getName()));
064: assertNotSame(fontEmpty, fontArial);
065: as = sc.addAttribute(as, StyleConstants.FontSize, new Integer(
066: 12));
067: Font fontSize = sc.getFont(as);
068: assertSame(fontArial, fontSize); // As default font size is 12
069: as = sc.addAttribute(as, StyleConstants.Bold, Boolean.FALSE);
070: assertSame(fontSize, sc.getFont(as));
071: as = sc.addAttribute(as, StyleConstants.Bold, Boolean.TRUE);
072: Font fontBold = sc.getFont(as);
073: assertNotSame(fontSize, fontBold);
074: assertTrue(fontBold.isBold());
075: }
076:
077: public void testGetFontMetrics() {
078: Font plain = sc.getFont(FONT_FAMILY, Font.PLAIN, 12);
079: assertSame(sc.getFontMetrics(plain), sc.getFontMetrics(plain));
080: }
081:
082: /*
083: * Font getFont(String, int, int)
084: */
085: public void testGetFontStringintint() {
086: Font one = sc.getFont(FONT_FAMILY, Font.PLAIN, 12);
087: Font two = sc.getFont(FONT_FAMILY, Font.PLAIN, 12);
088: assertSame(one, two);
089: assertTrue(one.isPlain());
090: assertEquals(12, one.getSize());
091: one = sc.getFont(FONT_FAMILY, Font.BOLD | Font.ITALIC, 12);
092: two = sc.getFont(FONT_FAMILY, Font.BOLD | Font.ITALIC, 12);
093: assertSame(one, two);
094: assertTrue(one.isBold());
095: assertTrue(one.isItalic());
096: }
097:
098: public void testGetForeground() {
099: Color color = sc.getForeground(sc.getEmptySet());
100: assertSame(Color.black, color);
101: Color colorAttrValue = new Color(0xFFE4E1);
102: AttributeSet as = sc.addAttribute(sc.getEmptySet(),
103: StyleConstants.Foreground, colorAttrValue);
104: color = sc.getForeground(as);
105: assertSame(colorAttrValue, color);
106: Color newAttrValue = new Color(0xFFE4E1);
107: as = sc.addAttribute(as, StyleConstants.Foreground,
108: newAttrValue);
109: color = sc.getForeground(as);
110: assertSame(colorAttrValue, color);
111: assertNotSame(colorAttrValue, newAttrValue);
112: }
113:
114: @Override
115: protected void setUp() throws Exception {
116: sc = StyleContextTest.sc = new StyleContext();
117: }
118: }
|