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: package javax.swing;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Component;
022: import java.awt.FlowLayout;
023: import java.awt.IllegalComponentStateException;
024: import java.awt.KeyboardFocusManager;
025: import java.awt.LayoutManager;
026: import java.beans.PropertyChangeEvent;
027: import java.beans.PropertyChangeListener;
028:
029: import javax.accessibility.AccessibleContext;
030: import javax.accessibility.AccessibleRole;
031:
032: import junit.framework.Test;
033: import junit.framework.TestSuite;
034:
035: public class JAppletTest extends SwingTestCase {
036: /*
037: * This class is used to test protected methods
038: */
039: private static class TestApplet extends JApplet {
040: public static boolean createRootPaneCalled = false;
041: public static boolean setRootPaneCalled = false;
042:
043: public JRootPane createRootPane() {
044: createRootPaneCalled = true;
045: return super .createRootPane();
046: }
047:
048: public void setRootPane(final JRootPane root) {
049: setRootPaneCalled = true;
050: super .setRootPane(root);
051: }
052:
053: public void setRootPaneCheckingEnabled(final boolean enabled) {
054: super .setRootPaneCheckingEnabled(enabled);
055: }
056:
057: public boolean isRootPaneCheckingEnabled() {
058: return super .isRootPaneCheckingEnabled();
059: }
060:
061: public void addImpl(final Component comp,
062: final Object constraints, final int index) {
063: super .addImpl(comp, constraints, index);
064: }
065:
066: public static void initStaticVars() {
067: createRootPaneCalled = false;
068: setRootPaneCalled = false;
069: }
070:
071: public String paramString() {
072: return super .paramString();
073: }
074: }
075:
076: private JApplet applet;
077:
078: /*
079: * This class is used to test that some property is (or is not) a bound property
080: */
081: private class MyPropertyChangeListener implements
082: PropertyChangeListener {
083: public boolean ok;
084:
085: MyPropertyChangeListener() {
086: ok = false;
087: }
088:
089: public void propertyChange(final PropertyChangeEvent e) {
090: ok = true;
091: }
092: }
093:
094: public static Test suite() {
095: TestSuite suite = new TestSuite(JAppletTest.class);
096: return suite;
097: }
098:
099: /*
100: * @see TestCase#setUp()
101: */
102: protected void setUp() throws Exception {
103: super .setUp();
104: applet = new JApplet();
105: TestApplet.initStaticVars();
106: }
107:
108: /*
109: * @see TestCase#tearDown()
110: */
111: protected void tearDown() throws Exception {
112: super .tearDown();
113: }
114:
115: /**
116: * Constructor for JAppletTest.
117: * @param name
118: */
119: public JAppletTest(final String name) {
120: super (name);
121: }
122:
123: /*
124: * Class under test for void JApplet()
125: */
126: public void testJApplet() {
127: TestApplet applet = new TestApplet();
128:
129: assertTrue("rootPaneCheckingEnabled is true", applet
130: .isRootPaneCheckingEnabled());
131:
132: assertTrue("layout is not null", applet.getLayout() != null);
133: assertTrue("layout is BorderLayout",
134: applet.getLayout() instanceof BorderLayout);
135:
136: assertTrue("rootPane is not null", applet.getRootPane() != null);
137:
138: assertTrue("locale is set", applet.getLocale() == JComponent
139: .getDefaultLocale());
140:
141: assertTrue("background is set", applet.isBackgroundSet());
142: assertTrue("background is set to white",
143: applet.getBackground() == java.awt.Color.white);
144:
145: assertTrue(
146: "rootPane.windowDecorationStyle is NONE",
147: applet.getRootPane().getWindowDecorationStyle() == JRootPane.NONE);
148:
149: // test that defaultFocusTraversalPolicy is set
150: assertTrue("focusTraversalPolicy is set", applet
151: .isFocusTraversalPolicySet());
152: assertTrue("focusTraversalPolicy is set correctly", applet
153: .getFocusTraversalPolicy() == KeyboardFocusManager
154: .getCurrentKeyboardFocusManager()
155: .getDefaultFocusTraversalPolicy());
156: assertFalse(applet.isFocusCycleRoot());
157: assertTrue(applet.isFocusTraversalPolicyProvider());
158: }
159:
160: /*
161: * Class under test for
162: * void setRootPaneCheckingEnabled(boolean enabled)
163: * boolean isRootPaneCheckingEnabled()
164: */
165: public void testSetIsRootPaneCheckingEnabled() {
166: TestApplet applet = new TestApplet();
167:
168: assertTrue("rootPaneCheckingEnabled is true by default", applet
169: .isRootPaneCheckingEnabled());
170:
171: applet.setRootPaneCheckingEnabled(false);
172: assertFalse("rootPaneCheckingEnabled is set to false", applet
173: .isRootPaneCheckingEnabled());
174: }
175:
176: /*
177: * Class under test for void addImpl(Component, Object, int)
178: */
179: public void testAddImpl() {
180: TestApplet applet = new TestApplet();
181: JComponent comp = new JPanel();
182:
183: // rootPaneCheckingEnabled is true, no exception since 1.5
184: applet.setRootPaneCheckingEnabled(true);
185: boolean ok = false;
186: try {
187: applet.addImpl(comp, null, 0);
188: } catch (Error e) {
189: ok = true;
190: } finally {
191: assertFalse("no exception", ok);
192: assertTrue("The component is added to contentPane", comp
193: .getParent() == applet.getContentPane());
194: }
195:
196: // rootPaneCheckingEnabled is false, no exception
197: applet.setRootPaneCheckingEnabled(false);
198: ok = false;
199: try {
200: applet.addImpl(comp, null, 0);
201: } catch (Error e) {
202: ok = true;
203: } finally {
204: assertFalse("no exception", ok);
205: assertTrue("the component is added to JWindow", comp
206: .getParent() == applet);
207: assertTrue("index of the component is 0", applet
208: .getComponent(0) == comp);
209: }
210: }
211:
212: /*
213: * Class under test for
214: * void setRootPane(JRootPane)
215: * JRootPane getRootPane()
216: */
217: public void testSetGetRootPane() {
218: TestApplet applet = new TestApplet();
219: assertTrue("setRootPane() is called from the constructor",
220: TestApplet.setRootPaneCalled);
221:
222: MyPropertyChangeListener listener = new MyPropertyChangeListener();
223: applet.addPropertyChangeListener("rootPane", listener);
224: JRootPane root = new JRootPane();
225: applet.setRootPane(root);
226: assertTrue(applet.getRootPane() == root);
227: assertFalse("rootPane is not a bound property", listener.ok);
228:
229: // test setting rootPane to null
230: applet.setRootPane(null);
231: assertNull(applet.getRootPane());
232: assertTrue("rootPane is removed from the container", applet
233: .getComponentCount() == 0);
234: }
235:
236: /*
237: * Class under test for JRootPane createRootPane()
238: */
239: public void testCreateRootPane() {
240: TestApplet applet = new TestApplet();
241: assertTrue("createRootPane() is called from the constructor",
242: TestApplet.createRootPaneCalled);
243:
244: JRootPane root = applet.createRootPane();
245: assertTrue("createRootPane() cannot return null", root != null);
246: }
247:
248: /*
249: * Class under test for
250: * void setJMenuBar(JMenuBar)
251: * JMenuBar getJMenuBar()
252: */
253: public void testSetGetJMenuBarJMenuBar() {
254: assertNull(applet.getJMenuBar());
255:
256: JMenuBar menuBar = new JMenuBar();
257: applet.setJMenuBar(menuBar);
258: assertTrue(applet.getJMenuBar() == menuBar);
259:
260: applet.setJMenuBar(null);
261: assertNull(applet.getJMenuBar());
262: }
263:
264: /*
265: * Class under test for
266: * void setLayeredPane(JLayeredPane)
267: * JLayeredPane getLayeredPane()
268: */
269: public void testSetGetLayeredPane() {
270: MyPropertyChangeListener listener = new MyPropertyChangeListener();
271: applet.addPropertyChangeListener("layeredPane", listener);
272:
273: JLayeredPane pane = new JLayeredPane();
274: applet.setLayeredPane(pane);
275: assertTrue(applet.getLayeredPane() == pane);
276: assertFalse("layeredPane is not a bound property", listener.ok);
277:
278: // test throwing exception if the parameter is null
279: boolean ok = false;
280: try {
281: applet.setLayeredPane(null);
282: } catch (IllegalComponentStateException e) {
283: ok = true;
284: } finally {
285: assertTrue(ok);
286: }
287: // layeredPane cannot be null, even after setLayeredPane(null)
288: assertTrue(applet.getLayeredPane() != null);
289:
290: // setLayeredPane() method is not called by the constructor
291: // (seems that there is an error in docs)
292: }
293:
294: /*
295: * Class under test for AccessibleContext getAccessibleContext()
296: */
297: public void testGetAccessibleContext() {
298: AccessibleContext c = applet.getAccessibleContext();
299:
300: assertTrue("instance of AccessibleJApplet",
301: c instanceof JApplet.AccessibleJApplet);
302: assertTrue("AccessibleRole is ok",
303: c.getAccessibleRole() == AccessibleRole.FRAME);
304: assertNull("AccessibleName is ok", c.getAccessibleName());
305: assertNull("AccessibleDescription is ok", c
306: .getAccessibleDescription());
307: assertTrue("AccessibleChildrenCount == 1", c
308: .getAccessibleChildrenCount() == 1);
309: }
310:
311: /*
312: * Class under test for String paramString()
313: */
314: public void testParamString() {
315: TestApplet applet = new TestApplet();
316: assertTrue("paramString() cannot return null", applet
317: .paramString() != null);
318: }
319:
320: /*
321: * Class under test for void setLayout(LayoutManager)
322: */
323: public void testSetLayout() {
324: TestApplet applet = new TestApplet();
325: LayoutManager contentLayout = applet.getContentPane()
326: .getLayout();
327: LayoutManager appletLayout = applet.getLayout();
328:
329: // rootPaneCheckingEnabled is true, no exception since 1.5
330: applet.setRootPaneCheckingEnabled(true);
331: boolean ok = false;
332: try {
333: applet.setLayout(new FlowLayout());
334: } catch (Error e) {
335: ok = true;
336: } finally {
337: assertFalse("no exception since 1.5", ok);
338: assertTrue("contentPane layout is changed", applet
339: .getContentPane().getLayout() != contentLayout);
340: assertTrue("Applet layout shouldn't be changed", applet
341: .getLayout() == appletLayout);
342: applet.getContentPane().setLayout(contentLayout);
343: }
344:
345: // rootPaneCheckingEnabled is false, exception may not be thrown
346: applet.setRootPaneCheckingEnabled(false);
347: ok = false;
348: try {
349: applet.setLayout(new FlowLayout());
350: } catch (Error e) {
351: ok = true;
352: } finally {
353: assertFalse("no exception", ok);
354: assertTrue(
355: "contentPane layout shouldn't be changed",
356: applet.getContentPane().getLayout() == contentLayout);
357: assertTrue("Applet layout is changed",
358: applet.getLayout() != appletLayout);
359: }
360: }
361:
362: /*
363: * Class under test for void update(Graphics)
364: */
365: public void testUpdate() {
366: // Note: painting code, cannot test
367: }
368:
369: /*
370: * Class under test for
371: * void setContentPane(Container)
372: * Container getContentPane()
373: */
374: public void testSetGetContentPane() {
375: MyPropertyChangeListener listener = new MyPropertyChangeListener();
376: applet.addPropertyChangeListener("contentPane", listener);
377:
378: JPanel pane = new JPanel();
379: applet.setContentPane(pane);
380: assertTrue(applet.getContentPane() == pane);
381: assertFalse("contentPane is not a bound property", listener.ok);
382:
383: // test throwing exception if the parameter is null
384: boolean ok = false;
385: try {
386: applet.setContentPane(null);
387: } catch (IllegalComponentStateException e) {
388: ok = true;
389: } finally {
390: assertTrue(ok);
391: }
392: // contentPane cannot be null, even after setContentPane(null)
393: assertTrue(applet.getContentPane() != null);
394:
395: // setContentPane() method is not called by the constructor
396: // (seems that there is an error in docs)
397: }
398:
399: /*
400: * Class under test for
401: * void setGlassPane(Component)
402: * Component getGlassPane()
403: */
404: public void testSetGetGlassPane() {
405: MyPropertyChangeListener listener = new MyPropertyChangeListener();
406: applet.addPropertyChangeListener("glassPane", listener);
407:
408: JPanel pane = new JPanel();
409: applet.setGlassPane(pane);
410: assertTrue(applet.getGlassPane() == pane);
411: assertFalse("glassPane is not a bound property", listener.ok);
412:
413: // test throwing exception if the parameter is null
414: boolean ok = false;
415: try {
416: applet.setGlassPane(null);
417: } catch (NullPointerException e) {
418: ok = true;
419: } finally {
420: assertTrue(ok);
421: }
422: // glassPane cannot be null, even after setGlassPane(null)
423: assertTrue(applet.getGlassPane() != null);
424:
425: // setGlassPane() method is not called by the constructor
426: // (seems that there is an error in docs)
427: }
428:
429: /*
430: * Class under test for void remove(Component)
431: */
432: public void testRemove() {
433: JComponent comp = new JPanel();
434: applet.getContentPane().add(comp);
435: assertTrue("label is in contentPane", applet.isAncestorOf(comp));
436: applet.remove(comp);
437: assertFalse("label is removed from contentPane", applet
438: .isAncestorOf(comp));
439:
440: ((JPanel) applet.getGlassPane()).add(comp);
441: applet.remove(comp);
442: assertTrue("label is not removed from glassPane", applet
443: .isAncestorOf(comp));
444:
445: // test removing directly from the container
446: applet.setRootPaneCheckingEnabled(false);
447: applet.add(comp, BorderLayout.EAST);
448: assertTrue("added", comp.getParent() == applet);
449: applet.remove(comp);
450: assertTrue("not removed", comp.getParent() == applet);
451:
452: // test removing rootPane
453: assertTrue(applet.isAncestorOf(applet.getRootPane()));
454: applet.remove(applet.getRootPane());
455: // rootPane is removed from the container
456: assertFalse(applet.isAncestorOf(applet.getRootPane()));
457: // but getRootPane() still returns it
458: assertTrue(applet.getRootPane() != null);
459: }
460: }
|