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 Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import junit.framework.TestCase;
023:
024: public class GridBagLayoutRTest extends TestCase {
025:
026: static Panel buttonsPanel = new Panel();
027: static Panel tabbedPane = new Panel();
028:
029: public static void main(String[] args) {
030: junit.textui.TestRunner.run(GridBagLayoutRTest.class);
031: }
032:
033: @Override
034: protected void setUp() throws Exception {
035: super .setUp();
036: }
037:
038: public final void testLayoutContainer() {
039: Panel dialogPanel = getDialogPanel();
040: dialogPanel.setSize(542, 444);
041: dialogPanel.doLayout();
042: double maxX = buttonsPanel.getBounds().getMaxX();
043: assertEquals(maxX, tabbedPane.getBounds().getMaxX(), 0.0);
044:
045: }
046:
047: private static Panel getDialogPanel() {
048: GridBagConstraints gridBagConstraints182 = new GridBagConstraints();
049: GridBagConstraints gridBagConstraints172 = new GridBagConstraints();
050: Panel dialogPanel = new Panel();
051: dialogPanel.setLayout(new GridBagLayout());
052: dialogPanel.setSize(532, 389);
053: gridBagConstraints172.gridx = 0;
054: gridBagConstraints172.gridy = 1;
055: gridBagConstraints172.anchor = java.awt.GridBagConstraints.EAST;
056: gridBagConstraints172.gridwidth = 3;
057: gridBagConstraints182.gridx = 0;
058: gridBagConstraints182.gridy = 0;
059: gridBagConstraints182.weightx = 1.0;
060: gridBagConstraints182.weighty = 1.0;
061: gridBagConstraints182.fill = java.awt.GridBagConstraints.BOTH;
062: buttonsPanel.setPreferredSize(new java.awt.Dimension(170, 40));
063: tabbedPane.setPreferredSize(new java.awt.Dimension(100, 100));
064: dialogPanel.add(tabbedPane, gridBagConstraints182);
065: dialogPanel.add(buttonsPanel, gridBagConstraints172);
066: return dialogPanel;
067: }
068:
069: public final void testPreferredLayoutSize() {
070: GridBagLayout gbl = new GridBagLayout();
071: Panel p = new Panel(gbl);
072: GridBagConstraints gbc = new GridBagConstraints();
073: gbc.gridwidth = GridBagConstraints.REMAINDER;
074: gbc.weightx = 1.0;
075: gbc.weighty = 1.0;
076: gbc.fill = GridBagConstraints.BOTH;
077: Button big = new Button("Big button");
078: Button b1 = new Button("b1");
079: Button b2 = new Button("b2");
080: big.setPreferredSize(new Dimension(500, 15));
081: big.setMinimumSize(new Dimension(500, 15));
082: b1.setPreferredSize(new Dimension(50, 15));
083: b1.setMinimumSize(new Dimension(30, 10));
084: b2.setPreferredSize(new Dimension(50, 15));
085: b2.setMinimumSize(new Dimension(30, 10));
086: p.add(big, gbc);
087: gbc.gridwidth = 1;
088: gbc.weightx = 0.75;
089: p.add(b1, gbc);
090: gbc.weightx = 0.25;
091: p.add(b2, gbc);
092: assertEquals(big.getPreferredSize().width, gbl
093: .preferredLayoutSize(p).width);
094: assertEquals(2 * b1.getPreferredSize().height, gbl
095: .preferredLayoutSize(p).height);
096:
097: }
098:
099: public final void testAddLayoutComponent() {
100: GridBagLayout gbl = new GridBagLayout();
101: try {
102: gbl.addLayoutComponent(null, new Object());
103: } catch (IllegalArgumentException iae) {
104: return;
105: }
106: fail();
107: }
108:
109: public final void testInvalidate() {
110: GridBagLayout gbl = new GridBagLayout();
111: gbl.invalidateLayout(null);
112: }
113:
114: static void addToGridBag(Panel panel, Component comp, int x, int y,
115: int w, int h, double weightx, double weighty) {
116: GridBagLayout gbl = (GridBagLayout) panel.getLayout();
117: GridBagConstraints c = new GridBagConstraints();
118: c.fill = GridBagConstraints.BOTH;
119: c.gridx = x;
120: c.gridy = y;
121: c.gridwidth = w;
122: c.gridheight = h;
123: c.weightx = weightx;
124: c.weighty = weighty;
125: panel.add(comp);
126: gbl.setConstraints(comp, c);
127: }
128:
129: public final void testZeroWeights() {
130: Dimension minSize = new Dimension(10, 25);
131: final Panel p = new Panel(new GridBagLayout());
132: addToGridBag(p, new Button(), 0, 0, 1, 1, 0, 0);
133: addToGridBag(p, new Button(), 0, 1, 1, 1, 0, 0);
134: addToGridBag(p, new Button(), 0, 2, 1, 1, 0, 0);
135: for (int i = 0; i < p.getComponentCount(); i++) {
136: p.getComponent(i).setMinimumSize(minSize);
137: }
138: Dimension size = new Dimension(minSize);
139: size.height -= 15;
140: p.setSize(size);
141: p.doLayout();
142: assertEquals("first component has zero height", 0, p
143: .getComponent(0).getHeight());
144: assertEquals("second component has height < min", 18, p
145: .getComponent(1).getHeight());
146: assertEquals("third component has min height", minSize.height,
147: p.getComponent(2).getHeight());
148: }
149: }
|