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.awt.GraphicsEnvironment;
023: import java.util.Arrays;
024: import java.util.Enumeration;
025: import java.util.List;
026:
027: import javax.swing.BasicSwingTestCase;
028: import javax.swing.text.AttributeSet;
029: import javax.swing.text.StyleConstants;
030: import javax.swing.text.html.CSS.Attribute;
031:
032: public class StyleSheet_ConvertAttr_FontFamilyTest extends
033: BasicSwingTestCase {
034: private StyleSheet ss;
035: private AttributeSet empty;
036: private AttributeSet attr;
037: private Object cssValue;
038: private Object scValue;
039: private static final String[] fonts;
040: private static final List fontList;
041:
042: static {
043: GraphicsEnvironment ge = GraphicsEnvironment
044: .getLocalGraphicsEnvironment();
045: fonts = ge.getAvailableFontFamilyNames();
046: fontList = Arrays.asList(fonts);
047: }
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: ss = new StyleSheet();
052: empty = ss.getEmptySet();
053: }
054:
055: /**
056: * Contains a known font on Windows.
057: */
058: public void testFontFamilyW() {
059: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
060: "Arial");
061: Enumeration names = attr.getAttributeNames();
062: Object name = names.nextElement();
063: assertSame(Attribute.class, name.getClass());
064: assertFalse(names.hasMoreElements());
065:
066: cssValue = attr.getAttribute(name);
067: scValue = attr.getAttribute(StyleConstants.FontFamily);
068: assertSame(String.class, scValue.getClass());
069: assertNotSame(String.class, cssValue.getClass());
070: assertNotSame(scValue, cssValue);
071: assertFalse(scValue.equals(cssValue));
072: if (fontList.contains("Arial")) {
073: assertEquals("Arial", scValue.toString());
074: } else {
075: assertEquals("SansSerif", scValue.toString());
076: }
077: }
078:
079: /**
080: * Contains a known font on Linux.
081: */
082: public void testFontFamilyL() {
083: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
084: "Lucida Sans");
085: Enumeration names = attr.getAttributeNames();
086: Object name = names.nextElement();
087: assertSame(Attribute.class, name.getClass());
088: assertFalse(names.hasMoreElements());
089:
090: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
091: scValue = attr.getAttribute(StyleConstants.FontFamily);
092: assertSame(String.class, scValue.getClass());
093: assertNotSame(String.class, cssValue.getClass());
094: assertNotSame(scValue, cssValue);
095: assertFalse(scValue.equals(cssValue));
096: if (fontList.contains("Lucida Sans")) {
097: assertEquals("Lucida Sans", scValue.toString());
098: } else {
099: assertEquals("SansSerif", scValue.toString());
100: }
101: }
102:
103: /**
104: * The value refers to the unknown font.
105: */
106: public void testFontFamilyNotExists() {
107: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
108: "Helvet");
109:
110: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
111: scValue = attr.getAttribute(StyleConstants.FontFamily);
112: assertSame(String.class, scValue.getClass());
113: assertNotSame(String.class, cssValue.getClass());
114: assertNotSame(scValue, cssValue);
115: assertFalse(scValue.equals(cssValue));
116: assertEquals("Helvet", cssValue.toString());
117: assertEquals("SansSerif", scValue.toString());
118: }
119:
120: /**
121: * Multiple fonts are listed.
122: * The name of the first font found on the system is returned as result
123: * of the convertion.
124: */
125: public void testFontFamilyMultiple() {
126: GraphicsEnvironment ge = GraphicsEnvironment
127: .getLocalGraphicsEnvironment();
128: List fontList = Arrays.asList(ge.getAvailableFontFamilyNames());
129: boolean hasGaramond = fontList.contains("Garamond");
130: boolean hasTimes = fontList.contains("Times New Roman");
131:
132: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
133: "Garamond, Times New Roman, serif");
134:
135: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
136: scValue = attr.getAttribute(StyleConstants.FontFamily);
137: assertSame(String.class, scValue.getClass());
138: assertNotSame(String.class, cssValue.getClass());
139: assertNotSame(scValue, cssValue);
140: assertFalse(scValue.equals(cssValue));
141: assertEquals("Garamond, Times New Roman, serif", cssValue
142: .toString());
143: if (hasGaramond) {
144: assertEquals("Garamond", scValue.toString());
145: } else if (hasTimes) {
146: assertEquals("Times New Roman", scValue.toString());
147: } else {
148: assertEquals(isHarmony() ? "Serif" : "SansSerif", scValue
149: .toString());
150: }
151: }
152:
153: /**
154: * Multiple fonts are listed. The first item refers to non-existent font,
155: * the second to the known font on Windows systems.
156: */
157: public void testFontFamilyMultipleNonExistentW() {
158: GraphicsEnvironment ge = GraphicsEnvironment
159: .getLocalGraphicsEnvironment();
160: List fontList = Arrays.asList(ge.getAvailableFontFamilyNames());
161: boolean hasTimes = fontList.contains("Times New Roman");
162:
163: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
164: "NonExistentFont, Times New Roman, serif");
165:
166: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
167: scValue = attr.getAttribute(StyleConstants.FontFamily);
168: assertSame(String.class, scValue.getClass());
169: assertNotSame(String.class, cssValue.getClass());
170: assertNotSame(scValue, cssValue);
171: assertFalse(scValue.equals(cssValue));
172: assertEquals("NonExistentFont, Times New Roman, serif",
173: cssValue.toString());
174: if (hasTimes) {
175: // The test is expected to be here when run on Windows
176: assertEquals(isHarmony() ? "Times New Roman" : "SansSerif",
177: scValue.toString());
178: } else {
179: assertEquals(isHarmony() ? "Serif" : "SansSerif", scValue
180: .toString());
181: }
182: }
183:
184: /**
185: * Multiple fonts are listed. The first item refers to non-existent font,
186: * the second to the known font on Linux systems.
187: */
188: public void testFontFamilyMultipleNonExistentL() {
189: boolean hasLucida = fontList.contains("Lucida Sans");
190:
191: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
192: "NonExistentFont, Lucida Sans, serif");
193:
194: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
195: scValue = attr.getAttribute(StyleConstants.FontFamily);
196: assertSame(String.class, scValue.getClass());
197: assertNotSame(String.class, cssValue.getClass());
198: assertNotSame(scValue, cssValue);
199: assertFalse(scValue.equals(cssValue));
200: assertEquals("NonExistentFont, Lucida Sans, serif", cssValue
201: .toString());
202: if (hasLucida) {
203: // The test is expected to be here when run on Linux
204: assertEquals(isHarmony() ? "Lucida Sans" : "SansSerif",
205: scValue.toString());
206: } else {
207: assertEquals(isHarmony() ? "Serif" : "SansSerif", scValue
208: .toString());
209: }
210: }
211:
212: /**
213: * Multiple fonts are listed. The two first items refer to non-existent
214: * fonts, the last item is generic family. It is the generic family
215: * that expected to be the result of convertion.
216: */
217: public void testFontFamilyMultipleNonExistentG() {
218: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
219: "NonExistentFont, UnknownFont, serif");
220:
221: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
222: scValue = attr.getAttribute(StyleConstants.FontFamily);
223: assertSame(String.class, scValue.getClass());
224: assertNotSame(String.class, cssValue.getClass());
225: assertNotSame(scValue, cssValue);
226: assertFalse(scValue.equals(cssValue));
227: assertEquals("NonExistentFont, UnknownFont, serif", cssValue
228: .toString());
229: assertEquals(isHarmony() ? "Serif" : "SansSerif", scValue
230: .toString());
231: }
232:
233: /**
234: * Attribute set was added <code>CSS.Attribute.FONT_FAMILY</code>
235: * property. It is not expected to convert values to and from.
236: * <code>StyleConstants</code>.
237: */
238: public void testFontFamilyNonStyleConstants() {
239: attr = ss.addAttribute(empty, Attribute.FONT_FAMILY, "serif");
240: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
241: scValue = attr.getAttribute(StyleConstants.FontFamily);
242: assertEquals("serif", cssValue.toString());
243:
244: if (isHarmony()) {
245: assertSame(String.class, scValue.getClass());
246: assertNotSame(String.class, cssValue.getClass());
247: assertEquals("Serif", scValue.toString());
248: } else {
249: assertSame(String.class, cssValue.getClass());
250: assertNull(scValue);
251: }
252: }
253:
254: /**
255: * Generic family: serif.
256: */
257: public void testFontFamilySerif() {
258: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
259: "serif");
260: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
261: assertNotSame(String.class, cssValue.getClass());
262: assertEquals("serif", cssValue.toString());
263:
264: scValue = attr.getAttribute(StyleConstants.FontFamily);
265: assertSame(String.class, scValue.getClass());
266: assertEquals("Serif", scValue);
267: }
268:
269: /**
270: * Generic family: sans-serif.
271: */
272: public void testFontFamilySansSerif() {
273: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
274: "sans-serif");
275: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
276: assertNotSame(String.class, cssValue.getClass());
277: assertEquals("sans-serif", cssValue.toString());
278:
279: scValue = attr.getAttribute(StyleConstants.FontFamily);
280: assertSame(String.class, scValue.getClass());
281: assertEquals("SansSerif", scValue);
282: }
283:
284: /**
285: * Generic family: cursive.
286: * <p>Falls back to default sans-serif.
287: */
288: public void testFontFamilyCursive() {
289: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
290: "cursive");
291: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
292: assertNotSame(String.class, cssValue.getClass());
293: assertEquals("cursive", cssValue.toString());
294:
295: scValue = attr.getAttribute(StyleConstants.FontFamily);
296: assertSame(String.class, scValue.getClass());
297: assertEquals("SansSerif", scValue);
298: }
299:
300: /**
301: * Generic family: fantasy.
302: * <p>Falls back to default sans-serif.
303: */
304: public void testFontFamilyFantasy() {
305: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
306: "fantasy");
307: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
308: assertNotSame(String.class, cssValue.getClass());
309: assertEquals("fantasy", cssValue.toString());
310:
311: scValue = attr.getAttribute(StyleConstants.FontFamily);
312: assertSame(String.class, scValue.getClass());
313: assertEquals("SansSerif", scValue);
314: }
315:
316: /**
317: * Generic family: monospace.
318: */
319: public void testFontFamilyMonospace() {
320: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
321: "monospace");
322: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
323: assertNotSame(String.class, cssValue.getClass());
324: assertEquals("monospace", cssValue.toString());
325:
326: scValue = attr.getAttribute(StyleConstants.FontFamily);
327: assertSame(String.class, scValue.getClass());
328: assertEquals("Monospaced", scValue);
329: }
330:
331: /**
332: * Multiple fonts are listed.
333: * The first item is generic font family.
334: */
335: public void testFontFamilyGenericPlusFont() {
336: attr = ss.addAttribute(empty, StyleConstants.FontFamily,
337: "monospace, Lucida Sans");
338:
339: cssValue = attr.getAttribute(Attribute.FONT_FAMILY);
340: scValue = attr.getAttribute(StyleConstants.FontFamily);
341: assertEquals("monospace, Lucida Sans", cssValue.toString());
342: assertEquals("Monospaced", scValue.toString());
343: }
344: }
|