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.FontMetrics;
023: import java.io.StringReader;
024:
025: import javax.swing.BasicSwingTestCase;
026: import javax.swing.SizeRequirements;
027: import javax.swing.text.Element;
028: import javax.swing.text.LabelView;
029: import javax.swing.text.PlainView;
030: import javax.swing.text.View;
031: import javax.swing.text.ViewFactory;
032: import javax.swing.text.html.ParagraphViewTest.ParagraphViewImpl;
033:
034: /**
035: * Tests <code>calculateMinorAxisRequirements</code> method.
036: */
037: public class ParagraphView_RequirementsTest extends BasicSwingTestCase {
038: private static final int CHAR_WIDTH = InlineViewTest.FixedPainter.CHAR_WIDTH;
039:
040: private HTMLEditorKit kit;
041: private HTMLDocument doc;
042: private ParagraphView view;
043: private ViewFactory factory;
044:
045: private FontMetrics metrics;
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049: setIgnoreNotImplemented(true);
050: kit = new HTMLEditorKit();
051: doc = (HTMLDocument) kit.createDefaultDocument();
052: StringReader reader = new StringReader(
053: "<html><head></head><body>"
054: + "<p><small>one long</small>Word"
055: + "<p>very LongWord" + "</body></html>");
056: kit.read(reader, doc, 0);
057:
058: metrics = getFontMetrics(null, CHAR_WIDTH);
059: factory = new ViewFactory() {
060: public View create(final Element element) {
061: InlineView result = new InlineView(element) {
062: protected FontMetrics getFontMetrics() {
063: return metrics;
064: }
065: };
066: result
067: .setGlyphPainter(BlockViewTest.InlineViewFactory.painter);
068: return result;
069: }
070: };
071: }
072:
073: public void testCalculateMinorAxisRequirements01() {
074: view = new ParagraphViewImpl(doc.getParagraphElement(10),
075: factory);
076: SizeRequirements sr = view.calculateMinorAxisRequirements(
077: View.X_AXIS, null);
078: assertEquals(4 * CHAR_WIDTH, sr.minimum);
079: assertEquals((isHarmony() ? 13 : 12) * CHAR_WIDTH, sr.preferred);
080: assertEquals(Integer.MAX_VALUE, sr.maximum);
081: }
082:
083: public void testCalculateMinorAxisRequirements02() throws Exception {
084: view = new ParagraphViewImpl(doc.getParagraphElement(20),
085: factory);
086: SizeRequirements sr = view.calculateMinorAxisRequirements(
087: View.X_AXIS, null);
088: assertEquals(8 * CHAR_WIDTH, sr.minimum);
089: assertEquals((isHarmony() ? 14 : 13) * CHAR_WIDTH, sr.preferred);
090: assertEquals(Integer.MAX_VALUE, sr.maximum);
091: }
092:
093: public void testCalculateMinorAxisRequirements03() throws Exception {
094: factory = new ViewFactory() {
095: public View create(Element element) {
096: LabelView result = new LabelView(element);
097: result
098: .setGlyphPainter(BlockViewTest.InlineViewFactory.painter);
099: return result;
100: }
101: };
102: view = new ParagraphViewImpl(doc.getParagraphElement(10),
103: factory);
104: SizeRequirements sr = view.calculateMinorAxisRequirements(
105: View.X_AXIS, null);
106:
107: final View layoutPool = ((ParagraphViewImpl) view)
108: .getLayoutPool();
109: int min = 0;
110: int pref = 0;
111: for (int i = 0; i < layoutPool.getViewCount(); i++) {
112: View child = layoutPool.getView(i);
113: int ps = (int) child.getPreferredSpan(View.X_AXIS);
114: min = Math.max(min, ps);
115: pref += ps;
116: }
117: assertEquals(min, 8 * CHAR_WIDTH);
118: assertEquals(pref, (isHarmony() ? 13 : 12) * CHAR_WIDTH);
119:
120: assertEquals(8 * CHAR_WIDTH, sr.minimum);
121: assertEquals((isHarmony() ? 13 : 12) * CHAR_WIDTH, sr.preferred);
122: assertEquals(Integer.MAX_VALUE, sr.maximum);
123: }
124:
125: public void testCalculateMinorAxisRequirements04() throws Exception {
126: factory = new ViewFactory() {
127: public View create(Element element) {
128: PlainView result = new PlainView(element) {
129: public float getPreferredSpan(int axis) {
130: if (axis == X_AXIS) {
131: return CHAR_WIDTH
132: * (getEndOffset() - getStartOffset());
133: }
134: return super .getPreferredSpan(axis);
135: }
136: };
137: return result;
138: }
139: };
140: view = new ParagraphViewImpl(doc.getParagraphElement(10),
141: factory);
142: SizeRequirements sr = view.calculateMinorAxisRequirements(
143: View.X_AXIS, null);
144: assertEquals(8 * CHAR_WIDTH, sr.minimum);
145: assertEquals(13 * CHAR_WIDTH, sr.preferred);
146: assertEquals(Integer.MAX_VALUE, sr.maximum);
147: }
148: }
|