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: public class GridLayoutTest extends AWTTestCase {
023:
024: @SuppressWarnings("serial")
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 b1, b2, b3, b4, b5, b6;
037: private GridLayout 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 GridLayout();
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 GridLayoutTest(String name) {
058: super (name);
059:
060: Dimension min = new Dimension(MIN_SIZE, MIN_SIZE), pref = new Dimension(
061: PREF_SIZE, PREF_SIZE);
062: b1 = new TestButton("1", min, pref);
063: b2 = new TestButton("2", min, pref);
064: b3 = new TestButton("3", min, pref);
065: b4 = new TestButton("4", min, pref);
066: b5 = new TestButton("5", min, pref);
067: b6 = new TestButton("6", min, pref);
068: }
069:
070: public static void main(String[] args) {
071: junit.textui.TestRunner.run(BorderLayoutTest.class);
072: }
073:
074: public void testGridLayout() {
075: assertEquals(layout.getColumns(), 0);
076: assertEquals(layout.getRows(), 1);
077: assertEquals(layout.getHgap(), 0);
078: assertEquals(layout.getVgap(), 0);
079: }
080:
081: public void testGridLayoutintint() {
082: layout = new GridLayout(2, 3);
083: assertEquals(layout.getColumns(), 3);
084: assertEquals(layout.getRows(), 2);
085: assertEquals(layout.getHgap(), 0);
086: assertEquals(layout.getVgap(), 0);
087:
088: boolean bothZero = false;
089: try {
090: layout = new GridLayout(0, 0);
091: } catch (IllegalArgumentException e) {
092: bothZero = true;
093: }
094: assertTrue(bothZero);
095: }
096:
097: public void testGridLayoutintintintint() {
098: layout = new GridLayout(2, 3, 10, 20);
099: assertEquals(layout.getColumns(), 3);
100: assertEquals(layout.getRows(), 2);
101: assertEquals(layout.getHgap(), 10);
102: assertEquals(layout.getVgap(), 20);
103:
104: boolean bothZero = false;
105: try {
106: layout = new GridLayout(0, 0);
107: } catch (IllegalArgumentException e) {
108: bothZero = true;
109: }
110: assertTrue(bothZero);
111: }
112:
113: public void testToString() {
114: layout = new GridLayout(1, 2, 3, 4);
115: assertTrue(new String(
116: "java.awt.GridLayout[hgap=3,vgap=4,rows=1,cols=2]")
117: .equals(layout.toString()));
118: }
119:
120: public final void testGetSetHgap() {
121: layout.setHgap(10);
122: assertEquals(layout.getHgap(), 10);
123: layout.setHgap(-1);
124: assertEquals(layout.getHgap(), -1);
125: }
126:
127: public final void testGetSetVgap() {
128: layout.setVgap(10);
129: assertEquals(layout.getVgap(), 10);
130: layout.setVgap(-1);
131: assertEquals(layout.getVgap(), -1);
132: }
133:
134: public void testGetSetColumns() {
135: layout = new GridLayout(2, 3);
136: layout.setColumns(10);
137: assertEquals(layout.getColumns(), 10);
138:
139: boolean bothZero = false;
140: try {
141: layout.setRows(0);
142: layout.setColumns(0);
143: } catch (IllegalArgumentException e) {
144: bothZero = true;
145: }
146: assertTrue(bothZero);
147: }
148:
149: public void testGetSetRows() {
150: layout = new GridLayout(2, 3);
151: layout.setRows(10);
152: assertEquals(layout.getRows(), 10);
153:
154: boolean bothZero = false;
155: try {
156: layout.setColumns(0);
157: layout.setRows(0);
158: } catch (IllegalArgumentException e) {
159: bothZero = true;
160: }
161: assertTrue(bothZero);
162: }
163:
164: public void testAddLayoutComponent() {
165: Container c = new Container();
166: c.setSize(200, 20);
167: c.add(b1);
168: c.add(b2);
169: layout.addLayoutComponent("", b3);
170: assertEquals(new Dimension(), b1.getSize());
171: assertEquals(new Dimension(), b2.getSize());
172: assertEquals(new Dimension(), b3.getSize());
173: layout.layoutContainer(c);
174: Dimension expected = new Dimension(c.getWidth()
175: / c.getComponentCount(), c.getHeight());
176: assertEquals(expected, b1.getSize());
177: assertEquals(expected, b2.getSize());
178: // verify that addLayoutComponent has no effect:
179: assertEquals(new Dimension(), b3.getSize());
180: }
181:
182: public void testRemoveLayoutComponent() {
183: Container c = new Container();
184: c.setSize(200, 20);
185: c.add(b1);
186: c.add(b2);
187: layout.removeLayoutComponent(b2);
188: assertEquals(new Dimension(), b1.getSize());
189: assertEquals(new Dimension(), b2.getSize());
190: layout.layoutContainer(c);
191: Dimension expected = new Dimension(c.getWidth()
192: / c.getComponentCount(), c.getHeight());
193: assertEquals(expected, b1.getSize());
194: // verify that removeLayoutComponent has no effect:
195: assertEquals(expected, b2.getSize());
196: }
197:
198: public void testMinimumLayoutSize() {
199: Dimension expected = new Dimension();
200: Insets insets = frame.getInsets();
201:
202: assertEquals(layout.minimumLayoutSize(frame), new Dimension(
203: insets.left + insets.right, insets.top + insets.bottom));
204:
205: layout.setColumns(0);
206: layout.setRows(2);
207: layout.setHgap(10);
208: layout.setVgap(20);
209: frame.add(b1);
210: frame.add(b2);
211: frame.add(b3);
212: frame.add(b4);
213: b5.setFont(new Font("dialog", Font.PLAIN, 20));
214: frame.add(b5);
215: frame.add(b6);
216: frame.validate();
217:
218: expected.width = b5.getMinimumSize().width * 6 / 2
219: + layout.getHgap() * (6 / 2 - 1) + insets.left
220: + insets.right;
221: expected.height = b5.getMinimumSize().height * 6 / 3
222: + layout.getVgap() * (6 / 3 - 1) + insets.top
223: + insets.bottom;
224: assertEquals(layout.minimumLayoutSize(frame), expected);
225: }
226:
227: public void testPreferredLayoutSize() {
228: Dimension expected = new Dimension();
229: Insets insets = frame.getInsets();
230:
231: assertEquals(layout.preferredLayoutSize(frame), new Dimension(
232: insets.left + insets.right, insets.top + insets.bottom));
233:
234: layout.setColumns(0);
235: layout.setRows(2);
236: layout.setHgap(10);
237: layout.setVgap(20);
238: frame.add(b1);
239: frame.add(b2);
240: frame.add(b3);
241: frame.add(b4);
242: b5.setFont(new Font("dialog", Font.PLAIN, 20));
243: frame.add(b5);
244: frame.add(b6);
245: frame.validate();
246:
247: expected.width = b5.getPreferredSize().width * 6 / 2
248: + layout.getHgap() * (6 / 2 - 1) + insets.left
249: + insets.right;
250: expected.height = b5.getPreferredSize().height * 6 / 3
251: + layout.getVgap() * (6 / 3 - 1) + insets.top
252: + insets.bottom;
253: assertEquals(layout.preferredLayoutSize(frame), expected);
254: }
255:
256: public void testLayoutContainer() {
257:
258: Insets insets = frame.getInsets();
259:
260: frame.add(b1);
261: frame.add(b2);
262: frame.add(b3);
263: frame.add(b4);
264: frame.add(b5);
265: frame.add(b6);
266: b5.setFont(new Font("dialog", Font.PLAIN, 20));
267: layout.setHgap(10);
268: layout.setVgap(20);
269: layout.setColumns(0);
270: layout.setRows(2);
271: frame.setSize(insets.left + insets.right + 320, insets.top
272: + insets.bottom + 220);
273: frame.validate();
274:
275: assertEquals(b1.getBounds(), new Rectangle(insets.left,
276: insets.top, 100, 100));
277: assertEquals(b6.getBounds(), new Rectangle(insets.left + 220,
278: insets.top + 120, 100, 100));
279:
280: frame
281: .setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
282: frame.validate();
283: assertEquals(b1.getBounds(), new Rectangle(insets.left + 220,
284: insets.top, 100, 100));
285: assertEquals(b6.getBounds(), new Rectangle(insets.left,
286: insets.top + 120, 100, 100));
287:
288: frame
289: .setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
290: layout.setColumns(1);
291: layout.setRows(0);
292: frame.setSize(insets.left + insets.right + 200, insets.top
293: + insets.bottom + 700);
294: frame.validate();
295: assertEquals(b1.getBounds(), new Rectangle(insets.left,
296: insets.top, 200, 100));
297: assertEquals(b6.getBounds(), new Rectangle(insets.left,
298: insets.top + 600, 200, 100));
299: }
300:
301: }
|