001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.util.Enumeration;
023:
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.text.AttributeSet;
026: import javax.swing.text.MutableAttributeSet;
027: import javax.swing.text.SimpleAttributeSet;
028: import javax.swing.text.html.CSS.Attribute;
029:
030: public abstract class StyleSheet_ConvertAttr_MarginTestCase extends
031: BasicSwingTestCase {
032:
033: protected StyleSheet ss;
034:
035: protected AttributeSet empty;
036: protected AttributeSet attr;
037: protected MutableAttributeSet simple;
038:
039: protected Attribute cssKey;
040: protected Object cssValue;
041: protected Object scKey;
042: protected Object scValue;
043:
044: protected String defUnits;
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048: ss = new StyleSheet();
049: empty = ss.getEmptySet();
050: simple = new SimpleAttributeSet();
051: defUnits = isHarmony() ? "pt" : "";
052: }
053:
054: // Specifies size in points.
055: public void testLength() {
056: attr = ss.addAttribute(empty, scKey, new Float(11.1));
057:
058: Enumeration names = attr.getAttributeNames();
059: Object name = names.nextElement();
060: assertSame(cssKey, name);
061: assertFalse(names.hasMoreElements());
062:
063: cssValue = attr.getAttribute(cssKey);
064: scValue = attr.getAttribute(scKey);
065: assertSame(Float.class, scValue.getClass());
066: assertNotSame(Float.class, cssValue.getClass());
067: assertNotSame(String.class, cssValue.getClass());
068: assertEquals(11.1f, ((Float) scValue).floatValue());
069: assertEquals("11.1" + defUnits, cssValue.toString());
070: }
071:
072: public void testLengthString() {
073: if (!isHarmony()) {
074: testExceptionalCase(new ClassCastCase() {
075: public void exceptionalAction() throws Exception {
076: ss.addAttribute(empty, scKey, "11.1pt");
077: }
078: });
079: return;
080: }
081: attr = ss.addAttribute(empty, scKey, "11.1pt");
082:
083: cssValue = attr.getAttribute(cssKey);
084: scValue = attr.getAttribute(scKey);
085: assertEquals(11.1f, ((Float) scValue).floatValue());
086: assertEquals("11.1pt", cssValue.toString());
087: }
088:
089: public void testLengthInteger() {
090: testExceptionalCase(new ExceptionalCase() {
091: public void exceptionalAction() throws Exception {
092: ss.addAttribute(empty, scKey, new Integer(11));
093: }
094:
095: public Class expectedExceptionClass() {
096: return isHarmony() ? NullPointerException.class
097: : ClassCastException.class;
098: }
099: });
100: }
101:
102: public void testLength11_1() {
103: ss.addCSSAttribute(simple, cssKey, "11.1");
104: if (isHarmony()) {
105: assertEquals(0, simple.getAttributeCount());
106: return;
107: }
108: attr = ss.createSmallAttributeSet(simple);
109:
110: cssValue = attr.getAttribute(cssKey);
111: scValue = attr.getAttribute(scKey);
112: assertEquals("11.1", cssValue.toString());
113: assertEquals(11.1f, ((Float) scValue).floatValue());
114: }
115:
116: public void testLength0() {
117: ss.addCSSAttribute(simple, cssKey, "0");
118: attr = ss.createSmallAttributeSet(simple);
119:
120: cssValue = attr.getAttribute(cssKey);
121: scValue = attr.getAttribute(scKey);
122: assertEquals("0", cssValue.toString());
123: assertEquals(0f, ((Float) scValue).floatValue());
124: }
125:
126: public void testLength0px() {
127: ss.addCSSAttribute(simple, cssKey, "0px");
128: attr = ss.createSmallAttributeSet(simple);
129:
130: cssValue = attr.getAttribute(cssKey);
131: scValue = attr.getAttribute(scKey);
132: assertEquals("0px", cssValue.toString());
133: assertEquals(0f, ((Float) scValue).floatValue());
134: }
135:
136: public void testLength11_1pt() {
137: ss.addCSSAttribute(simple, cssKey, "11.1pt");
138: attr = ss.createSmallAttributeSet(simple);
139:
140: cssValue = attr.getAttribute(cssKey);
141: scValue = attr.getAttribute(scKey);
142: assertSame(Float.class, scValue.getClass());
143: assertNotSame(Float.class, cssValue.getClass());
144: assertNotSame(String.class, cssValue.getClass());
145: assertEquals("11.1pt", cssValue.toString());
146: assertEquals(11.1f, ((Float) scValue).floatValue());
147: }
148:
149: public void testLength11_1px() {
150: ss.addCSSAttribute(simple, cssKey, "11.1px");
151: attr = ss.createSmallAttributeSet(simple);
152:
153: cssValue = attr.getAttribute(cssKey);
154: scValue = attr.getAttribute(scKey);
155: assertEquals("11.1px", cssValue.toString());
156: assertEquals(14.43f, ((Float) scValue).floatValue());
157: }
158:
159: public void testLength11_1mm() {
160: ss.addCSSAttribute(simple, cssKey, "11.1mm");
161: attr = ss.createSmallAttributeSet(simple);
162:
163: cssValue = attr.getAttribute(cssKey);
164: scValue = attr.getAttribute(scKey);
165: assertEquals("11.1mm", cssValue.toString());
166: assertEquals(31.464506f, ((Float) scValue).floatValue(),
167: 0.00007f);
168: }
169:
170: public void testLength11_1cm() {
171: ss.addCSSAttribute(simple, cssKey, "11.1cm");
172: attr = ss.createSmallAttributeSet(simple);
173:
174: cssValue = attr.getAttribute(cssKey);
175: scValue = attr.getAttribute(scKey);
176: assertEquals("11.1cm", cssValue.toString());
177: assertEquals(314.64506f, ((Float) scValue).floatValue(),
178: 0.0007f);
179: }
180:
181: public void testLength11_1pc() {
182: ss.addCSSAttribute(simple, cssKey, "11.1pc");
183: attr = ss.createSmallAttributeSet(simple);
184:
185: cssValue = attr.getAttribute(cssKey);
186: scValue = attr.getAttribute(scKey);
187: assertEquals("11.1pc", cssValue.toString());
188: assertEquals(133.20001f, ((Float) scValue).floatValue());
189: }
190:
191: public void testLength11_1in() {
192: ss.addCSSAttribute(simple, cssKey, "11.1in");
193: attr = ss.createSmallAttributeSet(simple);
194:
195: cssValue = attr.getAttribute(cssKey);
196: scValue = attr.getAttribute(scKey);
197: assertEquals("11.1in", cssValue.toString());
198: assertEquals(799.2f, ((Float) scValue).floatValue());
199: }
200:
201: public void testLengthMinus11_1pt() {
202: ss.addCSSAttribute(simple, cssKey, "-11.1pt");
203: attr = ss.createSmallAttributeSet(simple);
204:
205: cssValue = attr.getAttribute(cssKey);
206: scValue = attr.getAttribute(scKey);
207: assertEquals("-11.1pt", cssValue.toString());
208: assertEquals(isHarmony() ? -11.1f : 0, ((Float) scValue)
209: .floatValue());
210: }
211:
212: private static void assertEquals(final float expected,
213: final float actual) {
214: assertEquals(expected, actual, 0f);
215: }
216: }
|