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.Container;
024: import java.awt.Font;
025: import javax.swing.JTextArea;
026: import junit.framework.TestCase;
027:
028: /**
029: * Tests GlyphView class, its methods to get attributes.
030: *
031: */
032: public class GlyphView_AttributesTest extends TestCase {
033: private DefaultStyledDocument styledDoc;
034:
035: private PlainDocument plainDoc;
036:
037: private Element leaf;
038:
039: private Element line;
040:
041: private GlyphView styledView;
042:
043: private GlyphView plainView;
044:
045: private GlyphView styledWithParent;
046:
047: private GlyphView plainWithParent;
048:
049: private Container parent;
050:
051: private static final int NONE = -1;
052:
053: private static final int FORE = 0;
054:
055: private static final int BACK = 1;
056:
057: private static final int FONT = 2;
058:
059: private static final int AREA_FORE = 3;
060:
061: private static final int AREA_FONT = 4;
062:
063: private static final String[] PROPERTY_NAMES = { "Foreground",
064: "Background", "Font", "Area.Foreground", "Area.Font" };
065:
066: /**
067: * 0: StyledDocument.getForeground
068: * 1: StyledDocument.getBackground
069: * 2: StyledDocument.getFont
070: * 3: JTextArea.getForeground
071: * 4: JTextArea.getFont
072: */
073: private final boolean[] calledMethods = new boolean[5];
074:
075: private static final String text = "test text";
076:
077: private static final Color THE_FOREGROUND = new Color(127, 152, 51);
078:
079: private static final Font THE_FONT = new Font("Crazy Font",
080: Font.PLAIN, 36);
081:
082: @Override
083: protected void setUp() throws Exception {
084: super .setUp();
085: styledDoc = new DefaultStyledDocument() {
086: private static final long serialVersionUID = 1L;
087:
088: @Override
089: public Color getForeground(AttributeSet attrs) {
090: calledMethods[FORE] = true;
091: assertSame(leaf, attrs);
092: return super .getForeground(attrs);
093: }
094:
095: @Override
096: public Color getBackground(AttributeSet attrs) {
097: calledMethods[BACK] = true;
098: assertSame(leaf, attrs);
099: return super .getBackground(attrs);
100: }
101:
102: @Override
103: public Font getFont(AttributeSet attrs) {
104: calledMethods[FONT] = true;
105: assertSame(leaf, attrs);
106: return super .getFont(attrs);
107: }
108: };
109: parent = new JTextArea() {
110: private static final long serialVersionUID = 1L;
111:
112: @Override
113: public Color getForeground() {
114: calledMethods[AREA_FORE] = true;
115: return THE_FOREGROUND;
116: }
117:
118: @Override
119: public Font getFont() {
120: calledMethods[AREA_FONT] = true;
121: return THE_FONT;
122: }
123: };
124: styledDoc.insertString(0, text, null);
125: leaf = styledDoc.getCharacterElement(0);
126: styledView = new GlyphView(leaf);
127: styledWithParent = new GlyphView(leaf) {
128: @Override
129: public Container getContainer() {
130: return parent;
131: }
132: };
133: plainDoc = new PlainDocument();
134: plainDoc.insertString(0, text, null);
135: line = plainDoc.getDefaultRootElement().getElement(0);
136: plainView = new GlyphView(line);
137: plainWithParent = new GlyphView(line) {
138: @Override
139: public Container getContainer() {
140: return parent;
141: }
142: };
143: for (int i = 0; i < calledMethods.length; i++) {
144: calledMethods[i] = false;
145: }
146: }
147:
148: public void testGetForeground() {
149: assertSame(StyleConstants.getForeground(leaf.getAttributes()),
150: styledView.getForeground());
151: assertCalledMethods(FORE);
152: assertSame(StyleConstants.getForeground(leaf.getAttributes()),
153: styledWithParent.getForeground());
154: assertCalledMethods(FORE);
155: assertNull(plainView.getForeground());
156: assertCalledMethods(NONE);
157: assertSame(plainWithParent.getForeground(), plainWithParent
158: .getForeground());
159: assertCalledMethods(AREA_FORE);
160: }
161:
162: public void testGetBackground() {
163: assertNull(styledView.getBackground());
164: assertCalledMethods(NONE);
165: assertNull(styledWithParent.getBackground());
166: assertCalledMethods(NONE);
167: assertNull(plainView.getBackground());
168: assertCalledMethods(NONE);
169: assertNull(plainWithParent.getBackground());
170: assertCalledMethods(NONE);
171: setAttribute(StyleConstants.Background, THE_FOREGROUND);
172: assertSame(THE_FOREGROUND, styledView.getBackground());
173: assertCalledMethods(BACK);
174: assertSame(THE_FOREGROUND, styledWithParent.getBackground());
175: assertCalledMethods(BACK);
176: }
177:
178: public void testGetFont() {
179: Font font = styledView.getFont();
180: assertEquals(
181: StyleConstants.getFontFamily(leaf.getAttributes()),
182: font.getFamily());
183: assertEquals(StyleConstants.getFontSize(leaf.getAttributes()),
184: font.getSize());
185: assertCalledMethods(FONT);
186: font = styledWithParent.getFont();
187: assertEquals(
188: StyleConstants.getFontFamily(leaf.getAttributes()),
189: font.getFamily());
190: assertEquals(StyleConstants.getFontSize(leaf.getAttributes()),
191: font.getSize());
192: assertCalledMethods(FONT);
193: assertNull(plainView.getFont());
194: assertCalledMethods(NONE);
195: assertSame(THE_FONT, plainWithParent.getFont());
196: assertCalledMethods(AREA_FONT);
197: }
198:
199: public void testIsUnderline() {
200: assertFalse(styledView.isUnderline());
201: assertFalse(styledWithParent.isUnderline());
202: assertFalse(plainView.isUnderline());
203: assertFalse(plainWithParent.isUnderline());
204: setAttribute(StyleConstants.Underline, Boolean.TRUE);
205: assertTrue(styledView.isUnderline());
206: assertTrue(styledWithParent.isUnderline());
207: }
208:
209: public void testIsStrikeThrough() {
210: assertFalse(styledView.isStrikeThrough());
211: assertFalse(styledWithParent.isStrikeThrough());
212: assertFalse(plainView.isUnderline());
213: assertFalse(plainWithParent.isStrikeThrough());
214: setAttribute(StyleConstants.StrikeThrough, Boolean.TRUE);
215: assertTrue(styledView.isStrikeThrough());
216: assertTrue(styledWithParent.isStrikeThrough());
217: }
218:
219: public void testIsSubscript() {
220: assertFalse(styledView.isSubscript());
221: assertFalse(styledWithParent.isSubscript());
222: assertFalse(plainView.isSubscript());
223: assertFalse(plainWithParent.isSubscript());
224: setAttribute(StyleConstants.Subscript, Boolean.TRUE);
225: assertTrue(styledView.isSubscript());
226: assertTrue(styledWithParent.isSubscript());
227: }
228:
229: public void testIsSuperscript() {
230: assertFalse(styledView.isSuperscript());
231: assertFalse(styledWithParent.isSuperscript());
232: assertFalse(plainView.isSuperscript());
233: assertFalse(plainWithParent.isSuperscript());
234: setAttribute(StyleConstants.Superscript, Boolean.TRUE);
235: assertTrue(styledView.isSuperscript());
236: assertTrue(styledWithParent.isSuperscript());
237: }
238:
239: private void setAttribute(final Object key, final Object value) {
240: MutableAttributeSet attrs = new SimpleAttributeSet();
241: attrs.addAttribute(key, value);
242: styledDoc.setCharacterAttributes(leaf.getStartOffset(), leaf
243: .getEndOffset()
244: - leaf.getStartOffset(), attrs, false);
245: }
246:
247: private void assertCalledMethods(final int index) {
248: for (int i = 0; i < calledMethods.length; i++) {
249: if (i == index) {
250: assertTrue(PROPERTY_NAMES[i] + " isn't true",
251: calledMethods[i]);
252: } else {
253: assertFalse(PROPERTY_NAMES[i] + " isn't false",
254: calledMethods[i]);
255: }
256: calledMethods[i] = false;
257: }
258: }
259: }
|