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.PlainView;
028: import javax.swing.text.SimpleAttributeSet;
029: import javax.swing.text.StyleConstants;
030: import javax.swing.text.View;
031: import javax.swing.text.ViewFactory;
032:
033: public class ParagraphViewTest extends BasicSwingTestCase {
034: public static class ParagraphViewImpl extends ParagraphView {
035: private final ViewFactory factory;
036:
037: public ParagraphViewImpl(final Element element,
038: final ViewFactory factory) {
039: super (element);
040: this .factory = factory;
041: loadChildren(getViewFactory());
042: }
043:
044: public ViewFactory getViewFactory() {
045: return factory;
046: }
047:
048: public View getLayoutPool() {
049: return layoutPool;
050: }
051: }
052:
053: private static class ParagraphViewNotVisible extends
054: ParagraphViewImpl {
055: public ParagraphViewNotVisible(final Element element,
056: final ViewFactory factory) {
057: super (element, factory);
058: }
059:
060: public boolean isVisible() {
061: return false;
062: }
063: }
064:
065: private HTMLEditorKit kit;
066: private HTMLDocument doc;
067: private Element block;
068: private ParagraphView view;
069: private ViewFactory factory;
070: private AttributeSet attrs;
071:
072: protected void setUp() throws Exception {
073: super .setUp();
074: setIgnoreNotImplemented(true);
075: kit = new HTMLEditorKit();
076: doc = (HTMLDocument) kit.createDefaultDocument();
077: StringReader reader = new StringReader("<html><head></head>"
078: + "<body>" + "<p>Normal <em>em inside</em> paragraph."
079: + "</body></html>");
080: kit.read(reader, doc, 0);
081:
082: block = doc.getParagraphElement(10);
083: assertEquals(HTML.Tag.P.toString(), block.getName());
084:
085: factory = new BlockViewTest.InlineViewFactory();
086: view = new ParagraphViewImpl(block, factory);
087: attrs = view.getAttributes();
088: }
089:
090: public void testParagraphView() {
091: assertEquals(View.Y_AXIS, view.getAxis());
092: assertNotSame(block.getAttributes(), view.getAttributes());
093: }
094:
095: public void testGetAttributes() {
096: assertSame(attrs, view.getAttributes());
097: }
098:
099: public void testGetAttributesStyleSheet() {
100: final Marker ssMarker = new Marker(true);
101: view = new ParagraphView(block) {
102: protected StyleSheet getStyleSheet() {
103: ssMarker.setOccurred();
104: return super .getStyleSheet();
105: };
106: };
107: assertTrue(ssMarker.isOccurred());
108: view.getAttributes();
109: assertFalse(ssMarker.isOccurred());
110: }
111:
112: public void testGetMinimumSpanNotVisible() {
113: view = new ParagraphViewNotVisible(block, factory);
114: assertFalse(view.isVisible());
115: assertEquals(0, view.getMinimumSpan(View.X_AXIS), 0f);
116: assertEquals(0, view.getMinimumSpan(View.Y_AXIS), 0f);
117: }
118:
119: public void testGetPreferredSpanNotVisible() {
120: view = new ParagraphViewNotVisible(block, factory);
121: assertFalse(view.isVisible());
122: assertEquals(0, view.getPreferredSpan(View.X_AXIS), 0f);
123: assertEquals(0, view.getPreferredSpan(View.Y_AXIS), 0f);
124: }
125:
126: public void testGetMaximumSpanNotVisible() {
127: view = new ParagraphViewNotVisible(block, factory);
128: assertFalse(view.isVisible());
129: assertEquals(0, view.getMaximumSpan(View.X_AXIS), 0f);
130: assertEquals(0, view.getMaximumSpan(View.Y_AXIS), 0f);
131: }
132:
133: public void testIsVisible() {
134: assertEquals(0, view.getViewCount());
135: assertTrue(view.isVisible());
136: }
137:
138: public void testIsVisibleNotLoaded() {
139: view = new ParagraphView(block);
140: testExceptionalCase(new NullPointerCase() {
141: public void exceptionalAction() throws Exception {
142: view.isVisible(); // Since layoutPool is null
143: }
144: });
145: }
146:
147: public void testIsVisibleNotVisible() {
148: final int elemCount = block.getElementCount();
149: final int[] createCount = new int[] { 0 };
150: final int[] isVisibleCount = new int[] { 0 };
151: final View[] viewsRequested = new View[elemCount];
152: final boolean[] results = new boolean[elemCount];
153:
154: view = new ParagraphViewImpl(block, new ViewFactory() {
155: public View create(final Element element) {
156: ++createCount[0];
157: return new InlineView(element) {
158: public boolean isVisible() {
159: viewsRequested[isVisibleCount[0]] = this ;
160: results[isVisibleCount[0]] = getEndOffset()
161: - getStartOffset() == 1;
162: ++isVisibleCount[0];
163: return results[isVisibleCount[0] - 1];
164: }
165: };
166: }
167: });
168: assertEquals(elemCount, createCount[0]);
169: assertEquals(0, view.getViewCount());
170:
171: assertFalse(view.isVisible());
172:
173: assertEquals(elemCount - 1, isVisibleCount[0]);
174: final View layoutPool = ((ParagraphViewImpl) view)
175: .getLayoutPool();
176: for (int i = 0; i < isVisibleCount[0]; i++) {
177: assertSame("@ " + i, layoutPool.getView(i),
178: viewsRequested[i]);
179: assertFalse("@ " + i, results[i]);
180: }
181: }
182:
183: public void testIsVisibleOneChild() throws Exception {
184: doc = (HTMLDocument) kit.createDefaultDocument();
185: StringReader reader = new StringReader(
186: "<html><body>00001111</body></html>");
187: kit.read(reader, doc, 0);
188: final SimpleAttributeSet attr = new SimpleAttributeSet();
189: attr.addAttribute(StyleConstants.NameAttribute,
190: HTML.Tag.CONTENT);
191: doc.insertString(5, "\n", attr);
192: block = doc.getParagraphElement(3);
193:
194: view = new ParagraphViewImpl(block, factory);
195: assertTrue(view.isVisible());
196: }
197:
198: public void testIsVisibleCRChild() throws Exception {
199: doc = (HTMLDocument) kit.createDefaultDocument();
200: StringReader reader = new StringReader(
201: "<html><body>0000</body></html>");
202: kit.read(reader, doc, 0);
203: final boolean[] ordinaryViewVisible = new boolean[] { true };
204: block = doc.getParagraphElement(1);
205:
206: view = new ParagraphViewImpl(block, new ViewFactory() {
207: public View create(Element element) {
208: return new InlineView(element) {
209: public boolean isVisible() {
210: return getElement().getAttributes()
211: .getAttribute("CR") != null
212: || ordinaryViewVisible[0];
213: }
214: };
215: }
216: });
217: final View layoutPool = ((ParagraphViewImpl) view)
218: .getLayoutPool();
219: assertEquals(2, layoutPool.getViewCount());
220: assertTrue(layoutPool.getView(0).isVisible());
221: assertTrue(layoutPool.getView(1).isVisible());
222:
223: assertTrue(view.isVisible());
224:
225: ordinaryViewVisible[0] = false;
226: assertFalse(layoutPool.getView(0).isVisible());
227: assertTrue(layoutPool.getView(1).isVisible());
228: assertNotNull(layoutPool.getView(1).getElement()
229: .getAttributes().getAttribute("CR"));
230:
231: assertFalse(view.isVisible());
232: }
233:
234: public void testIsVisibleNewLineChild() throws Exception {
235: doc = (HTMLDocument) kit.createDefaultDocument();
236: StringReader reader = new StringReader(
237: "<html><body>0000</body></html>");
238: kit.read(reader, doc, 0);
239: final SimpleAttributeSet attr = new SimpleAttributeSet();
240: attr.addAttribute(StyleConstants.NameAttribute,
241: HTML.Tag.CONTENT);
242: doc.insertString(2, "\n\n", attr);
243: block = doc.getParagraphElement(3);
244: assertEquals(3, block.getStartOffset());
245: assertEquals(4, block.getEndOffset());
246:
247: view = new ParagraphViewImpl(block, factory);
248: final View layoutPool = ((ParagraphViewImpl) view)
249: .getLayoutPool();
250: assertEquals(1, layoutPool.getViewCount());
251:
252: final View newLineView = layoutPool.getView(0);
253: assertTrue(newLineView.isVisible());
254: assertNull(newLineView.getElement().getAttributes()
255: .getAttribute("CR"));
256:
257: assertTrue(view.isVisible());
258: }
259:
260: public void testSetParent() {
261: final Marker propertiesMarker = new Marker(true);
262: view = new ParagraphView(block) {
263: protected void setPropertiesFromAttributes() {
264: propertiesMarker.setOccurred();
265: super .setPropertiesFromAttributes();
266: }
267: };
268: assertTrue(propertiesMarker.isOccurred());
269: view.setParent(null);
270: assertFalse(propertiesMarker.isOccurred());
271:
272: view.setParent(new PlainView(doc.getDefaultRootElement()));
273: assertTrue(propertiesMarker.isOccurred());
274:
275: view.setParent(null);
276: assertFalse(propertiesMarker.isOccurred());
277: }
278:
279: public void testGetStyleSheet() {
280: assertSame(doc.getStyleSheet(), view.getStyleSheet());
281: }
282:
283: public void testSetPropertiesFromAttributes() {
284: final Marker spaceAbove = new Marker();
285: final Marker spaceBelow = new Marker();
286: final Marker leftIndent = new Marker();
287: final Marker rightIndent = new Marker();
288: final Marker flIndent = new Marker();
289: final Marker alignment = new Marker();
290: final Marker lineSpacing = new Marker();
291: final Marker paddingTop = new Marker();
292: final Marker paddingRight = new Marker();
293: final Marker paddingBottom = new Marker();
294: final Marker paddingLeft = new Marker();
295:
296: view = new ParagraphViewImpl(block, factory) {
297: private AttributeSet attributes;
298:
299: public AttributeSet getAttributes() {
300: if (attributes == null) {
301: attributes = new SimpleAttributeSet(super
302: .getAttributes()) {
303: public Object getAttribute(final Object name) {
304: if (name == StyleConstants.SpaceAbove) {
305: spaceAbove.setOccurred();
306: } else if (name == StyleConstants.SpaceBelow) {
307: spaceBelow.setOccurred();
308: } else if (name == StyleConstants.LeftIndent) {
309: leftIndent.setOccurred();
310: } else if (name == StyleConstants.RightIndent) {
311: rightIndent.setOccurred();
312: } else if (name == StyleConstants.FirstLineIndent) {
313: flIndent.setOccurred();
314: } else if (name == StyleConstants.Alignment) {
315: alignment.setOccurred();
316: } else if (name == StyleConstants.LineSpacing) {
317: lineSpacing.setOccurred();
318: } else if (name == CSS.Attribute.PADDING_TOP) {
319: paddingTop.setOccurred();
320: } else if (name == CSS.Attribute.PADDING_RIGHT) {
321: paddingRight.setOccurred();
322: } else if (name == CSS.Attribute.PADDING_BOTTOM) {
323: paddingBottom.setOccurred();
324: } else if (name == CSS.Attribute.PADDING_LEFT) {
325: paddingLeft.setOccurred();
326: }
327: return super .getAttribute(name);
328: }
329: };
330: }
331: return attributes;
332: }
333: };
334:
335: assertTrue(spaceAbove.isOccurred());
336: assertTrue(spaceBelow.isOccurred());
337: assertTrue(leftIndent.isOccurred());
338: assertTrue(rightIndent.isOccurred());
339: assertTrue(flIndent.isOccurred());
340: assertTrue(alignment.isOccurred());
341: assertTrue(lineSpacing.isOccurred());
342: assertTrue(paddingTop.isOccurred());
343: assertTrue(paddingRight.isOccurred());
344: assertTrue(paddingBottom.isOccurred());
345: assertTrue(paddingLeft.isOccurred());
346: }
347:
348: public void testGetBoxPainter() {
349: final Marker bpMarker = new Marker();
350: StyleSheet ss = new StyleSheet() {
351: public BoxPainter getBoxPainter(AttributeSet attr) {
352: bpMarker.setOccurred();
353: return super .getBoxPainter(attr);
354: }
355: };
356: doc = new HTMLDocument(ss);
357: block = doc.getParagraphElement(doc.getLength());
358: view = new ParagraphView(block);
359: assertTrue(bpMarker.isOccurred());
360: }
361:
362: // public void testPaint() {
363: //
364: // }
365: }
|