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 Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Point;
023: import java.awt.Rectangle;
024: import java.beans.PropertyVetoException;
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027: import java.io.InputStream;
028: import java.io.ObjectInputStream;
029: import java.io.ObjectOutputStream;
030:
031: public class DefaultDesktopManagerTest extends SwingTestCase {
032: private DefaultDesktopManager manager;
033:
034: private JDesktopPane desktop;
035:
036: private JInternalFrame frame;
037:
038: private JFrame rootFrame;
039:
040: public DefaultDesktopManagerTest(final String name) {
041: super (name);
042: }
043:
044: /*
045: * Creates and shows rootFrame. This method is used when JInternalFrame
046: * need to be selected (isSelected() == true) for testing purposes.
047: */
048: protected void createAndShowRootFrame() {
049: frame.setSize(70, 100);
050: rootFrame = new JFrame();
051: rootFrame.setContentPane(desktop);
052: //rootFrame.getContentPane().add(frame);
053: rootFrame.setSize(100, 200);
054: frame.setVisible(true);
055: rootFrame.setVisible(true);
056: }
057:
058: /*
059: * @see TestCase#setUp()
060: */
061: @Override
062: protected void setUp() throws Exception {
063: super .setUp();
064: desktop = new JDesktopPane();
065: manager = new DefaultDesktopManager();
066: desktop.setDesktopManager(manager);
067: frame = new JInternalFrame();
068: desktop.add(frame);
069: }
070:
071: /*
072: * @see TestCase#tearDown()
073: */
074: @Override
075: protected void tearDown() throws Exception {
076: super .tearDown();
077: if (rootFrame != null) {
078: rootFrame.dispose();
079: rootFrame = null;
080: }
081: }
082:
083: /*
084: * Class under test for void DefaultDesktopManager()
085: */
086: public void testDefaultDesktopManager() {
087: // nothing to test
088: }
089:
090: /*
091: * Class under test for
092: * void setIcon(JInternalFrame, Boolean)
093: * boolean wasIcon(JInternalFrame)
094: */
095: public void testSetWasIcon() {
096: desktop.setDesktopManager(manager);
097: assertFalse("wasIcon is false by default", manager
098: .wasIcon(frame));
099: // test set to true
100: manager.setWasIcon(frame, Boolean.TRUE);
101: assertTrue("wasIcon is set to true", manager.wasIcon(frame));
102: // test set to false
103: manager.setWasIcon(frame, Boolean.FALSE);
104: assertFalse("wasIcon is set to false", manager.wasIcon(frame));
105: }
106:
107: /*
108: * Class under test for
109: * void setPreviousBounds(JInternalFrame, Rectangle)
110: * Rectangle getPreviousBounds(JInternalFrame)
111: */
112: public void testSetGetPreviousBounds() {
113: Rectangle bounds = new Rectangle(1, 1, 12, 13);
114: desktop.setDesktopManager(manager);
115: manager.setPreviousBounds(frame, bounds);
116: assertTrue("bounds are set",
117: manager.getPreviousBounds(frame) == bounds);
118: assertTrue("bounds are set as normal bounds", frame
119: .getNormalBounds() == bounds);
120: }
121:
122: /*
123: * Class under test for void getBoundsForIconOf(JInternalFrame)
124: */
125: public void testGetBoundsForIconOf() {
126: final int totalWidth = 330;
127: final int totalHeight = 65;
128: desktop.setDesktopManager(manager);
129: desktop.setSize(totalWidth, totalHeight);
130: JInternalFrame frame2 = new JInternalFrame("frame 2");
131: desktop.add(frame2);
132: JInternalFrame frame3 = new JInternalFrame("frame 3");
133: desktop.add(frame3);
134: desktop.add(new JPanel());
135: int width = frame.getDesktopIcon().getWidth();
136: int height = frame.getDesktopIcon().getHeight();
137: Rectangle bounds = manager.getBoundsForIconOf(frame);
138: assertEquals("x", bounds.x, 0);
139: assertEquals("y", bounds.y, totalHeight - height);
140: assertEquals("width", bounds.width, width);
141: assertEquals("height", bounds.height, height);
142: width = frame2.getDesktopIcon().getWidth();
143: height = frame2.getDesktopIcon().getHeight();
144: manager.iconifyFrame(frame);
145: manager.iconifyFrame(frame);
146: bounds = manager.getBoundsForIconOf(frame2);
147: assertEquals("x", bounds.x, width);
148: assertEquals("y", bounds.y, totalHeight - height);
149: assertEquals("width", bounds.width, width);
150: assertEquals("height", bounds.height, height);
151: width = frame3.getDesktopIcon().getWidth();
152: height = frame3.getDesktopIcon().getHeight();
153: manager.iconifyFrame(frame2);
154: bounds = manager.getBoundsForIconOf(frame3);
155: assertEquals("x", bounds.x, 0);
156: assertEquals("y", bounds.y, totalHeight - 2 * height);
157: assertEquals("width", bounds.width, width);
158: assertEquals("height", bounds.height, height);
159: JInternalFrame frame4 = new JInternalFrame();
160: width = frame4.getDesktopIcon().getWidth();
161: height = frame4.getDesktopIcon().getHeight();
162: bounds = manager.getBoundsForIconOf(frame4);
163: assertEquals("x", bounds.x, 0);
164: assertEquals("y", bounds.y, 0);
165: assertEquals("width", bounds.width, width);
166: assertEquals("height", bounds.height, height);
167: }
168:
169: /*
170: * Class under test for void removeIconFor(JInternalFrame)
171: */
172: public void testRemoveIconFor() {
173: desktop.setDesktopManager(manager);
174: // test when f is not iconified
175: manager.removeIconFor(frame);
176: // test when f is iconified
177: manager.iconifyFrame(frame);
178: manager.removeIconFor(frame);
179: assertFalse("icon is removed", desktop.isAncestorOf(frame));
180: // test when the internal frame is not inside any desktop pane
181: manager.removeIconFor(new JInternalFrame());
182: }
183:
184: /*
185: * Class under test for void openFrame(JInternalFrame)
186: */
187: public void testOpenFrame() {
188: JInternalFrame frame2 = new JInternalFrame();
189: // test openFrame for the internal frame outside the container
190: manager.openFrame(frame2);
191: // test ordinary openFrame
192: desktop.add(frame2.getDesktopIcon());
193: manager.openFrame(frame2);
194: assertFalse("desktopIcon is removed", desktop
195: .isAncestorOf(frame2.getDesktopIcon()));
196: assertTrue("frame is added", desktop.isAncestorOf(frame2));
197: // test openFrame for the frame that is already opened
198: manager.openFrame(frame2);
199: }
200:
201: /*
202: * Class under test for void minimizeFrame(JInternalFrame)
203: */
204: public void testMinimizeFrame() {
205: desktop.setSize(100, 200);
206: Rectangle bounds = new Rectangle(1, 1, 50, 60);
207: frame.setBounds(bounds);
208: manager.maximizeFrame(frame);
209: // test ordinary minimize
210: manager.minimizeFrame(frame);
211: assertTrue("the bounds are restored", frame.getBounds().equals(
212: bounds));
213: assertFalse("the copy of bounds is used",
214: frame.getBounds() == bounds);
215: // test minimize the second time
216: manager.minimizeFrame(frame);
217: assertTrue("the bounds are restored", frame.getBounds().equals(
218: bounds));
219: // test minimize some separate frame
220: manager.minimizeFrame(new JInternalFrame());
221: }
222:
223: /*
224: * Class under test for maximizeFrame(JInternalFrame)
225: */
226: public void testMaximizeFrame() {
227: desktop.setSize(100, 200);
228: Rectangle bounds = new Rectangle(1, 1, 50, 60);
229: frame.setBounds(bounds);
230: // test simple maximize
231: manager.maximizeFrame(frame);
232: assertTrue("x ok", frame.getX() == 0);
233: assertTrue("y ok", frame.getY() == 0);
234: assertTrue("width ok", frame.getWidth() == desktop.getWidth());
235: assertTrue("height ok", frame.getHeight() == desktop
236: .getHeight());
237: assertTrue("normal bounds are saved", frame.getNormalBounds()
238: .equals(bounds));
239: assertFalse("bounds are save in a copy", frame
240: .getNormalBounds() == bounds);
241: // test maximize for the internal frame that is not inside of any
242: // desktop pane - NullPointernException
243: //manager.maximizeFrame(new JInternalFrame());
244: }
245:
246: /*
247: * Class under test for
248: * void iconifyFrame(JInternalFrame)
249: * void deiconifyFrame(JInternalFrame)
250: */
251: public void testIconifyDeiconifyFrame() {
252: desktop.setDesktopManager(manager);
253: frame.setLayer(1);
254: createAndShowRootFrame();
255: // test correct iconify
256: //try {
257: // SwingUtilities.invokeAndWait(new Runnable() {
258: // public void run() {
259: manager.activateFrame(frame);
260: manager.iconifyFrame(frame);
261: // }
262: // });
263: //} catch (Exception e) {
264: // System.out.println(e.getCause());
265: // assertFalse("exception", true);
266: //}
267: assertFalse("frame is removed", desktop.isAncestorOf(frame));
268: assertTrue("icon is added", desktop.isAncestorOf(frame
269: .getDesktopIcon()));
270: assertTrue("wasIcon is true", manager.wasIcon(frame));
271: assertTrue("layer is set", JLayeredPane.getLayer(frame
272: .getDesktopIcon()) == frame.getLayer());
273: //assertTrue("isIcon is true", frame.isIcon());
274: // test iconify for the frame that is already iconified
275: manager.iconifyFrame(frame);
276: // call with null argument causes to NullPointerException
277: // test correct deiconify
278: //try {
279: // SwingUtilities.invokeAndWait(new Runnable() {
280: // public void run() {
281: manager.deiconifyFrame(frame);
282: // }
283: // });
284: //} catch (Exception e) {
285: // assertFalse("exception", true);
286: //}
287: assertTrue("frame is added", desktop.isAncestorOf(frame));
288: assertFalse("icon is removed", desktop.isAncestorOf(frame
289: .getDesktopIcon()));
290: assertTrue("wasIcon is true", manager.wasIcon(frame));
291: // test deiconify for the deiconified frame
292: manager.deiconifyFrame(frame);
293: // test iconify for the internal frame without desktop pane
294: manager.iconifyFrame(new JInternalFrame());
295: }
296:
297: /*
298: * Class under test for void deactivateFrame(JInternalFrame)
299: */
300: public void testDeactivateFrame() {
301: manager.activateFrame(frame);
302: assertTrue("activated", desktop.getSelectedFrame() == frame);
303: // test ordinary deactivate
304: manager.deactivateFrame(frame);
305: assertNull("deactivated", desktop.getSelectedFrame());
306: // test deactivate of the internal frame without desktop pane
307: manager.deactivateFrame(new JInternalFrame());
308: }
309:
310: /*
311: * Class under test for void closeFrame(JInternalFrame)
312: */
313: public void testCloseFrame() {
314: // test ordinary close
315: manager.closeFrame(frame);
316: assertFalse("frame is removed", desktop.isAncestorOf(frame));
317: assertFalse("desktopIcon is removed", desktop
318: .isAncestorOf(frame.getDesktopIcon()));
319: // test close of the iconified internal frame
320: desktop.add(frame);
321: manager.iconifyFrame(frame);
322: manager.closeFrame(frame);
323: assertFalse("frame is removed", desktop.isAncestorOf(frame));
324: assertFalse("desktopIcon is removed", desktop
325: .isAncestorOf(frame.getDesktopIcon()));
326: // test close of the closed internal frame
327: manager.closeFrame(frame);
328: }
329:
330: /*
331: * Class under test for void activateFrame(JInternalFrame)
332: */
333: public void testActivateFrame() {
334: JInternalFrame frame2 = new JInternalFrame("frame2");
335: frame2.setVisible(true);
336: desktop.add(frame2);
337: JInternalFrame frame3 = new JInternalFrame("frame3");
338: frame3.setVisible(true);
339: frame3.setLayer(1);
340: desktop.add(frame3);
341: createAndShowRootFrame();
342: try {
343: frame3.setSelected(true);
344: } catch (PropertyVetoException e) {
345: assertFalse("exception", true);
346: }
347: assertTrue("frame3 is selected", frame3.isSelected());
348: // test activate
349: manager.activateFrame(frame);
350: assertTrue("moved to the front", desktop.getIndexOf(frame) == 1);
351: assertFalse("frame3 is not selected", frame3.isSelected());
352: assertTrue("", desktop.getSelectedFrame() == frame);
353: // test activate of already activated frame
354: manager.activateFrame(frame);
355: // test activate some separate internal frame
356: manager.activateFrame(new JInternalFrame());
357: }
358:
359: /*
360: * Class under test for
361: * void setBoundsForFrame(JInternalFrame, int, int, int, int)
362: */
363: public void testSetBoundsForFrame() {
364: Rectangle bounds = new Rectangle(1, 2, 50, 60);
365: // test ordinary setBoundsForFrame
366: manager.setBoundsForFrame(frame, bounds.x, bounds.y,
367: bounds.width, bounds.height);
368: assertTrue("the bounds are set", frame.getBounds().equals(
369: bounds));
370: // test setBoundsForFrame for a separate frame
371: manager.setBoundsForFrame(new JInternalFrame(), bounds.x,
372: bounds.y, bounds.width, bounds.height);
373: // test setBoundsForFrame for some component
374: manager.setBoundsForFrame(new JPanel(), bounds.x, bounds.y,
375: bounds.width, bounds.height);
376: // Note: could test that repaint was called
377: }
378:
379: /*
380: * Class under test for void resizeFrame(JComponent, int, int, int, int)
381: */
382: public void testResizeFrame() {
383: Rectangle bounds = new Rectangle(1, 2, 50, 60);
384: createAndShowRootFrame();
385: // OUTLINE_DRAG_MODE, the frame's bounds are not changed
386: desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
387: manager.beginResizingFrame(frame, 0);
388: manager.resizeFrame(frame, bounds.x, bounds.y, bounds.width,
389: bounds.height);
390: assertFalse("the bounds aren't set", frame.getBounds().equals(
391: bounds));
392: // LIVE_DRAG_MODE, the frame bounds are changed
393: desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
394: manager.beginResizingFrame(frame, 0);
395: manager.resizeFrame(frame, bounds.x, bounds.y, bounds.width,
396: bounds.height);
397: assertTrue("the bounds are set", frame.getBounds().equals(
398: bounds));
399: }
400:
401: /*
402: * Class under test for void dragFrame(JComponent, int, int)
403: */
404: public void testDragFrame() {
405: Point p = new Point(3, 5);
406: createAndShowRootFrame();
407: // OUTLINE_DRAG_MODE, the frame's location is not changed
408: desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
409: manager.beginDraggingFrame(frame);
410: manager.dragFrame(frame, p.x, p.y);
411: assertFalse("the location isn't changed", frame.getLocation()
412: .equals(p));
413: // LIVE_DRAG_MODE, the frame's location is changed
414: desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
415: manager.beginDraggingFrame(frame);
416: manager.dragFrame(frame, p.x, p.y);
417: assertTrue("the location is changed", frame.getLocation()
418: .equals(p));
419: }
420:
421: /*
422: * Class under test for void beginResizingFrame(JInternalFrame)
423: */
424: public void testBeginResizingFrame() {
425: // we can only test that there is no crash
426: desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
427: // the normal case
428: manager.beginResizingFrame(frame, 0);
429: // the frame without a parent
430: manager.beginResizingFrame(new JInternalFrame(), 0);
431: // the frame without a JDesktopPane parent
432: JPanel parent = new JPanel();
433: parent.add(frame);
434: manager.beginResizingFrame(frame, 0);
435: }
436:
437: /*
438: * Class under test for void beginDraggingFrame(JComponent)
439: */
440: public void testBeginDraggingFrame() {
441: desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
442: // the normal case
443: manager.beginDraggingFrame(frame);
444: // the frame without a parent
445: //manager.beginDraggingFrame(new JInternalFrame());
446: // the frame without a JDesktopPane parent
447: JPanel parent = new JPanel();
448: parent.add(frame);
449: manager.beginDraggingFrame(frame);
450: }
451:
452: /*
453: * Class under test for void endResizingFrame(JComponent)
454: */
455: public void testEndResizingFrame() {
456: Rectangle bounds = new Rectangle(1, 2, 50, 60);
457: createAndShowRootFrame();
458: desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
459: manager.beginResizingFrame(frame, 0);
460: manager.resizeFrame(frame, bounds.x, bounds.y, bounds.width,
461: bounds.height);
462: assertFalse("the bounds aren't changed", frame.getBounds()
463: .equals(bounds));
464: manager.endResizingFrame(frame);
465: assertTrue("the bounds are changed", frame.getBounds().equals(
466: bounds));
467: }
468:
469: /*
470: * Class under test for void endDraggingFrame(JComponent)
471: */
472: public void testEndDraggingFrame() {
473: Point p = new Point(3, 5);
474: createAndShowRootFrame();
475: desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
476: manager.beginDraggingFrame(frame);
477: manager.dragFrame(frame, p.x, p.y);
478: assertFalse("the location isn't changed", frame.getLocation()
479: .equals(p));
480: manager.endDraggingFrame(frame);
481: assertTrue("the location is changed", frame.getLocation()
482: .equals(p));
483: }
484:
485: /*
486: * Class under test for serialization.
487: */
488: public void testSerialize() throws Exception {
489: ByteArrayOutputStream fo = new ByteArrayOutputStream();
490: ObjectOutputStream so = new ObjectOutputStream(fo);
491: so.writeObject(manager);
492: // so.flush();
493: so.close();
494: // reading
495: DefaultDesktopManager manager2 = null;
496: InputStream fi = new ByteArrayInputStream(fo.toByteArray());
497: ObjectInputStream si = new ObjectInputStream(fi);
498: manager2 = (DefaultDesktopManager) si.readObject();
499: assertNotNull(manager2);
500: si.close();
501: }
502: }
|