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.util.Enumeration;
023: import java.util.EventListener;
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.event.CaretListener;
026: import javax.swing.event.ChangeListener;
027: import javax.swing.text.StyleContext.NamedStyle;
028:
029: public class StyleContext_NamedStyleTest extends StyleTest {
030: protected NamedStyle ns;
031:
032: protected NamedStyle withName;
033:
034: @Override
035: protected void setUp() throws Exception {
036: super .setUp();
037: ns = (NamedStyle) style;
038: StyleContext defContext = StyleContext.getDefaultStyleContext();
039: withName = defContext.new NamedStyle("styleName", null);
040: }
041:
042: /*
043: * NamedStyle(String, Style)
044: */
045: public void testNamedStyleStringStyle() {
046: Style parent = StyleContext.getDefaultStyleContext().new NamedStyle();
047: parent.addAttribute("key", "value");
048: String name = "style_name";
049: ns = StyleContext.getDefaultStyleContext().new NamedStyle(name,
050: parent);
051: assertEquals(parent, ns.getResolveParent());
052: assertEquals(name, ns.getName());
053: }
054:
055: /*
056: * NamedStyle(String, Style)
057: */
058: public void testNamedStyleStringNullStyle() {
059: Style parent = StyleContext.getDefaultStyleContext().new NamedStyle(
060: "parentName", null);
061: parent.addAttribute("key", "value");
062: ns = StyleContext.getDefaultStyleContext().new NamedStyle(null,
063: parent);
064: assertEquals(parent, ns.getResolveParent());
065: assertSame(parent, ns.getResolveParent());
066: // Using getName we should have null
067: assertNull(ns.getName());
068: // But using get attribute, we should get parent name 'cause this
069: // method resolves attribute values through parent
070: assertEquals("parentName", ns
071: .getAttribute(AttributeSet.NameAttribute));
072: }
073:
074: /*
075: * NamedStyle(String, Style)
076: */
077: public void testNamedStyleStringStyleNull() {
078: ns = StyleContext.getDefaultStyleContext().new NamedStyle(
079: "styleName", null);
080: assertNull(ns.getResolveParent());
081: assertEquals("styleName", ns.getName());
082: }
083:
084: /*
085: * NamedStyle(String, Style)
086: */
087: public void testNamedStyleStringNullStyleNull() {
088: ns = StyleContext.getDefaultStyleContext().new NamedStyle(null,
089: null);
090: assertNull(ns.getResolveParent());
091: assertNull(ns.getName());
092: }
093:
094: /*
095: * NamedStyle(Style)
096: */
097: public void testNamedStyleStyle() {
098: Style parent = StyleContext.getDefaultStyleContext().new NamedStyle();
099: parent.addAttribute("key", "value");
100: ns = StyleContext.getDefaultStyleContext().new NamedStyle(
101: parent);
102: assertEquals(parent, ns.getResolveParent());
103: assertEquals(1, ns.getAttributeCount());
104: assertEquals("value", ns.getAttribute("key"));
105: }
106:
107: /*
108: * NamedStyle()
109: */
110: public void testNamedStyle() {
111: ns = StyleContext.getDefaultStyleContext().new NamedStyle();
112: assertEquals(0, ns.getAttributeCount());
113: assertNull(ns.getAttribute(AttributeSet.NameAttribute));
114: assertNull(ns.getAttribute(AttributeSet.ResolveAttribute));
115: }
116:
117: public void testSetNameNull() {
118: ns = StyleContext.getDefaultStyleContext().new NamedStyle(
119: "styleName", null);
120: ns.setName(null);
121: assertEquals("styleName", ns.getName());
122: }
123:
124: public void testSetName() {
125: ns = StyleContext.getDefaultStyleContext().new NamedStyle();
126: ns.setName("styleName");
127: assertEquals("styleName", ns.getName());
128: assertEquals("styleName", ns
129: .getAttribute(AttributeSet.NameAttribute));
130: }
131:
132: public void testSetNameListeners() {
133: ns.addChangeListener(this );
134: bStateChanged = false;
135: ns.setName("styleName");
136: assertTrue(bStateChanged);
137: }
138:
139: public void testGetNameSetAttribute() {
140: assertEquals("styleName", withName.getName());
141: assertEquals("styleName", withName
142: .getAttribute(AttributeSet.NameAttribute));
143: // Add name attribute
144: withName.addAttribute(AttributeSet.NameAttribute, new String(
145: "Changed Name"));
146: assertEquals("Changed Name", withName.getName());
147: assertEquals("Changed Name", withName
148: .getAttribute(AttributeSet.NameAttribute));
149: }
150:
151: public void testContainsNameAttribute() {
152: SimpleAttributeSet as = new SimpleAttributeSet();
153: as.addAttribute(AttributeSet.NameAttribute, "styleName");
154: assertTrue(withName.containsAttribute(
155: AttributeSet.NameAttribute, "styleName"));
156: assertTrue(withName.containsAttributes(as));
157: }
158:
159: public void testNameNotString() {
160: ns = StyleContext.getDefaultStyleContext().new NamedStyle();
161: ns.addAttribute(AttributeSet.NameAttribute, new Integer(15));
162: assertEquals(new Integer(15), ns
163: .getAttribute(AttributeSet.NameAttribute));
164: assertEquals((new Integer(15)).toString(), ns.getName());
165: }
166:
167: public void testRemoveNameAttribute() {
168: withName.removeAttribute(AttributeSet.NameAttribute);
169: assertNull(withName.getName());
170: }
171:
172: public void testGetAttrNames() {
173: withName.addAttribute("key", "value");
174: boolean wasKey = false;
175: boolean wasName = false;
176: Enumeration<?> keys = withName.getAttributeNames();
177: while (keys.hasMoreElements()) {
178: Object key = keys.nextElement();
179: wasKey = wasKey || key.equals("key");
180: wasName = wasName || key.equals(AttributeSet.NameAttribute);
181: }
182: assertTrue(wasKey);
183: assertTrue(wasName);
184: }
185:
186: public void testGetAttrCountName() {
187: assertEquals(1, withName.getAttributeCount());
188: }
189:
190: public void testGetListeners() {
191: ns.addChangeListener(this );
192: EventListener[] listeners = ns
193: .getListeners(ChangeListener.class);
194: assertEquals(1, listeners.length);
195: assertSame(this , listeners[0]);
196: listeners = ns.getListeners(CaretListener.class);
197: assertEquals(0, listeners.length);
198: ns.removeChangeListener(this );
199: listeners = ns.getListeners(ChangeListener.class);
200: assertEquals(0, listeners.length);
201: }
202:
203: public void testGetChangeListeners() {
204: ChangeListener[] listeners = ns.getChangeListeners();
205: assertEquals(0, listeners.length);
206: ns.addChangeListener(this );
207: listeners = ns.getChangeListeners();
208: assertEquals(1, listeners.length);
209: assertSame(this , listeners[0]);
210: ns.removeChangeListener(this );
211: listeners = ns.getChangeListeners();
212: assertEquals(0, listeners.length);
213: }
214:
215: public void testToString() {
216: String str = ns.toString();
217: assertNotNull(ns.toString());
218: assertTrue(str.startsWith("NamedStyle:null {"));
219: String[] attrs = str.substring(17, str.length() - 1).split(",");
220: String[] expected = { "key1=value1", "key2=value2",
221: "key3=value3" };
222: boolean[] found = { false, false, false };
223: assertEquals(expected.length, attrs.length);
224: for (int i = 0; i < expected.length; i++) {
225: for (int j = 0; j < attrs.length && !found[i]; j++) {
226: found[i] = found[i] || expected[i].equals(attrs[j]);
227: }
228: }
229: for (int i = 0; i < found.length; i++) {
230: if (!found[i]) {
231: fail(expected[i] + " was not found");
232: }
233: }
234: }
235:
236: public void testFireStateChanged() {
237: ns.addChangeListener(this );
238: bStateChanged = false;
239: ns.fireStateChanged();
240: assertTrue(bStateChanged);
241: ns.removeChangeListener(this );
242: bStateChanged = false;
243: ns.fireStateChanged();
244: assertFalse(bStateChanged);
245: }
246:
247: public void testChangeEvent() {
248: assertNull(ns.changeEvent);
249: ns.fireStateChanged();
250: assertNull(ns.changeEvent);
251: ns.addChangeListener(this );
252: assertNull(ns.changeEvent);
253: ns.fireStateChanged();
254: assertEquals(ns, ns.changeEvent.getSource());
255: }
256:
257: public void testSerializable() throws Exception {
258: ns.setName("styleName");
259: NamedStyle read = (NamedStyle) BasicSwingTestCase
260: .serializeObject(ns);
261: assertTrue(ns.isEqual(read));
262: }
263:
264: public void testListenerList() {
265: assertNotNull(ns.listenerList);
266: assertTrue(ns.listenerList.getListenerCount() == 0);
267: ns.addChangeListener(this );
268: assertTrue(ns.listenerList.getListenerCount() == 1);
269: }
270:
271: public void testSetResolveParentNull() {
272: AttributeSet parent = mas.getResolveParent();
273: int count = mas.getAttributeCount();
274: if (parent == null) {
275: // Set parent to a non-null value
276: mas.setResolveParent(new SimpleAttributeSet());
277: // The number of attributes has increased by one
278: assertEquals(++count, mas.getAttributeCount());
279: }
280: // Set the parent to null
281: mas.setResolveParent(null);
282: assertNull(mas.getResolveParent());
283: assertEquals(count - 1, mas.getAttributeCount());
284: }
285:
286: /**
287: * Test copyAttributesMethod when a NamedStyle has name and parent.
288: *
289: */
290: public void testCopyAttributesWithName() {
291: StyleContext def = new StyleContext();
292: Style parent = def.addStyle("parentStyle", null);
293: Style style = def.addStyle("aStyle", parent);
294: // Copy parent
295: Style copyParent = (Style) parent.copyAttributes();
296: assertTrue(copyParent instanceof StyleContext.NamedStyle);
297: assertEquals("parentStyle", copyParent.getName());
298: assertNull(copyParent.getResolveParent());
299: // Copy style
300: Style copyStyle = (Style) style.copyAttributes();
301: assertTrue(copyStyle instanceof StyleContext.NamedStyle);
302: assertEquals("aStyle", copyStyle.getName());
303: assertSame(parent, copyStyle.getResolveParent());
304: }
305: }
|