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;
021:
022: import java.awt.Rectangle;
023: import javax.swing.BasicSwingTestCase;
024: import javax.swing.SizeRequirements;
025: import javax.swing.text.ViewTestHelpers.ChildView;
026: import javax.swing.text.ViewTestHelpers.ChildrenFactory;
027:
028: public class BoxViewRTest extends BasicSwingTestCase {
029: private Document doc;
030:
031: private Element root;
032:
033: private BoxView view;
034:
035: private static final int width = 150;
036:
037: private static final int height = 325;
038:
039: private static class Child extends ChildView {
040: private int width = -1;
041:
042: private int height = -1;
043:
044: public Child(final Element element, final int id) {
045: super (element, id);
046: }
047:
048: @Override
049: public void setSize(float width, float height) {
050: this .width = (int) width;
051: this .height = (int) height;
052: super .setSize(width, height);
053: }
054:
055: public int getWidth() {
056: return width;
057: }
058:
059: public int getHeight() {
060: return height;
061: }
062: }
063:
064: @Override
065: protected void setUp() throws Exception {
066: super .setUp();
067: doc = new DefaultStyledDocument();
068: root = doc.getDefaultRootElement();
069: view = new BoxView(root, View.Y_AXIS);
070: }
071:
072: /**
073: * Checks that children have their sizes set after setSize.
074: */
075: public void testSetSizeChildren() throws Exception {
076: View[] children = new View[] {
077: new Child(root.getElement(0), 0),
078: new Child(root.getElement(1), 1), };
079: view.replace(0, view.getViewCount(), children);
080: view.setSize(width, height);
081: Child child = (Child) view.getView(0);
082: assertEquals(view.getSpan(View.X_AXIS, 0), child.getWidth());
083: assertEquals(view.getSpan(View.Y_AXIS, 0), child.getHeight());
084: child = (Child) view.getView(1);
085: assertEquals(view.getSpan(View.X_AXIS, 1), child.getWidth());
086: assertEquals(view.getSpan(View.Y_AXIS, 1), child.getHeight());
087: }
088:
089: /**
090: * Checks that children have their sizes set after layout.
091: */
092: public void testLayoutChildren() throws Exception {
093: View[] children = new View[] {
094: new Child(root.getElement(0), 0),
095: new Child(root.getElement(1), 1), };
096: view.replace(0, view.getViewCount(), children);
097: view.layout(width, height);
098: Child child = (Child) view.getView(0);
099: final int childWidth = child.getWidth();
100: final int childHeight = child.getHeight();
101: assertEquals(view.getSpan(View.X_AXIS, 0), childWidth);
102: assertEquals(view.getSpan(View.Y_AXIS, 0), childHeight);
103: assertTrue(childWidth != -1);
104: assertTrue(childHeight != -1);
105: child = (Child) view.getView(1);
106: assertEquals(view.getSpan(View.X_AXIS, 1), child.getWidth());
107: assertEquals(view.getSpan(View.Y_AXIS, 1), child.getHeight());
108: }
109:
110: /**
111: * Tests that layout will succeed even if a child changes its preferences
112: * while layout is being performed.
113: * <p>There's no limit to layout tries in our implementation. Hence layout may
114: * cause stack overflow if a child always changes its preferences.
115: */
116: public void testLayout() throws Exception {
117: View[] children = new View[] {
118: new Child(root.getElement(0), 0),
119: new Child(root.getElement(1), 1) {
120: private int iteration = 0;
121:
122: private int[] heights = { 20, 40, 15, 11, 23 };
123:
124: @Override
125: public float getPreferredSpan(int axis) {
126: if (axis == X_AXIS) {
127: return super .getPreferredSpan(axis);
128: }
129: return heights[iteration < heights.length ? iteration
130: : heights.length];
131: }
132:
133: @Override
134: public void setSize(float width, float height) {
135: super .setSize(width, height);
136: if (++iteration < heights.length) {
137: preferenceChanged(view, false, true);
138: }
139: }
140: } };
141: view.replace(0, view.getViewCount(), children);
142: view.layout(300, 500);
143: Rectangle shape = new Rectangle(300, 500);
144: Rectangle[] allocs = { new Rectangle(0, 0, 25, 32),
145: new Rectangle(56, 32, 75, isHarmony() ? 23 : 40) };
146: for (int i = 0; i < view.getViewCount(); i++) {
147: assertEquals("@ " + i, allocs[i], view.getChildAllocation(
148: i, shape));
149: }
150: assertEquals(75, (int) view.getPreferredSpan(View.X_AXIS));
151: assertEquals(isHarmony() ? 55 : 47, (int) view
152: .getPreferredSpan(View.Y_AXIS));
153: }
154:
155: /**
156: * Tests that layout is invalidated upon change of axis.
157: */
158: public void testSetAxisLayout() {
159: assertEquals(View.Y_AXIS, view.getAxis());
160: view.layout(width, height);
161: assertTrue(view.isAllocationValid());
162: assertTrue(view.isLayoutValid(View.X_AXIS));
163: assertTrue(view.isLayoutValid(View.Y_AXIS));
164: view.setAxis(View.X_AXIS);
165: assertFalse(view.isLayoutValid(View.X_AXIS));
166: assertFalse(view.isLayoutValid(View.Y_AXIS));
167: assertFalse(view.isAllocationValid());
168: }
169:
170: public void testBaselineLayout() throws BadLocationException {
171: doc.insertString(0, "uno\ndos\t2\ntres\ncuatro", null);
172: final Marker major = new Marker(true);
173: final Marker minor = new Marker(true);
174: final Marker baseline = new Marker(true);
175: view = new BoxView(root, View.Y_AXIS) {
176: @Override
177: protected SizeRequirements baselineRequirements(int axis,
178: SizeRequirements r) {
179: baseline.setOccurred();
180: return super .baselineRequirements(axis, r);
181: }
182:
183: @Override
184: protected SizeRequirements calculateMajorAxisRequirements(
185: int axis, SizeRequirements r) {
186: major.setOccurred();
187: return super .calculateMajorAxisRequirements(axis, r);
188: }
189:
190: @Override
191: protected SizeRequirements calculateMinorAxisRequirements(
192: int axis, SizeRequirements r) {
193: minor.setOccurred();
194: return super .calculateMinorAxisRequirements(axis, r);
195: }
196: };
197: view.loadChildren(new ChildrenFactory());
198: final int[] offsets = new int[view.getViewCount()];
199: final int[] spans = new int[view.getViewCount()];
200: view.baselineLayout(width, View.X_AXIS, offsets, spans);
201: assertFalse(baseline.isOccurred());
202: assertFalse(major.isOccurred());
203: assertTrue(minor.isOccurred());
204: view.baselineLayout(height, View.Y_AXIS, offsets, spans);
205: assertFalse(baseline.isOccurred());
206: assertTrue(major.isOccurred());
207: assertFalse(minor.isOccurred());
208: view.baselineLayout(width, View.X_AXIS, offsets, spans);
209: assertFalse(baseline.isOccurred());
210: assertFalse(major.isOccurred());
211: assertFalse(minor.isOccurred());
212: view.baselineLayout(height, View.Y_AXIS, offsets, spans);
213: assertFalse(baseline.isOccurred());
214: assertFalse(major.isOccurred());
215: assertFalse(minor.isOccurred());
216: }
217:
218: /**
219: * Test <code>getResizeWeight</code> where minor axis requirements state
220: * they are not resizeble.
221: */
222: public void testGetResizeWeight() {
223: view = new BoxView(root, View.Y_AXIS) {
224: @Override
225: protected SizeRequirements calculateMinorAxisRequirements(
226: final int axis, final SizeRequirements r) {
227: SizeRequirements result = r != null ? r
228: : new SizeRequirements();
229: result.minimum = result.preferred = result.maximum = 121;
230: return result;
231: }
232: };
233: assertEquals(0, view.getResizeWeight(View.X_AXIS)); // minor
234: }
235: }
|