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.FlowLayout;
024: import java.awt.GraphicsConfiguration;
025: import java.awt.GraphicsEnvironment;
026: import java.awt.IllegalComponentStateException;
027: import java.awt.Image;
028: import java.awt.KeyboardFocusManager;
029: import java.awt.LayoutManager;
030: import java.awt.event.WindowEvent;
031: import java.awt.image.BufferedImage;
032: import java.beans.PropertyChangeEvent;
033: import java.beans.PropertyChangeListener;
034: import java.security.Permission;
035: import javax.accessibility.AccessibleContext;
036: import javax.accessibility.AccessibleRole;
037: import javax.accessibility.AccessibleState;
038: import org.apache.harmony.x.swing.StringConstants;
039:
040: public class JFrameTest extends SwingTestCase {
041: /*
042: * This class is used to test that some methods were called.
043: */
044: private static class TestFrame extends JFrame {
045: private static final long serialVersionUID = 1L;
046:
047: public static boolean createRootPaneCalled = false;
048:
049: public static boolean setRootPaneCalled = false;
050:
051: public boolean disposeCalled = false;
052:
053: @Override
054: public JRootPane createRootPane() {
055: createRootPaneCalled = true;
056: return super .createRootPane();
057: }
058:
059: @Override
060: public void setRootPane(final JRootPane root) {
061: setRootPaneCalled = true;
062: super .setRootPane(root);
063: }
064:
065: public static void initStaticVars() {
066: createRootPaneCalled = false;
067: setRootPaneCalled = false;
068: }
069:
070: @Override
071: public void dispose() {
072: disposeCalled = true;
073: super .dispose();
074: }
075: }
076:
077: /*
078: * This class is used to test that some property is (or is not) a bound property
079: */
080: private class MyPropertyChangeListener implements
081: PropertyChangeListener {
082: public boolean ok;
083:
084: MyPropertyChangeListener() {
085: ok = false;
086: }
087:
088: public void propertyChange(final PropertyChangeEvent e) {
089: ok = true;
090: }
091: }
092:
093: private JFrame frame;
094:
095: public JFrameTest(final String name) {
096: super (name);
097: }
098:
099: /*
100: * @see TestCase#setUp()
101: */
102: @Override
103: protected void setUp() throws Exception {
104: super .setUp();
105: frame = new JFrame();
106: TestFrame.initStaticVars();
107: }
108:
109: /*
110: * @see TestCase#tearDown()
111: */
112: @Override
113: protected void tearDown() throws Exception {
114: super .tearDown();
115: if (frame.isDisplayable()) {
116: frame.dispose();
117: }
118: }
119:
120: /*
121: * Class under test for void JFrame()
122: */
123: public void testJFrame() {
124: frame = new JFrame();
125: assertEquals("title is empty", "", frame.getTitle());
126: assertFalse("JFrame is invisible by default", frame.isVisible());
127: assertTrue(frame.getLocale() == JComponent.getDefaultLocale());
128: // how to test throwing of HeadlessException
129: // when GraphicsEnvironment.isHeadless() returns true
130: // it is not critical because the exception is actually thrown by Frame() constructor
131: }
132:
133: public void testFrameInit() {
134: TestFrame frame = new TestFrame();
135: assertTrue("rootPaneCheckingEnabled is true", frame
136: .isRootPaneCheckingEnabled());
137: assertTrue("layout is not null", frame.getLayout() != null);
138: assertTrue("rootPane is not null", frame.getRootPane() != null);
139: assertTrue("locale is set", frame.getLocale() == JComponent
140: .getDefaultLocale());
141: assertTrue("background is set", frame.getBackground() == frame
142: .getContentPane().getBackground());
143: assertFalse("defaultLookAndFeelDecorated is false", JFrame
144: .isDefaultLookAndFeelDecorated());
145: assertFalse("isUndecorated is false", frame.isUndecorated());
146: assertTrue(
147: "rootPane.windowDecorationStyle is NONE",
148: frame.getRootPane().getWindowDecorationStyle() == JRootPane.NONE);
149: // test that defaultFocusTraversalPolicy is set
150: //frame.setFocusTraversalPolicy(null);
151: //frame.frameInit();
152: assertTrue("focusTraversalPolicy is set correctly", frame
153: .getFocusTraversalPolicy() == KeyboardFocusManager
154: .getCurrentKeyboardFocusManager()
155: .getDefaultFocusTraversalPolicy());
156: assertTrue("focusTraversalPolicy is set", frame
157: .isFocusTraversalPolicySet());
158: assertTrue(frame.isFocusCycleRoot());
159: assertFalse(frame.isFocusTraversalPolicyProvider());
160: JFrame.setDefaultLookAndFeelDecorated(true);
161: frame.frameInit();
162: assertTrue("isUndecorated is true", frame.isUndecorated());
163: assertTrue(
164: "rootPane.windowDecorationStyle is FRAME",
165: frame.getRootPane().getWindowDecorationStyle() == JRootPane.FRAME);
166: // restore default value
167: JFrame.setDefaultLookAndFeelDecorated(false);
168: }
169:
170: /*
171: * Class under test for
172: * void setDefaultCloseOperation(int operation)
173: * int getDefaultCloseOperation()
174: */
175: public void testSetGetDefaultCloseOperation() {
176: // default value is JFrame.HIDE_ON_CLOSE
177: assertEquals(WindowConstants.HIDE_ON_CLOSE, frame
178: .getDefaultCloseOperation());
179: // test setting valid value
180: MyPropertyChangeListener listener = new MyPropertyChangeListener();
181: frame.addPropertyChangeListener("defaultCloseOperation",
182: listener);
183: frame
184: .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
185: assertEquals(WindowConstants.DISPOSE_ON_CLOSE, frame
186: .getDefaultCloseOperation());
187: assertTrue("defaultCloseOperation is a bound property",
188: listener.ok);
189: // test setting invalid value
190: boolean ok = false;
191: try {
192: frame.setDefaultCloseOperation(101); // invalid value
193: } catch (IllegalArgumentException e) {
194: ok = true;
195: } finally {
196: assertTrue(ok);
197: }
198: // if JFrame.EXIT_ON_CLOSE has been specified and the SecurityManager
199: // will not allow the caller to invoke System.exit then SecurityException is thrown
200: class MySecurityManager extends SecurityManager {
201: @Override
202: public void checkExit(final int status) {
203: // exit is not allowed
204: throw new SecurityException();
205: }
206:
207: @Override
208: public void checkPermission(final Permission perm) {
209: // allow changing the security manager
210: }
211: }
212: MySecurityManager sm = new MySecurityManager();
213: SecurityManager oldSM = System.getSecurityManager();
214: System.setSecurityManager(sm);
215: ok = false;
216: try {
217: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
218: } catch (SecurityException e) {
219: ok = true;
220: } finally {
221: assertTrue("", ok);
222: System.setSecurityManager(oldSM);
223: }
224: }
225:
226: /*
227: * Class under test for
228: * static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
229: * static boolean isDefaultLookAndFeelDecorated()
230: */
231: public void testSetIsDefaultLookAndFeelDecorated() {
232: // test for default value
233: assertFalse(JFrame.isDefaultLookAndFeelDecorated());
234: JFrame.setDefaultLookAndFeelDecorated(true);
235: assertTrue(JFrame.isDefaultLookAndFeelDecorated());
236: // restore default value
237: JFrame.setDefaultLookAndFeelDecorated(false);
238: }
239:
240: /*
241: * Class under test for
242: * void setRootPaneCheckingEnabled(boolean enabled)
243: * boolean isRootPaneCheckingEnabled()
244: */
245: public void testSetIsRootPaneCheckingEnabled() {
246: TestFrame frame = new TestFrame();
247: assertTrue("rootPaneCheckingEnabled is true by default", frame
248: .isRootPaneCheckingEnabled());
249: frame.setRootPaneCheckingEnabled(false);
250: assertFalse("rootPaneCheckingEnabled is set to false", frame
251: .isRootPaneCheckingEnabled());
252: }
253:
254: /*
255: * Class under test for void JFrame(String, GraphicsConfiguration)
256: */
257: public void testJFrameStringGraphicsConfiguration() {
258: final String title = "Test frame.";
259: final GraphicsConfiguration gc = GraphicsEnvironment
260: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
261: .getDefaultConfiguration();
262: // test with valid title, valid gc
263: // would be nice to test non-default gc here
264: frame = new JFrame(title, gc);
265: assertEquals("Title is set properly", title, frame.getTitle());
266: assertFalse("JFrame is invisible by default", frame.isVisible());
267: assertTrue(frame.getLocale() == JComponent.getDefaultLocale());
268: assertTrue(frame.getGraphicsConfiguration() == gc);
269: frame = new JFrame(null, null);
270: assertNull("null instead of title can be used", frame
271: .getTitle());
272: assertFalse("JFrame is invisible by default", frame.isVisible());
273: assertTrue(frame.getLocale() == JComponent.getDefaultLocale());
274: assertTrue(frame.getGraphicsConfiguration() == gc);
275: // how to test throwing of HeadlessException
276: // when GraphicsEnvironment.isHeadless() returns true
277: // it is not critical because the exception is actually thrown by Frame() constructor
278: }
279:
280: /*
281: * Class under test for void JFrame(String)
282: */
283: public void testJFrameString() {
284: final String title = "Test frame.";
285: // test with valid title
286: frame = new JFrame(title);
287: assertEquals("Title is set properly", title, frame.getTitle());
288: assertFalse("JFrame is invisible by default", frame.isVisible());
289: assertTrue(frame.getLocale() == JComponent.getDefaultLocale());
290: frame = new JFrame((String) null);
291: assertNull("null instead of title can be used", frame
292: .getTitle());
293: assertFalse("JFrame is invisible by default", frame.isVisible());
294: assertTrue(frame.getLocale() == JComponent.getDefaultLocale());
295: // how to test throwing of HeadlessException
296: // when GraphicsEnvironment.isHeadless() returns true
297: // it is not critical because the exception is actually thrown by Frame() constructor
298: }
299:
300: /*
301: * Class under test for void JFrame(GraphicsConfiguration)
302: */
303: public void testJFrameGraphicsConfiguration() {
304: final GraphicsConfiguration gc = GraphicsEnvironment
305: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
306: .getDefaultConfiguration();
307: // test with valid gc
308: // would be nice to test non-default gc here
309: frame = new JFrame(gc);
310: assertEquals("title is empty", "", frame.getTitle());
311: assertFalse("JFrame is invisible by default", frame.isVisible());
312: assertTrue(frame.getLocale() == JComponent.getDefaultLocale());
313: assertTrue(frame.getGraphicsConfiguration() == gc);
314: frame = new JFrame((GraphicsConfiguration) null);
315: assertEquals("title is empty", "", frame.getTitle());
316: assertFalse("JFrame is invisible by default", frame.isVisible());
317: assertTrue(frame.getLocale() == JComponent.getDefaultLocale());
318: assertTrue(frame.getGraphicsConfiguration() == gc);
319: // how to test throwing of HeadlessException
320: // when GraphicsEnvironment.isHeadless() returns true
321: // it is not critical because the exception is actually thrown by Frame() constructor
322: }
323:
324: /*
325: * Class under test for void addImpl(Component, Object, int)
326: */
327: public void testAddImpl() {
328: JComponent comp = new JPanel();
329: // rootPaneCheckingEnabled is true, exception must be thrown
330: frame.setRootPaneCheckingEnabled(true);
331: boolean ok = false;
332: try {
333: frame.addImpl(comp, null, 0);
334: } catch (Error e) {
335: ok = true;
336: } finally {
337: assertFalse("no exception", ok);
338: assertTrue("The component is added to contentPane", comp
339: .getParent() == frame.getContentPane());
340: }
341: // rootPaneCheckingEnabled is false, exception may not be thrown
342: frame.setRootPaneCheckingEnabled(false);
343: ok = false;
344: try {
345: frame.addImpl(comp, null, 0);
346: } catch (Error e) {
347: ok = true;
348: } finally {
349: assertFalse("no exception", ok);
350: assertTrue("the component is added to JWindow", comp
351: .getParent() == frame);
352: assertTrue("index of the component is 0", frame
353: .getComponent(0) == comp);
354: }
355: }
356:
357: /*
358: * Class under test for
359: * void setRootPane(JRootPane)
360: * JRootPane getRootPane()
361: */
362: public void testSetGetRootPane() {
363: TestFrame frame = new TestFrame();
364: assertTrue("setRootPane() is called from the constructor",
365: TestFrame.setRootPaneCalled);
366: MyPropertyChangeListener listener = new MyPropertyChangeListener();
367: frame.addPropertyChangeListener("rootPane", listener);
368: JRootPane root = new JRootPane();
369: frame.setRootPane(root);
370: assertTrue(frame.getRootPane() == root);
371: assertFalse("rootPane is not a bound property", listener.ok);
372: // test setting rootPane to null
373: frame.setRootPane(null);
374: assertNull(frame.getRootPane());
375: assertTrue("rootPane is removed from the container", frame
376: .getComponentCount() == 0);
377: }
378:
379: /*
380: * Class under test for JRootPane createRootPane()
381: */
382: public void testCreateRootPane() {
383: TestFrame frame = new TestFrame();
384: assertTrue("createRootPane() is called from the constructor",
385: TestFrame.createRootPaneCalled);
386: JRootPane root = frame.createRootPane();
387: assertTrue("createRootPane() cannot return null", root != null);
388: }
389:
390: /*
391: * Class under test for
392: * void setJMenuBar(JMenuBar)
393: * JMenuBar getJMenuBar()
394: */
395: public void testSetGetJMenuBar() {
396: assertNull(frame.getJMenuBar());
397: JMenuBar menuBar = new JMenuBar();
398: frame.setJMenuBar(menuBar);
399: assertTrue(frame.getJMenuBar() == menuBar);
400: frame.setJMenuBar(null);
401: assertNull(frame.getJMenuBar());
402: }
403:
404: /*
405: * Class under test for
406: * void setLayeredPane(JLayeredPane)
407: * JLayeredPane getLayeredPane()
408: */
409: public void testSetGetLayeredPane() {
410: MyPropertyChangeListener listener = new MyPropertyChangeListener();
411: frame.addPropertyChangeListener("layeredPane", listener);
412: JLayeredPane pane = new JLayeredPane();
413: frame.setLayeredPane(pane);
414: assertTrue(frame.getLayeredPane() == pane);
415: assertFalse("layeredPane is not a bound property", listener.ok);
416: // test throwing exception if the parameter is null
417: boolean ok = false;
418: try {
419: frame.setLayeredPane(null);
420: } catch (IllegalComponentStateException e) {
421: ok = true;
422: } finally {
423: assertTrue(ok);
424: }
425: // layeredPane cannot be null, even after setLayeredPane(null)
426: assertTrue(frame.getLayeredPane() != null);
427: // setLayeredPane() method is not called by the constructor
428: // (seems that there is an error in docs)
429: }
430:
431: /*
432: * Class under test for AccessibleContext getAccessibleContext()
433: */
434: public void testGetAccessibleContext() {
435: AccessibleContext c = frame.getAccessibleContext();
436: assertTrue("class is ok", c instanceof JFrame.AccessibleJFrame);
437: assertTrue("AccessibleRole is ok",
438: c.getAccessibleRole() == AccessibleRole.FRAME);
439: assertNull("AccessibleDescription is ok", c
440: .getAccessibleDescription());
441: assertTrue("AccessibleChildrenCount == 1", c
442: .getAccessibleChildrenCount() == 1);
443: // test getAccessibleName()
444: assertTrue("AccessibleName is ok", c.getAccessibleName() == "");
445: frame.setTitle("aa");
446: assertTrue("AccessibleName is ok",
447: c.getAccessibleName() == "aa");
448: // test getAccessibleStateSet()
449: AccessibleState[] states = c.getAccessibleStateSet().toArray();
450: assertTrue("more than 2 states", states.length > 2);
451: frame.setVisible(true);
452: states = c.getAccessibleStateSet().toArray();
453: assertTrue("more than 4 states", states.length > 4);
454: }
455:
456: /*
457: * Class under test for String paramString()
458: */
459: public void testParamString() {
460: TestFrame frame = new TestFrame();
461: assertTrue("paramString() cannot return null", frame
462: .paramString() != null);
463: }
464:
465: /*
466: * Class under test for void processWindowEvent(WindowEvent)
467: */
468: public void testProcessWindowEvent() {
469: TestFrame frame = new TestFrame();
470: frame.setVisible(true);
471: WindowEvent e = new WindowEvent(frame,
472: WindowEvent.WINDOW_CLOSING);
473: // test DO_NOTHING_ON_CLOSE
474: frame
475: .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
476: frame.disposeCalled = false;
477: frame.processWindowEvent(e);
478: assertFalse("didn't call dispose()", frame.disposeCalled);
479: assertTrue("is visible", frame.isVisible());
480: // test HIDE_ON_CLOSE
481: frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
482: frame.disposeCalled = false;
483: frame.processWindowEvent(e);
484: assertFalse("didn't call dispose()", frame.disposeCalled);
485: assertFalse("is not visible", frame.isVisible());
486: // test DISPOSE_ON_CLOSE
487: frame
488: .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
489: frame.disposeCalled = false;
490: frame.setVisible(true);
491: frame.processWindowEvent(e);
492: assertTrue("called dispose()", frame.disposeCalled);
493: assertFalse("is not visible", frame.isVisible());
494: // could test EXIT_ON_CLOSE but it's rather hard
495: }
496:
497: /*
498: * Class under test for void setLayout(LayoutManager)
499: */
500: public void testSetLayout() {
501: TestFrame frame = new TestFrame();
502: LayoutManager contentLayout = frame.getContentPane()
503: .getLayout();
504: LayoutManager frameLayout = frame.getLayout();
505: // rootPaneCheckingEnabled is true, no exception since 1.5
506: frame.setRootPaneCheckingEnabled(true);
507: boolean ok = false;
508: try {
509: frame.setLayout(new FlowLayout());
510: } catch (Error e) {
511: ok = true;
512: } finally {
513: assertFalse("no exception since 1.5", ok);
514: assertTrue("contentPane layout is changed", frame
515: .getContentPane().getLayout() != contentLayout);
516: assertTrue("Frame layout shouldn't be changed", frame
517: .getLayout() == frameLayout);
518: frame.getContentPane().setLayout(contentLayout);
519: }
520: // rootPaneCheckingEnabled is false
521: frame.setRootPaneCheckingEnabled(false);
522: ok = false;
523: try {
524: frame.setLayout(new FlowLayout());
525: } catch (Error e) {
526: ok = true;
527: } finally {
528: assertFalse("no exception", ok);
529: assertTrue("contentPane layout shouldn't be changed", frame
530: .getContentPane().getLayout() == contentLayout);
531: assertTrue("Frame layout is changed",
532: frame.getLayout() != frameLayout);
533: }
534: }
535:
536: /*
537: * Class under test for void update(Graphics)
538: */
539: public void testUpdate() {
540: // Note: painting code, cannot test
541: }
542:
543: /*
544: * Class under test for
545: * void setContentPane(Container)
546: * Container getContentPane()
547: */
548: public void testSetGetContentPane() {
549: MyPropertyChangeListener listener = new MyPropertyChangeListener();
550: frame.addPropertyChangeListener("contentPane", listener);
551: JPanel pane = new JPanel();
552: frame.setContentPane(pane);
553: assertTrue(frame.getContentPane() == pane);
554: assertFalse("contentPane is not a bound property", listener.ok);
555: // test throwing exception if the parameter is null
556: boolean ok = false;
557: try {
558: frame.setContentPane(null);
559: } catch (IllegalComponentStateException e) {
560: ok = true;
561: } finally {
562: assertTrue(ok);
563: }
564: // contentPane cannot be null, even after setContentPane(null)
565: assertTrue(frame.getContentPane() != null);
566: // setContentPane() method is not called by the constructor
567: // (seems that there is an error in docs)
568: }
569:
570: /*
571: * Class under test for
572: * void setGlassPane(Component)
573: * Component getGlassPane()
574: */
575: public void testSetGetGlassPane() {
576: MyPropertyChangeListener listener = new MyPropertyChangeListener();
577: frame.addPropertyChangeListener("glassPane", listener);
578: JPanel pane = new JPanel();
579: frame.setGlassPane(pane);
580: assertTrue(frame.getGlassPane() == pane);
581: assertFalse("glassPane is not a bound property", listener.ok);
582: // test throwing exception if the parameter is null
583: boolean ok = false;
584: try {
585: frame.setGlassPane(null);
586: } catch (NullPointerException e) {
587: ok = true;
588: } finally {
589: assertTrue(ok);
590: }
591: // glassPane cannot be null, even after setGlassPane(null)
592: assertTrue(frame.getGlassPane() != null);
593: // setGlassPane() method is not called by the constructor
594: // (seems that there is an error in docs)
595: }
596:
597: /*
598: * Class under test for void remove(Component)
599: */
600: public void testRemove() {
601: TestFrame frame = new TestFrame();
602: JComponent comp = new JPanel();
603: frame.getContentPane().add(comp);
604: assertTrue("label is in contentPane", frame.isAncestorOf(comp));
605: frame.remove(comp);
606: assertFalse("label is removed from contentPane", frame
607: .isAncestorOf(comp));
608: ((JPanel) frame.getGlassPane()).add(comp);
609: frame.remove(comp);
610: assertTrue("label is not removed from glassPane", frame
611: .isAncestorOf(comp));
612: // test removing from JFrame
613: frame.setRootPaneCheckingEnabled(false);
614: frame.add(comp, BorderLayout.EAST);
615: assertTrue("added", comp.getParent() == frame);
616: frame.remove(comp);
617: assertTrue("not removed", comp.getParent() == frame);
618: // test removing null
619: // boolean ok = false;
620: // try {
621: // frame.remove((Component)null);
622: // } catch (NullPointerException e) {
623: // ok = true;
624: // } finally {
625: // assertTrue("exception", ok);
626: // }
627: // test removing rootPane
628: assertTrue(frame.isAncestorOf(frame.getRootPane()));
629: frame.remove(frame.getRootPane());
630: // rootPane is removed from the container
631: assertFalse(frame.isAncestorOf(frame.getRootPane()));
632: // but getRootPane() still returns it
633: assertTrue(frame.getRootPane() != null);
634: }
635:
636: /*
637: * Class under test for void setIconImage(Image image)
638: */
639: public void testSetIconImage() {
640: Image image = new BufferedImage(5, 5,
641: BufferedImage.TYPE_BYTE_INDEXED);
642: PropertyChangeController cont = new PropertyChangeController();
643: frame.addPropertyChangeListener(cont);
644: frame.setIconImage(image);
645: assertEquals(image, frame.getIconImage());
646: assertTrue(cont.isChanged(StringConstants.ICON_IMAGE_PROPERTY));
647: }
648: }
|