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 javax.swing.BasicSwingTestCase;
023: import javax.swing.text.AttributeSet;
024: import javax.swing.text.BoxView;
025: import javax.swing.text.MutableAttributeSet;
026: import javax.swing.text.SimpleAttributeSet;
027: import javax.swing.text.View;
028: import javax.swing.text.html.CSS.Attribute;
029: import javax.swing.text.html.StyleSheet.BoxPainter;
030:
031: public class StyleSheet_BoxPainterTest extends BasicSwingTestCase {
032: private HTMLDocument doc;
033: private StyleSheet ss;
034: private BoxPainter bp;
035: private MutableAttributeSet attrs;
036: private View view;
037:
038: protected void setUp() throws Exception {
039: super .setUp();
040: ss = new StyleSheet();
041: doc = new HTMLDocument(ss);
042: attrs = new SimpleAttributeSet();
043: bp = ss.getBoxPainter(attrs);
044:
045: view = new BlockView(doc.getDefaultRootElement().getElement(0),
046: View.Y_AXIS) {
047: public AttributeSet getAttributes() {
048: return attrs;
049: }
050: };
051: }
052:
053: public void testGetInsetNoAttributes() throws Exception {
054: assertEquals(0, bp.getInset(View.TOP, view), 0f);
055: assertEquals(0, bp.getInset(View.RIGHT, view), 0f);
056: assertEquals(0, bp.getInset(View.BOTTOM, view), 0f);
057: assertEquals(0, bp.getInset(View.LEFT, view), 0f);
058: }
059:
060: public void testGetInsetWithMargin() throws Exception {
061: ss.addCSSAttribute(attrs, Attribute.MARGIN,
062: "11pt 21pt 30pt 03pt");
063: assertEquals(4, attrs.getAttributeCount());
064: assertEquals(0, bp.getInset(View.TOP, view), 0f);
065: assertEquals(0, bp.getInset(View.RIGHT, view), 0f);
066: assertEquals(0, bp.getInset(View.BOTTOM, view), 0f);
067: assertEquals(0, bp.getInset(View.LEFT, view), 0f);
068: }
069:
070: public void testGetInsetWithPadding() throws Exception {
071: ss.addCSSAttribute(attrs, Attribute.PADDING,
072: "11pt 21pt 30pt 03pt");
073: assertEquals(4, attrs.getAttributeCount());
074: assertEquals(11, bp.getInset(View.TOP, view), 0f);
075: assertEquals(21, bp.getInset(View.RIGHT, view), 0f);
076: assertEquals(30, bp.getInset(View.BOTTOM, view), 0f);
077: assertEquals(03, bp.getInset(View.LEFT, view), 0f);
078: }
079:
080: public void testGetInsetWithPaddingPercent() throws Exception {
081: ss.addCSSAttribute(attrs, Attribute.PADDING, "10%");
082: assertEquals(4, attrs.getAttributeCount());
083: assertEquals(0, bp.getInset(View.TOP, view), 0f);
084: assertEquals(0, bp.getInset(View.RIGHT, view), 0f);
085: assertEquals(0, bp.getInset(View.BOTTOM, view), 0f);
086: assertEquals(0, bp.getInset(View.LEFT, view), 0f);
087: }
088:
089: public void testGetInsetWithPaddingPercentWithParent()
090: throws Exception {
091: BoxView parent = new BoxView(doc.getDefaultRootElement(),
092: View.Y_AXIS) {
093: public int getWidth() {
094: return 361;
095: }
096:
097: public int getHeight() {
098: return 257;
099: }
100: };
101: view.setParent(parent);
102: ss.addCSSAttribute(attrs, Attribute.PADDING, "10%");
103: assertEquals(4, attrs.getAttributeCount());
104: final float width = isHarmony() ? 361 * 0.1f : 0;
105: assertEquals(width, bp.getInset(View.TOP, view), 1e-5f);
106: assertEquals(width, bp.getInset(View.RIGHT, view), 1e-5f);
107: assertEquals(width, bp.getInset(View.BOTTOM, view), 1e-5f);
108: assertEquals(width, bp.getInset(View.LEFT, view), 1e-5f);
109: }
110:
111: public void testGetInsetWithPaddingEm() throws Exception {
112: ss.addCSSAttribute(attrs, Attribute.PADDING, "1em");
113: final int fontSize = isHarmony() ? ss.getFont(attrs).getSize()
114: : 0;
115: assertEquals(4, attrs.getAttributeCount());
116: assertEquals(fontSize, bp.getInset(View.TOP, view), 0f);
117: assertEquals(fontSize, bp.getInset(View.RIGHT, view), 0f);
118: assertEquals(fontSize, bp.getInset(View.BOTTOM, view), 0f);
119: assertEquals(fontSize, bp.getInset(View.LEFT, view), 0f);
120: }
121:
122: public void testGetInsetWithPaddingEx() throws Exception {
123: ss.addCSSAttribute(attrs, Attribute.PADDING, "1ex");
124: final int fontSize = isHarmony() ? ss.getFont(attrs).getSize() / 2
125: : 0;
126: assertEquals(4, attrs.getAttributeCount());
127: assertEquals(fontSize, bp.getInset(View.TOP, view), 0f);
128: assertEquals(fontSize, bp.getInset(View.RIGHT, view), 0f);
129: assertEquals(fontSize, bp.getInset(View.BOTTOM, view), 0f);
130: assertEquals(fontSize, bp.getInset(View.LEFT, view), 0f);
131: }
132:
133: public void testGetInsetDifferentViews() throws Exception {
134: ss.addCSSAttribute(attrs, Attribute.MARGIN,
135: "11pt 21pt 30pt 03pt");
136: assertEquals(4, view.getAttributes().getAttributeCount());
137:
138: final MutableAttributeSet va = new SimpleAttributeSet();
139: final View v = new InlineView(doc.getDefaultRootElement()) {
140: public AttributeSet getAttributes() {
141: return va;
142: }
143: };
144: ss.addCSSAttribute(va, Attribute.MARGIN, "24pt 33pt 07pt 15pt");
145: assertEquals(4, va.getAttributeCount());
146:
147: assertNotSame(attrs, va);
148:
149: bp = ss.getBoxPainter(view.getAttributes());
150: if (isHarmony()) {
151: bp.setView(view);
152: }
153:
154: // view argument has no effect
155: assertEquals(11, bp.getInset(View.TOP, v), 0f);
156: assertEquals(21, bp.getInset(View.RIGHT, v), 0f);
157: assertEquals(30, bp.getInset(View.BOTTOM, v), 0f);
158: assertEquals(3, bp.getInset(View.LEFT, v), 0f);
159: }
160:
161: public void testGetInsetInvalid01() throws Exception {
162: testExceptionalCase(new IllegalArgumentCase() {
163: public void exceptionalAction() throws Exception {
164: bp.getInset(0, view);
165: }
166: });
167: }
168:
169: public void testGetInsetInvalid02() throws Exception {
170: testExceptionalCase(new IllegalArgumentCase() {
171: public void exceptionalAction() throws Exception {
172: bp.getInset(5, view);
173: }
174: });
175: }
176: }
|