0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Alexey A. Ivanov
0019: * @version $Revision$
0020: */package javax.swing.text.html;
0021:
0022: import java.awt.Color;
0023: import java.awt.Font;
0024: import java.io.File;
0025: import java.io.FileReader;
0026: import java.io.FileWriter;
0027: import java.io.IOException;
0028: import java.net.MalformedURLException;
0029: import java.net.URL;
0030: import java.util.Enumeration;
0031:
0032: import javax.swing.BasicSwingTestCase;
0033: import javax.swing.text.AttributeSet;
0034: import javax.swing.text.MutableAttributeSet;
0035: import javax.swing.text.SimpleAttributeSet;
0036: import javax.swing.text.Style;
0037: import javax.swing.text.StyleConstants;
0038: import javax.swing.text.StyleContext.NamedStyle;
0039: import javax.swing.text.StyleContext.SmallAttributeSet;
0040: import javax.swing.text.html.CSS.Attribute;
0041: import javax.swing.text.html.StyleSheet.BoxPainter;
0042: import javax.swing.text.html.StyleSheet.ListPainter;
0043:
0044: public class StyleSheetTest extends BasicSwingTestCase {
0045: private abstract class NumberFormatCase extends ExceptionalCase {
0046: public Class expectedExceptionClass() {
0047: return NumberFormatException.class;
0048: }
0049: }
0050:
0051: private static final int[] sizes = { 8, 10, 12, 14, 18, 24, 36 };
0052:
0053: private static final String CSS_RULES = "body {\n"
0054: + " background-color: yellow;\n" + " color: red;\n"
0055: + "}\n" + "p {\n" + " text-indent: 1.25cm\n" + "}\n";
0056:
0057: private StyleSheet ss;
0058: private AttributeSet empty;
0059: private AttributeSet attr;
0060: private MutableAttributeSet simple;
0061:
0062: protected void setUp() throws Exception {
0063: super .setUp();
0064: ss = new StyleSheet();
0065: empty = ss.getEmptySet();
0066: simple = new SimpleAttributeSet();
0067: }
0068:
0069: // public void testStyleSheet() {
0070: //
0071: // }
0072:
0073: /**
0074: * Shows that <code>StyleConstants.FontSize</code> is converted to
0075: * <code>CSS.Attribute.FONT_SIZE</code>.
0076: */
0077: public void testAddAttribute() {
0078: attr = ss.addAttribute(empty, StyleConstants.FontSize,
0079: new Integer(10));
0080: Enumeration names = attr.getAttributeNames();
0081: Object name = names.nextElement();
0082: assertSame(Attribute.FONT_SIZE, name);
0083: assertFalse(names.hasMoreElements());
0084:
0085: assertTrue(attr instanceof SmallAttributeSet);
0086: assertSame(SmallAttributeSet.class, attr.getClass()
0087: .getSuperclass());
0088: }
0089:
0090: /**
0091: * Tests <code>equals</code> method of <em>converter</em> attribute set.
0092: * <p>
0093: * This shows that result of <code>equals</code> depends on which object
0094: * is the receiver and which is the parameter. Also this shows that for
0095: * hash codes for <em>almost equal</em> objects are different.
0096: */
0097: public void testAddAttributeEquals() {
0098: attr = ss.addAttribute(empty, StyleConstants.FontSize,
0099: new Integer(10));
0100: simple.addAttribute(StyleConstants.FontSize, new Integer(10));
0101:
0102: assertTrue(attr.equals(simple));
0103: if (isHarmony()) {
0104: // In DRL implementation equals works fine
0105: assertTrue(simple.equals(attr));
0106: } else {
0107: assertFalse(simple.equals(attr));
0108: }
0109: assertEquals(attr, simple);
0110: assertFalse(attr.hashCode() == simple.hashCode());
0111: }
0112:
0113: /**
0114: * Shows that attribute is stored <em>AS IS</em> if there's no
0115: * <code>CSS</code> equivalent to <code>StyleConstants</code> attribute.
0116: */
0117: public void testAddAttributeNoCSS() {
0118: attr = ss.addAttribute(empty, StyleConstants.BidiLevel,
0119: new Integer(0));
0120: assertEquals(1, attr.getAttributeCount());
0121:
0122: Enumeration names = attr.getAttributeNames();
0123: Object name = names.nextElement();
0124: assertSame(StyleConstants.BidiLevel, name);
0125: assertFalse(names.hasMoreElements());
0126:
0127: assertTrue(attr instanceof SmallAttributeSet);
0128: assertSame(SmallAttributeSet.class, attr.getClass()
0129: .getSuperclass());
0130: }
0131:
0132: /**
0133: * Adding an attribute stored as CSS-attribute with StyleConstants key.
0134: */
0135: public void testAddAttributeCSSAsSC() throws Exception {
0136: ss.addCSSAttribute(simple, Attribute.FONT_SIZE, "21pt");
0137: assertEquals(1, simple.getAttributeCount());
0138: Object fs = simple.getAttribute(Attribute.FONT_SIZE);
0139: assertNotSame(String.class, fs.getClass());
0140:
0141: attr = ss.addAttribute(empty, StyleConstants.FontSize, fs);
0142: assertEquals(1, attr.getAttributeCount());
0143: Object css = attr.getAttribute(Attribute.FONT_SIZE);
0144: if (isHarmony()) {
0145: assertSame(fs, css);
0146: } else {
0147: assertNotSame(fs, css);
0148: assertFalse(css.equals(fs));
0149: assertSame(fs.getClass(), css.getClass());
0150: assertEquals(fs.toString(), css.toString());
0151: }
0152: }
0153:
0154: /**
0155: * Adding an attribute stored as CSS-attribute with CSS key.
0156: */
0157: public void testAddAttributeCSSAsCSS() throws Exception {
0158: ss.addCSSAttribute(simple, Attribute.FONT_SIZE, "21pt");
0159: assertEquals(1, simple.getAttributeCount());
0160: Object fs = simple.getAttribute(Attribute.FONT_SIZE);
0161: assertNotSame(String.class, fs.getClass());
0162:
0163: attr = ss.addAttribute(empty, Attribute.FONT_SIZE, fs);
0164: assertEquals(1, attr.getAttributeCount());
0165: Object css = attr.getAttribute(Attribute.FONT_SIZE);
0166: assertSame(fs, css);
0167: }
0168:
0169: /**
0170: * Adding an attribute stored as CSS-attribute with CSS key.
0171: */
0172: public void testAddAttributeCSSAsString() throws Exception {
0173: simple.addAttribute(Attribute.FONT_SIZE, "21pt");
0174: assertEquals(1, simple.getAttributeCount());
0175: Object fs = simple.getAttribute(Attribute.FONT_SIZE);
0176: assertSame(String.class, fs.getClass());
0177:
0178: attr = ss.addAttribute(empty, Attribute.FONT_SIZE, fs);
0179: assertEquals(1, attr.getAttributeCount());
0180: Object css = attr.getAttribute(Attribute.FONT_SIZE);
0181: if (isHarmony()) {
0182: assertNotSame(fs, css);
0183: assertEquals("21pt", css.toString());
0184: assertNotSame(fs.getClass(), css.getClass());
0185: assertNotSame(String.class, css.getClass());
0186: } else {
0187: assertSame(fs, css);
0188: }
0189:
0190: Object sc = attr.getAttribute(StyleConstants.FontSize);
0191: if (isHarmony()) {
0192: assertSame(Integer.class, sc.getClass());
0193: assertEquals(21, ((Integer) sc).intValue());
0194: } else {
0195: assertNull(sc);
0196: }
0197: }
0198:
0199: /**
0200: * Adding an attribute stored as CSS-attribute with CSS key.
0201: */
0202: public void testAddAttributeCSSAsInteger() throws Exception {
0203: simple.addAttribute(Attribute.FONT_SIZE, new Integer(21));
0204: assertEquals(1, simple.getAttributeCount());
0205: Object fs = simple.getAttribute(Attribute.FONT_SIZE);
0206: assertSame(Integer.class, fs.getClass());
0207:
0208: attr = ss.addAttribute(empty, Attribute.FONT_SIZE, fs);
0209: assertEquals(1, attr.getAttributeCount());
0210: Object css = attr.getAttribute(Attribute.FONT_SIZE);
0211: if (isHarmony()) {
0212: assertNotSame(fs, css);
0213: assertEquals("x-large", css.toString());
0214: assertNotSame(fs.getClass(), css.getClass());
0215: assertNotSame(String.class, css.getClass());
0216: } else {
0217: assertSame(fs, css);
0218: }
0219:
0220: Object sc = attr.getAttribute(StyleConstants.FontSize);
0221: if (isHarmony()) {
0222: assertSame(Integer.class, sc.getClass());
0223: assertEquals(24, ((Integer) sc).intValue());
0224: } else {
0225: assertNull(sc);
0226: }
0227: }
0228:
0229: /**
0230: * Adding an attribute with CSS-key but with invalid value for that key.
0231: */
0232: public void testAddAttributeInvalidValue01() throws Exception {
0233: if (isHarmony()) {
0234: testExceptionalCase(new NullPointerCase() {
0235: public void exceptionalAction() throws Exception {
0236: ss.addAttribute(empty, Attribute.FONT_SIZE,
0237: "not-numeral");
0238: }
0239: });
0240: return;
0241: }
0242: attr = ss.addAttribute(empty, Attribute.FONT_SIZE,
0243: "not-numeral");
0244: assertEquals(1, attr.getAttributeCount());
0245: Object css = attr.getAttribute(Attribute.FONT_SIZE);
0246: assertEquals("not-numeral", css.toString());
0247: assertSame(String.class, css.getClass());
0248: Object sc = attr.getAttribute(StyleConstants.FontSize);
0249: assertNull(sc);
0250: }
0251:
0252: /**
0253: * Adding an invalid value for SC-attribute.
0254: */
0255: public void testAddAttributeInvalidValue02() throws Exception {
0256: if (isHarmony()) {
0257: testExceptionalCase(new NullPointerCase() {
0258: public void exceptionalAction() throws Exception {
0259: ss.addAttribute(empty, StyleConstants.FontSize,
0260: "not-numeral");
0261: }
0262: });
0263: return;
0264: }
0265: attr = ss.addAttribute(empty, StyleConstants.FontSize,
0266: "not-numeral");
0267: assertEquals(1, attr.getAttributeCount());
0268: Object css = attr.getAttribute(Attribute.FONT_SIZE);
0269: assertEquals("not-numeral", css.toString());
0270: assertNotSame(String.class, css.getClass());
0271:
0272: Object sc = attr.getAttribute(StyleConstants.FontSize);
0273: assertSame(Integer.class, sc.getClass());
0274: assertEquals(12, ((Integer) sc).intValue());
0275: }
0276:
0277: /**
0278: * Shows that <code>StyleConstants</code> attributes are converted to
0279: * corresponding <code>CSS.Attribute</code> ones.
0280: */
0281: public void testAddAttributes() {
0282: simple.addAttribute(StyleConstants.FontSize, new Integer(10));
0283: simple.addAttribute(StyleConstants.Alignment, new Integer(
0284: StyleConstants.ALIGN_CENTER));
0285:
0286: attr = ss.addAttributes(empty, simple);
0287: assertEquals(2, attr.getAttributeCount());
0288:
0289: Enumeration names = attr.getAttributeNames();
0290: boolean hasSize = false;
0291: boolean hasAlign = false;
0292: while (names.hasMoreElements()) {
0293: Object name = names.nextElement();
0294: hasSize |= name == Attribute.FONT_SIZE;
0295: hasAlign |= name == Attribute.TEXT_ALIGN;
0296: }
0297: assertTrue(hasSize);
0298: assertTrue(hasAlign);
0299:
0300: assertTrue(attr instanceof SmallAttributeSet);
0301: assertSame(SmallAttributeSet.class, attr.getClass()
0302: .getSuperclass());
0303: }
0304:
0305: /**
0306: * Adding an attribute stored as CSS-attribute with StyleConstants key.
0307: */
0308: public void testAddAttributesCSSAsSC() {
0309: ss.addCSSAttribute(simple, Attribute.FONT_SIZE, "21pt");
0310: assertEquals(1, simple.getAttributeCount());
0311: Object fs = simple.getAttribute(Attribute.FONT_SIZE);
0312: assertNotSame(String.class, fs.getClass());
0313: simple.removeAttribute(Attribute.FONT_SIZE);
0314: simple.addAttribute(StyleConstants.FontSize, fs);
0315:
0316: attr = ss.addAttributes(empty, simple);
0317: assertEquals(1, attr.getAttributeCount());
0318: Object css = attr.getAttribute(Attribute.FONT_SIZE);
0319: if (isHarmony()) {
0320: assertSame(fs, css);
0321: } else {
0322: assertNotSame(fs, css);
0323: assertFalse(css.equals(fs));
0324: assertSame(fs.getClass(), css.getClass());
0325: assertEquals(fs.toString(), css.toString());
0326: }
0327: }
0328:
0329: /**
0330: * Adding an attribute stored as CSS-attribute with CSS key.
0331: */
0332: public void testAddAttributesCSSAsCSS() {
0333: ss.addCSSAttribute(simple, Attribute.FONT_SIZE, "21pt");
0334: assertEquals(1, simple.getAttributeCount());
0335: Object fs = simple.getAttribute(Attribute.FONT_SIZE);
0336: assertNotSame(String.class, fs.getClass());
0337:
0338: attr = ss.addAttributes(empty, simple);
0339: assertEquals(1, attr.getAttributeCount());
0340: Object css = attr.getAttribute(Attribute.FONT_SIZE);
0341: assertSame(fs, css);
0342: }
0343:
0344: public void testGetFont() {
0345: ss.addCSSAttribute(simple, Attribute.FONT_FAMILY, "serif");
0346: ss.addCSSAttribute(simple, Attribute.FONT_WEIGHT, "bold");
0347: assertEquals(2, simple.getAttributeCount());
0348:
0349: Font f = ss.getFont(simple);
0350: assertEquals(12, f.getSize());
0351: assertEquals("Serif", f.getName());
0352: assertEquals("Serif", f.getFamily());
0353: assertTrue(f.isBold());
0354: assertFalse(f.isItalic());
0355: }
0356:
0357: public void testGetFontCSSSizeAttribute() {
0358: ss.addCSSAttribute(simple, Attribute.FONT_SIZE, "21pt");
0359: assertEquals(1, simple.getAttributeCount());
0360:
0361: Font f = ss.getFont(simple);
0362: assertEquals(21, f.getSize());
0363: assertEquals("SansSerif", f.getName());
0364: }
0365:
0366: public void testGetFontSCSizeAttribute() {
0367: simple.addAttribute(StyleConstants.FontSize, new Integer(8));
0368:
0369: Font f = ss.getFont(simple);
0370: assertEquals(12, f.getSize());
0371: assertEquals("SansSerif", f.getName());
0372: }
0373:
0374: public void testGetFontCSSAndSCSizeAttributesMixed() {
0375: ss.addCSSAttribute(simple, Attribute.FONT_SIZE, "21pt");
0376: simple.addAttribute(StyleConstants.FontSize, new Integer(8));
0377: assertEquals(2, simple.getAttributeCount());
0378:
0379: Font f = ss.getFont(simple);
0380: assertEquals(21, f.getSize());
0381: assertEquals("SansSerif", f.getName());
0382: }
0383:
0384: public void testGetForeground() {
0385: ss.addCSSAttribute(simple, Attribute.COLOR,
0386: "rgb(50%, 25%, 75%)");
0387: assertEquals(1, simple.getAttributeCount());
0388:
0389: assertEquals(new Color(127, 63, 191), ss.getForeground(simple));
0390:
0391: assertSame(Color.BLACK, ss.getForeground(empty));
0392: }
0393:
0394: public void testGetForegroundSC() {
0395: simple.addAttribute(StyleConstants.Foreground, new Color(63,
0396: 127, 191));
0397: assertSame(Color.BLACK, ss.getForeground(empty));
0398: }
0399:
0400: public void testGetBackground() {
0401: ss.addCSSAttribute(simple, Attribute.BACKGROUND_COLOR,
0402: "rgb(77%, 55%, 33%)");
0403: assertEquals(1, simple.getAttributeCount());
0404:
0405: assertEquals(new Color(196, 140, 84), ss.getBackground(simple));
0406:
0407: assertNull(ss.getBackground(empty));
0408: }
0409:
0410: public void testGetBackgroundSC() {
0411: simple.addAttribute(StyleConstants.Background, new Color(140,
0412: 196, 84));
0413: assertNull(ss.getBackground(empty));
0414: }
0415:
0416: public void testRemoveAttribute() {
0417: attr = ss.addAttribute(empty, StyleConstants.FontSize,
0418: new Integer(10));
0419: assertEquals(1, attr.getAttributeCount());
0420: assertNotNull(attr.getAttribute(Attribute.FONT_SIZE));
0421: assertNotNull(attr.getAttribute(StyleConstants.FontSize));
0422:
0423: attr = ss.removeAttribute(attr, StyleConstants.FontSize);
0424: assertEquals(0, attr.getAttributeCount());
0425: assertNull(attr.getAttribute(Attribute.FONT_SIZE));
0426: assertNull(attr.getAttribute(StyleConstants.FontSize));
0427: }
0428:
0429: public void testRemoveAttributesAttributeSetAttributeSet_Copy() {
0430: initAttributes();
0431:
0432: simple = new SimpleAttributeSet(attr);
0433:
0434: attr = ss.removeAttributes(attr, simple);
0435: assertEquals(0, attr.getAttributeCount());
0436: assertNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0437: assertNull(attr.getAttribute(StyleConstants.Bold));
0438: assertNull(attr.getAttribute(Attribute.FONT_STYLE));
0439: assertNull(attr.getAttribute(StyleConstants.Italic));
0440: }
0441:
0442: public void testRemoveAttributesAttributeSetAttributeSet_CopyReversed() {
0443: initAttributes();
0444:
0445: simple = new SimpleAttributeSet(attr);
0446:
0447: attr = ss.removeAttributes(simple, attr);
0448: assertEquals(0, attr.getAttributeCount());
0449: assertNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0450: assertNull(attr.getAttribute(StyleConstants.Bold));
0451: assertNull(attr.getAttribute(Attribute.FONT_STYLE));
0452: assertNull(attr.getAttribute(StyleConstants.Italic));
0453: }
0454:
0455: public void testRemoveAttributesAttributeSetAttributeSet_StyleConstants() {
0456: initAttributes();
0457:
0458: simple.addAttribute(StyleConstants.Bold, Boolean.TRUE);
0459: simple.addAttribute(StyleConstants.Italic, Boolean.TRUE);
0460:
0461: assertTrue(attr.isEqual(simple));
0462:
0463: attr = ss.removeAttributes(attr, simple);
0464: if (isHarmony()) {
0465: assertEquals(0, attr.getAttributeCount());
0466: assertNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0467: assertNull(attr.getAttribute(StyleConstants.Bold));
0468: assertNull(attr.getAttribute(Attribute.FONT_STYLE));
0469: assertNull(attr.getAttribute(StyleConstants.Italic));
0470: } else {
0471: assertEquals(2, attr.getAttributeCount());
0472: assertNotNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0473: assertNotNull(attr.getAttribute(StyleConstants.Bold));
0474: assertNotNull(attr.getAttribute(Attribute.FONT_STYLE));
0475: assertNotNull(attr.getAttribute(StyleConstants.Italic));
0476: }
0477: }
0478:
0479: public void testRemoveAttributesAttributeSetAttributeSet_Reversed() {
0480: initAttributes();
0481:
0482: simple.addAttribute(StyleConstants.Bold, Boolean.TRUE);
0483: simple.addAttribute(StyleConstants.Italic, Boolean.TRUE);
0484:
0485: assertTrue(attr.isEqual(simple));
0486:
0487: attr = ss.removeAttributes(simple, attr);
0488: assertEquals(2, attr.getAttributeCount());
0489: assertNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0490: assertNotNull(attr.getAttribute(StyleConstants.Bold));
0491: assertNull(attr.getAttribute(Attribute.FONT_STYLE));
0492: assertNotNull(attr.getAttribute(StyleConstants.Italic));
0493: }
0494:
0495: public void testRemoveAttributesAttributeSetAttributeSet_Mixed() {
0496: initAttributes();
0497:
0498: simple.addAttribute(StyleConstants.Bold, Boolean.TRUE);
0499: ss.addCSSAttribute(simple, Attribute.FONT_STYLE, "italic");
0500:
0501: attr = ss.removeAttributes(attr, simple);
0502: if (isHarmony()) {
0503: assertEquals(0, attr.getAttributeCount());
0504: assertNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0505: assertNull(attr.getAttribute(StyleConstants.Bold));
0506: assertNull(attr.getAttribute(Attribute.FONT_STYLE));
0507: assertNull(attr.getAttribute(StyleConstants.Italic));
0508: } else {
0509: assertEquals(2, attr.getAttributeCount());
0510: assertNotNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0511: assertNotNull(attr.getAttribute(StyleConstants.Bold));
0512: assertNotNull(attr.getAttribute(Attribute.FONT_STYLE));
0513: assertNotNull(attr.getAttribute(StyleConstants.Italic));
0514: }
0515: }
0516:
0517: public void testRemoveAttributesAttributeSetAttributeSet_MixedSameValue() {
0518: initAttributes();
0519:
0520: assertEquals(0, simple.getAttributeCount());
0521: simple.addAttribute(StyleConstants.Bold, Boolean.TRUE);
0522: simple.addAttribute(Attribute.FONT_STYLE, attr
0523: .getAttribute(Attribute.FONT_STYLE));
0524: assertEquals(2, simple.getAttributeCount());
0525:
0526: assertEquals(2, attr.getAttributeCount());
0527: assertNotNull(Attribute.FONT_WEIGHT);
0528: assertNotNull(Attribute.FONT_STYLE);
0529:
0530: attr = ss.removeAttributes(attr, simple);
0531: if (isHarmony()) {
0532: assertEquals(0, attr.getAttributeCount());
0533: assertNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0534: assertNull(attr.getAttribute(StyleConstants.Bold));
0535: assertNull(attr.getAttribute(Attribute.FONT_STYLE));
0536: assertNull(attr.getAttribute(StyleConstants.Italic));
0537: } else {
0538: // FONT_STYLE was removed since simple contains the same
0539: // key-value pair as attr does
0540: assertEquals(1, attr.getAttributeCount());
0541: assertNotNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0542: assertNotNull(attr.getAttribute(StyleConstants.Bold));
0543: assertNull(attr.getAttribute(Attribute.FONT_STYLE));
0544: assertNull(attr.getAttribute(StyleConstants.Italic));
0545: }
0546: }
0547:
0548: public void testRemoveAttributesAttributeSetEnumeration_StyleConstants() {
0549: initAttributes();
0550:
0551: simple.addAttribute(StyleConstants.Bold, Boolean.FALSE);
0552: simple.addAttribute(StyleConstants.Italic, Boolean.FALSE);
0553:
0554: attr = ss.removeAttributes(attr, simple.getAttributeNames());
0555: if (isHarmony()) {
0556: assertEquals(0, attr.getAttributeCount());
0557: assertNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0558: assertNull(attr.getAttribute(StyleConstants.Bold));
0559: assertNull(attr.getAttribute(Attribute.FONT_STYLE));
0560: assertNull(attr.getAttribute(StyleConstants.Italic));
0561: } else {
0562: assertEquals(2, attr.getAttributeCount());
0563: assertNotNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0564: assertNotNull(attr.getAttribute(StyleConstants.Bold));
0565: assertNotNull(attr.getAttribute(Attribute.FONT_STYLE));
0566: assertNotNull(attr.getAttribute(StyleConstants.Italic));
0567: }
0568: }
0569:
0570: public void testRemoveAttributesAttributeSetEnumeration_CSS() {
0571: initAttributes();
0572:
0573: simple.addAttribute(Attribute.FONT_STYLE, Boolean.FALSE);
0574: simple.addAttribute(Attribute.FONT_WEIGHT, Boolean.FALSE);
0575:
0576: attr = ss.removeAttributes(attr, simple.getAttributeNames());
0577: assertEquals(0, attr.getAttributeCount());
0578: assertNull(attr.getAttribute(Attribute.FONT_WEIGHT));
0579: assertNull(attr.getAttribute(StyleConstants.Bold));
0580: assertNull(attr.getAttribute(Attribute.FONT_STYLE));
0581: assertNull(attr.getAttribute(StyleConstants.Italic));
0582: }
0583:
0584: /**
0585: * Adds a simple attribute to set.
0586: * (<code>CSS.Attribute.BACKGROUND_COLOR</code> is used.)
0587: */
0588: public void testAddCSSAttribute01() {
0589: assertEquals(0, simple.getAttributeCount());
0590: ss.addCSSAttribute(simple, Attribute.BACKGROUND_COLOR, "red");
0591:
0592: assertEquals(1, simple.getAttributeCount());
0593: Enumeration names = simple.getAttributeNames();
0594: Object name = names.nextElement();
0595: assertSame(Attribute.BACKGROUND_COLOR, name);
0596: assertFalse(names.hasMoreElements());
0597:
0598: Object value = simple.getAttribute(Attribute.BACKGROUND_COLOR);
0599: assertNotSame(Color.class, value.getClass());
0600: assertNotSame(String.class, value.getClass());
0601: assertEquals("red", value.toString());
0602:
0603: assertEquals("background-color=red ", simple.toString());
0604: }
0605:
0606: /**
0607: * Adds a shorthand attribute to set,
0608: * <code>CSS.Attribute.BACKGROUND</code> is used.
0609: * <p>Checks that this shorthand property is converted to individual
0610: * background-related properties.
0611: */
0612: public void testAddCSSAttribute02() {
0613: assertEquals(0, simple.getAttributeCount());
0614: ss
0615: .addCSSAttribute(simple, Attribute.BACKGROUND,
0616: "red repeat-y");
0617:
0618: assertEquals(5, simple.getAttributeCount());
0619:
0620: Attribute[] keys = { Attribute.BACKGROUND_ATTACHMENT,
0621: Attribute.BACKGROUND_COLOR, Attribute.BACKGROUND_IMAGE,
0622: Attribute.BACKGROUND_POSITION,
0623: Attribute.BACKGROUND_REPEAT };
0624: for (int i = 0; i < keys.length; i++) {
0625: assertTrue(keys[i] + " not found", simple
0626: .isDefined(keys[i]));
0627: }
0628:
0629: Object[] values = {
0630: Attribute.BACKGROUND_ATTACHMENT.getDefaultValue(),
0631: "red",
0632: isHarmony() ? Attribute.BACKGROUND_IMAGE
0633: .getDefaultValue() : null,
0634: isHarmony() ? "0% 0%" : Attribute.BACKGROUND_POSITION
0635: .getDefaultValue(), "repeat-y" };
0636: for (int i = 0; i < values.length; i++) {
0637: Object value = simple.getAttribute(keys[i]);
0638: assertNotNull("Attr value is null", value);
0639: assertEquals("Attr: " + keys[i],
0640: values[i] != null ? values[i].toString() : null,
0641: value.toString());
0642: }
0643: }
0644:
0645: /**
0646: * Shows that <code>equals</code> returns <code>false</code>
0647: * despite attribute sets contain equal values.
0648: */
0649: public void testAddCSSAttribute03() {
0650: assertEquals(0, simple.getAttributeCount());
0651: ss.addCSSAttribute(simple, Attribute.BACKGROUND_COLOR, "red");
0652: assertEquals(1, simple.getAttributeCount());
0653:
0654: MutableAttributeSet mas = new SimpleAttributeSet();
0655: mas.addAttribute(Attribute.BACKGROUND_COLOR, "red");
0656:
0657: Object key1 = simple.getAttributeNames().nextElement();
0658: Object key2 = mas.getAttributeNames().nextElement();
0659: assertEquals(key2, key1);
0660: assertSame(key2, key1);
0661:
0662: Object value1 = simple.getAttribute(key1);
0663: Object value2 = mas.getAttribute(key2);
0664: assertNotSame(value2, value1);
0665: assertFalse(value2.equals(value1));
0666: assertEquals(value2.toString(), value1.toString());
0667:
0668: assertEquals("background-color=red ", simple.toString());
0669: assertEquals("background-color=red ", mas.toString());
0670:
0671: assertFalse(simple.isEqual(mas));
0672: }
0673:
0674: public void testAddCSSAttributeFromHTML_Color() {
0675: assertTrue(ss.addCSSAttributeFromHTML(simple, Attribute.COLOR,
0676: "#112233"));
0677: assertEquals("#112233", simple.getAttribute(Attribute.COLOR)
0678: .toString());
0679:
0680: // Invalid color
0681: assertFalse(ss.addCSSAttributeFromHTML(simple, Attribute.COLOR,
0682: "color"));
0683: assertEquals("#112233", simple.getAttribute(Attribute.COLOR)
0684: .toString());
0685:
0686: // Standard color
0687: assertTrue(ss.addCSSAttributeFromHTML(simple, Attribute.COLOR,
0688: "red"));
0689: assertEquals("red", simple.getAttribute(Attribute.COLOR)
0690: .toString());
0691: }
0692:
0693: public void testAddCSSAttributeFromHTML_Align() {
0694: assertTrue(ss.addCSSAttributeFromHTML(simple,
0695: Attribute.TEXT_ALIGN, "left"));
0696: assertEquals("left", simple.getAttribute(Attribute.TEXT_ALIGN)
0697: .toString());
0698:
0699: // Incorrect align value. (The correct one is justify. Either is true
0700: // for CSS property)
0701: if (isHarmony()) {
0702: assertFalse(ss.addCSSAttributeFromHTML(simple,
0703: Attribute.TEXT_ALIGN, "justified"));
0704: // Old value preserved
0705: assertEquals("left", simple.getAttribute(
0706: Attribute.TEXT_ALIGN).toString());
0707: } else {
0708: assertTrue(ss.addCSSAttributeFromHTML(simple,
0709: Attribute.TEXT_ALIGN, "justified"));
0710: assertEquals("justified", simple.getAttribute(
0711: Attribute.TEXT_ALIGN).toString());
0712: }
0713: }
0714:
0715: public void testAddCSSAttributeFromHTML_Background() {
0716: assertTrue(ss.addCSSAttributeFromHTML(simple,
0717: Attribute.BACKGROUND_IMAGE, "bg.jpg"));
0718: assertEquals(isHarmony() ? "url(bg.jpg)" : "bg.jpg", simple
0719: .getAttribute(Attribute.BACKGROUND_IMAGE).toString());
0720: }
0721:
0722: public void testAddCSSAttributeFromHTML_AddCSS() {
0723: final Marker marker = new Marker();
0724: ss = new StyleSheet() {
0725: public void addCSSAttribute(final MutableAttributeSet attr,
0726: final Attribute key, final String value) {
0727: marker.setOccurred();
0728: super .addCSSAttribute(attr, key, value);
0729: }
0730: };
0731: assertTrue(ss.addCSSAttributeFromHTML(simple,
0732: Attribute.TEXT_ALIGN, "left"));
0733: assertEquals(isHarmony(), marker.isOccurred());
0734: }
0735:
0736: public void testCreateSmallAttributeSet() {
0737: final Object value = new Integer(12);
0738: simple.addAttribute(StyleConstants.FontSize, value);
0739: attr = ss.createSmallAttributeSet(simple);
0740: assertTrue(attr instanceof SmallAttributeSet);
0741: assertNotSame(SmallAttributeSet.class, attr.getClass());
0742:
0743: assertEquals(1, attr.getAttributeCount());
0744: assertSame(value, attr.getAttribute(StyleConstants.FontSize));
0745: assertNull(attr.getAttribute(Attribute.FONT_SIZE));
0746: }
0747:
0748: public void testCreateLargeAttributeSet() {
0749: final Object value = new Integer(12);
0750: simple.addAttribute(StyleConstants.FontSize, value);
0751: attr = ss.createLargeAttributeSet(simple);
0752: assertTrue(attr instanceof SimpleAttributeSet);
0753: assertNotSame(SimpleAttributeSet.class, attr.getClass());
0754:
0755: assertEquals(1, attr.getAttributeCount());
0756: assertSame(value, attr.getAttribute(StyleConstants.FontSize));
0757: assertNull(attr.getAttribute(Attribute.FONT_SIZE));
0758: }
0759:
0760: public void testGetBoxPainter() {
0761: BoxPainter bp = ss.getBoxPainter(empty);
0762: assertNotNull(bp);
0763: assertNotSame(bp, ss.getBoxPainter(empty));
0764: }
0765:
0766: public void testGetBoxPainterAttributes() {
0767: final Marker borderStyle = new Marker();
0768: final Marker marginTop = new Marker();
0769: final Marker marginRight = new Marker();
0770: final Marker marginBottom = new Marker();
0771: final Marker marginLeft = new Marker();
0772: final Marker backgroundColor = new Marker();
0773: final Marker backgroundImage = new Marker();
0774:
0775: simple = new SimpleAttributeSet() {
0776: public Object getAttribute(Object name) {
0777: if (name == CSS.Attribute.BORDER_STYLE) {
0778: borderStyle.setOccurred();
0779: } else if (name == CSS.Attribute.MARGIN_TOP) {
0780: marginTop.setOccurred();
0781: } else if (name == CSS.Attribute.MARGIN_RIGHT) {
0782: marginRight.setOccurred();
0783: } else if (name == CSS.Attribute.MARGIN_BOTTOM) {
0784: marginBottom.setOccurred();
0785: } else if (name == CSS.Attribute.MARGIN_LEFT) {
0786: marginLeft.setOccurred();
0787: } else if (name == CSS.Attribute.BACKGROUND_COLOR) {
0788: backgroundColor.setOccurred();
0789: } else if (name == CSS.Attribute.BACKGROUND_IMAGE) {
0790: backgroundImage.setOccurred();
0791: } else {
0792: fail("Unexpected attribute is requested: " + name);
0793: }
0794: return super .getAttribute(name);
0795: }
0796: };
0797: final BoxPainter bp = ss.getBoxPainter(simple);
0798:
0799: if (isHarmony()) {
0800: assertFalse(borderStyle.isOccurred());
0801: assertFalse(marginTop.isOccurred());
0802: assertFalse(marginRight.isOccurred());
0803: assertFalse(marginBottom.isOccurred());
0804: assertFalse(marginLeft.isOccurred());
0805: assertFalse(backgroundColor.isOccurred());
0806: assertFalse(backgroundImage.isOccurred());
0807:
0808: bp.setView(null);
0809:
0810: assertFalse(borderStyle.isOccurred());
0811:
0812: assertFalse(backgroundColor.isOccurred());
0813: assertFalse(backgroundImage.isOccurred());
0814: } else {
0815: assertTrue(borderStyle.isOccurred());
0816:
0817: assertTrue(backgroundColor.isOccurred());
0818: assertTrue(backgroundImage.isOccurred());
0819: }
0820:
0821: assertTrue(marginTop.isOccurred());
0822: assertTrue(marginRight.isOccurred());
0823: assertTrue(marginBottom.isOccurred());
0824: assertTrue(marginLeft.isOccurred());
0825: }
0826:
0827: public void testGetBoxPainterAttributesBorderStyle() {
0828: final Marker borderStyle = new Marker();
0829: final Marker borderTopWidth = new Marker();
0830: final Marker borderRightWidth = new Marker();
0831: final Marker borderBottomWidth = new Marker();
0832: final Marker borderLeftWidth = new Marker();
0833:
0834: simple = new SimpleAttributeSet() {
0835: public Object getAttribute(Object name) {
0836: if (name == CSS.Attribute.BORDER_STYLE) {
0837: borderStyle.setOccurred();
0838: } else if (name == CSS.Attribute.BORDER_TOP_WIDTH) {
0839: borderTopWidth.setOccurred();
0840: } else if (name == CSS.Attribute.BORDER_RIGHT_WIDTH) {
0841: borderRightWidth.setOccurred();
0842: } else if (name == CSS.Attribute.BORDER_BOTTOM_WIDTH) {
0843: borderBottomWidth.setOccurred();
0844: } else if (name == CSS.Attribute.BORDER_LEFT_WIDTH) {
0845: borderLeftWidth.setOccurred();
0846: } else if (name == CSS.Attribute.MARGIN_TOP
0847: || name == CSS.Attribute.MARGIN_RIGHT
0848: || name == CSS.Attribute.MARGIN_BOTTOM
0849: || name == CSS.Attribute.MARGIN_LEFT
0850: || name == CSS.Attribute.BACKGROUND_COLOR
0851: || name == CSS.Attribute.BACKGROUND_IMAGE) {
0852: ;
0853: } else {
0854: fail("Unexpected attribute is requested: " + name);
0855: }
0856: return super .getAttribute(name);
0857: }
0858: };
0859: ss.addCSSAttribute(simple, CSS.Attribute.BORDER_STYLE, "solid");
0860: final BoxPainter bp = ss.getBoxPainter(simple);
0861:
0862: if (isHarmony()) {
0863: assertFalse(borderStyle.isOccurred());
0864: assertFalse(borderTopWidth.isOccurred());
0865: assertFalse(borderRightWidth.isOccurred());
0866: assertFalse(borderBottomWidth.isOccurred());
0867: assertFalse(borderLeftWidth.isOccurred());
0868:
0869: bp.setView(null);
0870:
0871: assertFalse(borderStyle.isOccurred());
0872: assertFalse(borderTopWidth.isOccurred());
0873: } else {
0874: assertTrue(borderStyle.isOccurred());
0875: assertTrue(borderTopWidth.isOccurred());
0876: }
0877: assertFalse(borderRightWidth.isOccurred());
0878: assertFalse(borderBottomWidth.isOccurred());
0879: assertFalse(borderLeftWidth.isOccurred());
0880: }
0881:
0882: public void testGetListPainter() {
0883: ListPainter lp = ss.getListPainter(empty);
0884: assertNotNull(lp);
0885: assertNotSame(lp, ss.getListPainter(empty));
0886: }
0887:
0888: public void testGetListPainterAttributes() {
0889: final Marker listStyleImage = new Marker();
0890: final Marker listStyleType = new Marker();
0891:
0892: simple = new SimpleAttributeSet() {
0893: public Object getAttribute(Object name) {
0894: if (name == CSS.Attribute.LIST_STYLE_IMAGE) {
0895: listStyleImage.setOccurred();
0896: } else if (name == CSS.Attribute.LIST_STYLE_TYPE) {
0897: listStyleType.setOccurred();
0898: } else {
0899: fail("Unexpected attribute is requested: " + name);
0900: }
0901: return super .getAttribute(name);
0902: }
0903: };
0904: ss.getListPainter(simple);
0905:
0906: if (isHarmony()) {
0907: assertFalse(listStyleImage.isOccurred());
0908: assertFalse(listStyleType.isOccurred());
0909: } else {
0910: assertTrue(listStyleImage.isOccurred());
0911: assertTrue(listStyleType.isOccurred());
0912: }
0913: }
0914:
0915: public void testImportStyleSheet() throws Exception {
0916: final File cssFile = File.createTempFile(getName(), ".css");
0917: cssFile.deleteOnExit();
0918: final FileWriter writer = new FileWriter(cssFile);
0919: writer.write(CSS_RULES);
0920: writer.close();
0921:
0922: ss.importStyleSheet(cssFile.toURL());
0923:
0924: final Style body = ss.getStyle("body");
0925: assertEquals(2, body.getAttributeCount());
0926: final AttributeSet bodyAttr = body.getResolveParent();
0927: assertTrue(bodyAttr instanceof NamedStyle);
0928: assertEquals(2, bodyAttr.getAttributeCount());
0929: assertEquals("yellow", bodyAttr.getAttribute(
0930: Attribute.BACKGROUND_COLOR).toString());
0931: assertEquals("red", bodyAttr.getAttribute(Attribute.COLOR)
0932: .toString());
0933:
0934: final Style p = ss.getStyle("p");
0935: assertEquals(2, p.getAttributeCount());
0936: final AttributeSet pAttr = p.getResolveParent();
0937: assertEquals(1, pAttr.getAttributeCount());
0938: assertEquals("1.25cm", pAttr
0939: .getAttribute(Attribute.TEXT_INDENT).toString());
0940: }
0941:
0942: public void testImportStyleSheetAddStyles() throws Exception {
0943: final File cssFile = File.createTempFile(getName(), ".css");
0944: cssFile.deleteOnExit();
0945: final FileWriter writer = new FileWriter(cssFile);
0946: writer.write("body {\n" + " background-color: white;\n"
0947: + "}\n" + "body {\n" + " color: black;\n" + "}");
0948: writer.close();
0949:
0950: assertNull(ss.getStyle("body"));
0951:
0952: ss.importStyleSheet(cssFile.toURL());
0953:
0954: final Style body = ss.getStyle("body");
0955: assertEquals(2, body.getAttributeCount());
0956: final AttributeSet bodyAttr = body.getResolveParent();
0957: assertEquals(2, bodyAttr.getAttributeCount());
0958: assertEquals("white", bodyAttr.getAttribute(
0959: Attribute.BACKGROUND_COLOR).toString());
0960: assertEquals("black", bodyAttr.getAttribute(Attribute.COLOR)
0961: .toString());
0962: }
0963:
0964: public void testImportStyleSheetWithImports() throws Exception {
0965: final File cssFile = File.createTempFile(getName(), ".css");
0966: cssFile.deleteOnExit();
0967: FileWriter writer = new FileWriter(cssFile);
0968: writer.write("body {\n" + " color: white;\n" + "}");
0969: writer.close();
0970:
0971: final File importingCSS = File.createTempFile(getName()
0972: + "Importing", ".css");
0973: importingCSS.deleteOnExit();
0974: writer = new FileWriter(importingCSS);
0975: writer.write("@import url(" + cssFile.toURL() + ");\n"
0976: + "body {\n" + " text-align: center\n" + "}");
0977: writer.close();
0978:
0979: assertNull(ss.getStyle("body"));
0980: assertNull(ss.getStyleSheets());
0981:
0982: ss.importStyleSheet(importingCSS.toURL());
0983:
0984: assertNull(ss.getStyleSheets());
0985:
0986: final Style body = ss.getStyle("body");
0987: assertEquals(2, body.getAttributeCount());
0988: final AttributeSet bodyAttr = body.getResolveParent();
0989: assertEquals(2, bodyAttr.getAttributeCount());
0990: assertEquals("white", bodyAttr.getAttribute(Attribute.COLOR)
0991: .toString());
0992: assertEquals("center", bodyAttr.getAttribute(
0993: Attribute.TEXT_ALIGN).toString());
0994: }
0995:
0996: public void testImportStyleSheetNull() throws Exception {
0997: ss.importStyleSheet(null);
0998: }
0999:
1000: public void testLoadRules() throws Exception {
1001: final File cssFile = File.createTempFile(getName(), ".css");
1002: cssFile.deleteOnExit();
1003: final FileWriter writer = new FileWriter(cssFile);
1004: writer.write(CSS_RULES);
1005: writer.close();
1006:
1007: final Marker readerClose = new Marker();
1008: final FileReader reader = new FileReader(cssFile) {
1009: public void close() throws IOException {
1010: readerClose.setOccurred();
1011: super .close();
1012: }
1013: };
1014: ss.loadRules(reader, null);
1015: assertEquals(isHarmony(), readerClose.isOccurred());
1016: reader.close();
1017:
1018: final Style body = ss.getStyle("body");
1019: assertEquals(3, body.getAttributeCount());
1020: assertNull(body.getResolveParent());
1021: assertEquals("yellow", body.getAttribute(
1022: Attribute.BACKGROUND_COLOR).toString());
1023: assertEquals("red", body.getAttribute(Attribute.COLOR)
1024: .toString());
1025:
1026: final Style p = ss.getStyle("p");
1027: assertEquals(2, p.getAttributeCount());
1028: assertNull(p.getResolveParent());
1029: assertEquals("1.25cm", p.getAttribute(Attribute.TEXT_INDENT)
1030: .toString());
1031: }
1032:
1033: public void testLoadRulesWithImports() throws Exception {
1034: final File cssFile = File.createTempFile(getName(), ".css");
1035: cssFile.deleteOnExit();
1036: FileWriter writer = new FileWriter(cssFile);
1037: writer.write("body {\n" + " color: white;\n" + "}");
1038: writer.close();
1039:
1040: final File importingCSS = File.createTempFile(getName()
1041: + "Importing", ".css");
1042: importingCSS.deleteOnExit();
1043: writer = new FileWriter(importingCSS);
1044: writer.write("@import url('" + cssFile.toURL() + "');\n"
1045: + "body {\n" + " text-align: center\n" + "}");
1046: writer.close();
1047:
1048: assertNull(ss.getStyle("body"));
1049: assertNull(ss.getStyleSheets());
1050:
1051: ss.loadRules(new FileReader(importingCSS), null);
1052:
1053: assertNull(ss.getStyleSheets());
1054:
1055: final Style body = ss.getStyle("body");
1056: assertEquals(3, body.getAttributeCount());
1057: assertEquals("center", body.getAttribute(Attribute.TEXT_ALIGN)
1058: .toString());
1059: final AttributeSet bodyAttr = body.getResolveParent();
1060: assertEquals(1, bodyAttr.getAttributeCount());
1061: assertEquals("white", bodyAttr.getAttribute(Attribute.COLOR)
1062: .toString());
1063: }
1064:
1065: public void testSetBase() throws MalformedURLException {
1066: URL base = new URL("http://www.somesite.com/styles/");
1067: ss.setBase(base);
1068: assertSame(base, ss.getBase());
1069: }
1070:
1071: public void testGetBase() {
1072: assertNull(ss.getBase());
1073: }
1074:
1075: public void testGetIndexOfSize() {
1076: int[][] size = {
1077: // {scSizeSet, cssSize, scSizeRead}
1078: { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 2 }, { 10, 2 },
1079: { 11, 3 }, { 12, 3 }, { 13, 4 }, { 14, 4 }, { 15, 5 },
1080: { 16, 5 }, { 17, 5 }, { 18, 5 }, { 19, 6 }, { 20, 6 },
1081: { 21, 6 }, { 22, 6 }, { 23, 6 }, { 24, 6 }, { 25, 7 },
1082: { 26, 7 }, { 27, 7 }, { 28, 7 }, { 29, 7 }, { 30, 7 },
1083: { 31, 7 }, { 32, 7 }, { 33, 7 }, { 34, 7 }, { 35, 7 },
1084: { 36, 7 }, { 37, 7 }, { 38, 7 }, { 39, 7 }, { 40, 7 } };
1085: for (int i = 0; i < size.length; i++) {
1086: assertEquals("@ " + i, size[i][1], StyleSheet
1087: .getIndexOfSize(size[i][0]));
1088: }
1089: }
1090:
1091: public void testSetBaseFontSizeInt() {
1092: ss.setBaseFontSize(3);
1093: assertEquals(sizes[3], (int) ss.getPointSize("+1"));
1094: assertEquals(sizes[2], (int) ss.getPointSize("+0"));
1095: assertEquals(sizes[1], (int) ss.getPointSize("-1"));
1096:
1097: ss.setBaseFontSize(5);
1098: assertEquals(sizes[5], (int) ss.getPointSize("+1"));
1099: assertEquals(sizes[4], (int) ss.getPointSize("+0"));
1100: assertEquals(sizes[3], (int) ss.getPointSize("-1"));
1101: }
1102:
1103: public void testSetBaseFontSizeString() {
1104: assertEquals(7, sizes.length);
1105: for (int i = 0; i < sizes.length; i++) {
1106: ss.setBaseFontSize(String.valueOf(i + 1));
1107: assertEquals("@ " + i, sizes[i], (int) ss
1108: .getPointSize("+0"));
1109: }
1110: ss.setBaseFontSize("0");
1111: assertEquals(sizes[0], (int) ss.getPointSize("+0"));
1112: ss.setBaseFontSize("8");
1113: assertEquals(sizes[6], (int) ss.getPointSize("+0"));
1114: }
1115:
1116: public void testSetBaseFontSizeStringRelativeUp() {
1117: assertEquals(sizes[3], (int) ss.getPointSize("+0"));
1118: ss.setBaseFontSize("+1");
1119: assertEquals(sizes[5], (int) ss.getPointSize("+1"));
1120: assertEquals(sizes[4], (int) ss.getPointSize("+0"));
1121: assertEquals(sizes[3], (int) ss.getPointSize("-1"));
1122:
1123: ss.setBaseFontSize("+2");
1124: assertEquals(sizes[6], (int) ss.getPointSize("+1"));
1125: assertEquals(sizes[6], (int) ss.getPointSize("+0"));
1126: assertEquals(sizes[5], (int) ss.getPointSize("-1"));
1127: }
1128:
1129: public void testSetBaseFontSizeStringRelativeDown() {
1130: assertEquals(sizes[3], (int) ss.getPointSize("+0"));
1131: ss.setBaseFontSize("-1");
1132: assertEquals(sizes[3], (int) ss.getPointSize("+1"));
1133: assertEquals(sizes[2], (int) ss.getPointSize("+0"));
1134: assertEquals(sizes[1], (int) ss.getPointSize("-1"));
1135:
1136: ss.setBaseFontSize("-2");
1137: assertEquals(sizes[1], (int) ss.getPointSize("+1"));
1138: assertEquals(sizes[0], (int) ss.getPointSize("+0"));
1139: assertEquals(sizes[0], (int) ss.getPointSize("-1"));
1140: }
1141:
1142: public void testSetBaseFontSizeStringInvalidLetter() {
1143: testExceptionalCase(new NumberFormatCase() {
1144: public void exceptionalAction() throws Exception {
1145: ss.setBaseFontSize("a");
1146: }
1147: });
1148: }
1149:
1150: public void testSetBaseFontSizeStringInvalidMinus() {
1151: if (isHarmony()) {
1152: testExceptionalCase(new NumberFormatCase() {
1153: public void exceptionalAction() throws Exception {
1154: ss.setBaseFontSize("--1");
1155: }
1156: });
1157: } else {
1158: assertEquals(sizes[3], (int) ss.getPointSize("+0"));
1159: ss.setBaseFontSize("--1");
1160: assertEquals(sizes[4], (int) ss.getPointSize("+0"));
1161: testExceptionalCase(new NumberFormatCase() {
1162: public void exceptionalAction() throws Exception {
1163: ss.setBaseFontSize("---1");
1164: }
1165: });
1166: }
1167: }
1168:
1169: public void testSetBaseFontSizeStringInvalidPlus() {
1170: testExceptionalCase(new NumberFormatCase() {
1171: public void exceptionalAction() throws Exception {
1172: ss.setBaseFontSize("++1");
1173: }
1174: });
1175: }
1176:
1177: public void testGetPointSizeInt() {
1178: assertEquals(7, sizes.length);
1179: for (int i = 0; i < sizes.length; i++) {
1180: assertEquals("@ " + i, sizes[i], (int) ss
1181: .getPointSize(i + 1));
1182: }
1183: assertEquals(sizes[0], (int) ss.getPointSize(0));
1184: assertEquals(sizes[6], (int) ss.getPointSize(8));
1185: }
1186:
1187: public void testGetPointSizeString() {
1188: assertEquals(7, sizes.length);
1189: for (int i = 0; i < sizes.length; i++) {
1190: assertEquals("@ " + i, sizes[i], (int) ss
1191: .getPointSize(String.valueOf(i + 1)));
1192: }
1193: assertEquals(sizes[0], (int) ss.getPointSize("0"));
1194: assertEquals(sizes[6], (int) ss.getPointSize("7"));
1195: }
1196:
1197: public void testGetPointSizeStringRelative() {
1198: assertEquals(sizes[4], (int) ss.getPointSize("+1"));
1199: assertEquals(sizes[5], (int) ss.getPointSize("+2"));
1200: assertEquals(sizes[6], (int) ss.getPointSize("+3"));
1201: assertEquals(sizes[6], (int) ss.getPointSize("+4"));
1202: assertEquals(sizes[2], (int) ss.getPointSize("-1"));
1203: assertEquals(sizes[1], (int) ss.getPointSize("-2"));
1204: assertEquals(sizes[0], (int) ss.getPointSize("-3"));
1205: assertEquals(sizes[0], (int) ss.getPointSize("-4"));
1206: }
1207:
1208: public void testGetPointSizeStringRelativeBase() {
1209: ss.setBaseFontSize(5);
1210: assertEquals(sizes[5], (int) ss.getPointSize("+1"));
1211: assertEquals(sizes[6], (int) ss.getPointSize("+2"));
1212: assertEquals(sizes[6], (int) ss.getPointSize("+3"));
1213: assertEquals(sizes[3], (int) ss.getPointSize("-1"));
1214: assertEquals(sizes[2], (int) ss.getPointSize("-2"));
1215: assertEquals(sizes[1], (int) ss.getPointSize("-3"));
1216: }
1217:
1218: public void testGetPointSizeStringRelativeBaseLeftEnd() {
1219: ss.setBaseFontSize(1);
1220: assertEquals(sizes[1], (int) ss.getPointSize("+1"));
1221: assertEquals(sizes[0], (int) ss.getPointSize("-1"));
1222: assertEquals(sizes[0], (int) ss.getPointSize("-2"));
1223: assertEquals(sizes[0], (int) ss.getPointSize("-3"));
1224: }
1225:
1226: public void testGetPointSizeStringRelativeBaseRightEnd() {
1227: ss.setBaseFontSize(7);
1228: assertEquals(sizes[6], (int) ss.getPointSize("+1"));
1229: assertEquals(sizes[6], (int) ss.getPointSize("+2"));
1230: assertEquals(sizes[6], (int) ss.getPointSize("+3"));
1231: assertEquals(sizes[5], (int) ss.getPointSize("-1"));
1232: }
1233:
1234: public void testGetPointSizeStringInvalidLetter() {
1235: testExceptionalCase(new NumberFormatCase() {
1236: public void exceptionalAction() throws Exception {
1237: ss.getPointSize("a");
1238: }
1239: });
1240: }
1241:
1242: public void testGetPointSizeStringInvalidMinus() {
1243: if (isHarmony()) {
1244: testExceptionalCase(new NumberFormatCase() {
1245: public void exceptionalAction() throws Exception {
1246: ss.getPointSize("--1");
1247: }
1248: });
1249: } else {
1250: assertEquals(sizes[4], (int) ss.getPointSize("--1"));
1251: testExceptionalCase(new NumberFormatCase() {
1252: public void exceptionalAction() throws Exception {
1253: ss.getPointSize("---1");
1254: }
1255: });
1256: }
1257: }
1258:
1259: public void testGetPointSizeStringInvalidPlus() {
1260: testExceptionalCase(new NumberFormatCase() {
1261: public void exceptionalAction() throws Exception {
1262: ss.getPointSize("++1");
1263: }
1264: });
1265: }
1266:
1267: /**
1268: * Tests convertion of standard colors.
1269: */
1270: public void testStringToColor01() {
1271: final String[] names = { "aqua", "black", "blue", "fuchsia",
1272: "gray", "green", "lime", "maroon", "navy", "olive",
1273: "purple", "red", "silver", "teal", "white", "yellow" };
1274: final String[] hex = { "#00ffff", "#000000", "#0000ff",
1275: "#ff00ff", "#808080", "#008000", "#00ff00", "#800000",
1276: "#000080", "#808000", "#800080", "#ff0000", "#c0c0c0",
1277: "#008080", "#ffffff", "#ffff00" };
1278: final Color[] values = { Color.CYAN, Color.BLACK, Color.BLUE,
1279: Color.MAGENTA, Color.GRAY, new Color(0, 128, 0),
1280: Color.GREEN, new Color(128, 0, 0),
1281: new Color(0, 0, 128), new Color(128, 128, 0),
1282: new Color(128, 0, 128), Color.RED, Color.LIGHT_GRAY,
1283: new Color(0, 128, 128), Color.WHITE, Color.YELLOW };
1284:
1285: assertEquals(names.length, values.length);
1286: for (int i = 0; i < names.length; i++) {
1287: Color color = ss.stringToColor(names[i]);
1288: assertEquals("@ " + i + " '" + names[i] + "'", values[i],
1289: color);
1290: assertEquals("@ " + i + " '" + hex[i] + "'", values[i], ss
1291: .stringToColor(hex[i]));
1292: if (isHarmony()) {
1293: assertSame("@ " + i + " '" + names[i] + "'", color, ss
1294: .stringToColor(names[i]));
1295: assertSame("@ " + i + " '" + hex[i] + "'", color, ss
1296: .stringToColor(hex[i]));
1297: } else {
1298: assertNotSame("@ " + i + " '" + names[i] + "'", color,
1299: ss.stringToColor(names[i]));
1300: }
1301: }
1302: }
1303:
1304: /**
1305: * Tests convertion of hex strings.
1306: */
1307: public void testStringToColor02() {
1308: assertEquals(new Color(0x1E, 0x2F, 0xFF), ss
1309: .stringToColor("#1E2FFF"));
1310: assertEquals(new Color(0xFF, 0x11, 0x22), ss
1311: .stringToColor("#FF1122"));
1312: assertEquals(new Color(0x12, 0x33, 0x21), ss
1313: .stringToColor("#123321"));
1314:
1315: if (isHarmony()) {
1316: assertEquals(new Color(0xFF, 0xFF, 0xFF), ss
1317: .stringToColor("#fff"));
1318:
1319: assertNull(ss.stringToColor("#f"));
1320:
1321: assertNull(ss.stringToColor("15"));
1322: } else {
1323: assertEquals(new Color(0x00, 0x0F, 0xFF), ss
1324: .stringToColor("#fff"));
1325:
1326: assertEquals(new Color(0x0, 0x0, 0x0F), ss
1327: .stringToColor("#f"));
1328:
1329: assertEquals(new Color(0x0, 0x0, 0x15), ss
1330: .stringToColor("15"));
1331: }
1332:
1333: assertNull(ss.stringToColor("zoom"));
1334: }
1335:
1336: /**
1337: * Tests convertion of mixed-case standard names.
1338: */
1339: public void testStringToColor03() {
1340: assertEquals(Color.RED, ss.stringToColor("rEd"));
1341: assertEquals(Color.BLACK, ss.stringToColor("bLaCk"));
1342: assertEquals(Color.WHITE, ss.stringToColor("White"));
1343: }
1344:
1345: /**
1346: * Tests convertion of extended list of named colors.
1347: */
1348: public void testStringToColor04() {
1349: assertNull(ss.stringToColor("azure"));
1350: assertNull(ss.stringToColor("blanchedalmond"));
1351: assertNull(ss.stringToColor("mistyrose"));
1352: assertNull(ss.stringToColor("lavender"));
1353: assertNull(ss.stringToColor("floralwhite"));
1354: }
1355:
1356: /**
1357: * Tests with empty string.
1358: */
1359: public void testStringToColor05() {
1360: if (isHarmony()) {
1361: assertNull(ss.stringToColor(""));
1362: } else {
1363: assertEquals(Color.BLACK, ss.stringToColor(""));
1364: }
1365:
1366: if (!isHarmony()) {
1367: testExceptionalCase(new NullPointerCase() {
1368: public void exceptionalAction() throws Exception {
1369: ss.stringToColor(null);
1370: }
1371: });
1372: } else {
1373: assertNull(ss.stringToColor(null));
1374: }
1375: }
1376:
1377: public void testTranslateHTMLToCSS() {
1378: if (!isHarmony()) {
1379: // Calling ss.translateHTMLToCSS with all the classes I know which
1380: // implement AttributeSet throws ClassCastException
1381: //ss.translateHTMLToCSS(simple);
1382: //ss.translateHTMLToCSS(ss.createSmallAttributeSet(simple));
1383: //ss.translateHTMLToCSS(ss.createLargeAttributeSet(simple));
1384: //ss.translateHTMLToCSS(ss.new SmallAttributeSet(simple));
1385: //ss.translateHTMLToCSS(ss.getStyle("default"));
1386: //ss.translateHTMLToCSS(ss.getRule("default"));
1387: return;
1388: }
1389: simple.addAttribute(HTML.Attribute.BGCOLOR, "yellow");
1390: simple.addAttribute(HTML.Attribute.BACKGROUND, "bg.jpg");
1391: simple.addAttribute(HTML.Attribute.WIDTH, "100%");
1392: // System.out.println("Original:\n" + simple);
1393:
1394: attr = ss.translateHTMLToCSS(simple);
1395: // System.out.println("\nTranslated:\n" + attr);
1396: assertEquals("yellow", attr.getAttribute(
1397: Attribute.BACKGROUND_COLOR).toString());
1398: assertEquals("url(bg.jpg)", attr.getAttribute(
1399: Attribute.BACKGROUND_IMAGE).toString());
1400: assertEquals("100%", attr.getAttribute(Attribute.WIDTH)
1401: .toString());
1402: }
1403:
1404: private void initAttributes() {
1405: attr = ss
1406: .addAttribute(empty, StyleConstants.Bold, Boolean.TRUE);
1407: attr = ss.addAttribute(attr, StyleConstants.Italic,
1408: Boolean.TRUE);
1409: assertEquals(2, attr.getAttributeCount());
1410: assertNotNull(attr.getAttribute(Attribute.FONT_WEIGHT));
1411: assertNotNull(attr.getAttribute(StyleConstants.Bold));
1412: assertNotNull(attr.getAttribute(Attribute.FONT_STYLE));
1413: assertNotNull(attr.getAttribute(StyleConstants.Italic));
1414: }
1415: }
|