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.plaf.basic;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Insets;
024: import java.beans.PropertyVetoException;
025: import javax.swing.JInternalFrame;
026: import javax.swing.SwingTestCase;
027: import javax.swing.event.MouseInputListener;
028: import javax.swing.plaf.ComponentUI;
029:
030: public class BasicDesktopIconUITest extends SwingTestCase {
031: private static boolean belongs(final Object o, final Object[] array) {
032: for (int i = 0; i < array.length; i++) {
033: if (array[i] == o) {
034: return true;
035: }
036: }
037: return false;
038: }
039:
040: private static class MyBasicDesktopIconUI extends
041: BasicDesktopIconUI {
042: public MouseInputListener mouseInputListener;
043:
044: @Override
045: protected MouseInputListener createMouseInputListener() {
046: mouseInputListener = super .createMouseInputListener();
047: return mouseInputListener;
048: }
049: }
050:
051: private MyBasicDesktopIconUI ui;
052:
053: private JInternalFrame.JDesktopIcon icon;
054:
055: private JInternalFrame frame;
056:
057: /*
058: * @see TestCase#setUp()
059: */
060: @Override
061: protected void setUp() throws Exception {
062: super .setUp();
063: frame = new JInternalFrame();
064: icon = frame.getDesktopIcon();
065: ui = new MyBasicDesktopIconUI();
066: icon.setUI(ui);
067: }
068:
069: /*
070: * @see TestCase#tearDown()
071: */
072: @Override
073: protected void tearDown() throws Exception {
074: super .tearDown();
075: }
076:
077: public BasicDesktopIconUITest(final String name) {
078: super (name);
079: }
080:
081: /*
082: * Class under test for ComponentUI createUI(JComponent)
083: */
084: public void testCreateUI() {
085: ComponentUI ui1 = BasicDesktopIconUI.createUI(frame);
086: ComponentUI ui2 = BasicDesktopIconUI.createUI(frame);
087: assertTrue("not null", ui1 != null);
088: assertTrue("stateful", ui1 != ui2);
089: }
090:
091: /*
092: * Class under test for BasicDesktopIconUI()
093: */
094: public void testBasicDesktopIconUI() {
095: // nothing to test
096: }
097:
098: /*
099: * Class under test for MouseInputListener createMouseInputListener()
100: */
101: public void testCreateMouseInputListener() {
102: MouseInputListener l1 = ui.createMouseInputListener();
103: MouseInputListener l2 = ui.createMouseInputListener();
104: assertTrue("not null", l1 != null);
105: if (isHarmony()) {
106: assertSame("the same instance", l1, l2);
107: }
108: }
109:
110: /*
111: * Class under test for void deiconize()
112: */
113: public void testDeiconize() {
114: try {
115: frame.setIcon(true);
116: } catch (PropertyVetoException e) {
117: }
118: assertTrue("is icon", frame.isIcon());
119: ui.deiconize();
120: assertFalse("deiconized", frame.isIcon());
121: }
122:
123: /*
124: * Class under test for Insets getInsets(JComponent)
125: */
126: public void testGetInsets() {
127: final Insets validInsets = new Insets(5, 5, 5, 5);
128: Insets insets = ui.getInsets(icon);
129: assertTrue("not null", insets != null);
130: assertEquals("ok", validInsets, insets);
131: try { //Regression test for HARMONY-2664
132: ui.getInsets(null);
133: fail("NullPointerException should have been thrown");
134: } catch (NullPointerException e) {
135: // Expected
136: }
137: }
138:
139: /*
140: * Class under test for Dimension getMaximumSize(JComponent)
141: */
142: public void testGetMaximumSize() {
143: if (isHarmony()) {
144: assertEquals("== minimumSize", ui.getMinimumSize(icon), ui
145: .getMaximumSize(icon));
146: }
147: }
148:
149: /*
150: * Class under test for Dimension getMinimumSize(JComponent)
151: */
152: public void testGetMinimumSize() {
153: if (isHarmony()) {
154: assertEquals("== preferredSize", ui.getPreferredSize(icon),
155: ui.getMinimumSize(icon));
156: }
157: }
158:
159: /*
160: * Class under test for Dimension getPreferredSize(JComponent)
161: */
162: public void testGetPreferredSize() {
163: icon.setSize(ui.getPreferredSize(icon));
164: icon.doLayout();
165: assertEquals(ui.iconPane.getPreferredSize(), ui.iconPane
166: .getSize());
167: }
168:
169: /*
170: * Class under test for void installComponents()
171: */
172: public void testInstallComponents() {
173: int count = icon.getComponentCount();
174: ui.uninstallComponents();
175: assertEquals("uninstalled", count - 1, icon.getComponentCount());
176: ui.installComponents();
177: assertEquals("added 1 component", count, icon
178: .getComponentCount());
179: assertTrue("added iconPane", icon.isAncestorOf(ui.iconPane));
180: }
181:
182: /*
183: * Class under test for void uninstallComponents()
184: */
185: public void testUninstallComponents() {
186: int count = icon.getComponentCount();
187: assertTrue("added iconPane", icon.isAncestorOf(ui.iconPane));
188: ui.uninstallComponents();
189: assertEquals("uninstalled", count - 1, icon.getComponentCount());
190: assertFalse("removed iconPane", icon.isAncestorOf(ui.iconPane));
191: }
192:
193: /*
194: * Class under test for void installDefaults()
195: */
196: public void testInstallDefaults() {
197: icon.setBorder(null);
198: icon.setLayout(null);
199: ui.installDefaults();
200: assertTrue("opaque", icon.isOpaque());
201: assertTrue("border", icon.getBorder() != null);
202: assertNull("layout", icon.getLayout());
203: }
204:
205: /*
206: * Class under test for void uninstallDefaults()
207: */
208: public void testUninstallDefaults() {
209: ui.uninstallDefaults();
210: assertNull("border", icon.getBorder());
211: assertTrue("layout", icon.getLayout() instanceof BorderLayout);
212: }
213:
214: /*
215: * Class under test for void installListeners()
216: */
217: public void testInstallListeners() {
218: ui.uninstallListeners();
219: ui.installListeners();
220: MouseInputListener listener = ui.mouseInputListener;
221: assertTrue("installed mouseListener", belongs(listener, icon
222: .getMouseListeners()));
223: assertTrue("installed mouseMotionListener", belongs(listener,
224: icon.getMouseMotionListeners()));
225: }
226:
227: /*
228: * Class under test for void uninstallListeners()
229: */
230: public void testUninstallListeners() {
231: MouseInputListener listener = ui.createMouseInputListener();
232: ui.uninstallListeners();
233: assertFalse("uninstalled mouseListener", belongs(listener, icon
234: .getMouseListeners()));
235: assertFalse("uninstalled mouseMotionListener", belongs(
236: listener, icon.getMouseMotionListeners()));
237: }
238:
239: /*
240: * Class under test for void installUI(JComponent)
241: */
242: public void testInstallUI() {
243: assertTrue("desktopIcon", ui.desktopIcon == icon);
244: assertTrue("frame", ui.frame == frame);
245: assertTrue("iconPane", ui.iconPane != null);
246: assertTrue("width != 0", icon.getWidth() != 0);
247: assertTrue("height != 0", icon.getHeight() != 0);
248: }
249:
250: /*
251: * Class under test for void uninstallUI(JComponent)
252: */
253: public void testUninstallUI() {
254: // test that no crash occur
255: ui.uninstallUI(icon);
256: }
257: }
|