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 Michael Danilov
019: * @version $Revision$
020: */package java.awt;
021:
022: @SuppressWarnings("serial")
023: public class GridBagLayoutTest extends AWTTestCase {
024:
025: public static void main(String[] args) {
026: junit.textui.TestRunner.run(GridBagLayoutTest.class);
027: }
028:
029: class TestButton extends Button {
030: TestButton(String name) {
031: super (name);
032: }
033: }
034:
035: private final TestButton first, second;
036: private GridBagLayout layout;
037: private Frame frame;
038:
039: public GridBagLayoutTest(String arg0) {
040: super (arg0);
041:
042: first = new TestButton("First");
043: first.setFont(new Font("dialog", Font.PLAIN, 50));
044: second = new TestButton("Second");
045: second.setFont(new Font("dialog", Font.PLAIN, 50));
046: }
047:
048: @Override
049: protected void setUp() throws Exception {
050: super .setUp();
051:
052: frame = new Frame();
053: layout = new GridBagLayout();
054: frame.setLayout(layout);
055: frame.setVisible(true);
056: }
057:
058: @Override
059: protected void tearDown() throws Exception {
060: frame.dispose();
061:
062: super .tearDown();
063: }
064:
065: private void assertEquals(GridBagConstraints gbc1,
066: GridBagConstraints gbc2) {
067: assertNotNull(gbc1);
068: assertNotNull(gbc2);
069: assertEquals("gridx", gbc1.gridx, gbc2.gridx);
070: assertEquals("gridy", gbc1.gridy, gbc2.gridy);
071: assertEquals("gridwidth", gbc1.gridwidth, gbc2.gridwidth);
072: assertEquals("gridheight", gbc1.gridheight, gbc2.gridheight);
073: assertTrue("weightx", gbc1.weightx == gbc2.weightx);
074: assertTrue("weighty", gbc1.weighty == gbc2.weighty);
075: assertEquals("anchor", gbc1.anchor, gbc2.anchor);
076: assertEquals("fill", gbc1.fill, gbc2.fill);
077: assertEquals("insets", gbc1.insets, gbc2.insets);
078: assertEquals("ipadx", gbc1.ipadx, gbc2.ipadx);
079: assertEquals("ipady", gbc1.ipady, gbc2.ipady);
080: }
081:
082: public final void testToString() {
083: assertTrue(new String("java.awt.GridBagLayout").equals(layout
084: .toString()));
085: }
086:
087: public final void testAddLayoutComponentStringComponent() {
088: layout.addLayoutComponent((String) null, first);
089: assertEquals(0, layout.comptable.size());
090: layout.addLayoutComponent("", first);
091: assertEquals(0, layout.comptable.size());
092: layout.addLayoutComponent("q", first);
093: assertEquals(0, layout.comptable.size());
094: }
095:
096: public final void testAddLayoutComponentComponentObject() {
097: GridBagConstraints c = new GridBagConstraints();
098:
099: frame.add(first, c);
100: assertEquals(c, layout.getConstraints(first));
101: c.fill = GridBagConstraints.BOTH;
102: frame.add(second, c);
103: assertEquals(c, layout.getConstraints(second));
104: frame.remove(second);
105: assertNull(layout.comptable.get(second));
106: assertEquals(layout.defaultConstraints, layout
107: .getConstraints(second));
108: frame.remove(first);
109: assertNull(layout.comptable.get(first));
110: assertEquals(layout.defaultConstraints, layout
111: .getConstraints(first));
112:
113: frame.add(first, c);
114: assertEquals(c, layout.getConstraints(first));
115:
116: GridBagConstraints c1 = new GridBagConstraints();
117: c1.fill = GridBagConstraints.HORIZONTAL;
118: layout.addLayoutComponent(first, c1);
119: assertEquals(c1, layout.getConstraints(first));
120: // null constraints don't replace old:
121: layout.addLayoutComponent(first, null);
122: assertEquals(c1, layout.getConstraints(first));
123:
124: boolean notCons = false;
125: try {
126: frame.add(first, new Integer(3));
127: } catch (IllegalArgumentException e) {
128: notCons = true;
129: }
130: assertTrue(notCons);
131: }
132:
133: public final void testRemoveLayoutComponent() {
134: frame.add(first);
135: frame.add(second);
136: frame.remove(second);
137: frame.add(second);
138: frame.remove(first);
139: frame.remove(second);
140: }
141:
142: public final void testGetConstraints() {
143: GridBagConstraints c = layout.defaultConstraints;
144: assertEquals(c.gridx, GridBagConstraints.RELATIVE);
145: assertEquals(c.gridy, GridBagConstraints.RELATIVE);
146: assertEquals(c.gridwidth, 1);
147: assertEquals(c.gridheight, 1);
148: assertTrue(c.weightx == 0.);
149: assertTrue(c.weighty == 0.);
150: assertEquals(c.anchor, GridBagConstraints.CENTER);
151: assertEquals(c.fill, GridBagConstraints.NONE);
152: assertEquals(c.insets.top, 0);
153: assertEquals(c.ipadx, 0);
154: assertEquals(c.ipady, 0);
155:
156: layout.defaultConstraints.fill = GridBagConstraints.BOTH;
157: assertEquals(layout.defaultConstraints, layout
158: .getConstraints(first));
159:
160: }
161:
162: public final void testSetConstraints() {
163: GridBagConstraints c = new GridBagConstraints();
164:
165: frame.add(first, c);
166:
167: c.gridx = -100;
168: layout.setConstraints(second, c);
169: assertNotNull("new component was added to comptable",
170: layout.comptable.get(second));
171: assertEquals(c.gridx, layout.getConstraints(second).gridx);
172: }
173:
174: public final void testMinimumLayoutSize() {
175: Dimension expected;
176: Insets insets = frame.getInsets();
177: expected = new Dimension(insets.left + insets.right, insets.top
178: + insets.bottom);
179: assertEquals(expected, layout.minimumLayoutSize(frame));
180:
181: frame.add(first);
182: expected = new Dimension(first.getMinimumSize());
183: expected.height += insets.bottom + insets.top;
184: expected.width += insets.left + insets.right;
185: assertEquals(expected, layout.minimumLayoutSize(frame));
186: }
187:
188: public final void testPreferredLayoutSize() {
189: Insets insets = frame.getInsets();
190: Dimension expected = new Dimension(insets.left + insets.right,
191: insets.top + insets.bottom);
192: assertEquals(expected, layout.minimumLayoutSize(frame));
193:
194: frame.add(first);
195: expected = new Dimension(first.getPreferredSize());
196: expected.height += insets.bottom + insets.top;
197: expected.width += insets.left + insets.right;
198: assertEquals(expected, layout.preferredLayoutSize(frame));
199: }
200:
201: public final void testMaximumLayoutSize() {
202: assertEquals(
203: new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE),
204: layout.maximumLayoutSize(frame));
205: }
206:
207: public final void testGetLayoutDimensions() {
208: frame.add(first);
209: frame.validate();
210: frame.setSize(frame.getPreferredSize());
211: frame.validate();
212:
213: int dims[][] = layout.getLayoutDimensions();
214: assertEquals(first.getPreferredSize().width, dims[0][0]);
215: assertEquals(first.getPreferredSize().height, dims[1][0]);
216: assertEquals(0, dims[0][1]);
217: assertEquals(0, dims[1][1]);
218: }
219:
220: public final void testGetLayoutWeights() {
221: GridBagConstraints c = new GridBagConstraints();
222: c.weightx = 1.;
223: c.weighty = .5;
224: frame.add(first, c);
225: frame.validate();
226:
227: double ws[][] = layout.getLayoutWeights();
228: assertTrue(ws[0][0] == 1.);
229: assertTrue(ws[1][0] == .5);
230: assertTrue(ws[0][1] == 0.);
231: assertTrue(ws[1][1] == 0.);
232: }
233:
234: public final void testGetLayoutOrigin() {
235: GridBagConstraints c = new GridBagConstraints();
236: c.weightx = 1.;
237: c.weighty = 1.;
238: frame.add(first, c);
239: frame.setSize(frame.getPreferredSize());
240: frame.validate();
241:
242: Insets insets = frame.getInsets();
243: assertEquals(new Point(insets.left, insets.top), layout
244: .getLayoutOrigin());
245: }
246:
247: public final void testLocation() {
248: GridBagConstraints c = new GridBagConstraints();
249: frame.add(first, c);
250: frame.add(second, c);
251: frame.validate();
252: frame.setSize(frame.getPreferredSize());
253: frame.validate();
254:
255: Insets insets = frame.getInsets();
256: int x = insets.left + first.getPreferredSize().width + 1;
257: assertEquals(new Point(1, 0), layout
258: .location(x, insets.top + 1));
259: }
260:
261: public final void testLayoutContainer() {
262: GridBagConstraints c = new GridBagConstraints();
263: Insets insets = frame.getInsets();
264:
265: //Default constraints
266: frame.add(first);
267: frame.setSize(frame.getPreferredSize());
268: frame.validate();
269: Rectangle expected = new Rectangle(new Point(insets.left,
270: insets.top), first.getPreferredSize());
271: assertEquals(expected, first.getBounds());
272:
273: //Internal padding
274: c.ipadx = 10;
275: c.ipady = 20;
276: layout.setConstraints(first, c);
277: doLayout();
278:
279: expected.setSize(first.getPreferredSize());
280: expected.width += c.ipadx;
281: expected.height += c.ipady;
282: assertEquals(expected, first.getBounds());
283:
284: //External
285: c = new GridBagConstraints();
286: c.insets = new Insets(10, 20, 30, 40);
287: layout.setConstraints(first, c);
288: doLayout();
289: expected.setSize(first.getPreferredSize());
290: expected.x += c.insets.left;
291: expected.y += c.insets.top;
292: assertEquals(expected, first.getBounds());
293:
294: //Fill
295: c = new GridBagConstraints();
296: c.fill = GridBagConstraints.NONE;
297: layout.setConstraints(first, c);
298: layout.invalidateLayout(frame);
299: layout.layoutContainer(frame);
300: frame.setSize(frame.getPreferredSize().width + 10, frame
301: .getPreferredSize().height + 20);
302: frame.validate();
303: expected.setLocation(insets.left + 5, insets.top + 10);
304: expected.setSize(first.getPreferredSize());
305: assertEquals(expected, first.getBounds());
306:
307: c = new GridBagConstraints();
308: c.fill = GridBagConstraints.BOTH;
309: c.weightx = 1;
310: c.weighty = 1;
311: layout.setConstraints(first, c);
312: layout.invalidateLayout(frame);
313: layout.layoutContainer(frame);
314: expected.setLocation(insets.left, insets.top);
315: expected.width += 10;
316: expected.height += 20;
317: assertEquals(expected, first.getBounds());
318:
319: c = new GridBagConstraints();
320: c.fill = GridBagConstraints.HORIZONTAL;
321: c.weightx = 1;
322: layout.setConstraints(first, c);
323: layout.invalidateLayout(frame);
324: layout.layoutContainer(frame);
325: expected.y += 10;
326: expected.height = first.getPreferredSize().height;
327: assertEquals(expected, first.getBounds());
328:
329: c = new GridBagConstraints();
330: c.fill = GridBagConstraints.VERTICAL;
331: c.weighty = 1;
332: layout.setConstraints(first, c);
333: layout.invalidateLayout(frame);
334: layout.layoutContainer(frame);
335: expected.x += 5;
336: expected.y = insets.top;
337: expected.setSize(first.getPreferredSize());
338: expected.height += 20;
339: assertEquals(expected, first.getBounds());
340:
341: //anchor
342: c = new GridBagConstraints();
343: c.weightx = 1;
344: c.weighty = 1;
345:
346: c.anchor = GridBagConstraints.CENTER;
347: layout.setConstraints(first, c);
348: layout.invalidateLayout(frame);
349: layout.layoutContainer(frame);
350: expected.setLocation(insets.left, insets.top);
351: expected.translate(5, 10);
352: expected.setSize(first.getPreferredSize());
353: assertEquals(expected, first.getBounds());
354:
355: c.anchor = GridBagConstraints.NORTH;
356: layout.setConstraints(first, c);
357: layout.invalidateLayout(frame);
358: layout.layoutContainer(frame);
359: expected.translate(0, -10);
360: assertEquals(expected, first.getBounds());
361:
362: c.anchor = GridBagConstraints.NORTHEAST;
363: layout.setConstraints(first, c);
364: layout.invalidateLayout(frame);
365: layout.layoutContainer(frame);
366: expected.translate(5, 0);
367: assertEquals(expected, first.getBounds());
368:
369: c.anchor = GridBagConstraints.EAST;
370: layout.setConstraints(first, c);
371: layout.invalidateLayout(frame);
372: layout.layoutContainer(frame);
373: expected.translate(0, 10);
374: assertEquals(expected, first.getBounds());
375:
376: c.anchor = GridBagConstraints.SOUTHEAST;
377: layout.setConstraints(first, c);
378: layout.invalidateLayout(frame);
379: layout.layoutContainer(frame);
380: expected.translate(0, 10);
381: assertEquals(expected, first.getBounds());
382:
383: c.anchor = GridBagConstraints.SOUTH;
384: layout.setConstraints(first, c);
385: layout.invalidateLayout(frame);
386: layout.layoutContainer(frame);
387: expected.translate(-5, 0);
388: assertEquals(expected, first.getBounds());
389:
390: c.anchor = GridBagConstraints.SOUTHWEST;
391: layout.setConstraints(first, c);
392: layout.invalidateLayout(frame);
393: layout.layoutContainer(frame);
394: expected.translate(-5, 0);
395: assertEquals(expected, first.getBounds());
396:
397: c.anchor = GridBagConstraints.WEST;
398: layout.setConstraints(first, c);
399: layout.invalidateLayout(frame);
400: layout.layoutContainer(frame);
401: expected.translate(0, -10);
402: assertEquals(expected, first.getBounds());
403:
404: c.anchor = GridBagConstraints.NORTHWEST;
405: layout.setConstraints(first, c);
406: layout.invalidateLayout(frame);
407: layout.layoutContainer(frame);
408: expected.translate(0, -10);
409: assertEquals(expected, first.getBounds());
410:
411: //weights
412: c = new GridBagConstraints();
413: c.fill = GridBagConstraints.BOTH;
414: c.weightx = 1;
415: c.weighty = 1;
416: layout.setConstraints(first, c);
417: layout.invalidateLayout(frame);
418: layout.layoutContainer(frame);
419: expected.setLocation(insets.left, insets.top);
420: expected.width += 10;
421: expected.height += 20;
422: assertEquals(expected, first.getBounds());
423: c.weightx = 0;
424: c.weighty = 0;
425: layout.setConstraints(first, c);
426: layout.invalidateLayout(frame);
427: layout.layoutContainer(frame);
428: expected.setSize(first.getPreferredSize());
429: expected.translate(5, 10);
430: assertEquals(expected, first.getBounds());
431:
432: //coords
433: c = new GridBagConstraints();
434: c.gridx = 0;
435: c.gridy = 0;
436: layout.setConstraints(first, c);
437: c.gridx = GridBagConstraints.RELATIVE;
438: c.gridy = GridBagConstraints.RELATIVE;
439: frame.add(second, c);
440: layout.invalidateLayout(frame);
441: layout.layoutContainer(frame);
442: frame.setSize(frame.getPreferredSize().width, frame
443: .getPreferredSize().height);
444: frame.validate();
445: expected.setLocation(insets.left, insets.top);
446: assertEquals(expected, first.getBounds());
447: expected.translate(first.getPreferredSize().width, 0);
448: expected.setSize(second.getPreferredSize());
449: assertEquals(expected, second.getBounds());
450:
451: c.gridx = 1;
452: c.gridy = 1;
453: layout.setConstraints(second, c);
454: layout.invalidateLayout(frame);
455: layout.layoutContainer(frame);
456: frame.setSize(frame.getPreferredSize().width, frame
457: .getPreferredSize().height);
458: frame.validate();
459: expected.translate(0, first.getPreferredSize().height);
460: assertEquals(expected, second.getBounds());
461:
462: //size
463: frame.remove(first);
464: frame.remove(second);
465: layout = new GridBagLayout();
466: frame.setLayout(layout);
467:
468: Button third = new Button("Third");
469: third.setFont(new Font("dialog", Font.PLAIN, 50));
470:
471: c = new GridBagConstraints();
472: c.fill = GridBagConstraints.BOTH;
473: c.weightx = 1;
474: c.weighty = 1;
475: c.gridheight = GridBagConstraints.RELATIVE;
476: frame.add(first, c);
477: c.gridheight = GridBagConstraints.REMAINDER;
478: frame.add(second, c);
479: c.gridheight = 3;
480: frame.add(third, c);
481: frame.setSize(frame.getPreferredSize().width, frame
482: .getPreferredSize().height);
483: frame.validate();
484: Point p = new Point(insets.left, insets.top);
485: assertEquals(p, first.getLocation());
486: p.translate(0, first.getHeight());
487: assertEquals(p, second.getLocation());
488: p.translate(first.getWidth(), -first.getHeight());
489: assertEquals(p, third.getLocation());
490:
491: }
492:
493: private void doLayout() {
494: layout.invalidateLayout(frame);
495: layout.layoutContainer(frame);
496: frame.setSize(frame.getPreferredSize());
497: frame.validate();
498: }
499:
500: }
|