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.BorderLayout;
023: import java.awt.Component;
024: import java.awt.Container;
025: import java.awt.Dimension;
026: import java.awt.IllegalComponentStateException;
027: import java.awt.Insets;
028: import java.awt.LayoutManager;
029: import java.awt.Rectangle;
030: import java.beans.PropertyChangeEvent;
031: import java.beans.PropertyChangeListener;
032: import javax.accessibility.AccessibleContext;
033: import javax.accessibility.AccessibleRole;
034: import javax.swing.border.Border;
035: import javax.swing.border.EtchedBorder;
036: import javax.swing.plaf.ComponentUI;
037: import javax.swing.plaf.basic.BasicRootPaneUI;
038:
039: public class JRootPaneTest extends SwingTestCase {
040: /*
041: * This class overload protected methods with public methods
042: */
043: private class TestRootPane extends JRootPane {
044: private static final long serialVersionUID = 1L;
045:
046: @Override
047: public String paramString() {
048: return super .paramString();
049: }
050:
051: @Override
052: public Container createContentPane() {
053: return super .createContentPane();
054: }
055:
056: @Override
057: public Component createGlassPane() {
058: return super .createGlassPane();
059: }
060:
061: @Override
062: public JLayeredPane createLayeredPane() {
063: return super .createLayeredPane();
064: }
065:
066: @Override
067: public LayoutManager createRootLayout() {
068: return super .createRootLayout();
069: }
070: }
071:
072: /*
073: * This class is used to test that some property is (or is not) a bound property
074: */
075: private class MyPropertyChangeListener implements
076: PropertyChangeListener {
077: public boolean ok;
078:
079: MyPropertyChangeListener() {
080: ok = false;
081: }
082:
083: public void propertyChange(final PropertyChangeEvent e) {
084: ok = true;
085: }
086: }
087:
088: private JRootPane rootPane;
089:
090: public JRootPaneTest(final String name) {
091: super (name);
092: }
093:
094: /*
095: * @see TestCase#setUp()
096: */
097: @Override
098: protected void setUp() throws Exception {
099: super .setUp();
100: rootPane = new JRootPane();
101: JFrame.setDefaultLookAndFeelDecorated(false);
102: }
103:
104: /*
105: * @see TestCase#tearDown()
106: */
107: @Override
108: protected void tearDown() throws Exception {
109: super .tearDown();
110: }
111:
112: /*
113: * Class under test for String getUIClassID()
114: */
115: public void testGetUIClassID() {
116: assertEquals("RootPaneUI", rootPane.getUIClassID());
117: }
118:
119: @SuppressWarnings("deprecation")
120: public void testSetGetMenuBar() {
121: assertNull(rootPane.getMenuBar());
122: JMenuBar menuBar = new JMenuBar();
123: rootPane.setMenuBar(menuBar);
124: assertTrue(rootPane.getMenuBar() == menuBar);
125: rootPane.setMenuBar(null);
126: assertNull(rootPane.getMenuBar());
127: }
128:
129: public void testJRootPaneConstructor() {
130: assertTrue(rootPane.getContentPane() != null);
131: assertTrue(rootPane.getLayeredPane() != null);
132: assertTrue(rootPane.getGlassPane() != null);
133: assertNull(rootPane.getDefaultButton());
134: }
135:
136: public void testSetGetContentPane() {
137: JPanel contentPane = new JPanel();
138: contentPane.setOpaque(true);
139: rootPane.setContentPane(contentPane);
140: assertTrue(contentPane == rootPane.getContentPane());
141: }
142:
143: public void testSetGetLayeredPane() {
144: JLayeredPane pane = new JLayeredPane();
145: rootPane.setLayeredPane(pane);
146: assertTrue(pane == rootPane.getLayeredPane());
147: boolean thrown = false;
148: try {
149: rootPane.setLayeredPane(null);
150: } catch (IllegalComponentStateException e) {
151: thrown = true;
152: } finally {
153: assertTrue(thrown);
154: }
155: assertTrue(rootPane.getLayeredPane() != null);
156: }
157:
158: public void testSetGetGlassPane() {
159: JPanel pane = new JPanel();
160: pane.setVisible(false);
161: rootPane.setGlassPane(pane);
162: assertTrue(pane == rootPane.getGlassPane());
163: boolean thrown = false;
164: try {
165: rootPane.setGlassPane(null);
166: } catch (NullPointerException e) {
167: thrown = true;
168: } finally {
169: assertTrue(thrown);
170: }
171: assertTrue(rootPane.getGlassPane() != null);
172: }
173:
174: public void testSetGetJMenuBar() {
175: assertNull(rootPane.getJMenuBar());
176: JMenuBar menuBar = new JMenuBar();
177: rootPane.setJMenuBar(menuBar);
178: assertTrue(rootPane.getJMenuBar() == menuBar);
179: rootPane.setJMenuBar(null);
180: assertNull(rootPane.getJMenuBar());
181: }
182:
183: public void testSetGetUI() {
184: BasicRootPaneUI ui = new BasicRootPaneUI();
185: rootPane.setUI(ui);
186: assertTrue(rootPane.getUI() == ui);
187: }
188:
189: public void testUpdateUI() {
190: rootPane.updateUI();
191: ComponentUI ui1 = rootPane.getUI();
192: ComponentUI ui2 = UIManager.getUI(rootPane);
193: // at least names of classes must be the same
194: assertEquals(ui2.getClass().getName(), ui1.getClass().getName());
195: }
196:
197: public void testSetGetWindowDecorationStyle() {
198: // rootPane must be inside window in this test
199: JFrame frame = new JFrame();
200: rootPane = frame.getRootPane();
201: assertEquals(JRootPane.NONE, rootPane
202: .getWindowDecorationStyle());
203: int newStyle = JRootPane.FRAME;
204: rootPane.setWindowDecorationStyle(newStyle);
205: assertEquals(newStyle, rootPane.getWindowDecorationStyle());
206: // test for invalid style - an exception must be thrown
207: boolean ok = false;
208: try {
209: rootPane.setWindowDecorationStyle(101);
210: } catch (IllegalArgumentException e) {
211: ok = true;
212: } finally {
213: assertTrue(ok);
214: }
215: // test that this is a bound property
216: MyPropertyChangeListener listener = new MyPropertyChangeListener();
217: rootPane.addPropertyChangeListener("windowDecorationStyle",
218: listener);
219: assertFalse(listener.ok);
220: rootPane.setWindowDecorationStyle(JRootPane.ERROR_DIALOG);
221: assertTrue(listener.ok);
222: frame.dispose();
223: }
224:
225: public void testAddImpl() {
226: JPanel pane = new JPanel();
227: // setGlassPane() calls addImpl(), which enshures that glass pane
228: // has index 0
229: rootPane.setGlassPane(pane);
230: // glass pane must always have index 0
231: assertTrue(rootPane.getComponent(0) == pane);
232: pane = new JPanel();
233: rootPane.add(pane, 0);
234: // not a glass pane, cannot have index 0
235: assertFalse(rootPane.getComponent(0) == pane);
236: }
237:
238: public void testIsValidateRoot() {
239: assertTrue(rootPane.isValidateRoot());
240: }
241:
242: public void testIsOptimizedDrawingEnabled() {
243: rootPane.getGlassPane().setVisible(false);
244: assertTrue(rootPane.isOptimizedDrawingEnabled());
245: rootPane.getGlassPane().setVisible(true);
246: assertFalse(rootPane.isOptimizedDrawingEnabled());
247: }
248:
249: public void testParamString() {
250: TestRootPane pane = new TestRootPane();
251: assertTrue(pane.paramString() != null);
252: }
253:
254: public void testCreateRootLayout() {
255: TestRootPane pane = new TestRootPane();
256: LayoutManager layout = pane.createRootLayout();
257: assertTrue(layout != null);
258: assertTrue(layout instanceof JRootPane.RootLayout);
259: }
260:
261: public void testSetGetDefaultButton() {
262: assertNull(rootPane.getDefaultButton());
263: JButton button = new JButton();
264: rootPane.setDefaultButton(button);
265: assertTrue(rootPane.getDefaultButton() == button);
266: rootPane.setDefaultButton(null);
267: assertNull(rootPane.getDefaultButton());
268: // test that this is a bound property
269: MyPropertyChangeListener listener = new MyPropertyChangeListener();
270: rootPane.addPropertyChangeListener("defaultButton", listener);
271: assertFalse(listener.ok);
272: rootPane.setDefaultButton(button);
273: assertTrue(listener.ok);
274: rootPane.setDefaultButton(null);
275: button.setDefaultCapable(false);
276: rootPane.setDefaultButton(button);
277: assertSame(button, rootPane.getDefaultButton());
278: }
279:
280: /*
281: * Test inner class JRootPane.RootLayout
282: */
283: //
284: public void testRootLayout() {
285: final Dimension base = new Dimension(640, 480);
286: rootPane.setSize(base);
287: rootPane.getLayout().layoutContainer(rootPane);
288: // test without menu
289: assertEquals(new Rectangle(0, 0, base.width, base.height),
290: rootPane.getGlassPane().getBounds());
291: assertEquals(new Rectangle(0, 0, base.width, base.height),
292: rootPane.getLayeredPane().getBounds());
293: assertEquals(new Rectangle(0, 0, base.width, base.height),
294: rootPane.getContentPane().getBounds());
295: // test with menu
296: JMenuBar menuBar = new JMenuBar();
297: menuBar.add(new JMenu("Menu"));
298: rootPane.setJMenuBar(menuBar);
299: rootPane.getLayout().layoutContainer(rootPane);
300: assertEquals(new Rectangle(0, 0, base.width, base.height),
301: rootPane.getGlassPane().getBounds());
302: assertEquals(new Rectangle(0, 0, base.width, base.height),
303: rootPane.getLayeredPane().getBounds());
304: assertEquals(new Rectangle(0, menuBar.getHeight(), base.width,
305: base.height - menuBar.getHeight()), rootPane
306: .getContentPane().getBounds());
307: assertEquals(new Rectangle(0, 0, base.width, menuBar
308: .getHeight()), rootPane.getJMenuBar().getBounds());
309: // test with menu and border
310: Border border = BorderFactory
311: .createEtchedBorder(EtchedBorder.LOWERED);
312: rootPane.setBorder(border);
313: rootPane.getLayout().layoutContainer(rootPane);
314: Insets insets = border.getBorderInsets(menuBar);
315: int insetsWidth = insets.left + insets.right;
316: int insetsHeight = insets.top + insets.bottom;
317: assertEquals(new Rectangle(insets.left, insets.top, base.width
318: - insetsWidth, base.height - insetsHeight), rootPane
319: .getGlassPane().getBounds());
320: assertEquals(new Rectangle(insets.left, insets.top, base.width
321: - insetsWidth, base.height - insetsHeight), rootPane
322: .getLayeredPane().getBounds());
323: assertEquals(new Rectangle(0, menuBar.getHeight(), base.width
324: - insetsWidth, base.height - insetsHeight
325: - menuBar.getHeight()), rootPane.getContentPane()
326: .getBounds());
327: assertEquals(new Rectangle(0, 0, base.width - insetsWidth,
328: menuBar.getHeight()), rootPane.getJMenuBar()
329: .getBounds());
330: }
331:
332: /*
333: * Class under test for void addNotify()
334: */
335: public void testAddNotify() {
336: // Note: how to test?
337: }
338:
339: /*
340: * Class under test for void removeNotify()
341: */
342: public void testRemoveNotify() {
343: // Note: how to test?
344: }
345:
346: /*
347: * Class under test for Container createContentPane()
348: */
349: public void testCreateContentPane() {
350: TestRootPane root = new TestRootPane();
351: JComponent content = (JComponent) root.createContentPane();
352: assertTrue(content != null);
353: assertTrue(content.isOpaque());
354: assertTrue(content.getLayout() instanceof BorderLayout);
355: }
356:
357: /*
358: * Class under test for Container createGlassPane()
359: */
360: public void testCreateGlassPane() {
361: TestRootPane root = new TestRootPane();
362: JComponent glass = (JComponent) root.createGlassPane();
363: assertTrue(glass != null);
364: assertFalse(glass.isVisible());
365: // there is nothing about default opacity in the docs,
366: // but it really must be false
367: assertFalse(glass.isOpaque());
368: }
369:
370: /*
371: * Class under test for Container createLayeredPane()
372: */
373: public void testCreateLayeredPane() {
374: TestRootPane root = new TestRootPane();
375: JLayeredPane layered = root.createLayeredPane();
376: assertTrue(layered != null);
377: }
378:
379: /*
380: * Class under test for AccessibleContext getAccessibleContext()
381: */
382: public void testGetAccessibleContext() {
383: AccessibleContext c = rootPane.getAccessibleContext();
384: assertTrue("instanceof AccessibleJRootPane",
385: c instanceof JRootPane.AccessibleJRootPane);
386: assertTrue("AccessibleRole is ok",
387: c.getAccessibleRole() == AccessibleRole.ROOT_PANE);
388: assertNull("AccessibleName is ok", c.getAccessibleName());
389: assertNull("AccessibleDescription is ok", c
390: .getAccessibleDescription());
391: rootPane.add(new JPanel());
392: rootPane.getLayeredPane().add(new JPanel());
393: //System.out.println(c.getAccessibleChildrenCount());
394: //System.out.println(c.getAccessibleChild(0));
395: assertTrue("AccessibleChildrenCount == 1", c
396: .getAccessibleChildrenCount() == 1);
397: assertTrue("AccessibleChild(0) == contentPane", c
398: .getAccessibleChild(0) == rootPane.getContentPane());
399: }
400: }
|