KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > layout > FormLayoutTest


1 /*
2  * Copyright (c) 2003 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.forms.layout;
32
33 import java.awt.Dimension JavaDoc;
34
35 import javax.swing.JPanel JavaDoc;
36
37 import junit.framework.TestCase;
38
39 /**
40  * Tests the FormLayout's layout algorithm.
41  *
42  * @author Karsten Lentzsch
43  * @version $Revision: 1.2 $
44  */

45
46 public final class FormLayoutTest extends TestCase {
47     
48     private CellConstraints cc = new CellConstraints();
49
50
51     /**
52      * Checks basic layout functions.
53      */

54     public void testBasic() {
55         FormLayout layout = new FormLayout(
56             "1px, 2px, 3px, 5px, 7px",
57             "1px, 2px, 3px");
58             
59         JPanel JavaDoc panel = new JPanel JavaDoc(layout);
60         panel.doLayout();
61         FormLayout.LayoutInfo info = layout.getLayoutInfo(panel);
62         assertEquals("Columns", 6, info.columnOrigins.length);
63         assertEquals("Rows", 4, info.rowOrigins.length);
64         assertEquals("Column 0", 0, info.columnOrigins[0]);
65         assertEquals("Column 1", 1, info.columnOrigins[1]);
66         assertEquals("Column 2", 3, info.columnOrigins[2]);
67         assertEquals("Column 3", 6, info.columnOrigins[3]);
68         assertEquals("Column 4", 11, info.columnOrigins[4]);
69         assertEquals("Column 5", 18, info.columnOrigins[5]);
70     }
71     
72     
73     /**
74      * Checks whether components are aligned according to the column specs.
75      */

76     public void testHorizontalAlignments() {
77         TestComponent left = new TestComponent(2, 7, 4, 9);
78         TestComponent center = new TestComponent(2, 7, 4, 9);
79         TestComponent right = new TestComponent(2, 7, 4, 9);
80         TestComponent fill = new TestComponent(2, 7, 4, 9);
81         TestComponent def = new TestComponent(2, 7, 4, 9);
82         FormLayout layout = new FormLayout(
83             "left:10px, center:10px, right:10px, fill:10px, 10px",
84             "pref");
85             
86         JPanel JavaDoc panel = new JPanel JavaDoc(layout);
87         panel.add(left, cc.xy(1, 1));
88         panel.add(center, cc.xy(2, 1));
89         panel.add(right, cc.xy(3, 1));
90         panel.add(fill, cc.xy(4, 1));
91         panel.add(def, cc.xy(5, 1));
92         
93         panel.doLayout();
94         
95         assertEquals("Left.x", 0, left.getX());
96         assertEquals("Left.width", 4, left.getWidth());
97         assertEquals("Center.x", 13, center.getX());
98         assertEquals("Center.width", 4, center.getWidth());
99         assertEquals("Right.x", 26, right.getX());
100         assertEquals("Right.width", 4, right.getWidth());
101         assertEquals("Fill.x", 30, fill.getX());
102         assertEquals("Fill.width", 10, fill.getWidth());
103         assertEquals("Default.x", 40, def.getX());
104         assertEquals("Default.width", 10, def.getWidth());
105     }
106     
107     
108     /**
109      * Checks whether components are aligned according to the row specs.
110      */

111     public void testVerticalAlignments() {
112         TestComponent top = new TestComponent(7, 2, 9, 4);
113         TestComponent center = new TestComponent(7, 2, 9, 4);
114         TestComponent bottom = new TestComponent(7, 2, 9, 4);
115         TestComponent fill = new TestComponent(7, 2, 9, 4);
116         TestComponent def = new TestComponent(7, 2, 9, 4);
117         FormLayout layout = new FormLayout(
118             "pref",
119             "top:10px, center:10px, bottom:10px, fill:10px, 10px");
120             
121         JPanel JavaDoc panel = new JPanel JavaDoc(layout);
122         panel.add(top, cc.xy(1, 1));
123         panel.add(center, cc.xy(1, 2));
124         panel.add(bottom, cc.xy(1, 3));
125         panel.add(fill, cc.xy(1, 4));
126         panel.add(def, cc.xy(1, 5));
127         
128         panel.doLayout();
129         
130         assertEquals("Top.y", 0, top.getY());
131         assertEquals("Top.height", 4, top.getHeight());
132         assertEquals("Center.y", 13, center.getY());
133         assertEquals("Center.height", 4, center.getHeight());
134         assertEquals("Bottom.y", 26, bottom.getY());
135         assertEquals("Bottom.height", 4, bottom.getHeight());
136         assertEquals("Fill.y", 30, fill.getY());
137         assertEquals("Fill.height", 10, fill.getHeight());
138         assertEquals("Default.y", 43, def.getY());
139         assertEquals("Default.height", 4, def.getHeight());
140     }
141     
142
143     /**
144      * Tests bounded min and pref widths.
145      */

146     public void testBoundedWidth() {
147         TestComponent c1 = new TestComponent( 2, 7, 4, 9);
148         TestComponent c2 = new TestComponent(20, 7, 40, 9);
149         TestComponent c3 = new TestComponent( 2, 7, 4, 9);
150         TestComponent c4 = new TestComponent(20, 7, 40, 9);
151         TestComponent c5 = new TestComponent( 2, 7, 4, 9);
152         TestComponent c6 = new TestComponent(20, 7, 40, 9);
153         TestComponent c7 = new TestComponent( 2, 7, 4, 9);
154         TestComponent c8 = new TestComponent(20, 7, 40, 9);
155         FormLayout layout = new FormLayout(
156             "max(10px;min), max(10px;min), " +
157             "max(10px;pref), max(10px;pref), " +
158             "min(10px;min), min(10px;min), " +
159             "min(10px;pref), min(10px;pref)",
160             "pref");
161             
162         JPanel JavaDoc panel = new JPanel JavaDoc(layout);
163         panel.add(c1, cc.xy(1, 1));
164         panel.add(c2, cc.xy(2, 1));
165         panel.add(c3, cc.xy(3, 1));
166         panel.add(c4, cc.xy(4, 1));
167         panel.add(c5, cc.xy(5, 1));
168         panel.add(c6, cc.xy(6, 1));
169         panel.add(c7, cc.xy(7, 1));
170         panel.add(c8, cc.xy(8, 1));
171         
172         panel.doLayout();
173         
174         assertEquals("max(10px;c1_min).width", 10, c1.getWidth());
175         assertEquals("max(10px;c2_min).width", 20, c2.getWidth());
176         assertEquals("max(10px;c3_pref).width", 10, c3.getWidth());
177         assertEquals("max(10px;c4_pref).width", 40, c4.getWidth());
178         assertEquals("min(10px;c5_min).width", 2, c5.getWidth());
179         assertEquals("min(10px;c6_min).width", 10, c6.getWidth());
180         assertEquals("min(10px;c7_pref).width", 4, c7.getWidth());
181         assertEquals("min(10px;c8_pref).width", 10, c8.getWidth());
182     }
183     
184     /**
185      * Tests bounded min and pref widths.
186      */

187     public void testBoundedHeight() {
188         TestComponent c1 = new TestComponent(7, 2, 9, 4);
189         TestComponent c2 = new TestComponent(7, 20, 9, 40);
190         TestComponent c3 = new TestComponent(7, 2, 9, 4);
191         TestComponent c4 = new TestComponent(7, 20, 9, 40);
192         TestComponent c5 = new TestComponent(7, 2, 9, 4);
193         TestComponent c6 = new TestComponent(7, 20, 9, 40);
194         TestComponent c7 = new TestComponent(7, 2, 9, 4);
195         TestComponent c8 = new TestComponent(7, 20, 9, 40);
196         FormLayout layout = new FormLayout(
197             "pref",
198             "f:max(10px;min), f:max(10px;min), " +
199             "f:max(10px;pref), f:max(10px;pref), " +
200             "f:min(10px;min), f:min(10px;min), " +
201             "f:min(10px;pref), f:min(10px;pref)");
202             
203         JPanel JavaDoc panel = new JPanel JavaDoc(layout);
204         panel.add(c1, cc.xy(1, 1));
205         panel.add(c2, cc.xy(1, 2));
206         panel.add(c3, cc.xy(1, 3));
207         panel.add(c4, cc.xy(1, 4));
208         panel.add(c5, cc.xy(1, 5));
209         panel.add(c6, cc.xy(1, 6));
210         panel.add(c7, cc.xy(1, 7));
211         panel.add(c8, cc.xy(1, 8));
212         
213         panel.doLayout();
214         
215         assertEquals("max(10px;c1_min).height", 10, c1.getHeight());
216         assertEquals("max(10px;c2_min).height", 20, c2.getHeight());
217         assertEquals("max(10px;c3_pref).height", 10, c3.getHeight());
218         assertEquals("max(10px;c4_pref).height", 40, c4.getHeight());
219         assertEquals("min(10px;c5_min).height", 2, c5.getHeight());
220         assertEquals("min(10px;c6_min).height", 10, c6.getHeight());
221         assertEquals("min(10px;c7_pref).height", 4, c7.getHeight());
222         assertEquals("min(10px;c8_pref).height", 10, c8.getHeight());
223     }
224     
225     
226     // Testing components that span multiple columns/rows *********************
227

228     /**
229      * Checks and verifies that components that span multiple columns
230      * do not expand the container of no column grows.
231      */

232     public void testNoExtraExpansionIfAllColumnsAreFixed() {
233         TestComponent c1 = new TestComponent(10, 1, 50, 1);
234         TestComponent c2 = new TestComponent(10, 1, 50, 1);
235         TestComponent c3 = new TestComponent(10, 1, 50, 1);
236         TestComponent c4 = new TestComponent(10, 1, 50, 1);
237         FormLayout layout = new FormLayout(
238             "10px, 15px, 20px",
239             "pref, pref");
240             
241         JPanel JavaDoc panel = new JPanel JavaDoc(layout);
242         panel.add(c1, cc.xy (1, 1));
243         panel.add(c2, cc.xy (2, 1));
244         panel.add(c3, cc.xy (3, 1));
245         panel.add(c4, cc.xywh(1, 2, 2, 1));
246
247         Dimension JavaDoc preferredLayoutSize = layout.preferredLayoutSize(panel);
248         panel.setSize(preferredLayoutSize);
249         panel.doLayout();
250         int col1And2Width = c2.getX() + c2.getWidth();
251         int gridWidth = c3.getX() + c3.getWidth();
252         int totalWidth = preferredLayoutSize.width;
253         
254         assertEquals("Col1+2 width", 25, col1And2Width);
255         assertEquals("Grid width", 45, gridWidth);
256         assertEquals("Total width", 45, totalWidth);
257     }
258     
259
260     /**
261      * Checks and verifies that components that span multiple columns
262      * do not expand the container of no column grows.
263      */

264     public void testNoExtraExpansionIfSpannedColumnsAreFixed() {
265         TestComponent c1 = new TestComponent(10, 1, 50, 1);
266         TestComponent c2 = new TestComponent(10, 1, 50, 1);
267         TestComponent c3 = new TestComponent(10, 1, 50, 1);
268         TestComponent c4 = new TestComponent(10, 1, 50, 1);
269         FormLayout layout = new FormLayout(
270             "10px, 15px, 20px:grow",
271             "pref, pref");
272             
273         JPanel JavaDoc panel = new JPanel JavaDoc(layout);
274         panel.add(c1, cc.xy (1, 1));
275         panel.add(c2, cc.xy (2, 1));
276         panel.add(c3, cc.xy (3, 1));
277         panel.add(c4, cc.xywh(1, 2, 2, 1));
278         
279         Dimension JavaDoc preferredLayoutSize = layout.preferredLayoutSize(panel);
280         panel.setSize(preferredLayoutSize);
281         panel.doLayout();
282         int col1And2Width = c2.getX() + c2.getWidth();
283         int gridWidth = c3.getX() + c3.getWidth();
284         int totalWidth = preferredLayoutSize.width;
285         
286         assertEquals("Col1+2 width", 25, col1And2Width);
287         assertEquals("Grid width", 45, gridWidth);
288         assertEquals("Total width", 45, totalWidth); // 70 is wrong
289
}
290     
291
292     /**
293      * Checks and verifies that components that span multiple columns
294      * do not expand the container of no column grows.
295      */

296     public void testExtraExpansionIfSpannedColumnsGrow() {
297         TestComponent c1 = new TestComponent(10, 1, 50, 1);
298         TestComponent c2 = new TestComponent(10, 1, 50, 1);
299         TestComponent c3 = new TestComponent(10, 1, 50, 1);
300         TestComponent c4 = new TestComponent(10, 1, 50, 1);
301         FormLayout layout = new FormLayout(
302             "10px, 15px:grow, 20px",
303             "pref, pref");
304             
305         JPanel JavaDoc panel = new JPanel JavaDoc(layout);
306         panel.add(c1, cc.xy (1, 1));
307         panel.add(c2, cc.xy (2, 1));
308         panel.add(c3, cc.xy (3, 1));
309         panel.add(c4, cc.xywh(1, 2, 2, 1));
310         
311         Dimension JavaDoc preferredLayoutSize = layout.preferredLayoutSize(panel);
312         panel.setSize(preferredLayoutSize);
313         panel.doLayout();
314         int col1And2Width = c2.getX() + c2.getWidth();
315         int gridWidth = c3.getX() + c3.getWidth();
316         int totalWidth = preferredLayoutSize.width;
317         
318         assertEquals("Col1+2 width", 50, col1And2Width);
319         assertEquals("Grid width", 70, gridWidth);
320         assertEquals("Total width", 70, totalWidth);
321     }
322     
323
324     /**
325      * Checks and verifies that components that span multiple columns
326      * and that expand the container are measured using the correct measure.
327      */

328     public void testExtraExpansionHonorsCurrentMeasure() {
329         TestComponent c1 = new TestComponent(10, 1, 50, 1);
330         TestComponent c2 = new TestComponent(10, 1, 50, 1);
331         TestComponent c3 = new TestComponent(10, 1, 50, 1);
332         TestComponent c4 = new TestComponent(10, 1, 50, 1);
333         FormLayout layout = new FormLayout(
334             "10px, 15px:grow, 20px",
335             "pref, pref");
336             
337         JPanel JavaDoc panel = new JPanel JavaDoc(layout);
338         panel.add(c1, cc.xy (1, 1));
339         panel.add(c2, cc.xy (2, 1));
340         panel.add(c3, cc.xy (3, 1));
341         panel.add(c4, cc.xywh(1, 2, 2, 1));
342         
343         int minimumLayoutWidth = layout.minimumLayoutSize(panel).width;
344         int preferredLayoutWidth = layout.preferredLayoutSize(panel).width;
345         
346         assertEquals("Minimum layout width", 45, minimumLayoutWidth);
347         assertEquals("Preferred layout width", 70, preferredLayoutWidth);
348     }
349     
350
351 }
Popular Tags