0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Alexander T. Simbirtsev
0019: * @version $Revision$
0020: */package javax.swing.plaf.basic;
0021:
0022: import java.awt.Button;
0023: import java.awt.Color;
0024: import java.awt.Component;
0025: import java.awt.Container;
0026: import java.awt.Dimension;
0027: import java.awt.Font;
0028: import java.awt.FontMetrics;
0029: import java.awt.GridBagConstraints;
0030: import java.awt.Insets;
0031: import java.awt.LayoutManager;
0032: import java.awt.event.ActionListener;
0033: import java.awt.event.KeyEvent;
0034: import java.awt.image.BufferedImage;
0035: import javax.swing.BorderFactory;
0036: import javax.swing.Box;
0037: import javax.swing.BoxLayout;
0038: import javax.swing.Icon;
0039: import javax.swing.ImageIcon;
0040: import javax.swing.JButton;
0041: import javax.swing.JCheckBox;
0042: import javax.swing.JComponent;
0043: import javax.swing.JLabel;
0044: import javax.swing.JOptionPane;
0045: import javax.swing.JPanel;
0046: import javax.swing.JRadioButton;
0047: import javax.swing.JTextField;
0048: import javax.swing.KeyStroke;
0049: import javax.swing.SwingConstants;
0050: import javax.swing.SwingTestCase;
0051: import javax.swing.UIManager;
0052: import javax.swing.border.Border;
0053: import javax.swing.plaf.BorderUIResource;
0054: import javax.swing.plaf.ColorUIResource;
0055: import javax.swing.plaf.DimensionUIResource;
0056: import javax.swing.plaf.FontUIResource;
0057: import javax.swing.plaf.IconUIResource;
0058: import javax.swing.plaf.OptionPaneUI;
0059: import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonAreaLayout;
0060:
0061: public class BasicOptionPaneUITest extends SwingTestCase {
0062: protected BasicOptionPaneUI paneUI;
0063:
0064: /*
0065: * @see TestCase#setUp()
0066: */
0067: @Override
0068: protected void setUp() throws Exception {
0069: super .setUp();
0070: paneUI = new BasicOptionPaneUI() {
0071: public FontMetrics getFontMetrics(Font font) {
0072: return BasicOptionPaneUITest.this .getFontMetrics(font);
0073: }
0074: };
0075: }
0076:
0077: class MyLayoutManager implements LayoutManager {
0078: private final Dimension size;
0079:
0080: MyLayoutManager(final Dimension size) {
0081: this .size = size;
0082: }
0083:
0084: public void addLayoutComponent(String name, Component comp) {
0085: }
0086:
0087: public void layoutContainer(Container parent) {
0088: }
0089:
0090: public Dimension minimumLayoutSize(Container parent) {
0091: return size;
0092: }
0093:
0094: public Dimension preferredLayoutSize(Container parent) {
0095: return size;
0096: }
0097:
0098: public void removeLayoutComponent(Component comp) {
0099: }
0100: };
0101:
0102: public void testGetPreferredSize() {
0103: JOptionPane pane = new JOptionPane() {
0104: private static final long serialVersionUID = 1L;
0105:
0106: @Override
0107: public FontMetrics getFontMetrics(Font font) {
0108: return BasicOptionPaneUITest.this .getFontMetrics(font);
0109: }
0110: };
0111: Border messageAreaBorder = new BorderUIResource(BorderFactory
0112: .createEmptyBorder(3, 3, 3, 3));
0113: UIManager
0114: .put("OptionPane.messageAreaBorder", messageAreaBorder);
0115: messageAreaBorder = new BorderUIResource(BorderFactory
0116: .createEmptyBorder(4, 4, 4, 4));
0117: UIManager
0118: .put("OptionPane.messageAreaBorder", messageAreaBorder);
0119: Border border = new BorderUIResource(BorderFactory
0120: .createEmptyBorder(1, 1, 1, 1));
0121: UIManager.put("OptionPane.border", border);
0122: Dimension minimumSize = new DimensionUIResource(123, 456);
0123: UIManager.put("OptionPane.minimumSize", minimumSize);
0124: pane.setUI(paneUI);
0125: assertNull(paneUI.getPreferredSize(null));
0126: Dimension preferredSize = paneUI.getPreferredSize(pane);
0127: assertNotNull(preferredSize);
0128: assertTrue(preferredSize.width > 0);
0129: assertTrue(preferredSize.height > 0);
0130: LayoutManager layout = new MyLayoutManager(new Dimension(200,
0131: 500));
0132: pane.setLayout(layout);
0133: assertEquals(new Dimension(200, 500), paneUI
0134: .getPreferredSize(pane));
0135: layout = new MyLayoutManager(new Dimension(200, 300));
0136: pane.setLayout(layout);
0137: assertEquals(new Dimension(200, 456), paneUI
0138: .getPreferredSize(pane));
0139: layout = new MyLayoutManager(new Dimension(100, 500));
0140: pane.setLayout(layout);
0141: assertEquals(new Dimension(123, 500), paneUI
0142: .getPreferredSize(pane));
0143: layout = new MyLayoutManager(new Dimension(10, 10));
0144: pane.setLayout(layout);
0145: assertEquals(new Dimension(123, 456), paneUI
0146: .getPreferredSize(pane));
0147: }
0148:
0149: public void testInstallUI() {
0150: JOptionPane pane = new JOptionPane();
0151: pane.removeAll();
0152: pane.setLayout(null);
0153: paneUI.installUI(pane);
0154: assertNotNull(pane.getBackground());
0155: assertNotNull(paneUI.optionPane);
0156: assertEquals(2, pane.getComponentCount());
0157: assertNotNull(paneUI.optionPane.getLayout());
0158: assertTrue(paneUI.optionPane.getLayout() instanceof BoxLayout);
0159: }
0160:
0161: public void testUninstallUI() {
0162: JOptionPane pane = new JOptionPane();
0163: pane.setUI(paneUI);
0164: paneUI.uninstallUI(pane);
0165: assertEquals(0, pane.getComponentCount());
0166: assertNull(paneUI.optionPane);
0167: }
0168:
0169: public void testCreateUI() {
0170: assertTrue("created UI is not null", null != BasicOptionPaneUI
0171: .createUI(new JOptionPane()));
0172: assertTrue(
0173: "created UI is of the proper class",
0174: BasicOptionPaneUI.createUI(null) instanceof BasicOptionPaneUI);
0175: assertNotSame("created UIs are unique", BasicOptionPaneUI
0176: .createUI(null), BasicOptionPaneUI.createUI(null));
0177: }
0178:
0179: public void testSelectInitialValue() {
0180: //TODO Implement selectInitialValue().
0181: }
0182:
0183: public void testContainsCustomComponents() {
0184: OptionPaneUI ui = null;
0185: JOptionPane optionPane = null;
0186: optionPane = new JOptionPane();
0187: assertFalse(paneUI.containsCustomComponents(optionPane));
0188: assertFalse(paneUI.containsCustomComponents(null));
0189: paneUI.hasCustomComponents = true;
0190: assertTrue(paneUI.containsCustomComponents(optionPane));
0191: assertTrue(paneUI.containsCustomComponents(null));
0192: optionPane = new JOptionPane("Message",
0193: JOptionPane.ERROR_MESSAGE);
0194: ui = optionPane.getUI();
0195: assertFalse(ui.containsCustomComponents(optionPane));
0196: assertFalse(ui.containsCustomComponents(null));
0197: optionPane = new JOptionPane(new JButton("Message"),
0198: JOptionPane.ERROR_MESSAGE);
0199: ui = optionPane.getUI();
0200: assertTrue(ui.containsCustomComponents(optionPane));
0201: assertTrue(ui.containsCustomComponents(null));
0202: optionPane = new JOptionPane("Message",
0203: JOptionPane.ERROR_MESSAGE, JOptionPane.CLOSED_OPTION,
0204: null, new Object[] { "1", "2" });
0205: ui = optionPane.getUI();
0206: assertFalse(ui.containsCustomComponents(optionPane));
0207: assertFalse(ui.containsCustomComponents(null));
0208: optionPane = new JOptionPane("Message",
0209: JOptionPane.ERROR_MESSAGE, JOptionPane.CLOSED_OPTION,
0210: null, new Object[] { new Button("1"), "2" });
0211: ui = optionPane.getUI();
0212: assertTrue(ui.containsCustomComponents(optionPane));
0213: assertTrue(ui.containsCustomComponents(null));
0214: }
0215:
0216: public void testInstallDefaults() {
0217: JOptionPane pane = new JOptionPane();
0218: UIManager.put("OptionPane.background", new ColorUIResource(
0219: Color.red));
0220: UIManager.put("OptionPane.foreground", new ColorUIResource(
0221: Color.yellow));
0222: UIManager.put("OptionPane.messageForeground",
0223: new ColorUIResource(Color.green));
0224: Font font = new FontUIResource(pane.getFont().deriveFont(100f));
0225: UIManager.put("OptionPane.font", font);
0226: Border border = new BorderUIResource(BorderFactory
0227: .createEmptyBorder(1, 1, 1, 1));
0228: UIManager.put("OptionPane.border", border);
0229: Dimension minimumSize = new DimensionUIResource(123, 456);
0230: UIManager.put("OptionPane.minimumSize", minimumSize);
0231: pane.setUI(paneUI);
0232: pane.setOptions(new Object[] { "button" });
0233: paneUI.installDefaults();
0234: assertEquals(Color.red, pane.getBackground());
0235: assertEquals(Color.yellow, pane.getForeground());
0236: assertEquals(font, pane.getFont());
0237: assertEquals(border, pane.getBorder());
0238: assertEquals(minimumSize, paneUI.getMinimumOptionPaneSize());
0239: }
0240:
0241: public void testUninstallDefaults() {
0242: JOptionPane pane = new JOptionPane();
0243: pane.setUI(paneUI);
0244: assertNotNull(pane.getBackground());
0245: assertNotNull(pane.getForeground());
0246: assertNotNull(pane.getFont());
0247: assertNotNull(pane.getBorder());
0248: assertNotNull(paneUI.getMinimumOptionPaneSize());
0249: paneUI.uninstallDefaults();
0250: assertNotNull(pane.getBackground());
0251: assertNotNull(pane.getForeground());
0252: assertNotNull(pane.getFont());
0253: assertNull(pane.getBorder());
0254: assertNotNull(paneUI.getMinimumOptionPaneSize());
0255: }
0256:
0257: public void testInstallComponents() {
0258: JOptionPane pane = new JOptionPane();
0259: pane.removeAll();
0260: paneUI.optionPane = pane;
0261: JCheckBox fake = new JCheckBox();
0262: paneUI.inputComponent = fake;
0263: paneUI.installComponents();
0264: assertEquals(2, pane.getComponentCount());
0265: assertTrue(pane.getComponent(0) instanceof JPanel);
0266: assertTrue(pane.getComponent(1) instanceof JPanel);
0267: assertTrue(((JPanel) pane.getComponent(0)).getComponent(0) instanceof JPanel);
0268: assertTrue(((JPanel) pane.getComponent(1)).getComponent(0) instanceof JButton);
0269: assertNull(paneUI.inputComponent);
0270: paneUI.inputComponent = fake;
0271: paneUI.optionPane.setWantsInput(true);
0272: paneUI.installComponents();
0273: assertNotNull(paneUI.inputComponent);
0274: }
0275:
0276: public void testUninstallComponents() {
0277: JOptionPane pane = new JOptionPane();
0278: assertEquals(2, pane.getComponentCount());
0279: paneUI.optionPane = pane;
0280: pane.add(new JCheckBox());
0281: paneUI.inputComponent = new JCheckBox();
0282: paneUI.uninstallComponents();
0283: assertEquals(0, pane.getComponentCount());
0284: assertNotNull(paneUI.optionPane);
0285: assertNull(paneUI.inputComponent);
0286: }
0287:
0288: public void testCreateLayoutManager() {
0289: LayoutManager layout1 = paneUI.createLayoutManager();
0290: LayoutManager layout2 = paneUI.createLayoutManager();
0291: assertTrue("LayoutManager is not null", layout1 != null);
0292: assertEquals("LayoutManager's class ", "javax.swing.BoxLayout",
0293: layout1.getClass().getName());
0294: assertFalse("layout2 is not shared", layout1 == layout2);
0295: }
0296:
0297: public void testInstallListeners() {
0298: paneUI.optionPane = new JOptionPane();
0299: assertEquals(1,
0300: paneUI.optionPane.getPropertyChangeListeners().length);
0301: }
0302:
0303: public void testUninstallListeners() {
0304: paneUI.optionPane = new JOptionPane();
0305: paneUI.optionPane.setUI(paneUI);
0306: assertEquals(1,
0307: paneUI.optionPane.getPropertyChangeListeners().length);
0308: assertNotNull(paneUI.propertyChangeListener);
0309: paneUI.uninstallListeners();
0310: assertEquals(0,
0311: paneUI.optionPane.getPropertyChangeListeners().length);
0312: assertNull(paneUI.propertyChangeListener);
0313: }
0314:
0315: // Regression for HARMONY-2901
0316: public void testUninstallListenersNull() {
0317: assertNull(paneUI.optionPane);
0318: paneUI.uninstallListeners(); // no exception is expected
0319: }
0320:
0321: public void testCreatePropertyChangeListener() {
0322: assertNotNull(paneUI.createPropertyChangeListener());
0323: }
0324:
0325: public void testInstallUninstallKeyboardActions() {
0326: paneUI.optionPane = new JOptionPane();
0327: Object[] keys = null;
0328: paneUI.uninstallKeyboardActions();
0329: keys = paneUI.optionPane.getInputMap(
0330: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
0331: .allKeys();
0332: assertTrue(keys == null || keys.length == 0);
0333: keys = paneUI.optionPane.getInputMap(
0334: JComponent.WHEN_IN_FOCUSED_WINDOW).allKeys();
0335: assertTrue(keys == null || keys.length == 0);
0336: keys = paneUI.optionPane.getInputMap(JComponent.WHEN_FOCUSED)
0337: .allKeys();
0338: assertTrue(keys == null || keys.length == 0);
0339: keys = paneUI.optionPane.getActionMap().allKeys();
0340: assertTrue(keys == null || keys.length == 0);
0341: assertNull(paneUI.optionPane.getActionMap().getParent());
0342: paneUI.installKeyboardActions();
0343: keys = paneUI.optionPane.getInputMap(
0344: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
0345: .allKeys();
0346: assertTrue(keys == null || keys.length == 0);
0347: keys = paneUI.optionPane.getInputMap(
0348: JComponent.WHEN_IN_FOCUSED_WINDOW).allKeys();
0349: assertNotNull(keys);
0350: assertEquals(1, keys.length);
0351: assertEquals(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
0352: keys[0]);
0353: keys = paneUI.optionPane.getInputMap(JComponent.WHEN_FOCUSED)
0354: .allKeys();
0355: assertTrue(keys == null || keys.length == 0);
0356: keys = paneUI.optionPane.getActionMap().keys();
0357: assertTrue(keys == null || keys.length == 0);
0358: keys = paneUI.optionPane.getActionMap().getParent().keys();
0359: assertNotNull(keys);
0360: assertEquals(1, keys.length);
0361: assertEquals("close", keys[0]);
0362: }
0363:
0364: public void testGetMinimumOptionPaneSize() {
0365: assertEquals(new Dimension(262, 90), paneUI
0366: .getMinimumOptionPaneSize());
0367: }
0368:
0369: public void testCreateMessageArea() {
0370: JOptionPane pane = new JOptionPane();
0371: String message = "message message message message";
0372: pane.setUI(paneUI);
0373: pane.setMessageType(JOptionPane.ERROR_MESSAGE);
0374: pane.setMessage(message);
0375: Border messageAreaBorder = new BorderUIResource(BorderFactory
0376: .createEmptyBorder(3, 3, 3, 3));
0377: UIManager
0378: .put("OptionPane.messageAreaBorder", messageAreaBorder);
0379: paneUI.installDefaults();
0380: assertEquals(messageAreaBorder, ((JComponent) paneUI
0381: .createMessageArea()).getBorder());
0382: messageAreaBorder = new BorderUIResource(BorderFactory
0383: .createEmptyBorder(4, 4, 4, 4));
0384: UIManager
0385: .put("OptionPane.messageAreaBorder", messageAreaBorder);
0386: paneUI.installDefaults();
0387: assertEquals(messageAreaBorder, ((JComponent) paneUI
0388: .createMessageArea()).getBorder());
0389: JComponent messageArea = (JComponent) paneUI
0390: .createMessageArea();
0391: assertTrue(messageArea instanceof JPanel);
0392: assertTrue(messageArea.getComponent(0) instanceof JPanel);
0393: JPanel panel = (JPanel) messageArea.getComponent(0);
0394: assertTrue(panel.getComponent(0) instanceof JPanel);
0395: assertEquals(0, ((JPanel) panel.getComponent(0))
0396: .getComponentCount());
0397: assertEquals(new Dimension(15, 1), ((JPanel) panel
0398: .getComponent(0)).getPreferredSize());
0399: assertTrue(panel.getComponent(1) instanceof JPanel);
0400: assertEquals(1, ((JPanel) panel.getComponent(1))
0401: .getComponentCount());
0402: assertTrue(((JPanel) panel.getComponent(1)).getComponent(0) instanceof JLabel);
0403: JLabel label = (JLabel) ((JPanel) panel.getComponent(1))
0404: .getComponent(0);
0405: assertEquals("message", message, label.getText());
0406: assertTrue(messageArea.getComponent(1) instanceof JLabel);
0407: JLabel iconLabel = (JLabel) messageArea.getComponent(1);
0408: assertEquals(paneUI.getIconForType(JOptionPane.ERROR_MESSAGE),
0409: iconLabel.getIcon());
0410: pane = new JOptionPane();
0411: pane.setUI(paneUI);
0412: pane.setInitialValue(message + message);
0413: pane.setWantsInput(true);
0414: pane.setMessageType(JOptionPane.ERROR_MESSAGE);
0415: pane.setMessage(message);
0416: messageArea = (JComponent) paneUI.createMessageArea();
0417: assertTrue(messageArea instanceof JPanel);
0418: assertTrue(messageArea.getComponent(0) instanceof JPanel);
0419: panel = (JPanel) messageArea.getComponent(0);
0420: assertTrue(panel.getComponent(0) instanceof JPanel);
0421: assertEquals(0, ((JPanel) panel.getComponent(0))
0422: .getComponentCount());
0423: assertEquals(new Dimension(15, 1), ((JPanel) panel
0424: .getComponent(0)).getPreferredSize());
0425: assertTrue(panel.getComponent(1) instanceof JPanel);
0426: assertEquals(2, ((JPanel) panel.getComponent(1))
0427: .getComponentCount());
0428: assertTrue(((JPanel) panel.getComponent(1)).getComponent(0) instanceof JLabel);
0429: label = (JLabel) ((JPanel) panel.getComponent(1))
0430: .getComponent(0);
0431: assertEquals("message", message, label.getText());
0432: assertTrue(((JPanel) panel.getComponent(1)).getComponent(1) instanceof JTextField);
0433: JTextField text = (JTextField) ((JPanel) panel.getComponent(1))
0434: .getComponent(1);
0435: assertEquals("message", "", text.getText());
0436: assertTrue(messageArea.getComponent(1) instanceof JLabel);
0437: iconLabel = (JLabel) messageArea.getComponent(1);
0438: assertEquals(paneUI.getIconForType(JOptionPane.ERROR_MESSAGE),
0439: iconLabel.getIcon());
0440: }
0441:
0442: public void testAddMessageComponents() {
0443: Container messageContainer = new JPanel();
0444: paneUI.optionPane = new JOptionPane();
0445: Component child = null, parent = null;
0446: Object message1 = new JButton("Tarara");
0447: Object message2 = new ImageIcon();
0448: Object message3 = "Wu Tang";
0449: Object message4 = new Integer(111);
0450: Object[] message5 = new Object[] { "1", "2", "3" };
0451: GridBagConstraints constrains = new GridBagConstraints(100,
0452: 200, 300, 400, 1.0, 1.0, GridBagConstraints.CENTER,
0453: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 111,
0454: 222);
0455: paneUI.addMessageComponents(messageContainer, constrains,
0456: message1, -1, true);
0457: assertEquals("number of children", 1, messageContainer
0458: .getComponentCount());
0459: child = messageContainer.getComponent(0);
0460: assertEquals(message1, child);
0461: assertEquals(201, constrains.gridy);
0462: paneUI.addMessageComponents(messageContainer, constrains,
0463: message2, -1, true);
0464: assertEquals("number of children", 2, messageContainer
0465: .getComponentCount());
0466: child = messageContainer.getComponent(1);
0467: assertTrue(child instanceof JLabel);
0468: assertSame(message2, ((JLabel) child).getIcon());
0469: assertEquals(202, constrains.gridy);
0470: paneUI.addMessageComponents(messageContainer, constrains,
0471: message3, 10000, true);
0472: assertEquals("number of children", 3, messageContainer
0473: .getComponentCount());
0474: child = messageContainer.getComponent(2);
0475: assertTrue(child instanceof JLabel);
0476: assertSame(message3, ((JLabel) child).getText());
0477: assertEquals(203, constrains.gridy);
0478: paneUI.addMessageComponents(messageContainer, constrains,
0479: message3, 1, true);
0480: assertEquals("number of children", 4, messageContainer
0481: .getComponentCount());
0482: parent = messageContainer.getComponent(3);
0483: assertTrue(parent instanceof Box);
0484: assertEquals(2, ((Box) parent).getComponentCount());
0485: child = ((Box) parent).getComponent(0);
0486: assertTrue(child instanceof JLabel);
0487: assertEquals("Wu", ((JLabel) child).getText());
0488: child = ((Box) parent).getComponent(1);
0489: assertTrue(child instanceof JLabel);
0490: assertEquals("Tang", ((JLabel) child).getText());
0491: assertEquals(204, constrains.gridy);
0492: paneUI.addMessageComponents(messageContainer, constrains,
0493: message4, -1, true);
0494: assertEquals("number of children", 5, messageContainer
0495: .getComponentCount());
0496: child = messageContainer.getComponent(4);
0497: assertTrue(child instanceof Box);
0498: child = ((Box) child).getComponent(0);
0499: assertTrue(child instanceof JLabel);
0500: assertEquals(message4.toString(), ((JLabel) child).getText());
0501: assertEquals(205, constrains.gridy);
0502: paneUI.addMessageComponents(messageContainer, constrains,
0503: message5, -1, true);
0504: assertEquals("number of children", 8, messageContainer
0505: .getComponentCount());
0506: child = messageContainer.getComponent(5);
0507: assertTrue(child instanceof Box);
0508: child = ((Box) child).getComponent(0);
0509: assertTrue(child instanceof JLabel);
0510: assertEquals("1", ((JLabel) child).getText());
0511: child = messageContainer.getComponent(6);
0512: assertTrue(child instanceof Box);
0513: child = ((Box) child).getComponent(0);
0514: assertTrue(child instanceof JLabel);
0515: assertEquals("2", ((JLabel) child).getText());
0516: child = messageContainer.getComponent(7);
0517: assertTrue(child instanceof Box);
0518: child = ((Box) child).getComponent(0);
0519: assertTrue(child instanceof JLabel);
0520: assertEquals("3", ((JLabel) child).getText());
0521: assertEquals(208, constrains.gridy);
0522: messageContainer = new JPanel();
0523: constrains.gridy = 0;
0524: paneUI.addMessageComponents(messageContainer, constrains, "",
0525: 1000, false);
0526: assertEquals(0, messageContainer.getComponentCount());
0527: assertEquals(0, constrains.gridy);
0528: paneUI.addMessageComponents(messageContainer, constrains,
0529: "\n\n", 1000, false);
0530: assertEquals(2, messageContainer.getComponentCount());
0531: assertEquals(2, constrains.gridy);
0532: messageContainer = new JPanel();
0533: constrains.gridy = 0;
0534: paneUI.addMessageComponents(messageContainer, constrains,
0535: "1\n2\n\n3\n", 1000, false);
0536: assertEquals(4, constrains.gridy);
0537: assertEquals(4, messageContainer.getComponentCount());
0538: child = messageContainer.getComponent(0);
0539: assertTrue(child instanceof JLabel);
0540: assertEquals("1", ((JLabel) child).getText());
0541: child = messageContainer.getComponent(1);
0542: assertTrue(child instanceof JLabel);
0543: assertEquals("2", ((JLabel) child).getText());
0544: child = messageContainer.getComponent(3);
0545: assertTrue(child instanceof JLabel);
0546: assertEquals("3", ((JLabel) child).getText());
0547: messageContainer = new JPanel();
0548: constrains.gridy = 0;
0549: paneUI.addMessageComponents(messageContainer, constrains,
0550: "123", 1, false);
0551: assertEquals(1, constrains.gridy);
0552: assertEquals(1, messageContainer.getComponentCount());
0553: }
0554:
0555: public void testGetMessage() {
0556: Object message = new JButton();
0557: paneUI.optionPane = new JOptionPane();
0558: paneUI.optionPane.setMessage(message);
0559: assertEquals(message, paneUI.getMessage());
0560: }
0561:
0562: public void testAddIcon() {
0563: JPanel panel = new JPanel();
0564: paneUI.optionPane = new JOptionPane();
0565: paneUI.optionPane
0566: .setMessageType(JOptionPane.INFORMATION_MESSAGE);
0567: paneUI.addIcon(panel);
0568: JComponent label = (JComponent) panel.getComponent(0);
0569: assertTrue(label instanceof JLabel);
0570: assertEquals(paneUI
0571: .getIconForType(JOptionPane.INFORMATION_MESSAGE),
0572: ((JLabel) label).getIcon());
0573: assertEquals(SwingConstants.TOP, ((JLabel) label)
0574: .getVerticalAlignment());
0575: assertEquals(SwingConstants.CENTER, ((JLabel) label)
0576: .getHorizontalAlignment());
0577: assertEquals(SwingConstants.CENTER, ((JLabel) label)
0578: .getVerticalTextPosition());
0579: assertEquals(SwingConstants.TRAILING, ((JLabel) label)
0580: .getHorizontalTextPosition());
0581: paneUI.optionPane.setMessageType(JOptionPane.ERROR_MESSAGE);
0582: paneUI.addIcon(panel);
0583: label = (JComponent) panel.getComponent(0);
0584: assertTrue(label instanceof JLabel);
0585: assertEquals(paneUI
0586: .getIconForType(JOptionPane.INFORMATION_MESSAGE),
0587: ((JLabel) label).getIcon());
0588: label = (JComponent) panel.getComponent(1);
0589: assertTrue(label instanceof JLabel);
0590: assertEquals(paneUI.getIconForType(JOptionPane.ERROR_MESSAGE),
0591: ((JLabel) label).getIcon());
0592: assertEquals(SwingConstants.TOP, ((JLabel) label)
0593: .getVerticalAlignment());
0594: assertEquals(SwingConstants.CENTER, ((JLabel) label)
0595: .getHorizontalAlignment());
0596: assertEquals(SwingConstants.CENTER, ((JLabel) label)
0597: .getVerticalTextPosition());
0598: assertEquals(SwingConstants.TRAILING, ((JLabel) label)
0599: .getHorizontalTextPosition());
0600: }
0601:
0602: public void testGetIcon() {
0603: Icon icon = null;
0604: assertNull(paneUI.getIcon());
0605: JOptionPane optionPane = new JOptionPane();
0606: paneUI.installUI(optionPane);
0607: optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
0608: assertEquals(paneUI
0609: .getIconForType(JOptionPane.INFORMATION_MESSAGE),
0610: paneUI.getIcon());
0611: optionPane.setIcon(null);
0612: assertEquals(paneUI
0613: .getIconForType(JOptionPane.INFORMATION_MESSAGE),
0614: paneUI.getIcon());
0615: icon = new ImageIcon();
0616: optionPane.setIcon(icon);
0617: assertEquals(icon, paneUI.getIcon());
0618: optionPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
0619: assertEquals(icon, paneUI.getIcon());
0620: optionPane.setIcon(null);
0621: assertNull(paneUI.getIcon());
0622: }
0623:
0624: public void testGetIconForType() throws InterruptedException {
0625: paneUI.optionPane = new JOptionPane();
0626: paneUI.optionPane.setUI(paneUI);
0627: Icon icon11 = new IconUIResource(new ImageIcon(
0628: new BufferedImage(10, 20, BufferedImage.TYPE_INT_RGB)));
0629: Icon icon21 = new IconUIResource(new ImageIcon(
0630: new BufferedImage(30, 40, BufferedImage.TYPE_INT_RGB)));
0631: Icon icon31 = new IconUIResource(new ImageIcon(
0632: new BufferedImage(50, 60, BufferedImage.TYPE_INT_RGB)));
0633: Icon icon41 = new IconUIResource(new ImageIcon(
0634: new BufferedImage(70, 80, BufferedImage.TYPE_INT_RGB)));
0635: UIManager.put("OptionPane.errorIcon", icon11);
0636: UIManager.put("OptionPane.informationIcon", icon21);
0637: UIManager.put("OptionPane.questionIcon", icon31);
0638: UIManager.put("OptionPane.warningIcon", icon41);
0639: Icon icon12 = paneUI.getIconForType(JOptionPane.ERROR_MESSAGE);
0640: Icon icon22 = paneUI
0641: .getIconForType(JOptionPane.INFORMATION_MESSAGE);
0642: Icon icon32 = paneUI
0643: .getIconForType(JOptionPane.QUESTION_MESSAGE);
0644: Icon icon42 = paneUI
0645: .getIconForType(JOptionPane.WARNING_MESSAGE);
0646: Icon icon52 = paneUI.getIconForType(JOptionPane.PLAIN_MESSAGE);
0647: Icon icon62 = paneUI.getIconForType(100);
0648: assertEquals(icon11, icon12);
0649: assertEquals(icon21, icon22);
0650: assertEquals(icon31, icon32);
0651: assertEquals(icon41, icon42);
0652: assertNull(icon52);
0653: assertNull(icon62);
0654: assertSame("icons are shared", icon12, paneUI
0655: .getIconForType(JOptionPane.ERROR_MESSAGE));
0656: assertSame("icons are shared", icon22, paneUI
0657: .getIconForType(JOptionPane.INFORMATION_MESSAGE));
0658: assertSame("icons are shared", icon32, paneUI
0659: .getIconForType(JOptionPane.QUESTION_MESSAGE));
0660: assertSame("icons are shared", icon42, paneUI
0661: .getIconForType(JOptionPane.WARNING_MESSAGE));
0662: }
0663:
0664: public void testGetIconForType_Null() throws InterruptedException {
0665: try { // Regression test for HARMONY-2903
0666: new BasicOptionPaneUI().getIconForType(0);
0667: fail("NullPointerException should have been thrown");
0668: } catch (NullPointerException e) {
0669: // Expected
0670: }
0671: }
0672:
0673: public void testGetMaxCharactersPerLineCount() {
0674: paneUI.optionPane = new JOptionPane();
0675: paneUI.optionPane.setUI(paneUI);
0676: assertEquals(Integer.MAX_VALUE, paneUI
0677: .getMaxCharactersPerLineCount());
0678: }
0679:
0680: // Regression for HARMONY-2902
0681: public void testGetMaxCharactersPerLineCount_OptionPane() {
0682: final Marker marker = new Marker();
0683: paneUI.optionPane = new JOptionPane() {
0684: private static final long serialVersionUID = 1L;
0685:
0686: @Override
0687: public int getMaxCharactersPerLineCount() {
0688: marker.setOccurred();
0689: return super .getMaxCharactersPerLineCount();
0690: }
0691: };
0692: paneUI.optionPane.setUI(paneUI);
0693: marker.reset();
0694: assertEquals(Integer.MAX_VALUE, paneUI
0695: .getMaxCharactersPerLineCount());
0696: assertTrue(marker.isOccurred());
0697: }
0698:
0699: // Regression for HARMONY-2902
0700: public void testGetMaxCharactersPerLineCount_Null() {
0701: assertNull(paneUI.optionPane);
0702: testExceptionalCase(new NullPointerCase() {
0703: @Override
0704: public void exceptionalAction() throws Exception {
0705: paneUI.getMaxCharactersPerLineCount();
0706: }
0707: });
0708: }
0709:
0710: public void testBurstStringInto() {
0711: String message = "message ";
0712: JPanel panel = new JPanel();
0713: paneUI.burstStringInto(panel, message, 2);
0714: assertEquals(1, panel.getComponentCount());
0715: assertEquals("message", ((JLabel) panel.getComponent(0))
0716: .getText());
0717: message = "message \n\n message";
0718: panel = new JPanel();
0719: paneUI.burstStringInto(panel, message, 2);
0720: assertEquals(3, panel.getComponentCount());
0721: assertEquals("message", ((JLabel) panel.getComponent(0))
0722: .getText());
0723: assertEquals("\n\n", ((JLabel) panel.getComponent(1)).getText());
0724: assertEquals(" message", ((JLabel) panel.getComponent(2))
0725: .getText());
0726: panel = new JPanel();
0727: paneUI.burstStringInto(panel, message, 20);
0728: assertEquals(1, panel.getComponentCount());
0729: assertEquals(message, ((JLabel) panel.getComponent(0))
0730: .getText());
0731: panel = new JPanel();
0732: message = "";
0733: for (int i = 0; i < 4; i++) {
0734: message += "messagemessage \n";
0735: }
0736: paneUI.burstStringInto(panel, message, 50);
0737: assertEquals(2, panel.getComponentCount());
0738: assertEquals(
0739: "messagemessage \nmessagemessage \nmessagemessage",
0740: ((JLabel) panel.getComponent(0)).getText());
0741: assertEquals(" \nmessagemessage \n", ((JLabel) panel
0742: .getComponent(1)).getText());
0743: }
0744:
0745: public void testCreateSeparator() {
0746: assertNull(paneUI.createSeparator());
0747: }
0748:
0749: private void checkButton(final JButton button, final String text,
0750: final long threshold, final int mnemonic,
0751: final int numListeners, final Insets margin) {
0752: assertEquals("button text", text, button.getText());
0753: assertEquals("Threshold", threshold, button
0754: .getMultiClickThreshhold());
0755: assertEquals("button mnemonic", mnemonic, button.getMnemonic());
0756: assertEquals("listener", numListeners, button
0757: .getActionListeners().length);
0758: assertEquals("margin", margin, button.getMargin());
0759: }
0760:
0761: public void testCreateButtonArea() {
0762: Container buttonArea = null;
0763: paneUI.optionPane = new JOptionPane();
0764: JButton button;
0765: paneUI.optionPane.setOptionType(JOptionPane.YES_NO_OPTION);
0766: int threshold = 111;
0767: UIManager.put("OptionPane.buttonClickThreshhold", new Integer(
0768: threshold));
0769: Border buttonAreaBorder = new BorderUIResource(BorderFactory
0770: .createEmptyBorder(2, 2, 2, 2));
0771: UIManager.put("OptionPane.buttonAreaBorder", buttonAreaBorder);
0772: paneUI.installDefaults();
0773: buttonArea = paneUI.createButtonArea();
0774: assertEquals("class", JPanel.class, buttonArea.getClass());
0775: assertEquals("layout",
0776: BasicOptionPaneUI.ButtonAreaLayout.class, buttonArea
0777: .getLayout().getClass());
0778: assertEquals("layout padding", 6,
0779: ((BasicOptionPaneUI.ButtonAreaLayout) buttonArea
0780: .getLayout()).getPadding());
0781: assertEquals("border", buttonAreaBorder,
0782: ((JComponent) buttonArea).getBorder());
0783: assertEquals("border insets", new Insets(2, 2, 2, 2),
0784: ((JPanel) buttonArea).getBorder().getBorderInsets(
0785: buttonArea));
0786: assertEquals("number of buttons", 2, buttonArea
0787: .getComponentCount());
0788: assertEquals("button class", JButton.class, buttonArea
0789: .getComponent(0).getClass());
0790: button = (JButton) buttonArea.getComponent(0);
0791: checkButton(button, "Yes", threshold, KeyEvent.VK_Y, 1,
0792: new Insets(2, 8, 2, 8));
0793: assertFalse("custom", paneUI.containsCustomComponents(null));
0794: assertEquals("button class", JButton.class, buttonArea
0795: .getComponent(1).getClass());
0796: button = (JButton) buttonArea.getComponent(1);
0797: checkButton(button, "No", threshold, KeyEvent.VK_N, 1,
0798: new Insets(2, 8, 2, 8));
0799: assertFalse("custom", paneUI.containsCustomComponents(null));
0800: paneUI.optionPane
0801: .setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
0802: buttonArea = paneUI.createButtonArea();
0803: assertEquals("number of buttons", 3, buttonArea
0804: .getComponentCount());
0805: assertEquals("button class", JButton.class, buttonArea
0806: .getComponent(0).getClass());
0807: button = (JButton) buttonArea.getComponent(0);
0808: checkButton(button, "Yes", threshold, KeyEvent.VK_Y, 1,
0809: new Insets(2, 4, 2, 4));
0810: assertFalse("custom", paneUI.containsCustomComponents(null));
0811: assertEquals("button class", JButton.class, buttonArea
0812: .getComponent(1).getClass());
0813: button = (JButton) buttonArea.getComponent(1);
0814: checkButton(button, "No", threshold, KeyEvent.VK_N, 1,
0815: new Insets(2, 4, 2, 4));
0816: assertFalse("custom", paneUI.containsCustomComponents(null));
0817: assertEquals("button class", JButton.class, buttonArea
0818: .getComponent(2).getClass());
0819: button = (JButton) buttonArea.getComponent(2);
0820: checkButton(button, "Cancel", threshold, 0, 1, new Insets(2, 4,
0821: 2, 4));
0822: assertFalse("custom", paneUI.containsCustomComponents(null));
0823: paneUI.optionPane.setOptionType(JOptionPane.CANCEL_OPTION);
0824: buttonArea = paneUI.createButtonArea();
0825: assertEquals("number of buttons", 2, buttonArea
0826: .getComponentCount());
0827: assertEquals("button class", JButton.class, buttonArea
0828: .getComponent(0).getClass());
0829: button = (JButton) (buttonArea.getComponent(0));
0830: checkButton(button, "OK", threshold, 0, 1, new Insets(2, 8, 2,
0831: 8));
0832: assertFalse("custom", paneUI.containsCustomComponents(null));
0833: assertEquals("button class", JButton.class, buttonArea
0834: .getComponent(1).getClass());
0835: button = (JButton) (buttonArea.getComponent(1));
0836: button = (JButton) buttonArea.getComponent(1);
0837: checkButton(button, "Cancel", threshold, 0, 1, new Insets(2, 8,
0838: 2, 8));
0839: assertFalse("custom", paneUI.containsCustomComponents(null));
0840: paneUI.optionPane.setOptionType(JOptionPane.CLOSED_OPTION);
0841: buttonArea = paneUI.createButtonArea();
0842: assertEquals("number of buttons", 1, buttonArea
0843: .getComponentCount());
0844: assertEquals("button class", JButton.class, buttonArea
0845: .getComponent(0).getClass());
0846: button = (JButton) (buttonArea.getComponent(0));
0847: checkButton(button, "OK", threshold, 0, 1, new Insets(2, 8, 2,
0848: 8));
0849: assertFalse("custom", paneUI.containsCustomComponents(null));
0850: Object option1 = new JButton("Tarara");
0851: Object option2 = new Integer(100);
0852: Object option3 = "Eminem must tsum menimE";
0853: Object option4 = new Button("Tarara");
0854: Object option5 = new ImageIcon();
0855: paneUI.optionPane.setOptions(new Object[] { option1, option2,
0856: option3, option4, option5 });
0857: buttonArea = paneUI.createButtonArea();
0858: assertEquals("number of buttons", 5, buttonArea
0859: .getComponentCount());
0860: assertTrue("custom", paneUI.containsCustomComponents(null));
0861: assertEquals("button class", JButton.class, buttonArea
0862: .getComponent(0).getClass());
0863: button = (JButton) (buttonArea.getComponent(0));
0864: checkButton(button, "Tarara", 0, 0, 0, ((JButton) option1)
0865: .getMargin());
0866: assertEquals("button class", JButton.class, buttonArea
0867: .getComponent(1).getClass());
0868: button = (JButton) (buttonArea.getComponent(1));
0869: checkButton(button, "100", threshold, 0, 1, new Insets(2, 14,
0870: 2, 14));
0871: assertEquals("button class", JButton.class, buttonArea
0872: .getComponent(2).getClass());
0873: button = (JButton) (buttonArea.getComponent(2));
0874: checkButton(button, option3.toString(), threshold, 0, 1,
0875: new Insets(2, 14, 2, 14));
0876: assertEquals("button class", Button.class, buttonArea
0877: .getComponent(3).getClass());
0878: assertEquals("button text", "Tarara", ((Button) (buttonArea
0879: .getComponent(3))).getLabel());
0880: assertEquals("button class", JButton.class, buttonArea
0881: .getComponent(4).getClass());
0882: button = (JButton) (buttonArea.getComponent(4));
0883: assertEquals("button Icon", option5, button.getIcon());
0884: checkButton(button, "", threshold, 0, 1, new Insets(2, 14, 2,
0885: 14));
0886: }
0887:
0888: public void testAddButtonComponents() {
0889: Object option1 = new JRadioButton("Tarara");
0890: Object option2 = new Integer(100);
0891: Object option3 = "Eminem must tsum menimE";
0892: Object option4 = new Button("Tarara");
0893: Container buttonArea = new JPanel();
0894: buttonArea.setLayout(new ButtonAreaLayout(true, 6));
0895: paneUI.addButtonComponents(buttonArea, new Object[] { option1,
0896: option2, option3, option4 }, 0);
0897: assertEquals("number of buttons", 4, buttonArea
0898: .getComponentCount());
0899: assertEquals("button class", JRadioButton.class, buttonArea
0900: .getComponent(0).getClass());
0901: assertEquals("button text", "Tarara",
0902: ((JRadioButton) (buttonArea.getComponent(0))).getText());
0903: assertEquals("button class", JButton.class, buttonArea
0904: .getComponent(1).getClass());
0905: assertEquals("button text", "100", ((JButton) (buttonArea
0906: .getComponent(1))).getText());
0907: assertEquals("button class", JButton.class, buttonArea
0908: .getComponent(2).getClass());
0909: assertEquals("button text", option3, ((JButton) (buttonArea
0910: .getComponent(2))).getText());
0911: assertEquals("button class", Button.class, buttonArea
0912: .getComponent(3).getClass());
0913: assertEquals("button text", "Tarara", ((Button) (buttonArea
0914: .getComponent(3))).getLabel());
0915: }
0916:
0917: public void testCreateButtonActionListener() {
0918: ActionListener listener1 = paneUI.createButtonActionListener(0);
0919: ActionListener listener2 = paneUI.createButtonActionListener(1);
0920: assertTrue("listener is not null", listener1 != null);
0921: assertTrue("listener is not null", listener2 != null);
0922: assertEquals(
0923: "listener's class ",
0924: "javax.swing.plaf.basic.BasicOptionPaneUI$ButtonActionListener",
0925: listener1.getClass().getName());
0926: assertTrue("listener is not shared", listener1 != listener2);
0927: }
0928:
0929: public void testGetButtons() {
0930: Object[] buttons = null;
0931: paneUI.optionPane = new JOptionPane();
0932: paneUI.installDefaults();
0933: paneUI.optionPane.setOptionType(JOptionPane.YES_NO_OPTION);
0934: buttons = paneUI.getButtons();
0935: assertEquals("number of buttons", 2, buttons.length);
0936: assertEquals("button text", "Yes", buttons[0].toString());
0937: assertEquals("button text", "No", buttons[1].toString());
0938: paneUI.optionPane
0939: .setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
0940: buttons = paneUI.getButtons();
0941: assertEquals("number of buttons", 3, buttons.length);
0942: assertEquals("button text", "Yes", buttons[0].toString());
0943: assertEquals("button text", "No", buttons[1].toString());
0944: assertEquals("button text", "Cancel", buttons[2].toString());
0945: paneUI.optionPane.setOptionType(JOptionPane.CANCEL_OPTION);
0946: buttons = paneUI.getButtons();
0947: assertEquals("number of buttons", 2, buttons.length);
0948: assertEquals("button text", "OK", buttons[0].toString());
0949: assertEquals("button text", "Cancel", buttons[1].toString());
0950: paneUI.optionPane.setOptionType(JOptionPane.CLOSED_OPTION);
0951: buttons = paneUI.getButtons();
0952: assertEquals("number of buttons", 1, buttons.length);
0953: assertEquals("button text", "OK", buttons[0].toString());
0954: Object option1 = new JButton("Tarara");
0955: Object option2 = new Integer(100);
0956: Object option3 = "Eminem must tsum menimE";
0957: paneUI.optionPane.setOptions(new Object[] { option1, option2,
0958: option3 });
0959: buttons = paneUI.getButtons();
0960: assertEquals("number of buttons", 3, buttons.length);
0961: assertEquals("button ", option1, buttons[0]);
0962: assertEquals("button ", option2, buttons[1]);
0963: assertEquals("button ", option3, buttons[2]);
0964: }
0965:
0966: // Regression for HARMONY-2901
0967: public void testGetButtonsNull() {
0968: assertNull(paneUI.optionPane);
0969: assertNull(paneUI.getButtons());
0970: }
0971:
0972: public void testGetSizeButtonsToSameWidth() {
0973: assertTrue(paneUI.getSizeButtonsToSameWidth());
0974: }
0975:
0976: public void testGetInitialValueIndex() {
0977: JOptionPane optionPane = new JOptionPane("Message",
0978: JOptionPane.ERROR_MESSAGE, JOptionPane.CLOSED_OPTION,
0979: null, new Object[] { "1", "2", "3" }, "1");
0980: paneUI = (BasicOptionPaneUI) optionPane.getUI();
0981: assertEquals(0, paneUI.getInitialValueIndex());
0982: optionPane = new JOptionPane("Message",
0983: JOptionPane.ERROR_MESSAGE, JOptionPane.CLOSED_OPTION,
0984: null, new Object[] { "1", "2", "3", "2" }, "3");
0985: paneUI = (BasicOptionPaneUI) optionPane.getUI();
0986: assertEquals(2, paneUI.getInitialValueIndex());
0987: optionPane = new JOptionPane("Message",
0988: JOptionPane.ERROR_MESSAGE, JOptionPane.CLOSED_OPTION,
0989: null, new Object[] { "1", "2", "3", "2", "2" }, "2");
0990: paneUI = (BasicOptionPaneUI) optionPane.getUI();
0991: assertEquals(4, paneUI.getInitialValueIndex());
0992: optionPane = new JOptionPane("Message",
0993: JOptionPane.ERROR_MESSAGE, JOptionPane.CLOSED_OPTION,
0994: null, new Object[] { "1", "2", "3", "2" }, null);
0995: paneUI = (BasicOptionPaneUI) optionPane.getUI();
0996: assertEquals(-1, paneUI.getInitialValueIndex());
0997: optionPane = new JOptionPane("Message",
0998: JOptionPane.ERROR_MESSAGE, JOptionPane.CLOSED_OPTION,
0999: null, new Object[] { "1", "2", "3", "2" }, "4");
1000: paneUI = (BasicOptionPaneUI) optionPane.getUI();
1001: assertEquals(-1, paneUI.getInitialValueIndex());
1002: optionPane = new JOptionPane("Message",
1003: JOptionPane.ERROR_MESSAGE, JOptionPane.CLOSED_OPTION,
1004: null, null, null);
1005: paneUI = (BasicOptionPaneUI) optionPane.getUI();
1006: assertEquals(0, paneUI.getInitialValueIndex());
1007: }
1008:
1009: // Regression for HARMONY-2901
1010: public void testGetInitialValueIndexNull() throws Exception {
1011: assertNull(paneUI.optionPane);
1012: assertEquals(-1, paneUI.getInitialValueIndex());
1013: }
1014:
1015: public void testResetInputValue() {
1016: //TODO Implement resetInputValue().
1017: }
1018: }
|