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.beans.PropertyChangeListener;
023: import javax.swing.ActionMap;
024: import javax.swing.InputMap;
025: import javax.swing.JButton;
026: import javax.swing.JComponent;
027: import javax.swing.JFrame;
028: import javax.swing.JRootPane;
029: import javax.swing.SwingTestCase;
030: import javax.swing.SwingUtilities;
031: import javax.swing.plaf.ComponentUI;
032: import java.util.EventListener;
033:
034: public class BasicRootPaneUITest extends SwingTestCase {
035: private JRootPane rootPane;
036:
037: private BasicRootPaneUI ui;
038:
039: public BasicRootPaneUITest(final String name) {
040: super (name);
041: }
042:
043: /*
044: * @see TestCase#setUp()
045: */
046: @Override
047: protected void setUp() throws Exception {
048: super .setUp();
049: rootPane = new JRootPane();
050: ui = (BasicRootPaneUI) BasicRootPaneUI.createUI(rootPane);
051: }
052:
053: /*
054: * @see TestCase#tearDown()
055: */
056: @Override
057: protected void tearDown() throws Exception {
058: super .tearDown();
059: }
060:
061: /*
062: * Class under test for constructor
063: */
064: public void testBasicRootPaneUI() {
065: ui = new BasicRootPaneUI();
066: assertTrue(ui != null);
067: }
068:
069: /*
070: *
071: */
072: protected boolean isListenerInstalled(final JComponent c,
073: final PropertyChangeListener listener) {
074: EventListener[] listeners = rootPane
075: .getPropertyChangeListeners();
076: boolean result = false;
077: for (int i = 0; i < listeners.length; i++) {
078: if (listeners[i] == listener) {
079: result = true;
080: break;
081: }
082: }
083: return result;
084: }
085:
086: /*
087: * Class under test for:
088: * void installUI(JComponent)
089: * void uninstallUI(JComponent)
090: */
091: public void testInstallUninstallUI() {
092: ui.installUI(rootPane);
093: // check install listeners
094: assertTrue(isListenerInstalled(rootPane, ui));
095: // check install keyboard actions
096: int inputMapType = isHarmony() ? JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
097: : JComponent.WHEN_IN_FOCUSED_WINDOW;
098: assertTrue("inputMap installed", SwingUtilities.getUIInputMap(
099: rootPane, inputMapType) != null);
100: int inputMapLength = SwingUtilities.getUIInputMap(rootPane,
101: inputMapType).size();
102: assertTrue(SwingUtilities.getUIActionMap(rootPane) != null);
103: int actionMapLength = SwingUtilities.getUIActionMap(rootPane)
104: .size();
105: ui.uninstallUI(rootPane);
106: // check uninstall keyboard actions
107: InputMap inputMap = SwingUtilities.getUIInputMap(rootPane,
108: inputMapType);
109: if (inputMap != null) {
110: assertTrue("keys were uninstalled",
111: inputMap.size() < inputMapLength);
112: }
113: ActionMap actionMap = SwingUtilities.getUIActionMap(rootPane);
114: if (actionMap != null) {
115: assertTrue("actions were uninstalled",
116: actionMap.size() < actionMapLength);
117: }
118: // check uninstall listeners
119: assertFalse(isListenerInstalled(rootPane, ui));
120: }
121:
122: /*
123: * Class under test for void propertyChange()
124: */
125: public void testPropertyChange() {
126: JFrame frame = new JFrame();
127: rootPane = frame.getRootPane();
128: rootPane.setUI(ui);
129: int inputMapType = isHarmony() ? JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
130: : JComponent.WHEN_IN_FOCUSED_WINDOW;
131: InputMap inputMap = SwingUtilities.getUIInputMap(rootPane,
132: inputMapType);
133: assertTrue("inputMap != null", inputMap != null);
134: Object[] keys = SwingUtilities.getUIInputMap(rootPane,
135: inputMapType).keys();
136: int keysLength = (keys == null) ? 0 : keys.length;
137: // defaultButton is null, keysLength may be != 0 in 1.5
138: //assertTrue("", keys == null || keys.length == 0);
139: rootPane.setDefaultButton(new JButton());
140: keys = SwingUtilities.getUIInputMap(rootPane, inputMapType)
141: .keys();
142: // defaultButton != null
143: assertTrue("keys were added", keys.length > keysLength);
144: rootPane.setDefaultButton(null);
145: keys = SwingUtilities.getUIInputMap(rootPane, inputMapType)
146: .keys();
147: // defaultButton is null
148: assertTrue("keys were removed", keysLength == 0 && keys == null
149: || keys.length == keysLength);
150: }
151:
152: /*
153: * Class under test for ComponentUI createUI(JComponent)
154: */
155: public void testCreateUI() {
156: ComponentUI ui = BasicRootPaneUI.createUI(rootPane);
157: assertTrue(ui != null);
158: assertTrue(ui instanceof BasicRootPaneUI);
159: ComponentUI ui2 = BasicRootPaneUI.createUI(rootPane);
160: assertTrue("stateless", ui == ui2);
161: }
162: }
|