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.html;
021:
022: import java.util.Enumeration;
023:
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.text.AttributeSet;
026: import javax.swing.text.MutableAttributeSet;
027: import javax.swing.text.SimpleAttributeSet;
028: import javax.swing.text.StyleConstants;
029: import javax.swing.text.html.CSS.Attribute;
030:
031: /**
032: * Tests <code>MutableAttributeSet</code> returned by
033: * <code>StyleSheet.addAttribute</code> method. The returned attribute set
034: * contains <code>CSS.Attribute</code>. It converts the value to
035: * <code>StyleConstants</code> when a <code>StyleConstants</code> attribute
036: * is requested.
037: * <p>
038: * The attribute used for testing is <code>StyleConstants.Italic</code>.
039: * It corresponds to <code>Attribute.FONT_STYLE</code>.
040: *
041: * @see StyleSheet
042: * @see StyleSheet#addAttribute(Object, Object)
043: * @see StyleSheet#createLargeAttributeSet(AttributeSet)
044: * @see CSS.Attribute
045: * @see javax.swing.text.StyleContext.SmallAttributeSet
046: */
047: public class StyleSheet_Large_Test extends BasicSwingTestCase {
048: private static final Object scAttribute = StyleConstants.Italic;
049: private static final Object scValue = Boolean.TRUE;
050: private static final Object cssAttribute = Attribute.FONT_STYLE;
051:
052: private StyleSheet ss;
053: private AttributeSet empty;
054: private AttributeSet attr;
055: private MutableAttributeSet mutable;
056: private int count;
057:
058: protected void setUp() throws Exception {
059: super .setUp();
060: ss = new StyleSheet();
061: empty = ss.getEmptySet();
062: attr = empty;
063:
064: int no = 0;
065: while (!(attr instanceof MutableAttributeSet)) {
066: String number = String.valueOf(no++);
067: attr = ss.addAttribute(attr, "key" + number, "value"
068: + number);
069: }
070: attr = ss.addAttribute(attr, scAttribute, scValue);
071: assertTrue(attr instanceof MutableAttributeSet);
072: count = no + 1;
073:
074: mutable = (MutableAttributeSet) attr;
075: }
076:
077: public void testGetAttribute() throws Exception {
078: Object value = attr.getAttribute(cssAttribute);
079: assertNotSame(Boolean.class, value.getClass());
080: assertNotSame(String.class, value.getClass());
081: assertEquals("italic", value.toString());
082:
083: value = attr.getAttribute(scAttribute);
084: assertSame(Boolean.class, value.getClass());
085: assertTrue(((Boolean) value).booleanValue());
086: assertSame(scValue, value);
087: }
088:
089: public void testIsDefined() throws Exception {
090: assertTrue(attr.isDefined(scAttribute));
091: assertTrue(attr.isDefined(cssAttribute));
092: }
093:
094: public void testGetNames() throws Exception {
095: final Enumeration keys = attr.getAttributeNames();
096: int stringKeyCount = 0;
097: int nonStringKeyCount = 0;
098: while (keys.hasMoreElements()) {
099: Object key = keys.nextElement();
100: if (key instanceof String) {
101: stringKeyCount++;
102: } else {
103: nonStringKeyCount++;
104: assertSame(cssAttribute, key);
105: }
106: }
107:
108: assertEquals(count - 1, stringKeyCount);
109: assertEquals(1, nonStringKeyCount);
110: }
111:
112: public void testGetAttributeCount() throws Exception {
113: assertEquals(count, attr.getAttributeCount());
114: }
115:
116: public void testIsEqualSame() throws Exception {
117: assertTrue(attr.isEqual(attr));
118:
119: final MutableAttributeSet simple = new SimpleAttributeSet(attr);
120: assertTrue(attr.isEqual(simple));
121: assertTrue(simple.isEqual(attr));
122: }
123:
124: public void testIsEqualAnother() throws Exception {
125: final MutableAttributeSet simple = new SimpleAttributeSet();
126: fillAttributeSet(simple);
127:
128: if (isHarmony()) {
129: assertTrue(simple.isEqual(attr));
130: assertFalse(attr.isEqual(simple));
131: } else {
132: assertFalse(simple.isEqual(attr));
133: assertTrue(attr.isEqual(simple));
134: }
135: }
136:
137: public void testCopyAttributes() throws Exception {
138: final AttributeSet copy = attr.copyAttributes();
139: assertNotSame(attr, copy);
140: assertTrue(attr.isEqual(copy));
141: assertTrue(copy.isEqual(attr));
142: assertTrue(copy instanceof MutableAttributeSet);
143: }
144:
145: public void testContainsAttribute() throws Exception {
146: assertTrue(attr.containsAttribute(cssAttribute, attr
147: .getAttribute(cssAttribute)));
148: assertTrue(attr.containsAttribute(scAttribute, scValue));
149: }
150:
151: public void testContainsAttributeAnother() throws Exception {
152: final AttributeSet another = ss.addAttribute(empty,
153: scAttribute, scValue);
154:
155: assertEquals(attr.getAttribute(cssAttribute).toString(),
156: another.getAttribute(cssAttribute).toString());
157: if (isHarmony()) {
158: assertTrue(attr.containsAttribute(cssAttribute, another
159: .getAttribute(cssAttribute)));
160: } else {
161: assertFalse(attr.containsAttribute(cssAttribute, another
162: .getAttribute(cssAttribute)));
163: }
164: }
165:
166: public void testContainsAttributesSame() throws Exception {
167: assertTrue(attr.containsAttributes(attr));
168:
169: final MutableAttributeSet simple = new SimpleAttributeSet(attr);
170:
171: assertTrue(attr.containsAttributes(simple));
172: assertTrue(simple.containsAttributes(attr));
173: }
174:
175: public void testContainsAttributesMutable() throws Exception {
176: final MutableAttributeSet simple = new SimpleAttributeSet();
177: simple.addAttribute(scAttribute, scValue);
178:
179: assertTrue(attr.containsAttributes(simple));
180: assertFalse(simple.containsAttributes(attr));
181: }
182:
183: public void testContainsAttributesSmall() throws Exception {
184: final AttributeSet another = ss.addAttribute(empty,
185: scAttribute, scValue);
186:
187: if (isHarmony()) {
188: assertSame(attr.getAttribute(Attribute.FONT_STYLE), another
189: .getAttribute(Attribute.FONT_STYLE));
190: assertTrue(attr.containsAttributes(another));
191: assertFalse(another.containsAttributes(attr));
192: } else {
193: assertNotSame(attr.getAttribute(Attribute.FONT_STYLE),
194: another.getAttribute(Attribute.FONT_STYLE));
195: assertFalse(attr.containsAttributes(another));
196: assertFalse(another.containsAttributes(attr));
197: }
198: }
199:
200: public void testAddAttribute() throws Exception {
201: mutable.addAttribute(StyleConstants.Bold, Boolean.TRUE);
202: assertEquals(count + 1, mutable.getAttributeCount());
203: assertItalicButNotBold();
204: }
205:
206: public void testAddAttributes() throws Exception {
207: MutableAttributeSet simple = new SimpleAttributeSet();
208: simple.addAttribute(StyleConstants.Bold, Boolean.TRUE);
209:
210: mutable.addAttributes(simple);
211: assertEquals(count + 1, mutable.getAttributeCount());
212: assertItalicButNotBold();
213: }
214:
215: public void testRemoveAttribute() throws Exception {
216: mutable.removeAttribute(scAttribute);
217: assertEquals(count, mutable.getAttributeCount()); // Didn't change
218:
219: mutable.removeAttribute(cssAttribute);
220: assertEquals(count - 1, mutable.getAttributeCount());
221: }
222:
223: public void testRemoveAttributesEnumeration() throws Exception {
224: mutable.removeAttributes(new Enumeration() {
225: private int returnedCount = 0;
226:
227: public boolean hasMoreElements() {
228: return returnedCount == 0;
229: }
230:
231: public Object nextElement() {
232: return returnedCount++ == 0 ? scAttribute : null;
233: }
234: });
235: assertEquals(count, mutable.getAttributeCount()); // Didn't change
236:
237: mutable.removeAttributes(new Enumeration() {
238: private int returnedCount = 0;
239:
240: public boolean hasMoreElements() {
241: return returnedCount == 0;
242: }
243:
244: public Object nextElement() {
245: return returnedCount++ == 0 ? cssAttribute : null;
246: }
247: });
248: assertEquals(count - 1, mutable.getAttributeCount());
249: }
250:
251: public void testRemoveAttributesAttributeSet() throws Exception {
252: final MutableAttributeSet sc = new SimpleAttributeSet();
253: sc.addAttribute(scAttribute, scValue);
254:
255: mutable.removeAttributes(sc);
256: assertEquals(count, mutable.getAttributeCount()); // Didn't change
257:
258: final MutableAttributeSet css = new SimpleAttributeSet();
259: css.addAttribute(cssAttribute, mutable
260: .getAttribute(cssAttribute));
261: mutable.removeAttributes(css);
262: assertEquals(count - 1, mutable.getAttributeCount());
263: }
264:
265: private void fillAttributeSet(final MutableAttributeSet simple) {
266: for (int i = 0; i < count - 1; i++) {
267: simple.addAttribute("key" + i, "value" + i);
268: }
269: simple.addAttribute(scAttribute, scValue);
270: }
271:
272: private void assertItalicButNotBold() {
273: final Enumeration keys = attr.getAttributeNames();
274: int stringKeyCount = 0;
275: int nonStringKeyCount = 0;
276: boolean cssItalic = false;
277: boolean cssBold = false;
278: boolean scBold = false;
279: while (keys.hasMoreElements()) {
280: Object key = keys.nextElement();
281: if (key instanceof String) {
282: stringKeyCount++;
283: } else {
284: nonStringKeyCount++;
285: cssItalic |= cssAttribute == key;
286: cssBold |= Attribute.FONT_WEIGHT == key;
287: scBold |= StyleConstants.Bold == key;
288: }
289: }
290:
291: assertEquals(count - 1, stringKeyCount);
292: assertEquals(2, nonStringKeyCount);
293: assertTrue(cssItalic);
294: assertFalse(cssBold); // No convertion is performed when used
295: assertTrue(scBold); // as mutable set
296: }
297: }
|