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.io.StringReader;
023: import java.util.Enumeration;
024: import java.util.NoSuchElementException;
025:
026: import javax.swing.BasicSwingTestCase;
027: import javax.swing.event.ChangeEvent;
028: import javax.swing.event.ChangeListener;
029: import javax.swing.text.AttributeSet;
030: import javax.swing.text.Element;
031: import javax.swing.text.Style;
032: import javax.swing.text.html.CSS.Attribute;
033: import javax.swing.text.html.HTML.Tag;
034:
035: public class StyleSheet_ResolvedRulesTest extends BasicSwingTestCase {
036: private static final int P_EM_START_OFFSET = 20;
037: private static final int PSMALL_EM_START_OFFSET = 82;
038:
039: private HTMLDocument doc;
040: private StyleSheet ss;
041: private Style rule;
042:
043: protected void setUp() throws Exception {
044: super .setUp();
045: setIgnoreNotImplemented(true);
046: ss = new StyleSheet();
047: doc = new HTMLDocument(ss);
048: doc.setAsynchronousLoadPriority(-1);
049: HTMLEditorKit kit = new HTMLEditorKit();
050: StringReader reader = new StringReader(
051: "<html><head><style>\n"
052: + "body { font-family: serif; font-size: large }\n"
053: + "p { text-indent: 10pt }\n"
054: + "p.small { font-size: small}\n"
055: + "p.small em {color: red}"
056: + "</style></head>"
057: + "<body>"
058: + "<p>Normal paragraph: <em>em inside</em>"
059: + " paragraph."
060: + "<p class=\"small\">Paragraph with small class also contains"
061: + " <em>em tag</em> which should be in red."
062: + "</body></html>");
063: kit.read(reader, doc, 0);
064: }
065:
066: public void testGetRuleTagElement_NoContext() {
067: Element em = doc.getCharacterElement(P_EM_START_OFFSET);
068: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
069:
070: rule = ss.getRule(Tag.EM, em);
071: assertEquals(0, rule.getAttributeCount());
072: assertEquals("html body p em", rule.getName());
073: }
074:
075: public void testGetRuleTagElement_NoContext_Same() {
076: Element em = doc.getCharacterElement(P_EM_START_OFFSET);
077: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
078:
079: rule = ss.getRule(Tag.EM, em);
080: assertSame(rule, ss.getRule(Tag.EM, em));
081: }
082:
083: public void testGetRuleTagElement_Context() {
084: Element em = doc.getCharacterElement(PSMALL_EM_START_OFFSET);
085: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
086:
087: rule = ss.getRule(Tag.EM, em);
088: assertEquals(2, rule.getAttributeCount());
089: assertEquals("red", rule.getAttribute(Attribute.COLOR)
090: .toString());
091: assertEquals("p.small em", rule
092: .getAttribute(AttributeSet.NameAttribute));
093: assertEquals("html body p.small em", rule.getName());
094: }
095:
096: public void testGetRuleTagElement_Context_Same() {
097: Element em = doc.getCharacterElement(PSMALL_EM_START_OFFSET);
098: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
099:
100: rule = ss.getRule(Tag.EM, em);
101: assertSame(rule, ss.getRule(Tag.EM, em));
102: }
103:
104: public void testGetRuleTagElement_AutoChange_NoContext() {
105: Element em = doc.getCharacterElement(P_EM_START_OFFSET);
106: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
107:
108: rule = ss.getRule(Tag.EM, em);
109: assertEquals(0, rule.getAttributeCount());
110:
111: ss.addRule("em { background-color: rgb(255, 255, 150) }");
112: assertEquals(2, rule.getAttributeCount());
113: assertEquals("rgb(255, 255, 150)", rule.getAttribute(
114: Attribute.BACKGROUND_COLOR).toString());
115: assertEquals("em", rule
116: .getAttribute(AttributeSet.NameAttribute));
117: }
118:
119: public void testGetRuleTagElement_AutoChange_NoContextOverride() {
120: Element em = doc.getCharacterElement(P_EM_START_OFFSET);
121: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
122:
123: rule = ss.getRule(Tag.EM, em);
124: assertEquals(0, rule.getAttributeCount());
125:
126: ss.addRule("em { color: rgb(127, 0, 0) }");
127: assertEquals(2, rule.getAttributeCount());
128: assertEquals("rgb(127, 0, 0)", rule.getAttribute(
129: Attribute.COLOR).toString());
130: assertEquals("em", rule
131: .getAttribute(AttributeSet.NameAttribute));
132: }
133:
134: public void testGetRuleTagElement_AutoChange_Context() {
135: Element em = doc.getCharacterElement(PSMALL_EM_START_OFFSET);
136: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
137:
138: rule = ss.getRule(Tag.EM, em);
139: assertEquals(2, rule.getAttributeCount());
140:
141: ss.addRule("em { background-color: rgb(255, 255, 150) }");
142: assertEquals(4, rule.getAttributeCount());
143: assertEquals("rgb(255, 255, 150)", rule.getAttribute(
144: Attribute.BACKGROUND_COLOR).toString());
145: assertEquals("red", rule.getAttribute(Attribute.COLOR)
146: .toString());
147: assertEquals("p.small em", rule
148: .getAttribute(AttributeSet.NameAttribute));
149:
150: assertEquals(2, getNameAttributeCount(rule));
151: }
152:
153: public void testGetRuleTagElement_AutoChange_ContextOverride() {
154: Element em = doc.getCharacterElement(PSMALL_EM_START_OFFSET);
155: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
156:
157: rule = ss.getRule(Tag.EM, em);
158: assertEquals(2, rule.getAttributeCount());
159:
160: ss.addRule("em { color: rgb(127, 0, 0) }");
161: assertEquals(4, rule.getAttributeCount());
162: assertEquals("red", rule.getAttribute(Attribute.COLOR)
163: .toString());
164: assertEquals("p.small em", rule
165: .getAttribute(AttributeSet.NameAttribute));
166:
167: assertEquals(2, getNameAttributeCount(rule));
168: assertEquals(2, getAttributeCount(rule, Attribute.COLOR));
169: }
170:
171: public void testGetRuleTagElement_AutoChange_ChangeStyle() {
172: Element em = doc.getCharacterElement(PSMALL_EM_START_OFFSET);
173: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
174:
175: rule = ss.getRule(Tag.EM, em);
176: assertEquals(2, rule.getAttributeCount());
177:
178: ss.addCSSAttribute(rule, Attribute.BACKGROUND_COLOR, "#FFFFFF");
179: assertEquals(2, rule.getAttributeCount());
180: assertNull(rule.getAttribute(Attribute.BACKGROUND_COLOR));
181:
182: ss.addRule("em { color: rgb(127, 0, 0) }");
183: assertEquals(4, rule.getAttributeCount());
184: assertEquals("red", rule.getAttribute(Attribute.COLOR)
185: .toString());
186: assertEquals("p.small em", rule
187: .getAttribute(AttributeSet.NameAttribute));
188:
189: assertEquals(2, getNameAttributeCount(rule));
190: assertEquals(2, getAttributeCount(rule, Attribute.COLOR));
191: }
192:
193: public void testGetRuleTagElement_AutoChange_ChangeStyle_Override() {
194: Element em = doc.getCharacterElement(PSMALL_EM_START_OFFSET);
195: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
196:
197: rule = ss.getRule(Tag.EM, em);
198: assertEquals(2, rule.getAttributeCount());
199:
200: ss.addCSSAttribute(rule, Attribute.COLOR, "#000000");
201: assertEquals(2, rule.getAttributeCount());
202: assertEquals("red", rule.getAttribute(Attribute.COLOR)
203: .toString());
204:
205: ss.addRule("em { color: rgb(127, 0, 0) }");
206: assertEquals(4, rule.getAttributeCount());
207: assertEquals("red", rule.getAttribute(Attribute.COLOR)
208: .toString());
209: assertEquals("p.small em", rule
210: .getAttribute(AttributeSet.NameAttribute));
211:
212: assertEquals(2, getNameAttributeCount(rule));
213: assertEquals(2, getAttributeCount(rule, Attribute.COLOR));
214: }
215:
216: public void testGetRuleTagElement_AddStyle() {
217: Element em = doc.getCharacterElement(P_EM_START_OFFSET);
218: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
219:
220: assertEquals(5, getStyleNumber());
221: assertNull(ss.getStyle("em"));
222:
223: rule = ss.getRule(Tag.EM, em);
224: assertEquals("html body p em", rule.getName());
225: assertEquals(0, rule.getAttributeCount());
226:
227: assertEquals(5, getStyleNumber());
228: assertNull(ss.getStyle("em"));
229: }
230:
231: public void testGetRuleString_AddStyle() {
232: assertEquals(5, getStyleNumber());
233: assertNull(ss.getStyle("em"));
234:
235: rule = ss.getRule("em");
236: assertEquals("em", rule.getName());
237: assertEquals(0, rule.getAttributeCount());
238:
239: assertEquals(5, getStyleNumber());
240: assertNull(ss.getStyle("em"));
241: }
242:
243: public void testGetRule_NotSame() {
244: Element em = doc.getCharacterElement(P_EM_START_OFFSET); // With no context associated
245: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
246:
247: rule = ss.getRule(Tag.EM, em);
248: Style ruleEm = ss.getRule("em");
249: assertNotSame(rule, ruleEm);
250: assertEquals("html body p em", rule.getName());
251: assertEquals("em", ruleEm.getName());
252: }
253:
254: public void testGetRule_Same() {
255: Element em = doc.getCharacterElement(P_EM_START_OFFSET); // With no context associated
256: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
257:
258: rule = ss.getRule(Tag.EM, em);
259: assertEquals("html body p em", rule.getName());
260: assertSame(rule, ss.getRule("html body p em"));
261: }
262:
263: public void testGetRule_Same_Context() {
264: Element em = doc.getCharacterElement(PSMALL_EM_START_OFFSET); // With context associated
265: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
266:
267: rule = ss.getRule(Tag.EM, em);
268: assertEquals("html body p.small em", rule.getName());
269: assertSame(rule, ss.getRule("html body p.small em"));
270: }
271:
272: public void testGetRuleString_NoContext() {
273: rule = ss.getRule("em");
274: assertEquals(0, rule.getAttributeCount());
275: }
276:
277: public void testGetRuleString_ContextClass() {
278: rule = ss.getRule("p.small em");
279: assertEquals(2, rule.getAttributeCount());
280: }
281:
282: public void testGetRuleString_ContextNoClass() {
283: ss.addRule("p em { text-decoration: underline }");
284: rule = ss.getRule("p em");
285: assertEquals(2, rule.getAttributeCount());
286:
287: assertEquals("underline", rule.getAttribute(
288: Attribute.TEXT_DECORATION).toString());
289: assertEquals("p em", rule
290: .getAttribute(AttributeSet.NameAttribute));
291: }
292:
293: public void testGetRuleString_Autochange_ContextNoClass01() {
294: ss.addRule("p em { text-decoration: underline }");
295: rule = ss.getRule("p em");
296: assertEquals(2, rule.getAttributeCount());
297:
298: ss.addRule("em { color: rgb(127, 0, 0) }");
299: assertEquals(4, rule.getAttributeCount());
300: assertEquals("rgb(127, 0, 0)", rule.getAttribute(
301: Attribute.COLOR).toString());
302: assertEquals("underline", rule.getAttribute(
303: Attribute.TEXT_DECORATION).toString());
304: assertEquals("p em", rule
305: .getAttribute(AttributeSet.NameAttribute));
306:
307: assertEquals(2, getNameAttributeCount(rule));
308: }
309:
310: public void testGetRuleString_Autochange_ContextNoClass02() {
311: ss.addRule("p em { text-decoration: underline }");
312: rule = ss.getRule("p em");
313: assertEquals(2, rule.getAttributeCount());
314:
315: ss.addRule("p em { color: rgb(50%, 100%, 50%) }");
316: assertEquals(3, rule.getAttributeCount());
317: assertEquals("rgb(50%, 100%, 50%)", rule.getAttribute(
318: Attribute.COLOR).toString());
319: assertEquals("underline", rule.getAttribute(
320: Attribute.TEXT_DECORATION).toString());
321: assertEquals("p em", rule
322: .getAttribute(AttributeSet.NameAttribute));
323: }
324:
325: public void testGetRuleString_AutoChange_NoContext() {
326: rule = ss.getRule("em");
327: assertEquals(0, rule.getAttributeCount());
328:
329: ss.addRule("em { color: rgb(127, 0, 0) }");
330: assertEquals(2, rule.getAttributeCount());
331: assertEquals("rgb(127, 0, 0)", rule.getAttribute(
332: Attribute.COLOR).toString());
333: assertEquals("em", rule
334: .getAttribute(AttributeSet.NameAttribute));
335: }
336:
337: public void testGetRuleTagElement_Listeners() {
338: Element em = doc.getCharacterElement(P_EM_START_OFFSET);
339: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
340:
341: rule = ss.getRule(Tag.EM, em);
342: assertEquals(0, rule.getAttributeCount());
343: final boolean[] happened = new boolean[1];
344: ChangeListener listener = new ChangeListener() {
345: public void stateChanged(final ChangeEvent e) {
346: happened[0] = true;
347: }
348: };
349: rule.addChangeListener(listener);
350:
351: ss.addRule("em { background-color: rgb(255, 255, 150) }");
352: assertEquals(2, rule.getAttributeCount());
353: assertFalse(happened[0]);
354:
355: Style emStyle = ss.getStyle("em");
356: ss.addCSSAttribute(emStyle, CSS.Attribute.COLOR, "black");
357: assertEquals(3, rule.getAttributeCount());
358: assertFalse(happened[0]);
359: }
360:
361: public void testGetRuleTagElement_RemoveStyle() {
362: Element em = doc.getCharacterElement(P_EM_START_OFFSET);
363: assertNotNull(em.getAttributes().getAttribute(Tag.EM));
364:
365: ss.addRule("em { color: rgb(255, 255, 150) }");
366:
367: rule = ss.getRule(Tag.EM, em);
368: assertEquals(2, rule.getAttributeCount());
369: assertEquals("rgb(255, 255, 150)", rule.getAttribute(
370: Attribute.COLOR).toString());
371:
372: ss.removeStyle("em");
373: assertEquals(0, rule.getAttributeCount());
374: assertNull(rule.getAttribute(Attribute.COLOR));
375: }
376:
377: public void testGetRuleString_Partial() {
378: ss.addRule("em { color: rgb(255, 255, 150) }");
379:
380: rule = ss.getRule("em");
381:
382: Style qualifiedRule = ss.getRule("html body p em");
383: assertNotSame(rule, qualifiedRule);
384: assertTrue(rule.isEqual(qualifiedRule));
385: assertEquals(2, qualifiedRule.getAttributeCount());
386: assertEquals("rgb(255, 255, 150)", qualifiedRule.getAttribute(
387: Attribute.COLOR).toString());
388: }
389:
390: public void testGetRuleString_NoRules() {
391: rule = ss.getRule(HTML.Tag.ADDRESS.toString());
392:
393: assertEquals(0, rule.getAttributeCount());
394: final Enumeration keys = rule.getAttributeNames();
395: assertFalse(keys.hasMoreElements());
396:
397: testExceptionalCase(new ExceptionalCase() {
398: public void exceptionalAction() throws Exception {
399: keys.nextElement();
400: }
401:
402: public Class expectedExceptionClass() {
403: return NoSuchElementException.class;
404: }
405: });
406: }
407:
408: public void testGetRuleString_NoRulesStyles() {
409: ss.addStyleSheet(new StyleSheet());
410: rule = ss.getRule(HTML.Tag.ADDRESS.toString());
411:
412: assertEquals(0, rule.getAttributeCount());
413: final Enumeration keys = rule.getAttributeNames();
414: assertFalse(keys.hasMoreElements());
415:
416: testExceptionalCase(new ExceptionalCase() {
417: public void exceptionalAction() throws Exception {
418: keys.nextElement();
419: }
420:
421: public Class expectedExceptionClass() {
422: return NoSuchElementException.class;
423: }
424: });
425: }
426:
427: private static int getAttributeCount(final AttributeSet rule,
428: final Object attrKey) {
429: int result = 0;
430: Enumeration keys = rule.getAttributeNames();
431: while (keys.hasMoreElements()) {
432: Object key = keys.nextElement();
433: if (attrKey.equals(key)) {
434: ++result;
435: }
436: }
437: return result;
438: }
439:
440: private static int getNameAttributeCount(final AttributeSet rule) {
441: return getAttributeCount(rule, AttributeSet.NameAttribute);
442: }
443:
444: private int getStyleNumber() {
445: int result = 0;
446: final Enumeration styleNames = ss.getStyleNames();
447: while (styleNames.hasMoreElements()) {
448: ++result;
449: styleNames.nextElement();
450: }
451: return result;
452: }
453: }
|