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.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024: import java.beans.PropertyVetoException;
025: import javax.accessibility.AccessibleContext;
026: import javax.accessibility.AccessibleRole;
027: import javax.swing.plaf.ComponentUI;
028: import javax.swing.plaf.basic.BasicDesktopPaneUI;
029:
030: public class JDesktopPaneTest extends SwingTestCase {
031: /*
032: * This class is used to test protected methods.
033: */
034: private class TestJDesktopPane extends JDesktopPane {
035: private static final long serialVersionUID = 1L;
036:
037: @Override
038: public String paramString() {
039: return super .paramString();
040: }
041: }
042:
043: /*
044: * This class is used to test that some property is (or is not) a bound property
045: */
046: private class MyPropertyChangeListener implements
047: PropertyChangeListener {
048: public boolean ok;
049:
050: MyPropertyChangeListener() {
051: ok = false;
052: }
053:
054: public void propertyChange(final PropertyChangeEvent e) {
055: ok = true;
056: }
057: }
058:
059: private JDesktopPane desktop;
060:
061: public JDesktopPaneTest(final String name) {
062: super (name);
063: }
064:
065: /*
066: * @see TestCase#setUp()
067: */
068: @Override
069: protected void setUp() throws Exception {
070: super .setUp();
071: desktop = new JDesktopPane();
072: }
073:
074: /*
075: * @see TestCase#tearDown()
076: */
077: @Override
078: protected void tearDown() throws Exception {
079: super .tearDown();
080: }
081:
082: /*
083: * Class under test for boolean isOpaque()
084: */
085: public void testIsOpaque() {
086: assertTrue("always returns true", desktop.isOpaque());
087: desktop.setOpaque(false);
088: assertTrue("always returns true", desktop.isOpaque());
089: }
090:
091: /*
092: * Class under test for void updateUI()
093: */
094: public void testUpdateUI() {
095: desktop.updateUI();
096: ComponentUI ui1 = desktop.getUI();
097: ComponentUI ui2 = UIManager.getUI(desktop);
098: // at least names of classes must be the same
099: assertEquals(ui2.getClass().getName(), ui1.getClass().getName());
100: }
101:
102: /*
103: * Class under test for AccessibleContext getAccessibleContext()
104: */
105: public void testGetAccessibleContext() {
106: AccessibleContext c = desktop.getAccessibleContext();
107: assertTrue("instanceof AccessibleJDesktopPane",
108: c instanceof JDesktopPane.AccessibleJDesktopPane);
109: assertTrue("AccessibleRole is ok",
110: c.getAccessibleRole() == AccessibleRole.DESKTOP_PANE);
111: }
112:
113: /*
114: * Class under test for String paramString()
115: */
116: public void testParamString() {
117: TestJDesktopPane desktop = new TestJDesktopPane();
118: assertTrue(desktop.paramString() != null);
119: }
120:
121: public void testJDesktopPane() {
122: desktop = new JDesktopPane();
123: assertTrue(desktop.isFocusCycleRoot());
124: assertFalse(desktop.isFocusTraversalPolicyProvider());
125: assertTrue("ui != null", desktop.getUI() != null);
126: }
127:
128: /*
129: * Class under test for
130: * void setUI(DesktopPaneUI)
131: * DesktopPaneUI getUI()
132: */
133: public void testSetGetUI() {
134: BasicDesktopPaneUI ui = new BasicDesktopPaneUI();
135: desktop.setUI(ui);
136: assertTrue("UI is set", desktop.getUI() == ui);
137: }
138:
139: /*
140: * Class under test for
141: * void setSelectedFrame(JInternalFrame)
142: * JInternalFrame getSelectedFrame()
143: */
144: public void testSetGetSelectedFrame() {
145: JInternalFrame f = new JInternalFrame();
146: assertNull("null by default", desktop.getSelectedFrame());
147: desktop.setSelectedFrame(f);
148: assertTrue("is set", desktop.getSelectedFrame() == f);
149: desktop.setSelectedFrame(null);
150: assertNull("is set to null", desktop.getSelectedFrame());
151: }
152:
153: /*
154: * Adds some components to the desktop.
155: */
156: private void addComponents() {
157: // add one frame, layer 0
158: JInternalFrame frame1 = new JInternalFrame("frame1");
159: frame1.setVisible(true);
160: desktop.add(frame1);
161: // add iconified frame, layer 1
162: JInternalFrame frame2 = new JInternalFrame("frame2");
163: frame2.setVisible(true);
164: frame2.setLayer(1);
165: desktop.add(frame2);
166: try {
167: frame2.setIcon(true);
168: } catch (PropertyVetoException e) {
169: assertFalse("exception", true);
170: }
171: // add some non JInternalFrame component
172: JComponent comp = new JPanel();
173: desktop.add(comp);
174: }
175:
176: /*
177: * Class under test for JInternalFrame[] getAllFrames()
178: */
179: public void testGetAllFrames() {
180: JInternalFrame[] frames = desktop.getAllFrames();
181: assertTrue("empty array", frames.length == 0);
182: addComponents();
183: frames = desktop.getAllFrames();
184: assertTrue("2 frames", frames.length == 2);
185: }
186:
187: /*
188: * Class under test for JInternalFrame[] getAllFramesInLayer(int)
189: */
190: public void testGetAllFramesInLayer() {
191: JInternalFrame[] frames; // = desktop.getAllFramesInLayer(1);
192: //assertTrue("empty array", frames.length == 0);
193: addComponents();
194: frames = desktop.getAllFramesInLayer(0);
195: assertTrue("1 frame", frames.length == 1);
196: assertTrue("frame1", frames[0].getTitle() == "frame1");
197: // invisible frames are counted
198: frames[0].setVisible(false);
199: frames = desktop.getAllFramesInLayer(0);
200: assertTrue("1 frame", frames.length == 1);
201: // iconified frames are counted
202: frames = desktop.getAllFramesInLayer(1);
203: assertTrue("1 frame", frames.length == 1);
204: assertTrue("frame2", frames[0].getTitle() == "frame2");
205: // no frames in this layer
206: frames = desktop.getAllFramesInLayer(2);
207: assertTrue("empty array", frames.length == 0);
208: }
209:
210: /*
211: * Class under test for
212: * void setDesktopManager(DesktopManager)
213: * DesktopManager getDesktopManager()
214: */
215: public void testSetGetDesktopManager() {
216: MyPropertyChangeListener l = new MyPropertyChangeListener();
217: DesktopManager m = new DefaultDesktopManager();
218: assertTrue("not null by default",
219: desktop.getDesktopManager() != null);
220: desktop.addPropertyChangeListener("desktopManager", l);
221: desktop.setDesktopManager(m);
222: assertTrue("is set", desktop.getDesktopManager() == m);
223: assertTrue("bound property", l.ok);
224: desktop.setDesktopManager(null);
225: assertTrue("is not set to null",
226: desktop.getDesktopManager() != null);
227: }
228:
229: /*
230: * Class under test for String getUIClassID()
231: */
232: public void testGetUIClassID() {
233: assertTrue("", desktop.getUIClassID() == "DesktopPaneUI");
234: }
235:
236: /*
237: * Class under test for
238: * void setDragMode(int)
239: * int getDragMode()
240: */
241: public void testSetGetDragMode() {
242: assertTrue("initial ok",
243: desktop.getDragMode() == JDesktopPane.LIVE_DRAG_MODE);
244: desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
245: assertTrue("set",
246: desktop.getDragMode() == JDesktopPane.OUTLINE_DRAG_MODE);
247: desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
248: assertTrue("set",
249: desktop.getDragMode() == JDesktopPane.LIVE_DRAG_MODE);
250: }
251: }
|