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.View;
029: import javax.swing.text.html.HTML.Tag;
030:
031: public class StyleSheet_ViewAttributesTest extends BasicSwingTestCase {
032: private HTMLDocument doc;
033: private StyleSheet ss;
034: private View view;
035:
036: protected void setUp() throws Exception {
037: super .setUp();
038: setIgnoreNotImplemented(true);
039: ss = new StyleSheet();
040: doc = new HTMLDocument(ss);
041: }
042:
043: public void testViewGetAttributes() throws Exception {
044: final Marker marker = new Marker();
045: ss = new StyleSheet() {
046: public AttributeSet getViewAttributes(final View v) {
047: marker.setOccurred();
048: marker.setAuxiliary(v);
049: return super .getViewAttributes(v);
050: };
051: };
052: doc = new HTMLDocument(ss);
053: view = new InlineView(doc.getCharacterElement(0));
054: assertTrue(marker.isOccurred());
055: assertSame(view, marker.getAuxiliary());
056: }
057:
058: public void testGetViewAttributesGetRuleInline() throws Exception {
059: final Marker tag = new Marker();
060: final Marker sel = new Marker();
061: ss = new StyleSheet() {
062: public Style getRule(Tag t, Element elem) {
063: tag.setOccurred();
064: return super .getRule(t, elem);
065: }
066:
067: public Style getRule(String selector) {
068: sel.setOccurred();
069: return super .getRule(selector);
070: }
071: };
072: view = new InlineView(doc.getCharacterElement(0));
073: ss.getViewAttributes(view);
074: assertFalse(tag.isOccurred());
075: assertFalse(sel.isOccurred());
076: }
077:
078: public void testGetViewAttributesGetRuleBlock() throws Exception {
079: final Marker tag = new Marker();
080: final Marker sel = new Marker();
081: ss = new StyleSheet() {
082: public Style getRule(Tag t, Element elem) {
083: tag.setOccurred();
084: assertSame(HTML.Tag.P, t);
085: assertSame(view.getElement(), elem);
086: return super .getRule(t, elem);
087: }
088:
089: public Style getRule(String selector) {
090: sel.setOccurred();
091: return super .getRule(selector);
092: }
093: };
094: view = new BlockView(doc.getParagraphElement(0), View.Y_AXIS);
095: ss.getViewAttributes(view);
096: assertTrue(tag.isOccurred());
097: assertFalse(sel.isOccurred());
098: }
099:
100: public void testGetViewAttributesGetRuleInlineEm() throws Exception {
101: final Marker tag = new Marker();
102: final Marker sel = new Marker();
103: ss = new StyleSheet() {
104: public Style getRule(Tag t, Element elem) {
105: if (view != null) {
106: tag.setOccurred();
107: assertSame(HTML.Tag.EM, t);
108: assertSame(view.getElement(), elem);
109: }
110: return super .getRule(t, elem);
111: }
112:
113: public Style getRule(String selector) {
114: sel.setOccurred();
115: return super .getRule(selector);
116: }
117: };
118: doc = new HTMLDocument(ss);
119: HTMLEditorKit kit = new HTMLEditorKit();
120: kit.read(new StringReader("<em>emphasized</em>"), doc, 0);
121: Element inline = doc.getCharacterElement(1);
122: assertNotNull(inline.getAttributes().getAttribute(
123: AttributeSet.NameAttribute));
124: view = new InlineView(inline);
125: ss.getViewAttributes(view);
126: assertTrue(tag.isOccurred());
127: assertFalse(sel.isOccurred());
128: }
129:
130: public void testGetViewAttributesTranslate() throws Exception {
131: final Marker marker = new Marker();
132: final Element block = doc.getParagraphElement(0);
133: ss = new StyleSheet() {
134: public AttributeSet translateHTMLToCSS(
135: final AttributeSet attrs) {
136: marker.setOccurred();
137: assertSame(block, attrs);
138: return super .translateHTMLToCSS(attrs);
139: }
140: };
141: view = new BlockView(block, View.Y_AXIS);
142: assertFalse(marker.isOccurred());
143: ss.getViewAttributes(view);
144: assertTrue(marker.isOccurred());
145: }
146:
147: public void testGetViewAttributesResolverNull() throws Exception {
148: final Element block = doc.getParagraphElement(0);
149: view = new BlockView(block, View.Y_AXIS);
150: AttributeSet va = ss.getViewAttributes(view);
151: assertNull(view.getParent());
152: assertNull(va.getResolveParent());
153: }
154:
155: public void testGetViewAttributesResolver() throws Exception {
156: final Element block = doc.getParagraphElement(0);
157: final Element inline = block.getElement(0);
158:
159: view = new InlineView(inline);
160: View bv = new BlockView(block, View.Y_AXIS);
161: view.setParent(bv);
162:
163: AttributeSet va = ss.getViewAttributes(view);
164: assertSame(bv, view.getParent());
165: assertSame(bv.getAttributes(), va.getResolveParent());
166: }
167:
168: public void testGetViewAttributesResolverChange() throws Exception {
169: final Element block = doc.getParagraphElement(0);
170: final Element inline = block.getElement(0);
171:
172: view = new InlineView(inline);
173: View bv = new BlockView(block, View.Y_AXIS);
174:
175: AttributeSet va = ss.getViewAttributes(view);
176: assertNull(view.getParent());
177: assertNull(va.getResolveParent());
178:
179: view.setParent(bv);
180: assertSame(bv.getAttributes(), va.getResolveParent());
181: }
182: }
|