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.FlowLayout;
025: import java.awt.Frame;
026: import java.awt.GraphicsConfiguration;
027: import java.awt.GraphicsEnvironment;
028: import java.awt.IllegalComponentStateException;
029: import java.awt.KeyboardFocusManager;
030: import java.awt.LayoutManager;
031: import java.awt.Window;
032: import java.beans.PropertyChangeEvent;
033: import java.beans.PropertyChangeListener;
034: import javax.accessibility.AccessibleContext;
035: import javax.accessibility.AccessibleRole;
036:
037: public class JWindowTest extends SwingTestCase {
038: /*
039: * This class is used to test protected methods
040: */
041: static private class TestWindow extends JWindow {
042: private static final long serialVersionUID = 1L;
043:
044: public static boolean createRootPaneCalled = false;
045:
046: public static boolean setRootPaneCalled = false;
047:
048: @Override
049: public JRootPane createRootPane() {
050: createRootPaneCalled = true;
051: return super .createRootPane();
052: }
053:
054: @Override
055: public void setRootPane(final JRootPane root) {
056: setRootPaneCalled = true;
057: super .setRootPane(root);
058: }
059:
060: @Override
061: public void setRootPaneCheckingEnabled(final boolean enabled) {
062: super .setRootPaneCheckingEnabled(enabled);
063: }
064:
065: @Override
066: public boolean isRootPaneCheckingEnabled() {
067: return super .isRootPaneCheckingEnabled();
068: }
069:
070: @Override
071: public void addImpl(final Component comp,
072: final Object constraints, final int index) {
073: super .addImpl(comp, constraints, index);
074: }
075:
076: public static void initStaticVars() {
077: createRootPaneCalled = false;
078: setRootPaneCalled = false;
079: }
080:
081: @Override
082: public String paramString() {
083: return super .paramString();
084: }
085: }
086:
087: /*
088: * This class is used to test that some property is (or is not) a bound property
089: */
090: private class MyPropertyChangeListener implements
091: PropertyChangeListener {
092: public boolean ok;
093:
094: MyPropertyChangeListener() {
095: ok = false;
096: }
097:
098: public void propertyChange(final PropertyChangeEvent e) {
099: ok = true;
100: }
101: }
102:
103: private JWindow window;
104:
105: /*
106: * @see TestCase#setUp()
107: */
108: @Override
109: protected void setUp() throws Exception {
110: super .setUp();
111: window = new JWindow();
112: TestWindow.initStaticVars();
113: }
114:
115: /*
116: * @see TestCase#tearDown()
117: */
118: @Override
119: protected void tearDown() throws Exception {
120: super .tearDown();
121: }
122:
123: /**
124: * Constructor for JWindowTest.
125: * @param name
126: */
127: public JWindowTest(final String name) {
128: super (name);
129: }
130:
131: /*
132: * Class under test for void JWindow()
133: */
134: public void testJWindow() {
135: window = new JWindow();
136: assertTrue("owner is not null", window.getOwner() != null);
137: assertFalse("JWindow is invisible by default", window
138: .isVisible());
139: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
140: assertFalse("window is not focusable", window
141: .isFocusableWindow());
142: assertTrue(window.getContentPane().getLayout() instanceof BorderLayout);
143: }
144:
145: /*
146: * Class under test for void windowInit()
147: */
148: public void testWindowInit() {
149: TestWindow window = new TestWindow();
150: assertTrue("rootPaneCheckingEnabled is true", window
151: .isRootPaneCheckingEnabled());
152: assertTrue("layout is not null", window.getLayout() != null);
153: assertTrue("rootPane is not null", window.getRootPane() != null);
154: assertTrue("locale is set", window.getLocale() == JComponent
155: .getDefaultLocale());
156: assertTrue(
157: "rootPane.windowDecorationStyle is NONE",
158: window.getRootPane().getWindowDecorationStyle() == JRootPane.NONE);
159: // test that defaultFocusTraversalPolicy is set
160: assertTrue("focusTraversalPolicy is set correctly", window
161: .getFocusTraversalPolicy() == KeyboardFocusManager
162: .getCurrentKeyboardFocusManager()
163: .getDefaultFocusTraversalPolicy());
164: assertTrue("focusTraversalPolicy is set", window
165: .isFocusTraversalPolicySet());
166: assertTrue(window.isFocusCycleRoot());
167: assertFalse(window.isFocusTraversalPolicyProvider());
168: }
169:
170: /*
171: * Class under test for
172: * void setRootPaneCheckingEnabled(boolean enabled)
173: * boolean isRootPaneCheckingEnabled()
174: */
175: public void testSetIsRootPaneCheckingEnabled() {
176: TestWindow window = new TestWindow();
177: assertTrue("rootPaneCheckingEnabled is true by default", window
178: .isRootPaneCheckingEnabled());
179: window.setRootPaneCheckingEnabled(false);
180: assertFalse("rootPaneCheckingEnabled is set to false", window
181: .isRootPaneCheckingEnabled());
182: }
183:
184: /*
185: * Class under test for void JWindow(Window, GraphicsConfiguration)
186: */
187: public void testJWindowWindowGraphicsConfiguration() {
188: GraphicsConfiguration gc = GraphicsEnvironment
189: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
190: .getDefaultConfiguration();
191: Window owner = new JWindow();
192: // test with valid owner and valid gc
193: // would be nice to test non-default gc here
194: window = new JWindow(owner, gc);
195: assertTrue("owner is set", window.getOwner() == owner);
196: assertTrue(window.getGraphicsConfiguration() == gc);
197: assertFalse("JWindow is invisible by default", window
198: .isVisible());
199: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
200: assertFalse("window is not focusable", window
201: .isFocusableWindow());
202: // test with valid owner and gc == null
203: window = new JWindow(owner, (GraphicsConfiguration) null);
204: assertTrue("owner is set", window.getOwner() == owner);
205: assertTrue(window.getGraphicsConfiguration() == gc);
206: assertFalse("JWindow is invisible by default", window
207: .isVisible());
208: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
209: assertFalse("window is not focusable", window
210: .isFocusableWindow());
211: // test with owner == null and valid gc
212: window = new JWindow(null, gc);
213: assertTrue("owner is not null", window.getOwner() != null);
214: assertTrue(window.getGraphicsConfiguration() == gc);
215: assertFalse("JWindow is invisible by default", window
216: .isVisible());
217: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
218: assertFalse("window is not focusable", window
219: .isFocusableWindow());
220: // test with owner == null and gc == null
221: window = new JWindow(null, null);
222: assertTrue("owner is not null", window.getOwner() != null);
223: assertTrue(window.getGraphicsConfiguration() == window
224: .getOwner().getGraphicsConfiguration());
225: assertFalse("JWindow is invisible by default", window
226: .isVisible());
227: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
228: assertFalse("window is not focusable", window
229: .isFocusableWindow());
230: }
231:
232: /*
233: * Class under test for void JWindow(Window)
234: */
235: public void testJWindowWindow() {
236: Window owner = new JWindow();
237: window = new JWindow(owner);
238: // test with the correct owner
239: assertTrue("owner is set", window.getOwner() == owner);
240: assertFalse("JWindow is invisible by default", window
241: .isVisible());
242: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
243: assertFalse("window is not focusable", window
244: .isFocusableWindow());
245: // test with owner = null
246: window = new JWindow((Window) null);
247: assertTrue("owner is not null", window.getOwner() != null);
248: assertFalse("JWindow is invisible by default", window
249: .isVisible());
250: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
251: assertFalse("window is not focusable", window
252: .isFocusableWindow());
253: }
254:
255: /*
256: * Class under test for void JWindow(GraphicsConfiguration)
257: */
258: public void testJWindowGraphicsConfiguration() {
259: GraphicsConfiguration gc = GraphicsEnvironment
260: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
261: .getDefaultConfiguration();
262: // test with valid gc
263: // would be nice to test non-default gc here
264: window = new JWindow(gc);
265: assertTrue("owner is not null", window.getOwner() != null);
266: assertFalse("JWindow is invisible by default", window
267: .isVisible());
268: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
269: assertFalse("window is not focusable", window
270: .isFocusableWindow());
271: assertTrue(window.getGraphicsConfiguration() == gc);
272: // test with gc == null
273: window = new JWindow((GraphicsConfiguration) null);
274: assertTrue("owner is not null", window.getOwner() != null);
275: assertFalse("JWindow is invisible by default", window
276: .isVisible());
277: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
278: assertFalse("window is not focusable", window
279: .isFocusableWindow());
280: assertTrue(window.getGraphicsConfiguration() == gc);
281: }
282:
283: /*
284: * Class under test for void JWindow(Frame)
285: */
286: public void testJWindowFrame() {
287: Frame owner = new Frame();
288: window = new JWindow(owner);
289: // test with the correct owner
290: assertTrue("owner is set", window.getOwner() == owner);
291: assertFalse("JWindow is invisible by default", window
292: .isVisible());
293: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
294: assertFalse("window is not focusable", window
295: .isFocusableWindow());
296: // test with owner = null
297: window = new JWindow((Frame) null);
298: assertTrue("owner is not null", window.getOwner() != null);
299: assertFalse("JWindow is invisible by default", window
300: .isVisible());
301: assertTrue(window.getLocale() == JComponent.getDefaultLocale());
302: assertFalse("window is not focusable", window
303: .isFocusableWindow());
304: }
305:
306: /*
307: * Class under test for void addImpl(Component, Object, int)
308: */
309: public void testAddImpl() {
310: TestWindow window = new TestWindow();
311: JComponent comp = new JPanel();
312: // rootPaneCheckingEnabled is true, no exception since 1.5
313: window.setRootPaneCheckingEnabled(true);
314: boolean ok = false;
315: try {
316: window.addImpl(comp, null, 0);
317: } catch (Error e) {
318: ok = true;
319: } finally {
320: assertFalse("no exception", ok);
321: assertTrue("The component is added to contentPane", comp
322: .getParent() == window.getContentPane());
323: }
324: // rootPaneCheckingEnabled is false, no exception
325: window.setRootPaneCheckingEnabled(false);
326: ok = false;
327: try {
328: window.addImpl(comp, null, 0);
329: } catch (Error e) {
330: ok = true;
331: } finally {
332: assertFalse("no exception", ok);
333: assertTrue("the component is added to JWindow", comp
334: .getParent() == window);
335: assertTrue("index of the component is 0", window
336: .getComponent(0) == comp);
337: }
338: }
339:
340: /*
341: * Class under test for
342: * void setRootPane(JRootPane)
343: * JRootPane getRootPane()
344: */
345: public void testSetGetRootPane() {
346: TestWindow window = new TestWindow();
347: assertTrue("setRootPane() is called from the constructor",
348: TestWindow.setRootPaneCalled);
349: MyPropertyChangeListener listener = new MyPropertyChangeListener();
350: window.addPropertyChangeListener("rootPane", listener);
351: JRootPane root = new JRootPane();
352: window.setRootPane(root);
353: assertTrue(window.getRootPane() == root);
354: assertFalse("rootPane is not a bound property", listener.ok);
355: // test setting rootPane to null
356: window.setRootPane(null);
357: assertNull(window.getRootPane());
358: assertTrue("rootPane is removed from the container", window
359: .getComponentCount() == 0);
360: }
361:
362: /*
363: * Class under test for JRootPane createRootPane()
364: */
365: public void testCreateRootPane() {
366: TestWindow frame = new TestWindow();
367: assertTrue("createRootPane() is called from the constructor",
368: TestWindow.createRootPaneCalled);
369: JRootPane root = frame.createRootPane();
370: assertTrue("createRootPane() cannot return null", root != null);
371: }
372:
373: /*
374: * Class under test for
375: * void setLayeredPane(JLayeredPane)
376: * JLayeredPane getLayeredPane()
377: */
378: public void testSetGetLayeredPane() {
379: MyPropertyChangeListener listener = new MyPropertyChangeListener();
380: window.addPropertyChangeListener("layeredPane", listener);
381: JLayeredPane pane = new JLayeredPane();
382: window.setLayeredPane(pane);
383: assertTrue(window.getLayeredPane() == pane);
384: assertFalse("layeredPane is not a bound property", listener.ok);
385: // test throwing exception if the parameter is null
386: boolean ok = false;
387: try {
388: window.setLayeredPane(null);
389: } catch (IllegalComponentStateException e) {
390: ok = true;
391: } finally {
392: assertTrue(ok);
393: }
394: // layeredPane cannot be null, even after setLayeredPane(null)
395: assertTrue(window.getLayeredPane() != null);
396: // setLayeredPane() method is not called by the constructor
397: // (seems that there is an error in docs)
398: }
399:
400: /*
401: * Class under test for AccessibleContext getAccessibleContext()
402: */
403: public void testGetAccessibleContext() {
404: AccessibleContext c = window.getAccessibleContext();
405: assertTrue("instance of AccessibleJWindow",
406: c instanceof JWindow.AccessibleJWindow);
407: assertTrue("AccessibleRole is ok",
408: c.getAccessibleRole() == AccessibleRole.WINDOW);
409: assertNull("AccessibleName is ok", c.getAccessibleName());
410: assertNull("AccessibleDescription is ok", c
411: .getAccessibleDescription());
412: assertTrue("AccessibleChildrenCount == 1", c
413: .getAccessibleChildrenCount() == 1);
414: }
415:
416: /*
417: * Class under test for String paramString()
418: */
419: public void testParamString() {
420: TestWindow window = new TestWindow();
421: assertTrue("paramString() cannot return null", window
422: .paramString() != null);
423: }
424:
425: /*
426: * Class under test for void setLayout(LayoutManager)
427: */
428: public void testSetLayout() {
429: TestWindow window = new TestWindow();
430: LayoutManager contentLayout = window.getContentPane()
431: .getLayout();
432: LayoutManager frameLayout = window.getLayout();
433: // rootPaneCheckingEnabled is true, no exception since 1.5
434: window.setRootPaneCheckingEnabled(true);
435: boolean ok = false;
436: try {
437: window.setLayout(new FlowLayout());
438: } catch (Error e) {
439: ok = true;
440: } finally {
441: assertFalse("no exception since 1.5", ok);
442: assertTrue("contentPane layout is changed", window
443: .getContentPane().getLayout() != contentLayout);
444: assertTrue("Window layout shouldn't be changed", window
445: .getLayout() == frameLayout);
446: window.getContentPane().setLayout(contentLayout);
447: }
448: // rootPaneCheckingEnabled is false
449: window.setRootPaneCheckingEnabled(false);
450: ok = false;
451: try {
452: window.setLayout(new FlowLayout());
453: } catch (Error e) {
454: ok = true;
455: } finally {
456: assertFalse("no exception", ok);
457: assertTrue(
458: "contentPane layout shouldn't be changed",
459: window.getContentPane().getLayout() == contentLayout);
460: assertTrue("Window layout is changed)",
461: window.getLayout() != frameLayout);
462: }
463: }
464:
465: /*
466: * Class under test for void update(Graphics)
467: */
468: public void testUpdate() {
469: // Note: painting code, cannot test
470: }
471:
472: /*
473: * Class under test for
474: * void setContentPane(Container)
475: * Container getContentPane()
476: */
477: public void testSetGetContentPane() {
478: MyPropertyChangeListener listener = new MyPropertyChangeListener();
479: window.addPropertyChangeListener("contentPane", listener);
480: JPanel pane = new JPanel();
481: window.setContentPane(pane);
482: assertTrue(window.getContentPane() == pane);
483: assertFalse("contentPane is not a bound property", listener.ok);
484: // test throwing exception if the parameter is null
485: boolean ok = false;
486: try {
487: window.setContentPane(null);
488: } catch (IllegalComponentStateException e) {
489: ok = true;
490: } finally {
491: assertTrue(ok);
492: }
493: // contentPane cannot be null, even after setContentPane(null)
494: assertTrue(window.getContentPane() != null);
495: // setContentPane() method is not called by the constructor
496: // (seems that there is an error in docs)
497: }
498:
499: /*
500: * Class under test for
501: * void setGlassPane(Component)
502: * Component getGlassPane()
503: */
504: public void testSetGetGlassPane() {
505: MyPropertyChangeListener listener = new MyPropertyChangeListener();
506: window.addPropertyChangeListener("glassPane", listener);
507: JPanel pane = new JPanel();
508: window.setGlassPane(pane);
509: assertTrue(window.getGlassPane() == pane);
510: assertFalse("glassPane is not a bound property", listener.ok);
511: // test throwing exception if the parameter is null
512: boolean ok = false;
513: try {
514: window.setGlassPane(null);
515: } catch (NullPointerException e) {
516: ok = true;
517: } finally {
518: assertTrue(ok);
519: }
520: // glassPane cannot be null, even after setGlassPane(null)
521: assertTrue(window.getGlassPane() != null);
522: // setGlassPane() method is not called by the constructor
523: // (seems that there is an error in docs)
524: }
525:
526: /*
527: * Class under test for void remove(Component)
528: */
529: public void testRemove() {
530: JComponent comp = new JPanel();
531: window.getContentPane().add(comp);
532: assertTrue("label is in contentPane", window.isAncestorOf(comp));
533: window.remove(comp);
534: assertFalse("label is removed from contentPane", window
535: .isAncestorOf(comp));
536: ((JPanel) window.getGlassPane()).add(comp);
537: window.remove(comp);
538: assertTrue("label is not removed from glassPane", window
539: .isAncestorOf(comp));
540: // test removing directly from the container
541: window.setRootPaneCheckingEnabled(false);
542: window.add(comp, BorderLayout.EAST);
543: assertTrue("added", comp.getParent() == window);
544: window.remove(comp);
545: assertTrue("not removed", comp.getParent() == window);
546: // test removing null
547: // boolean ok = false;
548: // try {
549: // window.remove((Component)null);
550: // } catch (NullPointerException e) {
551: // ok = true;
552: // } finally {
553: // assertTrue("exception", ok);
554: // }
555: // test removing rootPane
556: assertTrue(window.isAncestorOf(window.getRootPane()));
557: window.remove(window.getRootPane());
558: // rootPane is removed from the container
559: assertFalse(window.isAncestorOf(window.getRootPane()));
560: // but getRootPane() still returns it
561: assertTrue(window.getRootPane() != null);
562: }
563: }
|