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 java.util.Enumeration;
025: import javax.swing.event.DocumentEvent;
026: import javax.swing.event.DocumentListener;
027: import javax.swing.text.StyleContext.NamedStyle;
028: import junit.framework.TestCase;
029:
030: /**
031: * Tests DefaultStyledDocument class: methods related to style management and
032: * getting standard attributes from an AttributeSet.
033: *
034: */
035: public class DefaultStyledDocument_StylesAndStdAttrsTest extends
036: TestCase {
037: private DefaultStyledDocument doc;
038:
039: private StyleContext styles;
040:
041: private AttributeSet attrSet;
042:
043: private boolean getBackground;
044:
045: private boolean getFont;
046:
047: private boolean getForeground;
048:
049: private static final String STYLE_NAME = "aStyle";
050:
051: public void testAddStyle() {
052: Style aStyle = doc.addStyle(STYLE_NAME, null);
053: assertSame(styles.getStyle(STYLE_NAME), aStyle);
054: }
055:
056: public void testGetBackground() {
057: assertFalse(getBackground);
058: doc.getBackground(attrSet);
059: assertTrue(getBackground);
060: }
061:
062: public void testGetFont() {
063: assertFalse(getFont);
064: doc.getFont(attrSet);
065: assertTrue(getFont);
066: }
067:
068: public void testGetForeground() {
069: assertFalse(getForeground);
070: doc.getForeground(attrSet);
071: assertTrue(getForeground);
072: }
073:
074: public void testGetStyle() {
075: Style aStyle = styles.addStyle(STYLE_NAME, null);
076: assertSame(aStyle, doc.getStyle(STYLE_NAME));
077: }
078:
079: public void testGetStyleNames() {
080: final String[] names = new String[] { "one", "two", "three" };
081: for (int i = 0; i < names.length; i++) {
082: styles.addStyle(names[i], null);
083: }
084: boolean[] found = new boolean[names.length];
085: Enumeration<?> styleNames = doc.getStyleNames();
086: while (styleNames.hasMoreElements()) {
087: Object name = styleNames.nextElement();
088: for (int i = 0; i < names.length; i++) {
089: found[i] = found[i] || name.equals(names[i]);
090: }
091: }
092: for (int i = 0; i < found.length; i++) {
093: assertTrue("@ " + i, found[i]);
094: }
095: }
096:
097: public void testRemoveStyle() {
098: doc.addStyle(STYLE_NAME, null);
099: assertNotNull(doc.getStyle(STYLE_NAME));
100: doc.removeStyle(STYLE_NAME);
101: assertNull(doc.getStyle(STYLE_NAME));
102: }
103:
104: public void testStyleChanged() {
105: final Style[] changed = new Style[1];
106: doc = new DefaultStyledDocument() {
107: private static final long serialVersionUID = 1L;
108:
109: @Override
110: protected void styleChanged(final Style style) {
111: changed[0] = style;
112: super .styleChanged(style);
113: }
114: };
115: styles = (StyleContext) doc.getAttributeContext();
116: final NamedStyle aStyle = (NamedStyle) doc.addStyle(STYLE_NAME,
117: null);
118: assertEquals(0, styles.getChangeListeners().length);
119: assertEquals(0, aStyle.listenerList.getListenerCount());
120: final DocumentListener listener = new DocumentListener() {
121: public void insertUpdate(DocumentEvent e) {
122: }
123:
124: public void removeUpdate(DocumentEvent e) {
125: }
126:
127: public void changedUpdate(DocumentEvent e) {
128: }
129: };
130: doc.addDocumentListener(listener);
131: assertEquals(1, styles.getChangeListeners().length);
132: assertEquals(1, aStyle.listenerList.getListenerCount());
133: final NamedStyle anotherStyle = (NamedStyle) styles.addStyle(
134: "otherStyle", aStyle);
135: assertEquals(1, anotherStyle.listenerList.getListenerCount());
136: final NamedStyle nullStyle = (NamedStyle) styles.addStyle(null,
137: null);
138: assertEquals(0, nullStyle.listenerList.getListenerCount());
139: assertNull(changed[0]);
140: aStyle.addAttribute("key", "value");
141: assertSame(aStyle, changed[0]);
142: doc.removeDocumentListener(listener);
143: assertEquals(0, styles.getChangeListeners().length);
144: assertEquals(0, aStyle.listenerList.getListenerCount());
145: assertEquals(0, anotherStyle.listenerList.getListenerCount());
146: }
147:
148: @Override
149: protected void setUp() throws Exception {
150: super .setUp();
151: doc = new DefaultStyledDocument(new StyleContext() {
152: private static final long serialVersionUID = 1L;
153:
154: @Override
155: public Color getBackground(AttributeSet as) {
156: getBackground = true;
157: return super .getBackground(as);
158: }
159:
160: @Override
161: public Font getFont(AttributeSet as) {
162: getFont = true;
163: return super .getFont(as);
164: }
165:
166: @Override
167: public Color getForeground(AttributeSet as) {
168: getForeground = true;
169: return super .getForeground(as);
170: }
171: });
172: styles = (StyleContext) doc.getAttributeContext();
173: attrSet = styles.getEmptySet();
174: }
175: }
|