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 BorderLayoutTest extends AWTTestCase {
024:
025: class TestButton extends Button {
026: TestButton(String name, Dimension min, Dimension pref) {
027: super (name);
028:
029: setMinimumSize(min);
030: setPreferredSize(pref);
031: }
032: }
033:
034: private final int MIN_SIZE = 50;
035: private final int PREF_SIZE = 100;
036: private final TestButton n, s, w, c, e;
037: private BorderLayout layout;
038: private Frame frame;
039:
040: @Override
041: protected void setUp() throws Exception {
042: super .setUp();
043:
044: frame = new Frame();
045: layout = new BorderLayout();
046: frame.setLayout(layout);
047: frame.setVisible(true);
048: }
049:
050: @Override
051: protected void tearDown() throws Exception {
052: frame.dispose();
053:
054: super .tearDown();
055: }
056:
057: public BorderLayoutTest() {
058: Dimension min = new Dimension(MIN_SIZE, MIN_SIZE), pref = new Dimension(
059: PREF_SIZE, PREF_SIZE);
060: n = new TestButton("n", min, pref);
061: s = new TestButton("s", min, pref);
062: c = new TestButton("c", min, pref);
063: w = new TestButton("w", min, pref);
064: e = new TestButton("e", min, pref);
065: }
066:
067: public final void testBorderLayoutintint() {
068: BorderLayout layout = new BorderLayout(10, 5);
069: assertEquals(layout.getHgap(), 10);
070: assertEquals(layout.getVgap(), 5);
071: }
072:
073: public final void testBorderLayout() {
074: BorderLayout layout = new BorderLayout();
075: assertEquals(layout.getHgap(), 0);
076: assertEquals(layout.getVgap(), 0);
077: }
078:
079: public final void testToString() {
080: BorderLayout layout = new BorderLayout(30, 300);
081: assertTrue(new String("java.awt.BorderLayout[hgap=30,vgap=300]")
082: .equals(layout.toString()));
083: }
084:
085: public final void testAddLayoutComponentComponentObject() {
086: frame.add(n, BorderLayout.NORTH);
087: frame.add(s, BorderLayout.SOUTH);
088: frame.add(w, BorderLayout.WEST);
089: frame.add(c, BorderLayout.CENTER);
090: frame.add(e, BorderLayout.EAST);
091: frame.remove(c);
092: frame.add(c);
093:
094: frame.remove(n);
095: frame.remove(s);
096: frame.remove(w);
097: frame.remove(c);
098:
099: TestButton t = new TestButton("", null, null);
100:
101: boolean notString = false;
102: boolean wrongString = false;
103: boolean oneTwice = false;
104: try {
105: frame.add(t, new Integer(5));
106: } catch (IllegalArgumentException e) {
107: notString = true;
108: }
109: try {
110: frame.add(t, "Dummy");
111: } catch (IllegalArgumentException e) {
112: wrongString = true;
113: }
114: try {
115: layout.addLayoutComponent(e, BorderLayout.WEST);
116: } catch (IllegalArgumentException e) {
117: oneTwice = true;
118: }
119: assertTrue(notString);
120: assertTrue(wrongString);
121: assertFalse(oneTwice);
122:
123: // Regression test HARMONY-1667
124: try {
125: layout.addLayoutComponent(null, BorderLayout.CENTER);
126: fail("Expected NPE");
127: } catch (NullPointerException e) {
128: // expected
129: }
130: }
131:
132: public final void testRemoveLayoutComponent() {
133: frame.add(n, BorderLayout.NORTH);
134: frame.add(s, BorderLayout.SOUTH);
135: frame.add(w, BorderLayout.WEST);
136: frame.add(c, BorderLayout.CENTER);
137: frame.add(e, BorderLayout.EAST);
138:
139: frame.removeAll();
140: }
141:
142: public final void testGetSetHgap() {
143: layout.setHgap(10);
144: assertEquals(layout.getHgap(), 10);
145: layout.setHgap(-1);
146: assertEquals(layout.getHgap(), -1);
147: }
148:
149: public final void testGetSetVgap() {
150: layout.setVgap(10);
151: assertEquals(layout.getVgap(), 10);
152: layout.setVgap(-1);
153: assertEquals(layout.getVgap(), -1);
154: }
155:
156: public final void testGetLayoutAlignmentX() {
157: assertTrue(layout.getLayoutAlignmentX(frame) == Component.CENTER_ALIGNMENT);
158: }
159:
160: public final void testGetLayoutAlignmentY() {
161: assertTrue(layout.getLayoutAlignmentY(frame) == Component.CENTER_ALIGNMENT);
162: }
163:
164: public final void testMaximumLayoutSize() {
165: assertEquals(layout.maximumLayoutSize(frame), new Dimension(
166: Integer.MAX_VALUE, Integer.MAX_VALUE));
167: }
168:
169: public final void testMinimumLayoutSize() {
170: Dimension expected = new Dimension();
171: Insets insets = frame.getInsets();
172:
173: assertEquals(layout.minimumLayoutSize(frame), new Dimension(
174: insets.left + insets.right, insets.top + insets.bottom));
175:
176: frame.add(n, BorderLayout.NORTH);
177: frame.add(s, BorderLayout.SOUTH);
178: frame.add(w, BorderLayout.WEST);
179: frame.add(c, BorderLayout.CENTER);
180: c.setFont(new Font("dialog", Font.PLAIN, 20));
181: frame.add(e, BorderLayout.EAST);
182: layout.setVgap(10);
183: layout.setHgap(20);
184:
185: expected.width = Math.max(n.getMinimumSize().width, Math.max(s
186: .getMinimumSize().width, w.getMinimumSize().width
187: + c.getMinimumSize().width + e.getMinimumSize().width
188: + 2 * layout.getHgap()))
189: + insets.left + insets.right;
190: expected.height = n.getMinimumSize().height
191: + s.getMinimumSize().height
192: + Math.max(c.getMinimumSize().height, Math.max(w
193: .getMinimumSize().height,
194: e.getMinimumSize().height)) + 2
195: * layout.getVgap() + insets.bottom + insets.top;
196: assertEquals(layout.minimumLayoutSize(frame), expected);
197:
198: frame.remove(s);
199: frame.validate();
200: expected.width = Math.max(n.getMinimumSize().width, Math.max(0,
201: w.getMinimumSize().width + c.getMinimumSize().width
202: + e.getMinimumSize().width + 2
203: * layout.getHgap()))
204: + insets.left + insets.right;
205: expected.height = n.getMinimumSize().height
206: + Math.max(c.getMinimumSize().height, Math.max(w
207: .getMinimumSize().height,
208: e.getMinimumSize().height)) + layout.getVgap()
209: + insets.bottom + insets.top;
210: assertEquals(layout.minimumLayoutSize(frame), expected);
211: frame.add(s, BorderLayout.SOUTH);
212:
213: frame.remove(c);
214: expected.width = Math.max(n.getMinimumSize().width, Math.max(s
215: .getMinimumSize().width, w.getMinimumSize().width
216: + e.getMinimumSize().width + layout.getHgap()))
217: + insets.left + insets.right;
218: expected.height = n.getMinimumSize().height
219: + s.getMinimumSize().height
220: + Math.max(c.getMinimumSize().height, e
221: .getMinimumSize().height) + 2
222: * layout.getVgap() + insets.bottom + insets.top;
223: assertEquals(layout.minimumLayoutSize(frame), expected);
224:
225: frame.remove(w);
226: frame.remove(e);
227: expected.width = Math.max(n.getMinimumSize().width, s
228: .getMinimumSize().width)
229: + insets.left + insets.right;
230: expected.height = n.getMinimumSize().height
231: + s.getMinimumSize().height + layout.getVgap()
232: + insets.bottom + insets.top;
233: assertEquals(layout.minimumLayoutSize(frame), expected);
234: }
235:
236: public final void testPreferredLayoutSize() {
237: Dimension expected = new Dimension();
238: Insets insets = frame.getInsets();
239:
240: assertEquals(layout.preferredLayoutSize(frame), new Dimension(
241: insets.left + insets.right, insets.top + insets.bottom));
242:
243: frame.add(n, BorderLayout.NORTH);
244: frame.add(s, BorderLayout.SOUTH);
245: frame.add(w, BorderLayout.WEST);
246: frame.add(c, BorderLayout.CENTER);
247: c.setFont(new Font("dialog", Font.PLAIN, 20));
248: frame.add(e, BorderLayout.EAST);
249: layout.setVgap(10);
250: layout.setHgap(20);
251:
252: expected.width = Math.max(n.getPreferredSize().width, Math.max(
253: s.getPreferredSize().width, w.getPreferredSize().width
254: + c.getPreferredSize().width
255: + e.getPreferredSize().width + 2
256: * layout.getHgap()))
257: + insets.left + insets.right;
258: expected.height = n.getPreferredSize().height
259: + s.getPreferredSize().height
260: + Math.max(c.getPreferredSize().height, Math.max(w
261: .getPreferredSize().height, e
262: .getPreferredSize().height)) + 2
263: * layout.getVgap() + insets.bottom + insets.top;
264: assertEquals(layout.preferredLayoutSize(frame), expected);
265:
266: frame.remove(s);
267: frame.validate();
268: expected.width = Math.max(n.getPreferredSize().width, Math.max(
269: 0, w.getPreferredSize().width
270: + c.getPreferredSize().width
271: + e.getPreferredSize().width + 2
272: * layout.getHgap()))
273: + insets.left + insets.right;
274: expected.height = n.getPreferredSize().height
275: + Math.max(c.getPreferredSize().height, Math.max(w
276: .getPreferredSize().height, e
277: .getPreferredSize().height)) + layout.getVgap()
278: + insets.bottom + insets.top;
279: assertEquals(layout.preferredLayoutSize(frame), expected);
280: frame.add(s, BorderLayout.SOUTH);
281:
282: frame.remove(c);
283: expected.width = Math.max(n.getPreferredSize().width, Math
284: .max(s.getPreferredSize().width,
285: w.getPreferredSize().width
286: + e.getPreferredSize().width
287: + layout.getHgap()))
288: + insets.left + insets.right;
289: expected.height = n.getPreferredSize().height
290: + s.getPreferredSize().height
291: + Math.max(c.getPreferredSize().height, e
292: .getPreferredSize().height) + 2
293: * layout.getVgap() + insets.bottom + insets.top;
294: assertEquals(layout.preferredLayoutSize(frame), expected);
295:
296: frame.remove(w);
297: frame.remove(e);
298: expected.width = Math.max(n.getPreferredSize().width, s
299: .getPreferredSize().width)
300: + insets.left + insets.right;
301: expected.height = n.getPreferredSize().height
302: + s.getPreferredSize().height + layout.getVgap()
303: + insets.bottom + insets.top;
304: assertEquals(layout.preferredLayoutSize(frame), expected);
305: }
306:
307: public final void testLayoutContainer() {
308:
309: Insets insets = frame.getInsets();
310:
311: frame.add(n, BorderLayout.NORTH);
312: frame.add(s, BorderLayout.SOUTH);
313: frame.add(w, BorderLayout.WEST);
314: frame.add(c, BorderLayout.CENTER);
315: c.setFont(new Font("dialog", Font.PLAIN, 20));
316: frame.add(e, BorderLayout.EAST);
317: layout.setHgap(10);
318: layout.setVgap(20);
319: frame.validate();
320:
321: int x = insets.left;
322: int y = insets.top;
323: int midH = Math.max(c.getPreferredSize().height, Math
324: .max(w.getPreferredSize().height,
325: e.getPreferredSize().height));
326: frame.setSize(layout.preferredLayoutSize(frame));
327: frame.validate();
328: assertEquals(n.getBounds(), new Rectangle(x, y,
329: frame.getSize().width - insets.left - insets.right, n
330: .getPreferredSize().height));
331: assertEquals(s.getBounds(), new Rectangle(x,
332: frame.getSize().height - insets.bottom
333: - s.getPreferredSize().height,
334: frame.getSize().width - insets.left - insets.right, s
335: .getPreferredSize().height));
336: y += n.getBounds().height + layout.getVgap();
337: assertEquals(w.getBounds(), new Rectangle(x, y, w
338: .getPreferredSize().width, midH));
339: x += w.getBounds().width + layout.getHgap();
340: assertEquals(c.getBounds(), new Rectangle(x, y,
341: frame.getSize().width - insets.right - insets.left
342: - w.getPreferredSize().width
343: - e.getPreferredSize().width - 2
344: * layout.getHgap(), midH));
345: x += c.getBounds().width + layout.getHgap();
346: assertEquals(e.getBounds(), new Rectangle(x, y, e
347: .getPreferredSize().width, midH));
348:
349: frame.setSize(frame.getSize().width, insets.top
350: + insets.bottom
351: + Math.max(s.getPreferredSize().height, n
352: .getPreferredSize().height) + 1);
353: frame.validate();
354: x = insets.left;
355: y = insets.top;
356: midH = Math.max(c.getPreferredSize().height, Math
357: .max(w.getPreferredSize().height,
358: e.getPreferredSize().height));
359: frame.setSize(layout.preferredLayoutSize(frame));
360: frame.validate();
361: assertEquals(n.getBounds(), new Rectangle(x, y,
362: frame.getSize().width - insets.left - insets.right, n
363: .getPreferredSize().height));
364: assertEquals(s.getBounds(), new Rectangle(x,
365: frame.getSize().height - insets.bottom
366: - s.getPreferredSize().height,
367: frame.getSize().width - insets.left - insets.right, s
368: .getPreferredSize().height));
369:
370: x = insets.left;
371: y = insets.top;
372: midH = Math.max(c.getPreferredSize().height, Math
373: .max(w.getPreferredSize().height,
374: e.getPreferredSize().height)) + 1;
375: frame.setSize(layout.preferredLayoutSize(frame).width + 1,
376: layout.preferredLayoutSize(frame).height + 1);
377: frame.validate();
378: assertEquals(n.getBounds(), new Rectangle(x, y,
379: frame.getSize().width - insets.left - insets.right, n
380: .getPreferredSize().height));
381: assertEquals(s.getBounds(), new Rectangle(x,
382: frame.getSize().height - insets.bottom
383: - s.getPreferredSize().height,
384: frame.getSize().width - insets.left - insets.right, s
385: .getPreferredSize().height));
386: y += n.getBounds().height + layout.getVgap();
387: assertEquals(w.getBounds(), new Rectangle(x, y, w
388: .getPreferredSize().width, midH));
389: x += w.getBounds().width + layout.getHgap();
390: assertEquals(c.getBounds(), new Rectangle(x, y,
391: frame.getSize().width - insets.right - insets.left
392: - w.getPreferredSize().width
393: - e.getPreferredSize().width - 2
394: * layout.getHgap(), midH));
395: x += c.getBounds().width + layout.getHgap();
396: assertEquals(e.getBounds(), new Rectangle(x, y, e
397: .getPreferredSize().width, midH));
398:
399: x = insets.left;
400: y = insets.top;
401: midH = Math.max(c.getPreferredSize().height, Math
402: .max(w.getPreferredSize().height,
403: e.getPreferredSize().height));
404: frame.remove(s);
405: frame.setSize(layout.preferredLayoutSize(frame));
406: frame.validate();
407: assertEquals(n.getBounds(), new Rectangle(x, y,
408: frame.getSize().width - insets.left - insets.right, n
409: .getPreferredSize().height));
410: y += n.getBounds().height + layout.getVgap();
411: assertEquals(w.getBounds(), new Rectangle(x, y, w
412: .getPreferredSize().width, midH));
413: x += w.getBounds().width + layout.getHgap();
414: assertEquals(c.getBounds(), new Rectangle(x, y,
415: frame.getSize().width - insets.right - insets.left
416: - w.getPreferredSize().width
417: - e.getPreferredSize().width - 2
418: * layout.getHgap(), midH));
419: x += c.getBounds().width + layout.getHgap();
420: assertEquals(e.getBounds(), new Rectangle(x, y, e
421: .getPreferredSize().width, midH));
422: frame.add(s, BorderLayout.SOUTH);
423:
424: x = insets.left;
425: y = insets.top;
426: midH = Math.max(c.getPreferredSize().height, e
427: .getPreferredSize().height);
428: frame.remove(w);
429: frame.setSize(layout.preferredLayoutSize(frame));
430: frame.validate();
431: assertEquals(n.getBounds(), new Rectangle(x, y,
432: frame.getSize().width - insets.left - insets.right, n
433: .getPreferredSize().height));
434: assertEquals(s.getBounds(), new Rectangle(x,
435: frame.getSize().height - insets.bottom
436: - s.getPreferredSize().height,
437: frame.getSize().width - insets.left - insets.right, s
438: .getPreferredSize().height));
439: y += n.getBounds().height + layout.getVgap();
440: assertEquals(c.getBounds(),
441: new Rectangle(x, y,
442: frame.getSize().width - insets.right
443: - insets.left
444: - e.getPreferredSize().width
445: - layout.getHgap(), midH));
446: x += c.getBounds().width + layout.getHgap();
447: assertEquals(e.getBounds(), new Rectangle(x, y, e
448: .getPreferredSize().width, midH));
449:
450: }
451:
452: public final void testGetLayoutComponentObject() {
453: frame.add(n, BorderLayout.NORTH);
454: frame.add(s, BorderLayout.SOUTH);
455: frame.add(w, BorderLayout.WEST);
456: frame.add(c, BorderLayout.CENTER);
457: frame.add(e, BorderLayout.EAST);
458:
459: assertEquals(layout.getLayoutComponent(BorderLayout.NORTH), n);
460: assertEquals(layout.getLayoutComponent(BorderLayout.SOUTH), s);
461: assertEquals(layout.getLayoutComponent(BorderLayout.WEST), w);
462: assertEquals(layout.getLayoutComponent(BorderLayout.CENTER), c);
463: assertEquals(layout.getLayoutComponent(BorderLayout.EAST), e);
464: assertNull(layout.getLayoutComponent(BorderLayout.PAGE_START));
465:
466: try {
467: layout.getLayoutComponent("Z");
468: fail();
469: } catch (IllegalArgumentException e) {
470: assertTrue(true);
471: }
472: }
473:
474: public final void testGetLayoutComponentContainerObject() {
475: frame.add(n, BorderLayout.NORTH);
476: frame.add(s, BorderLayout.SOUTH);
477: frame.add(w, BorderLayout.WEST);
478: frame.add(c, BorderLayout.CENTER);
479: frame.add(e, BorderLayout.EAST);
480:
481: assertEquals(n, layout.getLayoutComponent(frame,
482: BorderLayout.NORTH));
483: assertEquals(s, layout.getLayoutComponent(frame,
484: BorderLayout.SOUTH));
485: assertEquals(w, layout.getLayoutComponent(frame,
486: BorderLayout.WEST));
487: assertEquals(c, layout.getLayoutComponent(frame,
488: BorderLayout.CENTER));
489: assertEquals(e, layout.getLayoutComponent(frame,
490: BorderLayout.EAST));
491: try {
492: layout.getLayoutComponent(frame, BorderLayout.PAGE_START);
493: fail();
494: } catch (IllegalArgumentException e) {
495: assertTrue(true);
496: }
497: try {
498: layout.getLayoutComponent(frame, "Z");
499: fail();
500: } catch (IllegalArgumentException e) {
501: assertTrue(true);
502: }
503: frame.removeAll();
504: frame.add(n, BorderLayout.NORTH);
505: frame.add(s, BorderLayout.PAGE_END);
506: frame.add(new Button("q"), BorderLayout.SOUTH);
507: frame.add(w, BorderLayout.LINE_START);
508: frame.add(c, BorderLayout.CENTER);
509: frame.add(e, BorderLayout.LINE_END);
510: frame
511: .setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
512: assertEquals(n, layout.getLayoutComponent(frame,
513: BorderLayout.NORTH));
514: assertEquals(s, layout.getLayoutComponent(frame,
515: BorderLayout.SOUTH));
516: assertEquals(w, layout.getLayoutComponent(frame,
517: BorderLayout.EAST));
518: assertEquals(c, layout.getLayoutComponent(frame,
519: BorderLayout.CENTER));
520: assertEquals(e, layout.getLayoutComponent(frame,
521: BorderLayout.WEST));
522:
523: }
524:
525: public final void testGetConstraintsComponent() {
526: frame.add(n, BorderLayout.NORTH);
527: frame.add(s, BorderLayout.SOUTH);
528: frame.add(w, BorderLayout.WEST);
529: frame.add(c, BorderLayout.CENTER);
530: frame.add(e, BorderLayout.EAST);
531:
532: assertEquals(BorderLayout.NORTH, layout.getConstraints(n));
533: assertEquals(BorderLayout.SOUTH, layout.getConstraints(s));
534: assertEquals(BorderLayout.WEST, layout.getConstraints(w));
535: assertEquals(BorderLayout.CENTER, layout.getConstraints(c));
536: assertEquals(BorderLayout.EAST, layout.getConstraints(e));
537: assertNull(layout.getConstraints(null));
538: assertNull(layout.getConstraints(new Button("Z")));
539: }
540:
541: }
|