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.Color;
023: import java.util.Enumeration;
024:
025: import javax.swing.BasicSwingTestCase;
026: import javax.swing.text.AttributeSet;
027: import javax.swing.text.MutableAttributeSet;
028: import javax.swing.text.SimpleAttributeSet;
029: import javax.swing.text.Style;
030: import javax.swing.text.StyleConstants;
031: import javax.swing.text.StyleContext;
032: import javax.swing.text.html.CSS.Attribute;
033:
034: public class StyleSheet_RulesTest extends BasicSwingTestCase {
035: private StyleSheet ss;
036: private Enumeration rules;
037:
038: protected void setUp() throws Exception {
039: super .setUp();
040: ss = new StyleSheet();
041: }
042:
043: /**
044: * Several rules (selector - property list pairs) in one string.
045: */
046: public void testAddRule01() throws Exception {
047: ss.addRule("p { text-align: right; }\nem {color: red}");
048: Style pStyle = null;
049: Style emStyle = null;
050: Style defStyle = null;
051: rules = ss.getStyleNames();
052: while (rules.hasMoreElements()) {
053: String name = (String) rules.nextElement();
054: Style style = ss.getStyle(name);
055: if ("p".equals(name)) {
056: pStyle = style;
057: } else if ("em".equals(name)) {
058: emStyle = style;
059: } else if (StyleContext.DEFAULT_STYLE.equals(name)) {
060: defStyle = style;
061: } else {
062: fail("Unexpected style: " + style);
063: }
064: }
065:
066: assertEquals(2, pStyle.getAttributeCount());
067: assertEquals("right", pStyle.getAttribute(Attribute.TEXT_ALIGN)
068: .toString());
069: assertNotNull(pStyle.getAttribute(AttributeSet.NameAttribute));
070: assertEquals(StyleConstants.ALIGN_RIGHT, ((Integer) pStyle
071: .getAttribute(StyleConstants.Alignment)).intValue());
072:
073: assertEquals(2, emStyle.getAttributeCount());
074: assertEquals("red", emStyle.getAttribute(Attribute.COLOR)
075: .toString());
076: assertNotNull(emStyle.getAttribute(AttributeSet.NameAttribute));
077: assertEquals(Color.RED, emStyle
078: .getAttribute(StyleConstants.Foreground));
079:
080: assertEquals(1, defStyle.getAttributeCount());
081: assertNotNull(defStyle.getAttribute(AttributeSet.NameAttribute));
082: }
083:
084: /**
085: * Several properties defined for one selector.
086: */
087: public void testAddRule02() throws Exception {
088: ss.addRule("p { text-align: justify; background-color: black; "
089: + "color: white }");
090: Style pStyle = null;
091: rules = ss.getStyleNames();
092: while (rules.hasMoreElements()) {
093: String name = (String) rules.nextElement();
094: Style style = ss.getStyle(name);
095: if ("p".equals(name)) {
096: pStyle = style;
097: } else if (!StyleContext.DEFAULT_STYLE.equals(name)) {
098: fail("Unexpected style: " + style);
099: }
100: }
101:
102: assertEquals(4, pStyle.getAttributeCount());
103: assertEquals("justify", pStyle.getAttribute(
104: Attribute.TEXT_ALIGN).toString());
105: assertEquals(StyleConstants.ALIGN_JUSTIFIED, ((Integer) pStyle
106: .getAttribute(StyleConstants.Alignment)).intValue());
107:
108: assertEquals("white", pStyle.getAttribute(Attribute.COLOR)
109: .toString());
110: assertEquals(Color.WHITE, pStyle
111: .getAttribute(StyleConstants.Foreground));
112:
113: assertEquals("black", pStyle.getAttribute(
114: Attribute.BACKGROUND_COLOR).toString());
115: assertEquals(Color.BLACK, pStyle
116: .getAttribute(StyleConstants.Background));
117: assertNotNull(pStyle.getAttribute(AttributeSet.NameAttribute));
118: }
119:
120: /**
121: * Tests that <code>addRule</code> uses <code>addCSSAttribute</code>
122: * to fill the attribute set.
123: */
124: public void testAddRule03() throws Exception {
125: final Marker marker = new Marker();
126: ss = new StyleSheet() {
127: public void addCSSAttribute(final MutableAttributeSet attr,
128: final Attribute key, final String value) {
129: marker.setOccurred();
130: // assertFalse(attr instanceof Style);
131: assertSame(Attribute.FONT_SIZE, key);
132: assertEquals("21pt", value);
133: super .addCSSAttribute(attr, key, value);
134: }
135: };
136: ss.addRule(" p { font-size : 21pt } ");
137:
138: assertTrue(marker.isOccurred());
139: }
140:
141: /**
142: * Tests that <code>addRule</code> calls <code>addStyle</code> to actually
143: * add the rule.
144: */
145: public void testAddRule04() throws Exception {
146: final Marker marker = new Marker();
147: ss = new StyleSheet() {
148: public Style addStyle(String name, Style parent) {
149: marker.setOccurred();
150: if (marker.getAuxiliary() != null) {
151: assertEquals("p", name);
152: assertNull(parent);
153: } else {
154: assertEquals("default", name);
155: marker.setAuxiliary(name);
156: }
157: return super .addStyle(name, parent);
158: }
159: };
160: assertTrue(marker.isOccurred());
161: marker.setOccurred(false);
162:
163: ss.addRule(" p { font-size : 21pt } ");
164: assertTrue(marker.isOccurred());
165: }
166:
167: /**
168: * Tests that <code>addRule</code> stores rule as Style.
169: */
170: public void testAddRule05() throws Exception {
171: ss.addRule("em { text-decoration: underline }");
172: Style emStyle = ss.getStyle("em");
173:
174: assertEquals(2, emStyle.getAttributeCount());
175: assertEquals("underline", emStyle.getAttribute(
176: CSS.Attribute.TEXT_DECORATION).toString());
177: assertEquals("em", emStyle
178: .getAttribute(AttributeSet.NameAttribute));
179: }
180:
181: /**
182: * Adding Style IS/IS NOT equivalent to adding a rule.
183: */
184: public void testAddRule06() throws Exception {
185: Style emStyle = ss.getStyle("em");
186: assertNull(emStyle);
187:
188: emStyle = ss.addStyle("em", null);
189: ss.addCSSAttribute(emStyle, CSS.Attribute.TEXT_DECORATION,
190: "underline");
191:
192: Style emRule = ss.getRule("em");
193: assertNotNull(emRule);
194: assertEquals(isHarmony() ? 2 : 0, emRule.getAttributeCount());
195: }
196:
197: /**
198: * Adding a property with several selectors.
199: */
200: public void testAddRule07() throws Exception {
201: assertNull(ss.getStyle("ol"));
202: assertNull(ss.getStyle("ul"));
203:
204: ss.addRule("ol, ul { margin-left: 3pt }");
205:
206: Style ol = ss.getStyle("ol");
207: assertNotNull(ol.getAttribute(Attribute.MARGIN_LEFT));
208:
209: Style ul = ss.getStyle("ul");
210: assertNotNull(ul.getAttribute(Attribute.MARGIN_LEFT));
211: }
212:
213: /**
214: * Tags are not case-sensitive in HTML.
215: */
216: public void testAddRule08() throws Exception {
217: assertNull(ss.getStyle("h1"));
218: assertNull(ss.getStyle("H1"));
219: ss.addRule("H1 { color: blue }");
220: assertNotNull(ss.getStyle("h1"));
221: assertNull(ss.getStyle("H1"));
222:
223: Style h1 = ss.getRule("h1");
224: assertEquals(2, h1.getAttributeCount());
225: assertEquals("blue", h1.getAttribute(CSS.Attribute.COLOR)
226: .toString());
227:
228: Style H1 = ss.getRule("H1");
229: if (isHarmony()) {
230: assertEquals(2, H1.getAttributeCount());
231: assertEquals("blue", H1.getAttribute(CSS.Attribute.COLOR)
232: .toString());
233: assertSame(h1, H1);
234: } else {
235: assertEquals(0, H1.getAttributeCount());
236: assertNull(H1.getAttribute(CSS.Attribute.COLOR));
237: assertNotSame(h1, H1);
238: }
239: }
240:
241: /**
242: * Classes are case-sensitive.
243: */
244: public void testAddRule09() throws Exception {
245: assertNull(ss.getStyle(".header"));
246: assertNull(ss.getStyle(".HEADER"));
247: ss.addRule(".HEADER { color: blue }");
248: Style headerStyle = ss.getStyle(".header");
249: Style HEADERstyle = ss.getStyle(".HEADER");
250: Style headerRule = ss.getRule(".header");
251: Style HEADERrule = ss.getRule(".HEADER");
252: if (isHarmony()) {
253: assertNull(headerStyle);
254: assertNotNull(HEADERstyle);
255:
256: assertEquals(0, headerRule.getAttributeCount());
257: assertEquals(2, HEADERrule.getAttributeCount());
258: } else {
259: assertNotNull(headerStyle);
260: assertNull(HEADERstyle);
261:
262: assertEquals(2, headerRule.getAttributeCount());
263: assertEquals(0, HEADERrule.getAttributeCount());
264: }
265: assertNotSame(headerRule, HEADERrule);
266: }
267:
268: /**
269: * ids are case-sensitive.
270: */
271: public void testAddRule10() throws Exception {
272: assertNull(ss.getStyle(".id"));
273: assertNull(ss.getStyle(".ID"));
274: ss.addRule("#ID { color: blue }");
275: Style idStyle = ss.getStyle("#id");
276: Style IDstyle = ss.getStyle("#ID");
277: Style idRule = ss.getRule("#id");
278: Style IDrule = ss.getRule("#ID");
279: if (isHarmony()) {
280: assertNull(idStyle);
281: assertNotNull(IDstyle);
282:
283: assertEquals(0, idRule.getAttributeCount());
284: assertEquals(2, IDrule.getAttributeCount());
285: } else {
286: assertNotNull(idStyle);
287: assertNull(IDstyle);
288:
289: assertEquals(2, idRule.getAttributeCount());
290: assertEquals(0, IDrule.getAttributeCount());
291: }
292: assertNotSame(idRule, IDrule);
293: }
294:
295: /**
296: * Parses one property using <code>addCSSAttribute</code> to add
297: * the attribute to the set.
298: */
299: public void testGetDeclaration01() throws Exception {
300: final Marker marker = new Marker();
301: ss = new StyleSheet() {
302: public void addCSSAttribute(final MutableAttributeSet attr,
303: final Attribute key, final String value) {
304: marker.setOccurred();
305: assertSame(Attribute.FONT_SIZE, key);
306: assertEquals("13pt", value);
307: super .addCSSAttribute(attr, key, value);
308: }
309: };
310: AttributeSet attr = ss.getDeclaration("font-size: 13pt");
311: assertTrue(marker.isOccurred());
312: assertEquals("13pt", attr.getAttribute(Attribute.FONT_SIZE)
313: .toString());
314: // assertEquals(13, ((Integer)attr.getAttribute(StyleConstants.FontSize))
315: // .intValue());
316: assertNull(attr.getAttribute(StyleConstants.FontSize));
317:
318: assertSame(SimpleAttributeSet.class, attr.getClass());
319: }
320:
321: /**
322: * Parses several properties at once.
323: */
324: public void testGetDeclaration02() throws Exception {
325: AttributeSet attr = ss
326: .getDeclaration("font-family: monospace; "
327: + "color: rgb(11, 33, 99); "
328: + "text-align: center");
329: assertEquals(3, attr.getAttributeCount());
330: assertEquals("monospace", attr.getAttribute(
331: Attribute.FONT_FAMILY).toString());
332: assertEquals("rgb(11, 33, 99)", attr.getAttribute(
333: Attribute.COLOR).toString());
334: assertEquals("center", attr.getAttribute(Attribute.TEXT_ALIGN)
335: .toString());
336: }
337: }
|