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 javax.swing.event.ChangeEvent;
023: import javax.swing.event.ChangeListener;
024:
025: public class StyleTest extends MutableAttributeSetTest implements
026: ChangeListener {
027: protected Style style;
028:
029: /*
030: * @see MutableAttributeSetTest#setUp()
031: */
032: @Override
033: protected void setUp() throws Exception {
034: style = new StyleContext().new NamedStyle();
035: for (int i = 0; i < keys.length; i++) {
036: style.addAttribute(keys[i], values[i]);
037: }
038: mas = style;
039: as = style;
040: Style styleWithResolver = new StyleContext().new NamedStyle();
041: for (int i = 0; i < keys.length; i++) {
042: styleWithResolver.addAttribute(keys[i], values[i]);
043: }
044: Style resolverStyle = new StyleContext().new NamedStyle();
045: resolverStyle.addAttribute(keyInResolver, valueInResolver);
046: styleWithResolver.setResolveParent(resolverStyle);
047: asWithResolver = styleWithResolver;
048: resolverSet = resolverStyle;
049: }
050:
051: // Tests both methods: add and remove.
052: public void testChangeListener() {
053: style.addChangeListener(this );
054: bStateChanged = false;
055: style.addAttribute("key", "value");
056: assertTrue(bStateChanged);
057: style.removeChangeListener(this );
058: bStateChanged = false;
059: style.removeAttribute("key");
060: assertFalse(bStateChanged);
061: }
062:
063: // addAttribute that is already in the set
064: public void testChangeListenerAddAttrSameValue() {
065: style.addChangeListener(this );
066: bStateChanged = false;
067: AttributeSet copy = style.copyAttributes();
068: style.addAttribute(keys[0], values[0]);
069: // Actually no change happened
070: assertTrue(style.isEqual(copy));
071: // The listener is to be called
072: assertTrue(bStateChanged);
073: }
074:
075: // addAttribute whose key is in the set but with different value
076: public void testChangeListenerAddAttrDiffValue() {
077: style.addChangeListener(this );
078: bStateChanged = false;
079: style.addAttribute(keys[0], values[1]);
080: // The listener is to be called
081: assertTrue(bStateChanged);
082: }
083:
084: // removeAttribute which is defined
085: public void testChangeListenerRemoveAttrDef() {
086: style.addChangeListener(this );
087: bStateChanged = false;
088: style.removeAttribute(keys[0]);
089: // The listener is to be called
090: assertTrue(bStateChanged);
091: }
092:
093: // removeAttribute which is NOT defined
094: public void testChangeListenerRemoveAttrNotDef() {
095: style.addChangeListener(this );
096: bStateChanged = false;
097: AttributeSet copy = style.copyAttributes();
098: style.removeAttribute("key");
099: // Actually no change happened
100: assertTrue(style.isEqual(copy));
101: // The listener is to be called
102: assertTrue(bStateChanged);
103: }
104:
105: // addAttributes
106: public void testChangeListenerAddAttrs() {
107: style.addChangeListener(this );
108: bStateChanged = false;
109: AttributeSet copy = style.copyAttributes();
110: style.addAttributes(copy);
111: // The listener is to be called
112: assertTrue(bStateChanged);
113: }
114:
115: // removeAttributes(AttributeSet)
116: public void testChangeListenerRemoveAttrsSet() {
117: style.addChangeListener(this );
118: bStateChanged = false;
119: AttributeSet copy = style.copyAttributes();
120: style.removeAttributes(copy);
121: // The listener is to be called
122: assertTrue(bStateChanged);
123: }
124:
125: // removeAttributes(AttributeSet)
126: public void testChangeListenerRemoveAttrsEnum() {
127: style.addChangeListener(this );
128: bStateChanged = false;
129: style.removeAttributes(style.getAttributeNames());
130: // The listener is to be called
131: assertTrue(bStateChanged);
132: }
133:
134: public void testGetName() {
135: String name = "style name";
136: style.addAttribute(AttributeSet.NameAttribute, name);
137: assertEquals(name, style.getName());
138: }
139:
140: protected boolean bStateChanged;
141:
142: public void stateChanged(final ChangeEvent e) {
143: bStateChanged = true;
144: }
145: }
|