001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.layout;
032:
033: import java.awt.Dimension;
034:
035: import javax.swing.JPanel;
036:
037: import junit.framework.TestCase;
038:
039: /**
040: * Tests the FormLayout's layout algorithm.
041: *
042: * @author Karsten Lentzsch
043: * @version $Revision: 1.9 $
044: */
045:
046: public final class FormLayoutTest extends TestCase {
047:
048: private CellConstraints cc = new CellConstraints();
049:
050: /**
051: * Checks basic layout functions.
052: */
053: public void testBasic() {
054: FormLayout layout = new FormLayout("1px, 2px, 3px, 5px, 7px",
055: "1px, 2px, 3px");
056:
057: JPanel panel = new JPanel(layout);
058: panel.doLayout();
059: FormLayout.LayoutInfo info = layout.getLayoutInfo(panel);
060: assertEquals("Columns", 6, info.columnOrigins.length);
061: assertEquals("Rows", 4, info.rowOrigins.length);
062: assertEquals("Column 0", 0, info.columnOrigins[0]);
063: assertEquals("Column 1", 1, info.columnOrigins[1]);
064: assertEquals("Column 2", 3, info.columnOrigins[2]);
065: assertEquals("Column 3", 6, info.columnOrigins[3]);
066: assertEquals("Column 4", 11, info.columnOrigins[4]);
067: assertEquals("Column 5", 18, info.columnOrigins[5]);
068: }
069:
070: /**
071: * Checks whether components are aligned according to the column specs.
072: */
073: public void testHorizontalAlignments() {
074: TestComponent left = new TestComponent(2, 7, 4, 9);
075: TestComponent center = new TestComponent(2, 7, 4, 9);
076: TestComponent right = new TestComponent(2, 7, 4, 9);
077: TestComponent fill = new TestComponent(2, 7, 4, 9);
078: TestComponent def = new TestComponent(2, 7, 4, 9);
079: FormLayout layout = new FormLayout(
080: "left:10px, center:10px, right:10px, fill:10px, 10px",
081: "pref");
082:
083: JPanel panel = new JPanel(layout);
084: panel.add(left, cc.xy(1, 1));
085: panel.add(center, cc.xy(2, 1));
086: panel.add(right, cc.xy(3, 1));
087: panel.add(fill, cc.xy(4, 1));
088: panel.add(def, cc.xy(5, 1));
089:
090: panel.doLayout();
091:
092: assertEquals("Left.x", 0, left.getX());
093: assertEquals("Left.width", 4, left.getWidth());
094: assertEquals("Center.x", 13, center.getX());
095: assertEquals("Center.width", 4, center.getWidth());
096: assertEquals("Right.x", 26, right.getX());
097: assertEquals("Right.width", 4, right.getWidth());
098: assertEquals("Fill.x", 30, fill.getX());
099: assertEquals("Fill.width", 10, fill.getWidth());
100: assertEquals("Default.x", 40, def.getX());
101: assertEquals("Default.width", 10, def.getWidth());
102: }
103:
104: /**
105: * Checks whether components are aligned according to the row specs.
106: */
107: public void testVerticalAlignments() {
108: TestComponent top = new TestComponent(7, 2, 9, 4);
109: TestComponent center = new TestComponent(7, 2, 9, 4);
110: TestComponent bottom = new TestComponent(7, 2, 9, 4);
111: TestComponent fill = new TestComponent(7, 2, 9, 4);
112: TestComponent def = new TestComponent(7, 2, 9, 4);
113: FormLayout layout = new FormLayout("pref",
114: "top:10px, center:10px, bottom:10px, fill:10px, 10px");
115:
116: JPanel panel = new JPanel(layout);
117: panel.add(top, cc.xy(1, 1));
118: panel.add(center, cc.xy(1, 2));
119: panel.add(bottom, cc.xy(1, 3));
120: panel.add(fill, cc.xy(1, 4));
121: panel.add(def, cc.xy(1, 5));
122:
123: panel.doLayout();
124:
125: assertEquals("Top.y", 0, top.getY());
126: assertEquals("Top.height", 4, top.getHeight());
127: assertEquals("Center.y", 13, center.getY());
128: assertEquals("Center.height", 4, center.getHeight());
129: assertEquals("Bottom.y", 26, bottom.getY());
130: assertEquals("Bottom.height", 4, bottom.getHeight());
131: assertEquals("Fill.y", 30, fill.getY());
132: assertEquals("Fill.height", 10, fill.getHeight());
133: assertEquals("Default.y", 43, def.getY());
134: assertEquals("Default.height", 4, def.getHeight());
135: }
136:
137: /**
138: * Tests bounded min and pref widths.
139: */
140: public void testBoundedWidth() {
141: TestComponent c1 = new TestComponent(2, 7, 4, 9);
142: TestComponent c2 = new TestComponent(20, 7, 40, 9);
143: TestComponent c3 = new TestComponent(2, 7, 4, 9);
144: TestComponent c4 = new TestComponent(20, 7, 40, 9);
145: TestComponent c5 = new TestComponent(2, 7, 4, 9);
146: TestComponent c6 = new TestComponent(20, 7, 40, 9);
147: TestComponent c7 = new TestComponent(2, 7, 4, 9);
148: TestComponent c8 = new TestComponent(20, 7, 40, 9);
149: FormLayout layout = new FormLayout(
150: "max(10px;min), max(10px;min), "
151: + "max(10px;pref), max(10px;pref), "
152: + "min(10px;min), min(10px;min), "
153: + "min(10px;pref), min(10px;pref)", "pref");
154:
155: JPanel panel = new JPanel(layout);
156: panel.add(c1, cc.xy(1, 1));
157: panel.add(c2, cc.xy(2, 1));
158: panel.add(c3, cc.xy(3, 1));
159: panel.add(c4, cc.xy(4, 1));
160: panel.add(c5, cc.xy(5, 1));
161: panel.add(c6, cc.xy(6, 1));
162: panel.add(c7, cc.xy(7, 1));
163: panel.add(c8, cc.xy(8, 1));
164:
165: panel.doLayout();
166:
167: assertEquals("max(10px;c1_min).width", 10, c1.getWidth());
168: assertEquals("max(10px;c2_min).width", 20, c2.getWidth());
169: assertEquals("max(10px;c3_pref).width", 10, c3.getWidth());
170: assertEquals("max(10px;c4_pref).width", 40, c4.getWidth());
171: assertEquals("min(10px;c5_min).width", 2, c5.getWidth());
172: assertEquals("min(10px;c6_min).width", 10, c6.getWidth());
173: assertEquals("min(10px;c7_pref).width", 4, c7.getWidth());
174: assertEquals("min(10px;c8_pref).width", 10, c8.getWidth());
175: }
176:
177: /**
178: * Tests bounded min and pref widths.
179: */
180: public void testBoundedHeight() {
181: TestComponent c1 = new TestComponent(7, 2, 9, 4);
182: TestComponent c2 = new TestComponent(7, 20, 9, 40);
183: TestComponent c3 = new TestComponent(7, 2, 9, 4);
184: TestComponent c4 = new TestComponent(7, 20, 9, 40);
185: TestComponent c5 = new TestComponent(7, 2, 9, 4);
186: TestComponent c6 = new TestComponent(7, 20, 9, 40);
187: TestComponent c7 = new TestComponent(7, 2, 9, 4);
188: TestComponent c8 = new TestComponent(7, 20, 9, 40);
189: FormLayout layout = new FormLayout("pref",
190: "f:max(10px;min), f:max(10px;min), "
191: + "f:max(10px;pref), f:max(10px;pref), "
192: + "f:min(10px;min), f:min(10px;min), "
193: + "f:min(10px;pref), f:min(10px;pref)");
194:
195: JPanel panel = new JPanel(layout);
196: panel.add(c1, cc.xy(1, 1));
197: panel.add(c2, cc.xy(1, 2));
198: panel.add(c3, cc.xy(1, 3));
199: panel.add(c4, cc.xy(1, 4));
200: panel.add(c5, cc.xy(1, 5));
201: panel.add(c6, cc.xy(1, 6));
202: panel.add(c7, cc.xy(1, 7));
203: panel.add(c8, cc.xy(1, 8));
204:
205: panel.doLayout();
206:
207: assertEquals("max(10px;c1_min).height", 10, c1.getHeight());
208: assertEquals("max(10px;c2_min).height", 20, c2.getHeight());
209: assertEquals("max(10px;c3_pref).height", 10, c3.getHeight());
210: assertEquals("max(10px;c4_pref).height", 40, c4.getHeight());
211: assertEquals("min(10px;c5_min).height", 2, c5.getHeight());
212: assertEquals("min(10px;c6_min).height", 10, c6.getHeight());
213: assertEquals("min(10px;c7_pref).height", 4, c7.getHeight());
214: assertEquals("min(10px;c8_pref).height", 10, c8.getHeight());
215: }
216:
217: // Testing components that span multiple columns/rows *********************
218:
219: /**
220: * Checks and verifies that components that span multiple columns
221: * do not expand the container of no column grows.
222: */
223: public void testNoExtraExpansionIfAllColumnsAreFixed() {
224: TestComponent c1 = new TestComponent(10, 1, 50, 1);
225: TestComponent c2 = new TestComponent(10, 1, 50, 1);
226: TestComponent c3 = new TestComponent(10, 1, 50, 1);
227: TestComponent c4 = new TestComponent(10, 1, 50, 1);
228: FormLayout layout = new FormLayout("10px, 15px, 20px",
229: "pref, pref");
230:
231: JPanel panel = new JPanel(layout);
232: panel.add(c1, cc.xy(1, 1));
233: panel.add(c2, cc.xy(2, 1));
234: panel.add(c3, cc.xy(3, 1));
235: panel.add(c4, cc.xyw(1, 2, 2));
236:
237: Dimension preferredLayoutSize = layout
238: .preferredLayoutSize(panel);
239: panel.setSize(preferredLayoutSize);
240: panel.doLayout();
241: int col1And2Width = c2.getX() + c2.getWidth();
242: int gridWidth = c3.getX() + c3.getWidth();
243: int totalWidth = preferredLayoutSize.width;
244:
245: assertEquals("Col1+2 width", 25, col1And2Width);
246: assertEquals("Grid width", 45, gridWidth);
247: assertEquals("Total width", 45, totalWidth);
248: }
249:
250: /**
251: * Checks and verifies that components that span multiple columns
252: * do not expand the container of no column grows.
253: */
254: public void testNoExtraExpansionIfSpannedColumnsAreFixed() {
255: TestComponent c1 = new TestComponent(10, 1, 50, 1);
256: TestComponent c2 = new TestComponent(10, 1, 50, 1);
257: TestComponent c3 = new TestComponent(10, 1, 50, 1);
258: TestComponent c4 = new TestComponent(10, 1, 50, 1);
259: FormLayout layout = new FormLayout("10px, 15px, 20px:grow",
260: "pref, pref");
261:
262: JPanel panel = new JPanel(layout);
263: panel.add(c1, cc.xy(1, 1));
264: panel.add(c2, cc.xy(2, 1));
265: panel.add(c3, cc.xy(3, 1));
266: panel.add(c4, cc.xyw(1, 2, 2));
267:
268: Dimension preferredLayoutSize = layout
269: .preferredLayoutSize(panel);
270: panel.setSize(preferredLayoutSize);
271: panel.doLayout();
272: int col1And2Width = c2.getX() + c2.getWidth();
273: int gridWidth = c3.getX() + c3.getWidth();
274: int totalWidth = preferredLayoutSize.width;
275:
276: assertEquals("Col1+2 width", 25, col1And2Width);
277: assertEquals("Grid width", 45, gridWidth);
278: assertEquals("Total width", 45, totalWidth); // 70 is wrong
279: }
280:
281: /**
282: * Checks and verifies that components that span multiple columns
283: * do not expand the container of no column grows.
284: */
285: public void testExtraExpansionIfSpannedColumnsGrow() {
286: TestComponent c1 = new TestComponent(10, 1, 50, 1);
287: TestComponent c2 = new TestComponent(10, 1, 50, 1);
288: TestComponent c3 = new TestComponent(10, 1, 50, 1);
289: TestComponent c4 = new TestComponent(10, 1, 50, 1);
290: FormLayout layout = new FormLayout("10px, 15px:grow, 20px",
291: "pref, pref");
292:
293: JPanel panel = new JPanel(layout);
294: panel.add(c1, cc.xy(1, 1));
295: panel.add(c2, cc.xy(2, 1));
296: panel.add(c3, cc.xy(3, 1));
297: panel.add(c4, cc.xyw(1, 2, 2));
298:
299: Dimension preferredLayoutSize = layout
300: .preferredLayoutSize(panel);
301: panel.setSize(preferredLayoutSize);
302: panel.doLayout();
303: int col1And2Width = c2.getX() + c2.getWidth();
304: int gridWidth = c3.getX() + c3.getWidth();
305: int totalWidth = preferredLayoutSize.width;
306:
307: assertEquals("Col1+2 width", 50, col1And2Width);
308: assertEquals("Grid width", 70, gridWidth);
309: assertEquals("Total width", 70, totalWidth);
310: }
311:
312: /**
313: * Checks and verifies that components that span multiple columns
314: * and that expand the container are measured using the correct measure.
315: */
316: public void testExtraExpansionHonorsCurrentMeasure() {
317: TestComponent c1 = new TestComponent(10, 1, 50, 1);
318: TestComponent c2 = new TestComponent(10, 1, 50, 1);
319: TestComponent c3 = new TestComponent(10, 1, 50, 1);
320: TestComponent c4 = new TestComponent(10, 1, 50, 1);
321: FormLayout layout = new FormLayout("10px, 15px:grow, 20px",
322: "pref, pref");
323:
324: JPanel panel = new JPanel(layout);
325: panel.add(c1, cc.xy(1, 1));
326: panel.add(c2, cc.xy(2, 1));
327: panel.add(c3, cc.xy(3, 1));
328: panel.add(c4, cc.xyw(1, 2, 2));
329:
330: int minimumLayoutWidth = layout.minimumLayoutSize(panel).width;
331: int preferredLayoutWidth = layout.preferredLayoutSize(panel).width;
332:
333: assertEquals("Minimum layout width", 45, minimumLayoutWidth);
334: assertEquals("Preferred layout width", 70, preferredLayoutWidth);
335: }
336:
337: /**
338: * Tests the layout size, column and row sizes for a default specs.
339: */
340: public void testDefaultSize() {
341: TestComponent c1 = new TestComponent(10, 10, 50, 50);
342: FormLayout layout = new FormLayout("default", "default");
343:
344: JPanel panel = new JPanel(layout);
345: panel.add(c1, cc.xy(1, 1));
346:
347: Dimension minimumLayoutSize = layout.minimumLayoutSize(panel);
348: Dimension preferredLayoutSize = layout
349: .preferredLayoutSize(panel);
350: assertEquals("Minimum layout width", 10,
351: minimumLayoutSize.width);
352: assertEquals("Minimum layout height", 10,
353: minimumLayoutSize.height);
354: assertEquals("Preferred layout width", 50,
355: preferredLayoutSize.width);
356: assertEquals("Preferred layout height", 50,
357: preferredLayoutSize.height);
358:
359: panel.setSize(minimumLayoutSize);
360: panel.doLayout();
361: int columnWidth = c1.getWidth();
362: int rowHeight = c1.getHeight();
363: assertEquals("Column width (container min)", 10, columnWidth);
364: assertEquals("Row height (container min)", 10, rowHeight);
365:
366: panel.setSize(preferredLayoutSize);
367: panel.doLayout();
368: columnWidth = c1.getWidth();
369: rowHeight = c1.getHeight();
370: assertEquals("Column width (container pref)", 50, columnWidth);
371: assertEquals("Row height (container pref)", 50, rowHeight);
372: }
373:
374: /**
375: * Tests the combination of a default size spec with a lower bound
376: * that shall ensure a minimum size.
377: */
378: public void testDefaultWithLowerBound() {
379: TestComponent c1 = new TestComponent(10, 10, 50, 50);
380: FormLayout layout = new FormLayout("max(20px;default)",
381: "max(20px;default)");
382:
383: JPanel panel = new JPanel(layout);
384: panel.add(c1, cc.xy(1, 1));
385:
386: Dimension minimumLayoutSize = layout.minimumLayoutSize(panel);
387: Dimension preferredLayoutSize = layout
388: .preferredLayoutSize(panel);
389:
390: assertEquals("Minimum layout width", 20,
391: minimumLayoutSize.width);
392: assertEquals("Minimum layout height", 20,
393: minimumLayoutSize.height);
394: assertEquals("Preferred layout width", 50,
395: preferredLayoutSize.width);
396: assertEquals("Preferred layout height", 50,
397: preferredLayoutSize.height);
398:
399: panel.setSize(minimumLayoutSize);
400: panel.doLayout();
401: int columnWidth = c1.getWidth();
402: int rowHeight = c1.getHeight();
403: assertEquals("Column width (container min)", 20, columnWidth);
404: assertEquals("Row height (container min)", 20, rowHeight);
405:
406: panel.setSize(preferredLayoutSize);
407: panel.doLayout();
408: columnWidth = c1.getWidth();
409: rowHeight = c1.getHeight();
410: assertEquals("Column width (container pref)", 50, columnWidth);
411: assertEquals("Row height (container pref)", 50, rowHeight);
412: }
413:
414: /**
415: * Tests the combination of a default size spec with an upper bound
416: * that shall ensure a maximum size.
417: */
418: public void testDefaultWithUpperBound() {
419: TestComponent c1 = new TestComponent(10, 10, 50, 50);
420: FormLayout layout = new FormLayout("min(20px;default)",
421: "min(20px;default)");
422:
423: JPanel panel = new JPanel(layout);
424: panel.add(c1, cc.xy(1, 1));
425:
426: Dimension minimumLayoutSize = layout.minimumLayoutSize(panel);
427: Dimension preferredLayoutSize = layout
428: .preferredLayoutSize(panel);
429:
430: assertEquals("Minimum layout width", 10,
431: minimumLayoutSize.width);
432: assertEquals("Minimum layout height", 10,
433: minimumLayoutSize.height);
434: assertEquals("Preferred layout width", 20,
435: preferredLayoutSize.width);
436: assertEquals("Preferred layout height", 20,
437: preferredLayoutSize.height);
438:
439: panel.setSize(minimumLayoutSize);
440: panel.doLayout();
441: int columnWidth = c1.getWidth();
442: int rowHeight = c1.getHeight();
443: assertEquals("Column width (container min)", 10, columnWidth);
444: assertEquals("Row height (container min)", 10, rowHeight);
445:
446: panel.setSize(preferredLayoutSize);
447: panel.doLayout();
448: columnWidth = c1.getWidth();
449: rowHeight = c1.getHeight();
450: assertEquals("Column width (container pref)", 20, columnWidth);
451: assertEquals("Row height (container pref)", 20, rowHeight);
452: }
453:
454: }
|