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.StyleConstants;
027: import javax.swing.text.html.CSS.Attribute;
028:
029: public class StyleSheet_ConvertAttr_FontSizeTest extends
030: BasicSwingTestCase {
031: private static final String[] fontSizeTable = { "xx-small",
032: "x-small", "small", "medium", "large", "x-large",
033: "xx-large" };
034:
035: private StyleSheet ss;
036: private AttributeSet empty;
037: private AttributeSet attr;
038: private Object cssValue;
039: private Object scValue;
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043: ss = new StyleSheet();
044: empty = ss.getEmptySet();
045: }
046:
047: public void testFontSize() {
048: attr = ss.addAttribute(empty, StyleConstants.FontSize,
049: new Integer(10));
050:
051: Enumeration names = attr.getAttributeNames();
052: Object name = names.nextElement();
053: assertSame(Attribute.FONT_SIZE, name);
054: assertFalse(names.hasMoreElements());
055:
056: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
057: scValue = attr.getAttribute(StyleConstants.FontSize);
058: assertSame(Integer.class, scValue.getClass());
059: assertNotSame(Integer.class, cssValue.getClass());
060: assertNotSame(String.class, cssValue.getClass());
061: assertEquals(tableSizeToCSSKeyword(2), cssValue.toString());
062: assertEquals("10", scValue.toString());
063: assertEquals(10, ((Integer) scValue).intValue());
064: }
065:
066: public void testFontSizeXXSmall() {
067: attr = ss.addAttribute(empty, StyleConstants.FontSize,
068: "xx-small");
069:
070: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
071: scValue = attr.getAttribute(StyleConstants.FontSize);
072: assertSame(Integer.class, scValue.getClass());
073: assertNotSame(Integer.class, cssValue.getClass());
074: // assertNotSame(String.class, cssValue.getClass());
075: assertEquals("xx-small", cssValue.toString());
076: assertEquals(8, ((Integer) scValue).intValue());
077: }
078:
079: public void testFontSizeXSmall() {
080: attr = ss.addAttribute(empty, StyleConstants.FontSize,
081: "x-small");
082: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
083: scValue = attr.getAttribute(StyleConstants.FontSize);
084: assertSame(Integer.class, scValue.getClass());
085: assertEquals("x-small", cssValue.toString());
086: assertEquals(10, ((Integer) scValue).intValue());
087: }
088:
089: public void testFontSizeSmall() {
090: attr = ss.addAttribute(empty, StyleConstants.FontSize, "small");
091:
092: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
093: scValue = attr.getAttribute(StyleConstants.FontSize);
094: assertSame(Integer.class, scValue.getClass());
095: assertEquals("small", cssValue.toString());
096: assertEquals(12, ((Integer) scValue).intValue());
097: }
098:
099: public void testFontSizeMedium() {
100: attr = ss
101: .addAttribute(empty, StyleConstants.FontSize, "medium");
102:
103: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
104: scValue = attr.getAttribute(StyleConstants.FontSize);
105: assertSame(Integer.class, scValue.getClass());
106: assertEquals("medium", cssValue.toString());
107: assertEquals(14, ((Integer) scValue).intValue());
108: }
109:
110: public void testFontSizeLarge() {
111: attr = ss.addAttribute(empty, StyleConstants.FontSize, "large");
112:
113: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
114: scValue = attr.getAttribute(StyleConstants.FontSize);
115: assertSame(Integer.class, scValue.getClass());
116: assertEquals("large", cssValue.toString());
117: assertEquals(18, ((Integer) scValue).intValue());
118: }
119:
120: public void testFontSizeXLarge() {
121: attr = ss.addAttribute(empty, StyleConstants.FontSize,
122: "x-large");
123:
124: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
125: scValue = attr.getAttribute(StyleConstants.FontSize);
126: assertSame(Integer.class, scValue.getClass());
127: assertEquals("x-large", cssValue.toString());
128: assertEquals(24, ((Integer) scValue).intValue());
129: }
130:
131: public void testFontSizeXXLarge() {
132: attr = ss.addAttribute(empty, StyleConstants.FontSize,
133: "xx-large");
134:
135: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
136: scValue = attr.getAttribute(StyleConstants.FontSize);
137: assertSame(Integer.class, scValue.getClass());
138: assertEquals("xx-large", cssValue.toString());
139: assertEquals(36, ((Integer) scValue).intValue());
140: }
141:
142: public void testFontSizeSmaller() {
143: attr = ss.addAttribute(empty, StyleConstants.FontSize,
144: "smaller");
145:
146: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
147: scValue = attr.getAttribute(StyleConstants.FontSize);
148: assertSame(Integer.class, scValue.getClass());
149: assertEquals("smaller", cssValue.toString());
150: assertEquals(12, ((Integer) scValue).intValue());
151: }
152:
153: public void testFontSizeSmallerParented() {
154: AttributeSet parent = ss.addAttribute(empty,
155: StyleConstants.FontSize, "x-large");
156: assertEquals(24, ((Integer) parent
157: .getAttribute(StyleConstants.FontSize)).intValue());
158: attr = ss.addAttribute(empty, StyleConstants.ResolveAttribute,
159: parent);
160: attr = ss
161: .addAttribute(attr, StyleConstants.FontSize, "smaller");
162:
163: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
164: scValue = attr.getAttribute(StyleConstants.FontSize);
165: assertSame(Integer.class, scValue.getClass());
166: assertEquals("smaller", cssValue.toString());
167: assertEquals(12, ((Integer) scValue).intValue());
168: }
169:
170: public void testFontSizeLarger() {
171: attr = ss
172: .addAttribute(empty, StyleConstants.FontSize, "larger");
173:
174: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
175: scValue = attr.getAttribute(StyleConstants.FontSize);
176: assertSame(Integer.class, scValue.getClass());
177: assertEquals("larger", cssValue.toString());
178: assertEquals(12, ((Integer) scValue).intValue());
179: }
180:
181: public void testFontSizeLargerParented() {
182: AttributeSet parent = ss.addAttribute(empty,
183: StyleConstants.FontSize, "x-small");
184: assertEquals(10, ((Integer) parent
185: .getAttribute(StyleConstants.FontSize)).intValue());
186: attr = ss.addAttribute(empty, StyleConstants.ResolveAttribute,
187: parent);
188: attr = ss.addAttribute(attr, StyleConstants.FontSize, "larger");
189:
190: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
191: scValue = attr.getAttribute(StyleConstants.FontSize);
192: assertSame(Integer.class, scValue.getClass());
193: assertEquals("larger", cssValue.toString());
194: assertEquals(12, ((Integer) scValue).intValue());
195: }
196:
197: public void testFontSizeTable() {
198: int[][] size = {
199: // {scSizeSet, cssSize, scSizeRead}
200: { 6, 1, 8 }, { 7, 1, 8 }, { 8, 1, 8 }, { 9, 2, 10 },
201: { 10, 2, 10 }, { 11, 3, 12 }, { 12, 3, 12 },
202: { 13, 4, 14 }, { 14, 4, 14 }, { 15, 5, 18 },
203: { 16, 5, 18 }, { 17, 5, 18 }, { 18, 5, 18 },
204: { 19, 6, 24 }, { 20, 6, 24 }, { 21, 6, 24 },
205: { 22, 6, 24 }, { 23, 6, 24 }, { 24, 6, 24 },
206: { 25, 7, 36 }, { 26, 7, 36 }, { 27, 7, 36 },
207: { 28, 7, 36 }, { 29, 7, 36 }, { 30, 7, 36 },
208: { 31, 7, 36 }, { 32, 7, 36 }, { 33, 7, 36 },
209: { 34, 7, 36 }, { 35, 7, 36 }, { 36, 7, 36 },
210: { 37, 7, 36 }, { 38, 7, 36 }, { 39, 7, 36 },
211: { 40, 7, 36 } };
212: for (int i = 0; i < size.length; i++) {
213: attr = ss.addAttribute(empty, StyleConstants.FontSize,
214: new Integer(size[i][0]));
215:
216: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
217: scValue = attr.getAttribute(StyleConstants.FontSize);
218: assertSame("@ " + i, Integer.class, scValue.getClass());
219: assertEquals("@ " + i, tableSizeToCSSKeyword(size[i][1]),
220: cssValue.toString());
221: assertEquals("@ " + i, size[i][2], ((Integer) scValue)
222: .intValue());
223: }
224: }
225:
226: public void testFontSizePercentage() {
227: attr = ss.addAttribute(empty, StyleConstants.FontSize, "150%");
228:
229: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
230: scValue = attr.getAttribute(StyleConstants.FontSize);
231: assertSame(Integer.class, scValue.getClass());
232: assertNotSame(Integer.class, cssValue.getClass());
233: assertNotSame(String.class, cssValue.getClass());
234: assertEquals("150%", cssValue.toString());
235: assertEquals(12, ((Integer) scValue).intValue());
236: }
237:
238: public void testFontSizePercentageParented() {
239: AttributeSet parent = ss.addAttribute(empty,
240: StyleConstants.FontSize, "large");
241: attr = ss.addAttribute(empty, StyleConstants.ResolveAttribute,
242: parent);
243: attr = ss.addAttribute(attr, StyleConstants.FontSize, "80%");
244:
245: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
246: scValue = attr.getAttribute(StyleConstants.FontSize);
247: assertSame(Integer.class, scValue.getClass());
248: assertNotSame(Integer.class, cssValue.getClass());
249: assertNotSame(String.class, cssValue.getClass());
250: assertEquals("80%", cssValue.toString());
251: assertEquals(12, ((Integer) scValue).intValue());
252: }
253:
254: /**
255: * Points, 1pt = 1/72in
256: */
257: public void testFontSizeAbsolutePt() {
258: if (!isHarmony()) {
259: return;
260: }
261: attr = ss.addAttribute(empty, StyleConstants.FontSize, "20pt");
262:
263: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
264: scValue = attr.getAttribute(StyleConstants.FontSize);
265: assertSame(Integer.class, scValue.getClass());
266: assertEquals("20pt", cssValue.toString());
267: assertEquals(20, ((Integer) scValue).intValue());
268: }
269:
270: /**
271: * Pixels
272: */
273: public void testFontSizeAbsolutePx() {
274: if (!isHarmony()) {
275: return;
276: }
277: attr = ss.addAttribute(empty, StyleConstants.FontSize, "23px");
278: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
279: scValue = attr.getAttribute(StyleConstants.FontSize);
280: assertSame(Integer.class, scValue.getClass());
281: assertEquals(29, ((Integer) scValue).intValue());
282: assertEquals("23px", cssValue.toString());
283:
284: attr = ss.addAttribute(empty, StyleConstants.FontSize, "24px");
285: scValue = attr.getAttribute(StyleConstants.FontSize);
286: assertEquals(31, ((Integer) scValue).intValue());
287:
288: attr = ss.addAttribute(empty, StyleConstants.FontSize, "100px");
289: scValue = attr.getAttribute(StyleConstants.FontSize);
290: assertEquals(130, ((Integer) scValue).intValue());
291: attr = ss.addAttribute(empty, StyleConstants.FontSize, "200px");
292: scValue = attr.getAttribute(StyleConstants.FontSize);
293: assertEquals(260, ((Integer) scValue).intValue());
294: }
295:
296: /**
297: * Picas, 1pc = 12pt
298: */
299: public void testFontSizeAbsolutePc() {
300: if (!isHarmony()) {
301: return;
302: }
303: attr = ss.addAttribute(empty, StyleConstants.FontSize, "4pc");
304:
305: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
306: scValue = attr.getAttribute(StyleConstants.FontSize);
307: assertSame(Integer.class, scValue.getClass());
308: assertEquals("4pc", cssValue.toString());
309: assertEquals(48, ((Integer) scValue).intValue());
310: }
311:
312: public void testFontSizeAbsoluteMm() {
313: if (!isHarmony()) {
314: return;
315: }
316: attr = ss.addAttribute(empty, StyleConstants.FontSize, "20mm");
317:
318: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
319: scValue = attr.getAttribute(StyleConstants.FontSize);
320: assertSame(Integer.class, scValue.getClass());
321: assertEquals("20mm", cssValue.toString());
322: assertEquals(56, ((Integer) scValue).intValue());
323: }
324:
325: public void testFontSizeAbsoluteCm() {
326: if (!isHarmony()) {
327: return;
328: }
329: attr = ss.addAttribute(empty, StyleConstants.FontSize, "2cm");
330:
331: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
332: scValue = attr.getAttribute(StyleConstants.FontSize);
333: assertSame(Integer.class, scValue.getClass());
334: assertEquals("2cm", cssValue.toString());
335: assertEquals(56, ((Integer) scValue).intValue());
336: }
337:
338: public void testFontSizeRelativeEm() {
339: attr = ss.addAttribute(empty, StyleConstants.FontSize, "2em");
340:
341: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
342: scValue = attr.getAttribute(StyleConstants.FontSize);
343: assertSame(Integer.class, scValue.getClass());
344: assertEquals("2em", cssValue.toString());
345: assertEquals(12, ((Integer) scValue).intValue());
346: }
347:
348: public void testFontSizeRelativeEmParented() {
349: AttributeSet parent = ss.addAttribute(empty,
350: StyleConstants.FontSize, "x-large");
351: attr = ss.addAttribute(empty, StyleConstants.ResolveAttribute,
352: parent);
353: attr = ss.addAttribute(attr, StyleConstants.FontSize, "2em");
354:
355: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
356: scValue = attr.getAttribute(StyleConstants.FontSize);
357: assertSame(Integer.class, scValue.getClass());
358: assertEquals("2em", cssValue.toString());
359: assertEquals(12, ((Integer) scValue).intValue());
360: }
361:
362: public void testFontSizeRelativeEx() {
363: AttributeSet parent = ss.addAttribute(empty,
364: StyleConstants.FontSize, "x-large");
365: attr = ss.addAttribute(empty, StyleConstants.ResolveAttribute,
366: parent);
367: attr = ss.addAttribute(attr, StyleConstants.FontSize, "3ex");
368:
369: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
370: scValue = attr.getAttribute(StyleConstants.FontSize);
371: assertSame(Integer.class, scValue.getClass());
372: assertEquals("3ex", cssValue.toString());
373: assertEquals(12, ((Integer) scValue).intValue());
374: }
375:
376: public void testFontSizeRelativeExParented() {
377: attr = ss.addAttribute(empty, StyleConstants.FontSize, "3ex");
378:
379: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
380: scValue = attr.getAttribute(StyleConstants.FontSize);
381: assertSame(Integer.class, scValue.getClass());
382: assertEquals("3ex", cssValue.toString());
383: assertEquals(12, ((Integer) scValue).intValue());
384: }
385:
386: public void testFontSizeAbsoluteIn() {
387: if (!isHarmony()) {
388: return;
389: }
390:
391: attr = ss.addAttribute(empty, StyleConstants.FontSize, "2in");
392:
393: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
394: scValue = attr.getAttribute(StyleConstants.FontSize);
395: assertSame(Integer.class, scValue.getClass());
396: assertEquals("2in", cssValue.toString());
397: assertEquals(144, ((Integer) scValue).intValue());
398: }
399:
400: public void testFontSizeAbsoluteParented() {
401: AttributeSet parent = ss.addAttribute(empty,
402: StyleConstants.FontSize, "large");
403: attr = ss.addAttribute(empty, StyleConstants.ResolveAttribute,
404: parent);
405: attr = ss.addAttribute(attr, StyleConstants.FontSize, "80%");
406:
407: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
408: scValue = attr.getAttribute(StyleConstants.FontSize);
409: assertSame(Integer.class, scValue.getClass());
410: assertNotSame(Integer.class, cssValue.getClass());
411: assertNotSame(String.class, cssValue.getClass());
412: assertEquals("80%", cssValue.toString());
413: assertEquals(12, ((Integer) scValue).intValue());
414: }
415:
416: public void testFontSizeNonStyleConstants() {
417: attr = ss.addAttribute(empty, Attribute.FONT_SIZE, "2in");
418: cssValue = attr.getAttribute(Attribute.FONT_SIZE);
419: scValue = attr.getAttribute(StyleConstants.FontSize);
420:
421: if (isHarmony()) {
422: assertSame(Integer.class, scValue.getClass());
423: assertNotSame(Integer.class, cssValue.getClass());
424: assertNotSame(String.class, cssValue.getClass());
425: assertEquals(144, ((Integer) scValue).intValue());
426: } else {
427: assertSame(String.class, cssValue.getClass());
428: assertNull(scValue);
429: }
430: assertEquals("2in", cssValue.toString());
431: }
432:
433: private String tableSizeToCSSKeyword(final int size) {
434: if (isHarmony()) {
435: return fontSizeTable[size - 1];
436: } else {
437: return String.valueOf(size);
438: }
439: }
440: }
|