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