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:
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.text.AttributeSet;
026: import javax.swing.text.Element;
027: import javax.swing.text.Style;
028: import javax.swing.text.html.HTML.Tag;
029:
030: public class StyleSheet_ResolvedRulesClassTest extends
031: BasicSwingTestCase {
032: private HTMLDocument doc;
033: private StyleSheet ss;
034: private Style rule;
035: private Element p;
036: private HTMLEditorKit kit;
037:
038: protected void setUp() throws Exception {
039: super .setUp();
040: setIgnoreNotImplemented(true);
041: ss = new StyleSheet();
042: doc = new HTMLDocument(ss);
043: doc.setAsynchronousLoadPriority(-1);
044: kit = new HTMLEditorKit();
045: StringReader reader = new StringReader(
046: "<html><head>\n"
047: + "<style type=\"text/css\">"
048: + ".par { font-style: italic; color: blue }"
049: + "</style>"
050: + "</head>\n"
051: + "\n"
052: + "<body>\n"
053: + "<p class=\"par\">The first paragraph has <code>class</code>"
054: + " attribute with value"
055: + " <code>"par"</code>.</p>\n"
056: + "<p class=\"par\">Also does the second one.</p>\n"
057: + "<p>While the third paragraph has no attributes.</p>\n"
058: + "</body></html>");
059: kit.read(reader, doc, 0);
060: }
061:
062: public void testGetRule() {
063: p = doc.getParagraphElement(1);
064: assertEquals(1, p.getStartOffset());
065: assertEquals(59, p.getEndOffset());
066:
067: rule = ss.getRule(Tag.P, p);
068: assertEquals("html body p.par", rule.getName());
069: assertEquals(3, rule.getAttributeCount());
070: assertEquals(".par", rule
071: .getAttribute(AttributeSet.NameAttribute));
072: }
073:
074: public void testGetRuleSame() {
075: p = doc.getParagraphElement(59);
076: assertEquals(59, p.getStartOffset());
077: assertEquals(85, p.getEndOffset());
078: assertEquals("par", p.getAttributes().getAttribute(
079: HTML.Attribute.CLASS));
080:
081: rule = ss.getRule(Tag.P, p);
082: assertEquals("html body p.par", rule.getName());
083: assertEquals(3, rule.getAttributeCount());
084: assertEquals(".par", rule
085: .getAttribute(AttributeSet.NameAttribute));
086:
087: Element firstP = doc.getParagraphElement(1);
088: assertEquals(1, firstP.getStartOffset());
089: assertEquals(59, firstP.getEndOffset());
090: assertEquals("par", firstP.getAttributes().getAttribute(
091: HTML.Attribute.CLASS));
092:
093: Style firstPRule = ss.getRule(Tag.P, firstP);
094: assertEquals("html body p.par", rule.getName());
095: assertEquals(3, rule.getAttributeCount());
096: assertEquals(".par", rule
097: .getAttribute(AttributeSet.NameAttribute));
098:
099: assertSame(rule, firstPRule);
100: }
101:
102: public void testGetRuleAutoChange() {
103: p = doc.getParagraphElement(1);
104: assertEquals(1, p.getStartOffset());
105: assertEquals(59, p.getEndOffset());
106:
107: rule = ss.getRule(Tag.P, p);
108: assertEquals("html body p.par", rule.getName());
109: assertEquals(3, rule.getAttributeCount());
110:
111: ss.addRule("p { background-color: yellow }");
112:
113: assertEquals("html body p.par", rule.getName());
114: assertEquals(5, rule.getAttributeCount());
115: assertEquals(".par", rule
116: .getAttribute(AttributeSet.NameAttribute));
117: }
118:
119: public void testGetRuleOL_LI() throws Exception {
120: reInit();
121:
122: Element li = doc.getParagraphElement(1).getParentElement();
123: assertEquals("li", li.getName());
124: assertEquals(1, li.getStartOffset());
125: assertEquals(13, li.getEndOffset());
126:
127: rule = ss.getRule(Tag.LI, li);
128: assertEquals("html body ol li.first", rule.getName());
129: assertEquals(3, rule.getAttributeCount());
130: assertEquals(".first", rule
131: .getAttribute(AttributeSet.NameAttribute));
132: }
133:
134: public void testGetRuleUL_LI() throws Exception {
135: reInit();
136:
137: Element li = doc.getParagraphElement(22).getParentElement();
138: assertEquals("li", li.getName());
139: assertEquals(22, li.getStartOffset());
140: assertEquals(34, li.getEndOffset());
141:
142: rule = ss.getRule(Tag.LI, li);
143: assertEquals("html body ul li.first", rule.getName());
144: assertEquals(3, rule.getAttributeCount());
145: assertEquals(".first", rule
146: .getAttribute(AttributeSet.NameAttribute));
147: }
148:
149: private void reInit() throws Exception {
150: ss = new StyleSheet();
151: doc = new HTMLDocument(ss);
152: doc.setAsynchronousLoadPriority(-1);
153: StringReader reader = new StringReader("<html><head>\n"
154: + "<style type=\"text/css\">"
155: + ".first { font-style: italic; color: blue }"
156: + "</style>" + "</head>\n" + "\n" + "<body>\n"
157: + "<ol>\n"
158: + " <li class=\"first\">first class</li>\n"
159: + " <li>no class</li>\n" + "</ol>\n" + "\n"
160: + "<ul>\n"
161: + " <li class=\"first\">first class</li>\n"
162: + " <li>no class</li>\n" + "</ul>\n"
163: + "</body></html>");
164: kit.read(reader, doc, 0);
165: }
166: }
|