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 javax.swing.BasicSwingTestCase;
024: import javax.swing.text.StyleContext.SmallAttributeSet;
025: import junit.framework.TestCase;
026:
027: /**
028: * Tests for removeAttribute, removeAttributes(Enumeration),
029: * removeAttributes(AttributeSet) methods.
030: *
031: */
032: public class StyleContext_RemoveTest extends TestCase {
033: /**
034: * Class supporting testing of removeAttributes(Enumeration) method
035: * of StyleContext.
036: */
037: @SuppressWarnings("unchecked")
038: private class AttributeNameEnumeration implements Enumeration {
039: private int count;
040:
041: private int index;
042:
043: /**
044: * Creates a new enumeration with the specified number of
045: * attribute names, starting at index 0.
046: *
047: * @param count the number of elements
048: */
049: public AttributeNameEnumeration(final int count) {
050: this .index = 0;
051: this .count = count;
052: }
053:
054: /**
055: * Creates a new enumeration with the specified number of
056: * attribute names, starting at index specified.
057: *
058: * @param start the start index in internal attribute array
059: * @param count the number of elements
060: */
061: public AttributeNameEnumeration(final int start, final int count) {
062: this .index = start;
063: this .count = count + start;
064: }
065:
066: public boolean hasMoreElements() {
067: return index < count;
068: }
069:
070: public Object nextElement() {
071: return StyleContextTest.attr[2 * index++];
072: }
073: }
074:
075: private StyleContext sc;
076:
077: /**
078: * Just removes an attribute.
079: */
080: public void testRemoveAttribute() {
081: AttributeSet as = StyleContextTest.addAttribute(5);
082: as = sc.removeAttribute(as, StyleContextTest.attr[2]);
083: assertEquals(4, as.getAttributeCount());
084: }
085:
086: /**
087: * Removes an attribute, and the new set should be from cache.
088: */
089: public void testRemoveAttributeCache() {
090: AttributeSet as = StyleContextTest.addAttribute(4);
091: SimpleAttributeSet sas = new SimpleAttributeSet(as);
092: sas.addAttribute(StyleContextTest.attr[5 * 2],
093: StyleContextTest.attr[5 * 2 + 1]);
094: AttributeSet test = sc.removeAttribute(sas,
095: StyleContextTest.attr[5 * 2]);
096: assertEquals(4, test.getAttributeCount());
097: assertSame(as, test);
098: }
099:
100: /**
101: * Removes 1 attribute from a set, and the set is converted to
102: * SmallAttributeSet
103: *
104: */
105: public void testRemoveAttributeCollapse() {
106: AttributeSet as = StyleContextTest.addAttribute(10);
107: assertTrue(as instanceof SimpleAttributeSet);
108: as = sc.removeAttribute(as, StyleContextTest.attr[0]);
109: assertEquals(9, as.getAttributeCount());
110: assertTrue(as instanceof SmallAttributeSet);
111: }
112:
113: /**
114: * Removes the last attribute from a set.
115: */
116: public void testRemoveAttributeFromOne() {
117: AttributeSet as = StyleContextTest.addAttribute(1);
118: as = sc.removeAttribute(as, StyleContextTest.attr[0]);
119: assertEquals(0, as.getAttributeCount());
120: if (!BasicSwingTestCase.isHarmony()) {
121: assertTrue(as instanceof SmallAttributeSet);
122: } else {
123: assertSame(as, sc.getEmptySet());
124: }
125: }
126:
127: /**
128: * Removes an attribute, the new set being placed in the cache.
129: * Check that the set is in the cached with creation another
130: * set with the same attributes.
131: */
132: public void testRemoveAttributeNoCache() {
133: SimpleAttributeSet sas = new SimpleAttributeSet();
134: for (int i = 0; i < 6; i += 2) {
135: sas.addAttribute(StyleContextTest.attr[i],
136: StyleContextTest.attr[i + 1]);
137: }
138: AttributeSet test = sc.removeAttribute(sas,
139: StyleContextTest.attr[4]);
140: // Create the same set as test
141: AttributeSet as = StyleContextTest.addAttribute(2);
142: assertSame(test, as);
143: }
144:
145: public void testRemoveAttributeNoKey() {
146: AttributeSet as = StyleContextTest.addAttribute(2);
147: AttributeSet test = sc.removeAttribute(as,
148: StyleContextTest.attr[4]);
149: assertSame(as, test);
150: }
151:
152: /**
153: * Removes some attributes from the set. The result should be
154: * from the cache.
155: */
156: public void testRemoveAttributesEnumCache() {
157: AttributeSet cached = StyleContextTest.addAttribute(1);
158: AttributeSet as = StyleContextTest.addAttribute(cached, 1, 2);
159: AttributeSet test = sc.removeAttributes(as,
160: new AttributeNameEnumeration(1, 2));
161: assertEquals(1, test.getAttributeCount());
162: assertSame(cached, test);
163: }
164:
165: /**
166: * Removes all the attributes from a set.
167: * The result should be EmptyAttributeSet.
168: */
169: public void testRemoveAttributesEnumEmpty() {
170: AttributeSet as = StyleContextTest.addAttribute(3);
171: AttributeSet test = sc.removeAttributes(as,
172: new AttributeNameEnumeration(3));
173: assertEquals(0, test.getAttributeCount());
174: if (BasicSwingTestCase.isHarmony()) {
175: assertSame(test, sc.getEmptySet());
176: }
177: }
178:
179: /**
180: * Removes some attributes from a set. The result shouldn't be
181: * in the cache.
182: */
183: public void testRemoveAttributesEnumNoCache() {
184: AttributeSet as = StyleContextTest.addAttribute(4);
185: AttributeSet test = sc.removeAttributes(as,
186: new AttributeNameEnumeration(1, 2));
187: assertEquals(2, test.getAttributeCount());
188: // Create the same set as test
189: AttributeSet cached = StyleContextTest.addAttribute(1);
190: cached = StyleContextTest.addAttribute(cached, 3, 1);
191: assertSame(test, cached);
192: }
193:
194: /**
195: * Removes several attributes from a set. The name enumeration
196: * contains names that are not in the set.
197: */
198: public void testRemoveAttributesEnumNoKey() {
199: AttributeSet as = StyleContextTest.addAttribute(2);
200: as = StyleContextTest.addAttribute(as, 4, 2);
201: assertEquals(4, as.getAttributeCount());
202: AttributeSet test = sc.removeAttributes(as,
203: new AttributeNameEnumeration(1, 4));
204: assertEquals(2, test.getAttributeCount());
205: }
206:
207: /**
208: * Removes one attribute from a set. The resulting set should become
209: * SmallAttributeSet.
210: */
211: public void testRemoveAttributesEnumSmall() {
212: AttributeSet as = StyleContextTest.addAttribute(10);
213: assertTrue(as instanceof SimpleAttributeSet);
214: // Remove one element
215: AttributeSet test = sc.removeAttributes(as,
216: new AttributeNameEnumeration(1));
217: assertEquals(9, test.getAttributeCount());
218: if (BasicSwingTestCase.isHarmony()) {
219: assertTrue(test instanceof SmallAttributeSet);
220: }
221: }
222:
223: /**
224: * Removes some attributes from the set. The result should be
225: * from the cache.
226: */
227: public void testRemoveAttributesSetCache() {
228: AttributeSet cached = StyleContextTest.addAttribute(1);
229: AttributeSet as = StyleContextTest.addAttribute(cached, 1, 2);
230: AttributeSet test = sc.removeAttributes(as, StyleContextTest
231: .addAttribute(null, 1, 2));
232: assertEquals(1, test.getAttributeCount());
233: assertSame(cached, test);
234: }
235:
236: /**
237: * Removes some attributes from the set. The result should be
238: * from the cache. The set to remove contains a key with a different
239: * value.
240: */
241: public void testRemoveAttributesSetCacheSameKeys() {
242: AttributeSet cached = StyleContextTest.addAttribute(2);
243: AttributeSet as = StyleContextTest.addAttribute(cached, 2, 3);
244: AttributeSet test = sc.removeAttributes(as, sc.addAttribute(
245: StyleContextTest.addAttribute(null, 1, 4),
246: StyleConstants.Italic, Boolean.FALSE));
247: assertEquals(2, test.getAttributeCount());
248: assertSame(cached, test);
249: }
250:
251: /**
252: * Removes all the attributes from a set. The set to be removed is
253: * the same as the set to remove from.
254: * The result should be EmptyAttributeSet.
255: */
256: public void testRemoveAttributesSetEmpty() {
257: AttributeSet as = StyleContextTest.addAttribute(3);
258: AttributeSet test = sc.removeAttributes(as, as);
259: assertEquals(0, test.getAttributeCount());
260: if (BasicSwingTestCase.isHarmony()) {
261: assertSame(test, sc.getEmptySet());
262: }
263: }
264:
265: /**
266: * Removes all the attributes from a set. The set to be removed
267: * contains the same attribute key but has different values.
268: * The result should be EmptyAttributeSet.
269: */
270: public void testRemoveAttributesSetEmptySameKey() {
271: AttributeSet as = StyleContextTest.addAttribute(3);
272: AttributeSet test = sc.removeAttributes(as, sc.addAttribute(as,
273: StyleConstants.Bold, Boolean.FALSE));
274: assertEquals(1, test.getAttributeCount());
275: }
276:
277: /**
278: * Removes some attributes from a set. The result shouldn't be
279: * in the cache. The set to remove contains a key with a different
280: * value.
281: */
282: public void testRemoveAttributesSetNoCacheSameKeys() {
283: AttributeSet as = StyleContextTest.addAttribute(4);
284: AttributeSet test = sc.removeAttributes(as, sc.addAttribute(
285: StyleContextTest.addAttribute(null, 1, 2),
286: StyleConstants.Underline, Boolean.FALSE));
287: assertEquals(3, test.getAttributeCount());
288: // Create the same set as test
289: AttributeSet cached = StyleContextTest.addAttribute(1);
290: cached = StyleContextTest.addAttribute(cached, 2, 2);
291: assertSame(test, cached);
292: }
293:
294: /**
295: * Removes several attributes from a set. The set to be removed
296: * contains attributes with names that are not in the set from which
297: * attributes are to be removed.
298: */
299: public void testRemoveAttributesSetNoKey() {
300: AttributeSet as = StyleContextTest.addAttribute(2);
301: as = StyleContextTest.addAttribute(as, 4, 2);
302: assertEquals(4, as.getAttributeCount());
303: AttributeSet test = sc.removeAttributes(as, StyleContextTest
304: .addAttribute(null, 1, 4));
305: assertEquals(2, test.getAttributeCount());
306: }
307:
308: /**
309: * Removes one attribute from a set. The resulting set should become
310: * SmallAttributeSet.
311: */
312: public void testRemoveAttributesSetSmall() {
313: AttributeSet as = StyleContextTest.addAttribute(10);
314: assertTrue(as instanceof SimpleAttributeSet);
315: // Remove one element
316: AttributeSet test = sc.removeAttributes(as, StyleContextTest
317: .addAttribute(1));
318: assertEquals(9, test.getAttributeCount());
319: if (BasicSwingTestCase.isHarmony()) {
320: assertTrue(test instanceof SmallAttributeSet);
321: }
322: }
323:
324: @Override
325: protected void setUp() throws Exception {
326: sc = StyleContextTest.sc = new StyleContext();
327: }
328: }
|