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: * Created on 12.10.2004
0021: * * Window - Preferences - Java - Code Style - Code Templates
0022: *
0023: */package javax.swing;
0024:
0025: import java.awt.Button;
0026: import java.awt.Canvas;
0027: import java.awt.Color;
0028: import java.awt.Component;
0029: import java.awt.ComponentOrientation;
0030: import java.awt.Container;
0031: import java.awt.Dimension;
0032: import java.awt.Font;
0033: import java.awt.FontMetrics;
0034: import java.awt.Frame;
0035: import java.awt.Panel;
0036: import java.awt.Point;
0037: import java.awt.Rectangle;
0038: import java.awt.Window;
0039: import java.awt.event.ActionEvent;
0040: import java.awt.event.ActionListener;
0041: import java.awt.event.InputEvent;
0042: import java.awt.event.KeyEvent;
0043: import java.awt.event.MouseEvent;
0044: import java.awt.image.BufferedImage;
0045: import java.beans.PropertyChangeListener;
0046: import javax.accessibility.AccessibleState;
0047: import javax.accessibility.AccessibleStateSet;
0048: import javax.swing.border.EmptyBorder;
0049: import javax.swing.plaf.ComponentUI;
0050: import javax.swing.plaf.UIResource;
0051:
0052: public class SwingUtilitiesTest extends SwingTestCase implements
0053: SwingConstants {
0054: class UIActionMap extends ActionMap implements UIResource {
0055: private static final long serialVersionUID = 1L;
0056: }
0057:
0058: class UIInputMap extends InputMap implements UIResource {
0059: private static final long serialVersionUID = 1L;
0060: }
0061:
0062: /*
0063: * Class under test for String layoutCompoundLabel(JComponent, FontMetrics, String, Icon, int, int, int, int, Rectangle, Rectangle, Rectangle, int)
0064: */
0065: public void testLayoutCompoundLabelJComponentFontMetricsStringIconintintintintRectangleRectangleRectangleint() {
0066: JComponent panel = new JPanel();
0067: Font font = new Font("Fixed", Font.PLAIN, 12);
0068: FontMetrics metrics = getFontMetrics(font);
0069: Rectangle viewR = new Rectangle(0, 0, 150, 150);
0070: Rectangle iconR = new Rectangle(0, 0, 0, 0);
0071: Rectangle textR = new Rectangle(0, 0, 0, 0);
0072: String initialString = "Long enough text for this label, can you see that it is clipped now?";
0073: Icon icon = new ImageIcon(new BufferedImage(30, 40,
0074: BufferedImage.TYPE_4BYTE_ABGR));
0075: String clippedStr = null;
0076: clippedStr = SwingUtilities.layoutCompoundLabel(panel, metrics,
0077: initialString, icon, TOP, CENTER, BOTTOM, RIGHT, viewR,
0078: iconR, textR, 30);
0079: assertEquals("clipped string ", "Long...", clippedStr);
0080: assertEquals("icon rectangle ", new Rectangle(3, 0, 30, 40),
0081: iconR);
0082: assertEquals("text rectangle ", new Rectangle(63, 2, 84, 38),
0083: textR);
0084: viewR = new Rectangle(20, 20, 100, 100);
0085: iconR = new Rectangle(0, 0, 0, 0);
0086: textR = new Rectangle(0, 0, 0, 0);
0087: clippedStr = SwingUtilities.layoutCompoundLabel(panel, metrics,
0088: initialString, icon, CENTER, CENTER, TOP, RIGHT, viewR,
0089: iconR, textR, 30);
0090: assertEquals("clipped string ", "...", clippedStr);
0091: assertEquals("icon rectangle ", new Rectangle(22, 50, 30, 40),
0092: iconR);
0093: assertEquals("text rectangle ", new Rectangle(82, 50, 36, 38),
0094: textR);
0095: panel
0096: .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
0097: viewR = new Rectangle(20, 20, 130, 100);
0098: iconR = new Rectangle(0, 0, 0, 0);
0099: textR = new Rectangle(0, 0, 0, 0);
0100: clippedStr = SwingUtilities.layoutCompoundLabel(panel, metrics,
0101: initialString, icon, CENTER, CENTER, TOP, LEADING,
0102: viewR, iconR, textR, 30);
0103: assertEquals("clipped string ", "Lo...", clippedStr);
0104: assertEquals("icon rectangle ", new Rectangle(25, 50, 30, 40),
0105: iconR);
0106: assertEquals("text rectangle ", new Rectangle(85, 50, 60, 38),
0107: textR);
0108: viewR = new Rectangle(0, 0, 292, 200);
0109: iconR = new Rectangle(0, 0, 0, 0);
0110: textR = new Rectangle(0, 0, 0, 0);
0111: JLabel label = new JLabel();
0112: icon = new ImageIcon(new BufferedImage(16, 16,
0113: BufferedImage.TYPE_3BYTE_BGR));
0114: metrics = getFontMetrics(font, 34);
0115: clippedStr = SwingUtilities.layoutCompoundLabel(label, metrics,
0116: "Hello world you are great!", icon, BOTTOM, CENTER,
0117: BOTTOM, TRAILING, viewR, iconR, textR, label
0118: .getIconTextGap());
0119: assertEquals("clipped string ", "Hello...", clippedStr);
0120: assertEquals("icon rectangle ", new Rectangle(0, 184, 16, 16),
0121: iconR);
0122: assertEquals("text rectangle ",
0123: new Rectangle(20, 96, 272, 104), textR);
0124: if (isHarmony()) {
0125: icon = new ImageIcon(new BufferedImage(52, 64,
0126: BufferedImage.TYPE_4BYTE_ABGR));
0127: metrics = getFontMetrics(font, 12);
0128: viewR = new Rectangle(120, 120, 1280, 280);
0129: clippedStr = SwingUtilities.layoutCompoundLabel(panel,
0130: metrics, initialString, icon, CENTER, CENTER,
0131: CENTER, LEADING, viewR, iconR, textR, 30);
0132: assertEquals(
0133: "clipped string ",
0134: "Long enough text for this label, can you see that it is clipped now?",
0135: clippedStr);
0136: assertEquals("icon rectangle ", new Rectangle(311, 228, 52,
0137: 64), iconR);
0138: assertEquals("text rectangle ", new Rectangle(393, 241,
0139: 816, 38), textR);
0140: panel
0141: .applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
0142: viewR = new Rectangle(120, 120, 40, 40);
0143: clippedStr = SwingUtilities.layoutCompoundLabel(panel,
0144: metrics, initialString, icon, CENTER, CENTER,
0145: CENTER, LEADING, viewR, iconR, textR, 30);
0146: assertEquals("clipped string ", "...", clippedStr);
0147: assertEquals("icon rectangle ", new Rectangle(147, 108, 52,
0148: 64), iconR);
0149: assertEquals("text rectangle ", new Rectangle(81, 121, 36,
0150: 38), textR);
0151: }
0152: }
0153:
0154: /*
0155: * Class under test for String layoutCompoundLabel(FontMetrics, String, Icon, int, int, int, int, Rectangle, Rectangle, Rectangle, int)
0156: */
0157: public void testLayoutCompoundLabelFontMetricsStringIconintintintintRectangleRectangleRectangleint() {
0158: Font font = new Font("Fixed", Font.PLAIN, 12);
0159: FontMetrics metrics = getFontMetrics(font);
0160: Rectangle viewR = new Rectangle(0, 0, 10, 10);
0161: Rectangle iconR = new Rectangle(0, 0, 0, 0);
0162: Rectangle textR = new Rectangle(0, 0, 0, 0);
0163: String clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0164: null, null, CENTER, LEADING, CENTER, LEADING, viewR,
0165: iconR, textR, 2);
0166: assertEquals("clipped string ", "", clippedStr);
0167: assertTrue("icon rectangle ", iconR.isEmpty());
0168: assertTrue("text rectangle ", textR.isEmpty());
0169: viewR = new Rectangle(0, 0, 150, 150);
0170: iconR = new Rectangle(0, 0, 0, 0);
0171: textR = new Rectangle(0, 0, 0, 0);
0172: String initialString = "Long enough text for this label, can you see that it is clipped now?";
0173: Icon icon = null;
0174: metrics = getFontMetrics(font, 5);
0175: clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0176: initialString, icon, TOP, CENTER, BOTTOM, RIGHT, viewR,
0177: iconR, textR, 30);
0178: assertEquals("clipped string ",
0179: "Long enough text for this l...", clippedStr);
0180: assertTrue("icon rectangle ", iconR.isEmpty());
0181: assertEquals("text rectangle ", new Rectangle(0, 0, 150, 17),
0182: textR);
0183: viewR = new Rectangle(0, 0, 150, 150);
0184: iconR = new Rectangle(0, 0, 0, 0);
0185: textR = new Rectangle(0, 0, 0, 0);
0186: metrics = getFontMetrics(font, 5);
0187: icon = new ImageIcon(new BufferedImage(30, 40,
0188: BufferedImage.TYPE_4BYTE_ABGR));
0189: clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0190: initialString, icon, TOP, CENTER, BOTTOM, RIGHT, viewR,
0191: iconR, textR, 30);
0192: assertEquals("clipped string ", "Long enough tex...",
0193: clippedStr);
0194: assertEquals("icon rectangle ", new Rectangle(0, 0, 30, 40),
0195: iconR);
0196: assertEquals("text rectangle ", new Rectangle(60, 23, 90, 17),
0197: textR);
0198: viewR = new Rectangle(20, 20, 100, 100);
0199: iconR = new Rectangle(0, 0, 0, 0);
0200: textR = new Rectangle(0, 0, 0, 0);
0201: metrics = getFontMetrics(font, 5);
0202: clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0203: initialString, icon, CENTER, CENTER, TOP, RIGHT, viewR,
0204: iconR, textR, 30);
0205: assertEquals("clipped string ", "Long ...", clippedStr);
0206: assertEquals("icon rectangle ", new Rectangle(20, 50, 30, 40),
0207: iconR);
0208: assertEquals("text rectangle ", new Rectangle(80, 50, 40, 17),
0209: textR);
0210: icon = new ImageIcon(new BufferedImage(52, 64,
0211: BufferedImage.TYPE_4BYTE_ABGR));
0212: metrics = getFontMetrics(font, 24);
0213: viewR = new Rectangle(100, 100, 1500, 300);
0214: iconR = new Rectangle(0, 0, 0, 0);
0215: textR = new Rectangle(0, 0, 0, 0);
0216: clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0217: initialString, icon, CENTER, CENTER, CENTER, RIGHT,
0218: viewR, iconR, textR, 30);
0219: assertEquals(
0220: "clipped string ",
0221: "Long enough text for this label, can you see that it is ...",
0222: clippedStr);
0223: assertEquals("icon rectangle ",
0224: new Rectangle(101, 218, 52, 64), iconR);
0225: assertEquals("text rectangle ", new Rectangle(183, 213, 1416,
0226: 74), textR);
0227: icon = new ImageIcon(new BufferedImage(52, 64,
0228: BufferedImage.TYPE_4BYTE_ABGR));
0229: viewR = new Rectangle(100, 100, 1500, 300);
0230: iconR = new Rectangle(0, 0, 0, 0);
0231: textR = new Rectangle(0, 0, 0, 0);
0232: clippedStr = SwingUtilities.layoutCompoundLabel(metrics, null,
0233: icon, CENTER, CENTER, CENTER, RIGHT, viewR, iconR,
0234: textR, 30);
0235: assertEquals("clipped string ", "", clippedStr);
0236: assertEquals("icon rectangle ",
0237: new Rectangle(824, 218, 52, 64), iconR);
0238: assertTrue("text rectangle ", textR.isEmpty());
0239: icon = null;
0240: metrics = getFontMetrics(font, 6);
0241: viewR = new Rectangle(0, 0, 4, 300);
0242: iconR = new Rectangle(0, 0, 0, 0);
0243: textR = new Rectangle(0, 0, 0, 0);
0244: clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0245: initialString, icon, CENTER, CENTER, CENTER, RIGHT,
0246: viewR, iconR, textR, 0);
0247: assertEquals("clipped string ", "...", clippedStr);
0248: icon = null;
0249: viewR = new Rectangle(0, 0, 2, 300);
0250: iconR = new Rectangle(0, 0, 0, 0);
0251: textR = new Rectangle(0, 0, 0, 0);
0252: clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0253: initialString, icon, CENTER, CENTER, CENTER, LEADING,
0254: viewR, iconR, textR, 0);
0255: assertEquals("clipped string ", "...", clippedStr);
0256: icon = null;
0257: metrics = getFontMetrics(font, 30);
0258: viewR = new Rectangle(0, 0, 500, 300);
0259: iconR = new Rectangle(0, 0, 0, 0);
0260: textR = new Rectangle(0, 0, 0, 0);
0261: clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0262: initialString, icon, CENTER, CENTER, CENTER, RIGHT,
0263: viewR, iconR, textR, 0);
0264: assertEquals("clipped string ", "Long enough t...", clippedStr);
0265: icon = null;
0266: metrics = getFontMetrics(font, 16);
0267: viewR = new Rectangle(0, 0, 500, 300);
0268: iconR = new Rectangle(0, 0, 0, 0);
0269: textR = new Rectangle(0, 0, 0, 0);
0270: clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0271: "WWWWWWWWWWWWWWWWWWWW,,,,,,,,,,,,,,,,,,,,,,", icon,
0272: CENTER, CENTER, CENTER, RIGHT, viewR, iconR, textR, 0);
0273: assertEquals("clipped string ",
0274: "WWWWWWWWWWWWWWWWWWWW,,,,,,,,...", clippedStr);
0275: icon = new ImageIcon(new BufferedImage(52, 64,
0276: BufferedImage.TYPE_4BYTE_ABGR));
0277: metrics = getFontMetrics(font, 30);
0278: viewR = new Rectangle(100, 100, 1500, 300);
0279: iconR = new Rectangle(0, 0, 0, 0);
0280: textR = new Rectangle(0, 0, 0, 0);
0281: clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0282: initialString, icon, CENTER, CENTER, TOP, CENTER,
0283: viewR, iconR, textR, 30);
0284: assertEquals("clipped string ",
0285: "Long enough text for this label, can you see th...",
0286: clippedStr);
0287: assertEquals("icon rectangle ",
0288: new Rectangle(824, 279, 52, 64), iconR);
0289: assertEquals("text rectangle ", new Rectangle(100, 157, 1500,
0290: 92), textR);
0291: icon = new ImageIcon(new BufferedImage(52, 64,
0292: BufferedImage.TYPE_4BYTE_ABGR));
0293: metrics = getFontMetrics(font, 28);
0294: viewR = new Rectangle(100, 100, 1500, 300);
0295: iconR = new Rectangle(0, 0, 0, 0);
0296: textR = new Rectangle(0, 0, 0, 0);
0297: clippedStr = SwingUtilities.layoutCompoundLabel(metrics,
0298: initialString, icon, CENTER, CENTER, TOP, RIGHT, viewR,
0299: iconR, textR, 30);
0300: assertEquals("clipped string ",
0301: "Long enough text for this label, can you see th...",
0302: clippedStr);
0303: assertEquals("icon rectangle ",
0304: new Rectangle(109, 207, 52, 64), iconR);
0305: assertEquals("text rectangle ", new Rectangle(191, 207, 1400,
0306: 86), textR);
0307: if (isHarmony()) {
0308: Rectangle iconR2 = new Rectangle();
0309: Rectangle textR2 = new Rectangle();
0310: String clippedStr2 = SwingUtilities.layoutCompoundLabel(
0311: metrics, initialString, icon, CENTER, LEADING, TOP,
0312: TRAILING, viewR, iconR2, textR2, 30);
0313: assertEquals(clippedStr, clippedStr2);
0314: assertEquals(iconR, iconR2);
0315: assertEquals(textR, textR2);
0316: clippedStr2 = SwingUtilities.layoutCompoundLabel(metrics,
0317: initialString, icon, CENTER, TRAILING, TOP,
0318: LEADING, viewR, iconR2, textR2, 30);
0319: assertEquals(clippedStr, clippedStr2);
0320: assertEquals(iconR, iconR2);
0321: assertEquals(textR, textR2);
0322: }
0323: }
0324:
0325: public void testNotifyAction() {
0326: class NATestAction implements Action {
0327: public boolean enabled = true;
0328:
0329: protected ActionEvent eventHappened = null;
0330:
0331: public Object valueSaved = null;
0332:
0333: public void actionPerformed(final ActionEvent event) {
0334: eventHappened = event;
0335: }
0336:
0337: public boolean isEnabled() {
0338: return enabled;
0339: }
0340:
0341: public void setEnabled(final boolean e) {
0342: enabled = e;
0343: }
0344:
0345: public void addPropertyChangeListener(
0346: final PropertyChangeListener arg0) {
0347: }
0348:
0349: public void removePropertyChangeListener(
0350: final PropertyChangeListener arg0) {
0351: }
0352:
0353: public Object getValue(final String valueName) {
0354: if (valueName == Action.ACTION_COMMAND_KEY) {
0355: return valueSaved;
0356: }
0357: return null;
0358: }
0359:
0360: public void putValue(final String valueName,
0361: final Object value) {
0362: if (valueName == Action.ACTION_COMMAND_KEY) {
0363: valueSaved = value;
0364: }
0365: }
0366: }
0367: ;
0368: NATestAction action = new NATestAction();
0369: JComponent source = new JPanel();
0370: int modifiers = InputEvent.CTRL_DOWN_MASK;
0371: KeyEvent event = new KeyEvent(source, 0, 0, modifiers,
0372: KeyEvent.VK_CUT, 'a');
0373: boolean result = false;
0374: try {
0375: SwingUtilities.notifyAction(null, KeyStroke
0376: .getKeyStroke('a'), event, this , modifiers);
0377: } catch (NullPointerException e) {
0378: fail("NPE should not be thrown");
0379: }
0380: result = SwingUtilities.notifyAction(action, KeyStroke
0381: .getKeyStroke('a'), event, this , modifiers);
0382: assertTrue(action.eventHappened != null);
0383: assertFalse(event.isConsumed());
0384: assertTrue(result);
0385: action.setEnabled(false);
0386: action.eventHappened = null;
0387: result = SwingUtilities.notifyAction(action, KeyStroke
0388: .getKeyStroke('a'), event, this , modifiers);
0389: assertFalse(result);
0390: assertFalse(event.isConsumed());
0391: assertNull(action.eventHappened);
0392: action.setEnabled(true);
0393: result = SwingUtilities.notifyAction(action, KeyStroke
0394: .getKeyStroke('a'), event, this , modifiers);
0395: assertTrue(result);
0396: assertTrue(action.eventHappened != null);
0397: assertFalse(event.isConsumed());
0398: assertTrue(action.eventHappened.getActionCommand().equals("a"));
0399: action.valueSaved = "Yo!";
0400: result = SwingUtilities.notifyAction(action, KeyStroke
0401: .getKeyStroke('a'), event, this , modifiers);
0402: assertTrue(result);
0403: assertTrue(action.eventHappened != null);
0404: assertFalse(event.isConsumed());
0405: assertTrue(action.eventHappened.getActionCommand().equals(
0406: action.valueSaved));
0407: event = new KeyEvent(source, 0, 0, modifiers,
0408: KeyEvent.VK_UNDEFINED, KeyEvent.CHAR_UNDEFINED);
0409: action.valueSaved = null;
0410: result = SwingUtilities.notifyAction(action, KeyStroke
0411: .getKeyStroke('a'), event, this , modifiers);
0412: assertTrue(result);
0413: assertFalse(event.isConsumed());
0414: assertTrue(action.eventHappened != null);
0415: assertNull(action.eventHappened.getActionCommand());
0416: event = new KeyEvent(source, 0, 0, modifiers,
0417: KeyEvent.VK_UNDEFINED, KeyEvent.CHAR_UNDEFINED);
0418: action.setEnabled(false);
0419: result = SwingUtilities.notifyAction(action, KeyStroke
0420: .getKeyStroke('a'), event, this , modifiers);
0421: assertFalse(result);
0422: assertFalse(event.isConsumed());
0423: assertTrue(action.eventHappened != null);
0424: assertNull(action.eventHappened.getActionCommand());
0425: }
0426:
0427: @SuppressWarnings("deprecation")
0428: public void testConvertMouseEvent() {
0429: MouseEvent eventBefore, eventToPass, eventAfter;
0430: JWindow window1 = new JWindow();
0431: JWindow window2 = new JWindow();
0432: final JComponent panel1 = new JPanel();
0433: final JComponent panel2 = new JPanel();
0434: final JComponent panel3 = new JPanel();
0435: panel1.setPreferredSize(new Dimension(100, 100));
0436: panel2.setPreferredSize(new Dimension(100, 100));
0437: panel3.setPreferredSize(new Dimension(100, 100));
0438: panel1.add(panel2);
0439: panel1.setBorder(new EmptyBorder(15, 15, 15, 15));
0440: window1.getContentPane().add(panel1);
0441: window1.setLocation(100, 100);
0442: window1.pack();
0443: window1.show();
0444: window2.getContentPane().add(panel3);
0445: window2.setLocation(200, 200);
0446: window2.pack();
0447: window2.show();
0448: testExceptionalCase(new NullPointerCase() {
0449: @Override
0450: public void exceptionalAction() throws Exception {
0451: SwingUtilities.convertMouseEvent(null, null, null);
0452: }
0453: });
0454: testExceptionalCase(new NullPointerCase() {
0455: @Override
0456: public void exceptionalAction() throws Exception {
0457: SwingUtilities.convertMouseEvent(panel1, null, panel2);
0458: }
0459: });
0460: int id = 111;
0461: int when = 200;
0462: int x = 0;
0463: int y = 0;
0464: int clickCount = 3;
0465: int modifiers = InputEvent.CTRL_DOWN_MASK;
0466: boolean isPopupTrigger = false;
0467: int button = 2;
0468: eventBefore = new MouseEvent(panel1, id, when, modifiers, x, y,
0469: clickCount, isPopupTrigger, button);
0470: eventToPass = new MouseEvent(panel1, id, when, modifiers, x, y,
0471: clickCount, isPopupTrigger, button);
0472: final MouseEvent illegalEvent = eventBefore;
0473: testExceptionalCase(new IllegalArgumentCase() {
0474: @Override
0475: public void exceptionalAction() throws Exception {
0476: SwingUtilities.convertMouseEvent(null, illegalEvent,
0477: null);
0478: }
0479: });
0480: eventBefore = new MouseEvent(panel1, id, when, modifiers, x, y,
0481: clickCount, isPopupTrigger, button);
0482: eventToPass = new MouseEvent(panel1, id, when, modifiers, x, y,
0483: clickCount, isPopupTrigger, button);
0484: eventAfter = SwingUtilities.convertMouseEvent(panel1,
0485: eventToPass, panel3);
0486: assertTrue(eventBefore.getX() == eventToPass.getX()
0487: && eventBefore.getY() == eventToPass.getY());
0488: assertEquals(-100, eventAfter.getX());
0489: assertEquals(-100, eventAfter.getY());
0490: assertEquals(panel3, eventAfter.getSource());
0491: eventBefore = new MouseEvent(panel1, id, when, modifiers, x, y,
0492: clickCount, isPopupTrigger, button);
0493: eventToPass = new MouseEvent(panel1, id, when, modifiers, x, y,
0494: clickCount, isPopupTrigger, button);
0495: eventAfter = SwingUtilities.convertMouseEvent(panel3,
0496: eventToPass, panel2);
0497: assertTrue(eventBefore.getX() == eventToPass.getX()
0498: && eventBefore.getY() == eventToPass.getY());
0499: assertEquals(100, eventAfter.getX());
0500: assertEquals(80, eventAfter.getY());
0501: assertEquals(panel2, eventAfter.getSource());
0502: eventBefore = new MouseEvent(panel1, id, when, modifiers, x, y,
0503: clickCount, isPopupTrigger, button);
0504: eventToPass = new MouseEvent(panel1, id, when, modifiers, x, y,
0505: clickCount, isPopupTrigger, button);
0506: eventAfter = SwingUtilities.convertMouseEvent(panel2,
0507: eventToPass, null);
0508: assertTrue(eventBefore.getX() == eventToPass.getX()
0509: && eventBefore.getY() == eventToPass.getY());
0510: assertEquals(0, eventAfter.getX());
0511: assertEquals(20, eventAfter.getY());
0512: assertEquals(panel2, eventAfter.getSource());
0513: eventBefore = new MouseEvent(panel1, id, when, modifiers, x, y,
0514: clickCount, isPopupTrigger, button);
0515: eventToPass = new MouseEvent(panel1, id, when, modifiers, x, y,
0516: clickCount, isPopupTrigger, button);
0517: eventAfter = SwingUtilities.convertMouseEvent(null,
0518: eventToPass, panel2);
0519: assertTrue(eventBefore.getX() == eventToPass.getX()
0520: && eventBefore.getY() == eventToPass.getY());
0521: assertEquals(0, eventAfter.getX());
0522: assertEquals(-20, eventAfter.getY());
0523: assertEquals(panel2, eventAfter.getSource());
0524: }
0525:
0526: @SuppressWarnings("deprecation")
0527: public void testConvertRectangle() {
0528: int width = 100;
0529: int height = 200;
0530: Rectangle rectBefore, rectToPass, rectAfter;
0531: JWindow window1 = new JWindow();
0532: JWindow window2 = new JWindow();
0533: final JComponent panel1 = new JPanel();
0534: final JComponent panel2 = new JPanel();
0535: final JComponent panel3 = new JPanel();
0536: panel1.setPreferredSize(new Dimension(100, 100));
0537: panel2.setPreferredSize(new Dimension(100, 100));
0538: panel3.setPreferredSize(new Dimension(100, 100));
0539: //panel1.setLocation(50, 50);
0540: //panel2.setLocation(70, 70);
0541: //panel3.setLocation(500, 500);
0542: panel1.add(panel2);
0543: panel1.setBorder(new EmptyBorder(15, 15, 15, 15));
0544: //panel2.setBorder(new EmptyBorder(27, 27, 27, 27));
0545: window1.getContentPane().add(panel1);
0546: window1.setLocation(100, 100);
0547: window1.pack();
0548: window1.show();
0549: window2.getContentPane().add(panel3);
0550: window2.setLocation(200, 200);
0551: window2.pack();
0552: window2.show();
0553: testExceptionalCase(new NullPointerCase() {
0554: @Override
0555: public void exceptionalAction() throws Exception {
0556: SwingUtilities.convertRectangle(null, null, null);
0557: }
0558: });
0559: testExceptionalCase(new NullPointerCase() {
0560: @Override
0561: public void exceptionalAction() throws Exception {
0562: SwingUtilities.convertRectangle(panel1, null, panel2);
0563: }
0564: });
0565: rectBefore = new Rectangle(0, 0, width, height);
0566: rectToPass = new Rectangle(rectBefore);
0567: rectAfter = SwingUtilities.convertRectangle(null, rectBefore,
0568: null);
0569: assertEquals(rectBefore, rectAfter);
0570: assertEquals(rectBefore, rectToPass);
0571: rectBefore = new Rectangle(0, 0, width, height);
0572: rectToPass = new Rectangle(rectBefore);
0573: rectAfter = SwingUtilities.convertRectangle(panel1, rectToPass,
0574: panel3);
0575: assertEquals(rectBefore, rectToPass);
0576: assertFalse(rectAfter.equals(rectBefore));
0577: assertEquals(rectAfter,
0578: new Rectangle(-100, -100, width, height));
0579: rectBefore = new Rectangle(0, 0, width, height);
0580: rectToPass = new Rectangle(rectBefore);
0581: rectAfter = SwingUtilities.convertRectangle(panel3, rectToPass,
0582: panel2);
0583: assertEquals(rectBefore, rectToPass);
0584: assertFalse(rectAfter.equals(rectBefore));
0585: assertEquals(new Rectangle(100, 80, width, height), rectAfter);
0586: rectBefore = new Rectangle(0, 0, width, height);
0587: rectToPass = new Rectangle(rectBefore);
0588: rectAfter = SwingUtilities.convertRectangle(panel2, rectToPass,
0589: null);
0590: assertEquals(rectBefore, rectToPass);
0591: assertFalse(rectAfter.equals(rectBefore));
0592: assertEquals(new Rectangle(0, 20, width, height), rectAfter);
0593: rectBefore = new Rectangle(0, 0, width, height);
0594: rectToPass = new Rectangle(rectBefore);
0595: rectAfter = SwingUtilities.convertRectangle(null, rectToPass,
0596: panel2);
0597: assertEquals(rectBefore, rectToPass);
0598: assertFalse(rectAfter.equals(rectBefore));
0599: assertEquals(new Rectangle(0, -20, width, height), rectAfter);
0600: }
0601:
0602: /*
0603: * Class under test for Point convertPoint(Component, int, int, Component)
0604: * this function is being tested by testConvertPointComponentPointComponent()
0605: */
0606: public void testConvertPointComponentintintComponent() {
0607: }
0608:
0609: /*
0610: * Class under test for Point convertPoint(Component, Point, Component)
0611: */
0612: @SuppressWarnings("deprecation")
0613: public void testConvertPointComponentPointComponent() {
0614: Point pointBefore, pointToPass, pointAfter;
0615: JWindow window1 = new JWindow();
0616: JWindow window2 = new JWindow();
0617: JComponent panel1 = new JPanel();
0618: JComponent panel2 = new JPanel();
0619: JComponent panel3 = new JPanel();
0620: panel1.setPreferredSize(new Dimension(150, 150));
0621: panel2.setPreferredSize(new Dimension(100, 100));
0622: panel3.setPreferredSize(new Dimension(100, 100));
0623: panel1.add(panel2);
0624: panel1.setBorder(new EmptyBorder(15, 15, 15, 15));
0625: panel2.setBorder(new EmptyBorder(25, 25, 25, 25));
0626: panel3.setBorder(new EmptyBorder(35, 35, 35, 35));
0627: window1.getContentPane().add(panel1);
0628: window1.setLocation(100, 100);
0629: window1.pack();
0630: window1.show();
0631: window2.getContentPane().add(panel3);
0632: window2.setLocation(200, 200);
0633: window2.pack();
0634: window2.show();
0635: pointAfter = new Point(1, 1);
0636: try {
0637: pointAfter = SwingUtilities.convertPoint(null, null, null);
0638: } catch (NullPointerException e) {
0639: fail("NPE should not be thrown");
0640: }
0641: assertNull(pointAfter);
0642: pointBefore = new Point(0, 0);
0643: pointToPass = new Point(pointBefore);
0644: pointAfter = SwingUtilities.convertPoint(null, pointBefore,
0645: null);
0646: assertTrue(pointBefore.equals(pointAfter));
0647: assertTrue(pointBefore.equals(pointToPass));
0648: pointBefore = new Point(0, 0);
0649: pointToPass = new Point(pointBefore);
0650: pointAfter = SwingUtilities.convertPoint(panel1, pointToPass,
0651: panel3);
0652: assertTrue(pointBefore.equals(pointToPass));
0653: assertFalse(pointAfter.equals(pointBefore));
0654: assertTrue(pointAfter.equals(new Point(-100, -100)));
0655: pointBefore = new Point(0, 0);
0656: pointToPass = new Point(pointBefore);
0657: pointAfter = SwingUtilities.convertPoint(panel3, pointToPass,
0658: panel2);
0659: assertEquals(pointBefore, pointToPass);
0660: assertFalse(pointAfter.equals(pointBefore));
0661: assertEquals(new Point(75, 80), pointAfter);
0662: pointBefore = new Point(0, 0);
0663: pointToPass = new Point(pointBefore);
0664: pointAfter = SwingUtilities.convertPoint(panel2, pointToPass,
0665: null);
0666: assertEquals(pointBefore, pointToPass);
0667: assertFalse(pointAfter.equals(pointBefore));
0668: assertEquals(new Point(25, 20), pointAfter);
0669: pointBefore = new Point(0, 0);
0670: pointToPass = new Point(pointBefore);
0671: pointAfter = SwingUtilities.convertPoint(null, pointToPass,
0672: panel2);
0673: assertEquals(pointBefore, pointToPass);
0674: assertFalse(pointAfter.equals(pointBefore));
0675: assertEquals(new Point(-25, -20), pointAfter);
0676: }
0677:
0678: public void testGetAccessibleAt() {
0679: // TODO uncomment when Accessibility is implemented
0680: /*
0681: class JComponentInaccessible extends JComponent {
0682: public String getUIClassID() {
0683: return "PanelUI";
0684: }
0685: public void updateUI() {
0686: setUI((PanelUI)UIManager.getUI(this));
0687: }
0688: public JComponentInaccessible() {
0689: setDoubleBuffered(true);
0690: setOpaque(true);
0691: }
0692: };
0693: JComponentInaccessible inaccessible = new JComponentInaccessible();
0694: Point checkPoint = null;
0695: JWindow window1 = new JWindow();
0696: JComponent panel1 = new JPanel();
0697: JComponent panel2 = new JPanel();
0698: JComponent panel3 = new JPanel();
0699: panel1.setBackground(Color.GREEN);
0700: panel2.setPreferredSize(new Dimension(101, 101));
0701: panel2.setBackground(Color.YELLOW);
0702: panel3.setPreferredSize(new Dimension(110, 110));
0703: panel3.setBackground(Color.WHITE);
0704: inaccessible.setBackground(Color.RED);
0705: inaccessible.setForeground(Color.RED);
0706:
0707: inaccessible.setPreferredSize(new Dimension(51, 51));
0708:
0709: panel1.add(panel3);
0710: panel1.add(panel2);
0711: panel1.add(inaccessible);
0712: panel1.setBorder(new EmptyBorder(15, 15, 15, 15));
0713: panel2.setBorder(new EmptyBorder(26, 26, 26, 26));
0714: panel3.setBorder(new EmptyBorder(37, 37, 37, 37));
0715: window1.getContentPane().add(panel1);
0716: window1.pack();
0717: window1.show();
0718: window1.hide();
0719: window1.show();
0720:
0721: boolean thrown = false;
0722: try {
0723: SwingUtilities.getAccessibleAt(null, checkPoint);
0724: } catch (NullPointerException e) {
0725: thrown = true;
0726: }
0727: assertFalse(thrown);
0728: thrown = false;
0729: try {
0730: SwingUtilities.getAccessibleAt(panel1, null);
0731: } catch (NullPointerException e) {
0732: thrown = true;
0733: }
0734: assertTrue(thrown);
0735:
0736: checkPoint = SwingUtilities.convertPoint(panel2, 5, 5, panel1);
0737: assertEquals(panel1, SwingUtilities.getAccessibleAt(panel1, checkPoint));
0738:
0739: checkPoint = SwingUtilities.convertPoint(panel3, 5, 5, panel1);
0740: assertEquals(panel3, SwingUtilities.getAccessibleAt(panel1, checkPoint));
0741:
0742: checkPoint = SwingUtilities.convertPoint(inaccessible, 5, 5, panel1);
0743: assertEquals(panel1, SwingUtilities.getAccessibleAt(panel1, checkPoint));
0744: */
0745: }
0746:
0747: public void testCalculateInnerArea() {
0748: JWindow window = new JWindow();
0749: JComponent panel1 = new JPanel();
0750: JComponent panel2 = new JPanel();
0751: JComponent panel3 = new JPanel();
0752: JComponent component1 = new JPanel();
0753: JComponent component2 = new JPanel();
0754: JComponent component3 = new JPanel();
0755: Rectangle rect = new Rectangle();
0756: Rectangle rect1 = new Rectangle(20, 10, 260, 110);
0757: Rectangle rect2 = new Rectangle(0, 0, 100, 100);
0758: Rectangle rect3 = new Rectangle(0, 0, 50, 50);
0759: panel1.setBorder(new EmptyBorder(10, 20, 30, 40));
0760: panel2.setPreferredSize(new Dimension(100, 100));
0761: component1.setPreferredSize(new Dimension(50, 50));
0762: component2.setPreferredSize(new Dimension(60, 60));
0763: component3.setPreferredSize(new Dimension(70, 70));
0764: panel1.add(panel2);
0765: panel2.add(component1);
0766: panel3.add(component3);
0767: panel1.add(component2);
0768: panel1.add(panel3);
0769: window.getContentPane().add(panel1);
0770: window.pack();
0771: assertNull(SwingUtilities.calculateInnerArea(null, rect));
0772: assertNull(SwingUtilities.calculateInnerArea(null, null));
0773: assertEquals(rect2, SwingUtilities.calculateInnerArea(panel2,
0774: rect));
0775: assertEquals(rect2, SwingUtilities.calculateInnerArea(panel2,
0776: rect));
0777: assertEquals(rect3, SwingUtilities.calculateInnerArea(
0778: component1, rect));
0779: if (isHarmony()) {
0780: assertEquals(rect1, SwingUtilities.calculateInnerArea(
0781: panel1, null));
0782: assertEquals(rect1, SwingUtilities.calculateInnerArea(
0783: panel1, rect));
0784: }
0785: }
0786:
0787: protected boolean contains(final Rectangle[] rects,
0788: final Point point) {
0789: if (rects != null) {
0790: for (int i = 0; i < rects.length; i++) {
0791: if (rects[i].contains(point)) {
0792: return true;
0793: }
0794: }
0795: }
0796: return false;
0797: }
0798:
0799: public void testComputeDifference() {
0800: Rectangle rect11 = new Rectangle(0, 0, 300, 300);
0801: Rectangle rect12 = new Rectangle(100, 100, 100, 100);
0802: // different null testcases
0803: Rectangle rects[] = null;
0804: rects = SwingUtilities.computeDifference(rect12, null);
0805: assertTrue(rects != null && rects.length == 0);
0806: rects = SwingUtilities.computeDifference(null, null);
0807: assertTrue(rects != null && rects.length == 0);
0808: // real recatangles testcases
0809: rects = SwingUtilities.computeDifference(rect12, rect11);
0810: assertTrue(rects != null && rects.length == 0);
0811: rects = SwingUtilities.computeDifference(rect11, rect12);
0812: assertTrue(rects != null && rects.length == 4);
0813: assertTrue(contains(rects, new Point(0, 0)));
0814: assertTrue(contains(rects, new Point(10, 150)));
0815: assertTrue(contains(rects, new Point(10, 270)));
0816: assertTrue(contains(rects, new Point(150, 20)));
0817: assertFalse(contains(rects, new Point(150, 120)));
0818: assertTrue(contains(rects, new Point(150, 220)));
0819: assertTrue(contains(rects, new Point(220, 0)));
0820: assertTrue(contains(rects, new Point(210, 150)));
0821: assertTrue(contains(rects, new Point(210, 280)));
0822: Rectangle rect21 = new Rectangle(0, 0, 300, 300);
0823: Rectangle rect22 = new Rectangle(0, 310, 300, 300);
0824: rects = SwingUtilities.computeDifference(rect21, rect22);
0825: assertTrue(rects != null && (rects.length == 0));
0826: Rectangle rect31 = new Rectangle(100, 100, 100, 100);
0827: Rectangle rect32 = new Rectangle(50, 50, 100, 100);
0828: rects = SwingUtilities.computeDifference(rect31, rect32);
0829: assertTrue(rects != null && rects.length == 2);
0830: assertFalse(contains(rects, new Point(60, 50)));
0831: assertFalse(contains(rects, new Point(60, 140)));
0832: assertFalse(contains(rects, new Point(60, 190)));
0833: assertFalse(contains(rects, new Point(110, 50)));
0834: assertFalse(contains(rects, new Point(110, 140)));
0835: assertTrue(contains(rects, new Point(110, 190)));
0836: assertFalse(contains(rects, new Point(160, 60)));
0837: assertTrue(contains(rects, new Point(160, 140)));
0838: assertTrue(contains(rects, new Point(160, 190)));
0839: Rectangle rect41 = new Rectangle(50, 50, 100, 100);
0840: Rectangle rect42 = new Rectangle(100, 100, 100, 100);
0841: rects = SwingUtilities.computeDifference(rect41, rect42);
0842: assertTrue(rects != null && rects.length == 2);
0843: assertTrue(contains(rects, new Point(60, 50)));
0844: assertTrue(contains(rects, new Point(60, 140)));
0845: assertFalse(contains(rects, new Point(60, 190)));
0846: assertTrue(contains(rects, new Point(110, 50)));
0847: assertFalse(contains(rects, new Point(110, 140)));
0848: assertFalse(contains(rects, new Point(110, 190)));
0849: assertFalse(contains(rects, new Point(160, 60)));
0850: assertFalse(contains(rects, new Point(160, 140)));
0851: assertFalse(contains(rects, new Point(160, 190)));
0852: }
0853:
0854: public void testGetAncestorNamed() {
0855: String name1 = "name1";
0856: String name2 = "name2";
0857: String name3 = "name3";
0858: String name4 = "name4";
0859: String name5 = "name5";
0860: String name6 = "name6";
0861: Window window = new Window(new Frame(name1));
0862: window.setName(name2);
0863: JPanel panel1 = new JPanel();
0864: panel1.setName(name3);
0865: JPanel panel2 = new JPanel();
0866: panel2.setName(name4);
0867: JPanel panel3 = new JPanel();
0868: panel3.setName(name5);
0869: Component component1 = new Canvas();//new Button();
0870: component1.setName(name6);
0871: Component component2 = new JPanel();
0872: Component component3 = new JPanel();
0873: JPanel panel4 = new JPanel();
0874: assertNull(SwingUtilities.getAncestorNamed(name1, null));
0875: assertNull(SwingUtilities.getAncestorNamed(null, component3));
0876: assertNull(SwingUtilities.getAncestorNamed(null, null));
0877: panel4.add(panel1);
0878: panel1.add(panel2);
0879: panel2.add(component1);
0880: panel3.add(component3);
0881: window.add(panel4);
0882: window.add(component2);
0883: assertTrue(SwingUtilities.getAncestorNamed(name3, component1) == panel1);
0884: assertNull(SwingUtilities.getAncestorNamed(name6, component1));
0885: assertTrue(SwingUtilities.getAncestorNamed(name2, component1) == window);
0886: assertNull(SwingUtilities.getAncestorNamed(name5, component1));
0887: assertTrue(SwingUtilities.getAncestorNamed(name2, panel1) == window);
0888: assertTrue(SwingUtilities.getAncestorNamed(name2, component2) == window);
0889: assertTrue(SwingUtilities.getAncestorNamed(name5, component3) == panel3);
0890: assertNull(SwingUtilities.getAncestorNamed(null, component1));
0891: }
0892:
0893: public void testGetAncestorOfClass() {
0894: Window window = new Window(new Frame());
0895: JPanel panel1 = new JPanel();
0896: JPanel panel2 = new JPanel();
0897: JPanel panel3 = new JPanel();
0898: Component component1 = new Canvas();
0899: Component component2 = new Panel();
0900: Component component3 = new Panel();
0901: assertNull(SwingUtilities
0902: .getAncestorOfClass(JPanel.class, null));
0903: assertNull(SwingUtilities.getAncestorOfClass(null, component3));
0904: assertNull(SwingUtilities.getAncestorOfClass(null, null));
0905: panel1.add(panel2);
0906: panel2.add(component1);
0907: panel3.add(component3);
0908: window.add(panel1);
0909: window.add(component2);
0910: assertTrue(SwingUtilities.getAncestorOfClass(Window.class,
0911: component1) == window);
0912: assertTrue(SwingUtilities.getAncestorOfClass(JPanel.class,
0913: component1) == panel2);
0914: assertTrue(SwingUtilities.getAncestorOfClass(JPanel.class,
0915: panel2) == panel1);
0916: assertTrue(SwingUtilities.getAncestorOfClass(Window.class,
0917: component2) == window);
0918: assertNull(SwingUtilities.getAncestorOfClass(Window.class,
0919: component3));
0920: assertTrue(SwingUtilities.getAncestorOfClass(JPanel.class,
0921: component3) == panel3);
0922: class CustomPanel extends JPanel {
0923: private static final long serialVersionUID = 1L;
0924: }
0925: JPanel customPanel = new CustomPanel();
0926: JPanel childPanel = new JPanel();
0927: customPanel.add(childPanel);
0928: assertTrue(SwingUtilities.getAncestorOfClass(JPanel.class,
0929: childPanel) == customPanel);
0930: }
0931:
0932: /*
0933: * this method is being tested by testPaintComponentGraphicsComponentContainerintintintint()
0934: */
0935: public void testPaintComponentGraphicsComponentContainerRectangle() {
0936: }
0937:
0938: /*
0939: * Class under test for void paintComponent(Graphics, Component, Container, int, int, int, int)
0940: */
0941: public void testPaintComponentGraphicsComponentContainerintintintint() {
0942: // final JComponent component = new JButton("JButton");
0943: // JFrame window = new JFrame() {
0944: // public void paint(Graphics g) {
0945: // SwingUtilities.paintComponent(g, component, new Container(), 25, 25, 50, 50);
0946: // }
0947: // };
0948: // window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
0949: // window.setSize(150, 150);
0950: // window.show();
0951: // component.setPreferredSize(new Dimension(70, 70));
0952: // component.setForeground(Color.RED);
0953: // component.setBackground(Color.YELLOW);
0954: // JPanel panel = new JPanel();
0955: // window.getContentPane().add(panel);
0956: // while (!window.isActive());
0957: // while (window.isActive());
0958: }
0959:
0960: public void testGetRootPane() {
0961: assertNull(SwingUtilities.getRootPane(null));
0962: assertNull(SwingUtilities.getRootPane(new JButton()));
0963: JWindow window = new JWindow(new Frame());
0964: JPanel panel1 = new JPanel();
0965: JPanel panel2 = new JPanel();
0966: JPanel panel3 = new JPanel();
0967: JRootPane pane = new JRootPane();
0968: Component component1 = new Button();
0969: Component component2 = new JButton();
0970: Component component3 = new JButton();
0971: panel1.add(panel2);
0972: panel2.add(component1);
0973: panel3.add(component3);
0974: window.getContentPane().add(panel1);
0975: window.getContentPane().add(component2);
0976: assertEquals(window.getRootPane(), SwingUtilities
0977: .getRootPane(component1));
0978: assertNull(SwingUtilities.getRootPane(component3));
0979: assertEquals(window.getRootPane(), SwingUtilities
0980: .getRootPane(component2));
0981: assertEquals(window.getRootPane(), SwingUtilities
0982: .getRootPane(window));
0983: assertSame(pane, SwingUtilities.getRootPane(pane));
0984: }
0985:
0986: public void testReplaceUIInputMap() {
0987: UIInputMap uiInputMap1 = new UIInputMap();
0988: UIInputMap uiInputMap2 = new UIInputMap();
0989: JComponent component = new JPanel();
0990: InputMap initialMap = component
0991: .getInputMap(JComponent.WHEN_FOCUSED);
0992: InputMap anotherMap = new InputMap();
0993: component.setInputMap(JComponent.WHEN_FOCUSED, null);
0994: component.setInputMap(
0995: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
0996: assertNull(SwingUtilities.getUIInputMap(component,
0997: JComponent.WHEN_FOCUSED));
0998: SwingUtilities.replaceUIInputMap(component,
0999: JComponent.WHEN_FOCUSED, uiInputMap1);
1000: SwingUtilities.replaceUIInputMap(component,
1001: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
1002: uiInputMap2);
1003: assertNull(SwingUtilities.getUIInputMap(component,
1004: JComponent.WHEN_FOCUSED));
1005: assertNull(SwingUtilities.getUIInputMap(component,
1006: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
1007: component.setInputMap(JComponent.WHEN_FOCUSED, initialMap);
1008: component.setInputMap(
1009: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
1010: anotherMap);
1011: SwingUtilities.replaceUIInputMap(component,
1012: JComponent.WHEN_FOCUSED, uiInputMap1);
1013: assertTrue(SwingUtilities.getUIInputMap(component,
1014: JComponent.WHEN_FOCUSED) == uiInputMap1);
1015: SwingUtilities.replaceUIInputMap(component,
1016: JComponent.WHEN_FOCUSED, uiInputMap2);
1017: assertTrue(SwingUtilities.getUIInputMap(component,
1018: JComponent.WHEN_FOCUSED) == uiInputMap2);
1019: SwingUtilities.replaceUIInputMap(component,
1020: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
1021: uiInputMap2);
1022: assertTrue(SwingUtilities.getUIInputMap(component,
1023: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) == uiInputMap2);
1024: anotherMap.setParent(null);
1025: initialMap.setParent(anotherMap);
1026: assertNull(SwingUtilities.getUIInputMap(component,
1027: JComponent.WHEN_FOCUSED));
1028: SwingUtilities.replaceUIInputMap(component,
1029: JComponent.WHEN_FOCUSED, uiInputMap2);
1030: assertTrue(SwingUtilities.getUIInputMap(component,
1031: JComponent.WHEN_FOCUSED) == uiInputMap2);
1032: }
1033:
1034: public void testGetUIInputMap() {
1035: UIInputMap uiInputMap1 = new UIInputMap();
1036: UIInputMap uiInputMap2 = new UIInputMap();
1037: JComponent component = new JPanel();
1038: InputMap initialMap = component.getInputMap();
1039: InputMap anotherMap = new InputMap();
1040: component.setInputMap(JComponent.WHEN_FOCUSED, null);
1041: assertNull(SwingUtilities.getUIInputMap(component,
1042: JComponent.WHEN_FOCUSED));
1043: assertNull(SwingUtilities.getUIInputMap(component,
1044: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
1045: component.setInputMap(JComponent.WHEN_FOCUSED, uiInputMap1);
1046: assertNull(SwingUtilities.getUIInputMap(component,
1047: JComponent.WHEN_FOCUSED));
1048: component.setInputMap(JComponent.WHEN_FOCUSED, anotherMap);
1049: component.setInputMap(
1050: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
1051: anotherMap);
1052: assertNull(SwingUtilities.getUIInputMap(component,
1053: JComponent.WHEN_FOCUSED));
1054: anotherMap.setParent(initialMap);
1055: assertNull(SwingUtilities.getUIInputMap(component,
1056: JComponent.WHEN_FOCUSED));
1057: anotherMap.setParent(uiInputMap1);
1058: assertTrue(SwingUtilities.getUIInputMap(component,
1059: JComponent.WHEN_FOCUSED) == uiInputMap1);
1060: assertTrue(SwingUtilities.getUIInputMap(component,
1061: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) == uiInputMap1);
1062: uiInputMap1.setParent(uiInputMap2);
1063: assertTrue(SwingUtilities.getUIInputMap(component,
1064: JComponent.WHEN_FOCUSED) == uiInputMap1);
1065: assertTrue(SwingUtilities.getUIInputMap(component,
1066: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) == uiInputMap1);
1067: anotherMap.setParent(initialMap);
1068: initialMap.setParent(uiInputMap1);
1069: assertTrue(SwingUtilities.getUIInputMap(component,
1070: JComponent.WHEN_FOCUSED) == uiInputMap1);
1071: initialMap.setParent(uiInputMap2);
1072: assertTrue(SwingUtilities.getUIInputMap(component,
1073: JComponent.WHEN_FOCUSED) == uiInputMap2);
1074: }
1075:
1076: public void testReplaceUIActionMap() {
1077: UIActionMap uiActionMap1 = new UIActionMap();
1078: UIActionMap uiActionMap2 = new UIActionMap();
1079: JComponent component = new JPanel();
1080: ActionMap initialMap = component.getActionMap();
1081: ActionMap anotherMap = new ActionMap();
1082: component.setActionMap(null);
1083: assertNull(SwingUtilities.getUIActionMap(component));
1084: SwingUtilities.replaceUIActionMap(component, uiActionMap1);
1085: assertNull(SwingUtilities.getUIActionMap(component));
1086: component.setActionMap(initialMap);
1087: SwingUtilities.replaceUIActionMap(component, uiActionMap1);
1088: assertTrue(SwingUtilities.getUIActionMap(component) == uiActionMap1);
1089: SwingUtilities.replaceUIActionMap(component, uiActionMap2);
1090: assertTrue(SwingUtilities.getUIActionMap(component) == uiActionMap2);
1091: initialMap.setParent(anotherMap);
1092: assertNull(SwingUtilities.getUIActionMap(component));
1093: SwingUtilities.replaceUIActionMap(component, uiActionMap2);
1094: assertTrue(SwingUtilities.getUIActionMap(component) == uiActionMap2);
1095: }
1096:
1097: public void testGetUIActionMap() {
1098: UIActionMap uiActionMap1 = new UIActionMap();
1099: UIActionMap uiActionMap2 = new UIActionMap();
1100: JComponent component = new JPanel();
1101: ActionMap initialMap = component.getActionMap();
1102: ActionMap anotherMap = new ActionMap();
1103: component.setActionMap(null);
1104: assertNull(SwingUtilities.getUIActionMap(component));
1105: component.setActionMap(uiActionMap1);
1106: assertNull(SwingUtilities.getUIActionMap(component));
1107: component.setActionMap(anotherMap);
1108: assertNull(SwingUtilities.getUIActionMap(component));
1109: anotherMap.setParent(initialMap);
1110: assertNull(SwingUtilities.getUIActionMap(component));
1111: anotherMap.setParent(uiActionMap1);
1112: assertTrue(SwingUtilities.getUIActionMap(component) == uiActionMap1);
1113: uiActionMap1.setParent(uiActionMap2);
1114: assertTrue(SwingUtilities.getUIActionMap(component) == uiActionMap1);
1115: anotherMap.setParent(initialMap);
1116: initialMap.setParent(uiActionMap1);
1117: assertTrue(SwingUtilities.getUIActionMap(component) == uiActionMap1);
1118: initialMap.setParent(uiActionMap2);
1119: assertTrue(SwingUtilities.getUIActionMap(component) == uiActionMap2);
1120: }
1121:
1122: public void testGetAccessibleStateSet() {
1123: Component component1 = new JPanel();
1124: Component component3 = new JDialog();
1125: AccessibleStateSet stateSet = SwingUtilities
1126: .getAccessibleStateSet(component1);
1127: assertTrue(stateSet.contains(AccessibleState.ENABLED));
1128: assertTrue(stateSet.contains(AccessibleState.FOCUSABLE));
1129: assertTrue(stateSet.contains(AccessibleState.VISIBLE));
1130: assertTrue(stateSet.contains(AccessibleState.OPAQUE));
1131: assertTrue(stateSet.toArray().length == 4);
1132: stateSet = SwingUtilities.getAccessibleStateSet(component3);
1133: assertTrue(stateSet.contains(AccessibleState.ENABLED));
1134: assertTrue(stateSet.contains(AccessibleState.FOCUSABLE));
1135: assertTrue(stateSet.contains(AccessibleState.RESIZABLE));
1136: // assertTrue(stateSet.toArray().length == 3);
1137: }
1138:
1139: public void testGetAccessibleChild() {
1140: assertTrue(SwingUtilities
1141: .getAccessibleIndexInParent(new JButton()) == -1);
1142: JPanel panel = new JPanel();
1143: assertTrue(SwingUtilities.getAccessibleIndexInParent(panel) == -1);
1144: Component component1 = new JPanel();
1145: panel.add(component1);
1146: assertTrue(
1147: "Accessible component found in parent",
1148: SwingUtilities.getAccessibleChild(panel, 0) == component1);
1149: Component component2 = new Container();
1150: panel.add(component2);
1151: assertNull("inAccessible component not found in parent",
1152: SwingUtilities.getAccessibleChild(panel, 1));
1153: Component component3 = new JPanel();
1154: panel.add(component3);
1155: assertTrue(
1156: "Accessible component found in parent",
1157: SwingUtilities.getAccessibleChild(panel, 1) == component3);
1158: assertTrue(
1159: "Accessible component found in parent",
1160: SwingUtilities.getAccessibleChild(panel, 0) == component1);
1161: }
1162:
1163: public void testComputeStringWidth() {
1164: JFrame frame = new JFrame();
1165: JWindow window = new JWindow(frame);
1166: window.setVisible(true);
1167: final FontMetrics metrics = window.getGraphics()
1168: .getFontMetrics();
1169: // String string1 = "string1";
1170: // String string2 = "string1string1";
1171: // String string3 = "";
1172: // String string4 = " ";
1173: testExceptionalCase(new NullPointerCase() {
1174: @Override
1175: public void exceptionalAction() throws Exception {
1176: SwingUtilities.computeStringWidth(metrics, null);
1177: }
1178: });
1179: testExceptionalCase(new NullPointerCase() {
1180: @Override
1181: public void exceptionalAction() throws Exception {
1182: SwingUtilities.computeStringWidth(null, "string");
1183: }
1184: });
1185: // assertTrue(SwingUtilities.computeStringWidth(metrics, string1) == 38);
1186: // assertTrue(SwingUtilities.computeStringWidth(metrics, string2) == 76);
1187: // assertTrue(SwingUtilities.computeStringWidth(metrics, string3) == 0);
1188: // assertTrue(SwingUtilities.computeStringWidth(metrics, string4) == 12);
1189: frame.dispose();
1190: }
1191:
1192: public void testWindowForComponent() {
1193: Window window = new Window(new Frame());
1194: JPanel panel1 = new JPanel();
1195: JPanel panel2 = new JPanel();
1196: JPanel panel3 = new JPanel();
1197: Component component1 = new Button();
1198: Component component2 = new JButton();
1199: Component component3 = new JButton();
1200: assertNull(SwingUtilities.windowForComponent(component1));
1201: panel1.add(panel2);
1202: panel2.add(component1);
1203: panel3.add(component3);
1204: window.add(panel1);
1205: window.add(component2);
1206: assertTrue(SwingUtilities.windowForComponent(component1) == window);
1207: assertTrue(SwingUtilities.windowForComponent(component2) == window);
1208: assertNull(SwingUtilities.windowForComponent(component3));
1209: assertTrue(SwingUtilities.windowForComponent(window) != window);
1210: }
1211:
1212: public void testGetWindowAncestor() {
1213: Window window = new Window(new Frame());
1214: JPanel panel1 = new JPanel();
1215: JPanel panel2 = new JPanel();
1216: JPanel panel3 = new JPanel();
1217: Component component1 = new Button();
1218: Component component2 = new JButton();
1219: Component component3 = new JButton();
1220: /**
1221: boolean thrown = false;
1222: try {
1223: SwingUtilities.getWindowAncestor(null);
1224: } catch (NullPointerException e) {
1225: thrown = true;
1226: }
1227: assertTrue(thrown);
1228: */
1229: assertNull(SwingUtilities.getWindowAncestor(component1));
1230: panel1.add(panel2);
1231: panel2.add(component1);
1232: panel3.add(component3);
1233: window.add(panel1);
1234: window.add(component2);
1235: assertTrue(SwingUtilities.getWindowAncestor(component1) == window);
1236: assertTrue(SwingUtilities.getWindowAncestor(component2) == window);
1237: assertNull(SwingUtilities.getWindowAncestor(component3));
1238: assertTrue(SwingUtilities.getWindowAncestor(window) != window);
1239: }
1240:
1241: public void testIsRectangleContainingRectangle() {
1242: Rectangle rect1 = new Rectangle(100, 100, 100, 100);
1243: Rectangle rect2 = new Rectangle(150, 150, 30, 30);
1244: Rectangle rect3 = new Rectangle(150, 150, 50, 50);
1245: Rectangle rect4 = new Rectangle(0, 0, 150, 150);
1246: Rectangle rect5 = new Rectangle(100, 100, 1, 1);
1247: assertTrue(SwingUtilities.isRectangleContainingRectangle(rect1,
1248: rect2));
1249: assertTrue(SwingUtilities.isRectangleContainingRectangle(rect1,
1250: rect5));
1251: assertFalse(SwingUtilities.isRectangleContainingRectangle(
1252: rect2, rect1));
1253: assertTrue(SwingUtilities.isRectangleContainingRectangle(rect1,
1254: rect3));
1255: assertTrue(SwingUtilities.isRectangleContainingRectangle(rect3,
1256: rect2));
1257: assertTrue(SwingUtilities.isRectangleContainingRectangle(rect3,
1258: rect3));
1259: assertFalse(SwingUtilities.isRectangleContainingRectangle(
1260: rect1, rect4));
1261: assertFalse(SwingUtilities.isRectangleContainingRectangle(
1262: rect4, rect1));
1263: }
1264:
1265: public void testComputeUnion() {
1266: assertTrue(SwingUtilities.computeUnion(0, 0, 100, 100,
1267: new Rectangle(100, 100, 100, 100)).equals(
1268: new Rectangle(0, 0, 200, 200)));
1269: assertTrue(SwingUtilities.computeUnion(100, 100, 100, 100,
1270: new Rectangle(0, 0, 100, 100)).equals(
1271: new Rectangle(0, 0, 200, 200)));
1272: assertTrue(SwingUtilities.computeUnion(0, 0, 100, 100,
1273: new Rectangle(200, 200, 100, 100)).equals(
1274: new Rectangle(0, 0, 300, 300)));
1275: assertTrue(SwingUtilities.computeUnion(200, 200, 100, 100,
1276: new Rectangle(0, 0, 100, 100)).equals(
1277: new Rectangle(0, 0, 300, 300)));
1278: assertTrue(SwingUtilities.computeUnion(200, 200, 100, 100,
1279: new Rectangle(220, 220, 10, 10)).equals(
1280: new Rectangle(200, 200, 100, 100)));
1281: assertTrue(SwingUtilities.computeUnion(200, 200, 100, 100,
1282: new Rectangle(180, 220, 10, 100)).equals(
1283: new Rectangle(180, 200, 120, 120)));
1284: }
1285:
1286: public void testComputeIntersection() {
1287: assertEquals(new Dimension(0, 0), SwingUtilities
1288: .computeIntersection(0, 0, 100, 100,
1289: new Rectangle(100, 100, 100, 100)).getSize());
1290: assertEquals(new Rectangle(150, 150, 20, 20), SwingUtilities
1291: .computeIntersection(100, 100, 100, 100, new Rectangle(
1292: 150, 150, 20, 20)));
1293: assertEquals(new Rectangle(150, 150, 20, 20), SwingUtilities
1294: .computeIntersection(150, 150, 20, 20, new Rectangle(
1295: 100, 100, 100, 100)));
1296: assertEquals(new Rectangle(0, 0, 0, 0), SwingUtilities
1297: .computeIntersection(0, 0, 100, 100, new Rectangle(200,
1298: 200, 100, 100)));
1299: assertEquals(new Rectangle(0, 0, 0, 0), SwingUtilities
1300: .computeIntersection(0, 0, 100, 100, new Rectangle(0,
1301: 101, 100, 100)));
1302: assertEquals(new Rectangle(0, 0, 0, 0), SwingUtilities
1303: .computeIntersection(0, 0, 100, 100, new Rectangle(101,
1304: 0, 100, 100)));
1305: assertEquals(new Rectangle(200, 200, 50, 50), SwingUtilities
1306: .computeIntersection(200, 200, 100, 100, new Rectangle(
1307: 150, 150, 100, 100)));
1308: assertEquals(new Rectangle(200, 200, 50, 50), SwingUtilities
1309: .computeIntersection(150, 150, 100, 100, new Rectangle(
1310: 200, 200, 100, 100)));
1311: }
1312:
1313: public void testGetLocalBounds() {
1314: int width = 200;
1315: int height = 200;
1316: Component component = new JPanel();
1317: component.setSize(width, height);
1318: Rectangle bounds = SwingUtilities.getLocalBounds(component);
1319: assertTrue(bounds.x == 0);
1320: assertTrue(bounds.y == 0);
1321: assertTrue(bounds.width == width);
1322: assertTrue(bounds.height == height);
1323: component.setBounds(width, height, width, height);
1324: bounds = SwingUtilities.getLocalBounds(component);
1325: assertTrue(bounds.x == 0);
1326: assertTrue(bounds.y == 0);
1327: assertTrue(bounds.width == width);
1328: assertTrue(bounds.height == height);
1329: }
1330:
1331: public void testConvertPointToScreen() {
1332: Point point;
1333: JComponent panel1 = new JPanel();
1334: JComponent panel2 = new JPanel();
1335: panel1.setSize(100, 100);
1336: panel1.setLocation(50, 50);
1337: panel1.add(panel2);
1338: panel2.setSize(200, 200);
1339: panel2.setLocation(70, 70);
1340: point = new Point(0, 0);
1341: SwingUtilities.convertPointToScreen(point, panel1);
1342: assertTrue(point.equals(new Point(50, 50)));
1343: point = new Point(0, 0);
1344: SwingUtilities.convertPointToScreen(point, panel2);
1345: assertTrue(point.equals(new Point(120, 120)));
1346: point = new Point(66, 66);
1347: SwingUtilities.convertPointToScreen(point, panel2);
1348: assertTrue(point.equals(new Point(186, 186)));
1349: }
1350:
1351: public void testConvertPointFromScreen() {
1352: Point point;
1353: JComponent panel1 = new JPanel();
1354: JComponent panel2 = new JPanel();
1355: panel1.setSize(100, 100);
1356: panel1.setLocation(50, 50);
1357: panel1.add(panel2);
1358: panel2.setSize(200, 200);
1359: panel2.setLocation(70, 70);
1360: point = new Point(50, 50);
1361: SwingUtilities.convertPointFromScreen(point, panel1);
1362: assertTrue(point.equals(new Point(0, 0)));
1363: point = new Point(120, 120);
1364: SwingUtilities.convertPointFromScreen(point, panel2);
1365: assertTrue(point.equals(new Point(0, 0)));
1366: point = new Point(186, 186);
1367: SwingUtilities.convertPointFromScreen(point, panel2);
1368: assertTrue(point.equals(new Point(66, 66)));
1369: }
1370:
1371: public void testIsDescendingFrom() {
1372: final Window window = new Window(new Frame());
1373: JPanel panel1 = new JPanel();
1374: JPanel panel2 = new JPanel();
1375: JPanel panel3 = new JPanel();
1376: Component component1 = new Button();
1377: Component component2 = new JButton();
1378: Component component3 = new JButton();
1379: panel1.add(panel2);
1380: panel2.add(component1);
1381: panel3.add(component3);
1382: window.add(panel1);
1383: window.add(component2);
1384: assertTrue(SwingUtilities.isDescendingFrom(null, null));
1385: testExceptionalCase(new NullPointerCase() {
1386: @Override
1387: public void exceptionalAction() throws Exception {
1388: SwingUtilities.isDescendingFrom(null, window);
1389: }
1390: });
1391: assertFalse(SwingUtilities.isDescendingFrom(component1, null));
1392: assertTrue(SwingUtilities.isDescendingFrom(window, window));
1393: assertTrue(SwingUtilities.isDescendingFrom(component1, window));
1394: assertTrue(SwingUtilities.isDescendingFrom(component2, window));
1395: assertFalse(SwingUtilities.isDescendingFrom(component3, window));
1396: assertTrue(SwingUtilities.isDescendingFrom(panel1, window));
1397: assertTrue(SwingUtilities.isDescendingFrom(panel2, window));
1398: assertFalse(SwingUtilities.isDescendingFrom(panel3, window));
1399: }
1400:
1401: @SuppressWarnings("deprecation")
1402: public void testGetDeepestComponentAt() {
1403: if (isHarmony()) {
1404: return;
1405: }
1406: JDialog window = new JDialog();
1407: JPanel panel1 = new JPanel();
1408: JPanel panel2 = new JPanel();
1409: Component component1 = new JButton("1");
1410: Component component2 = new JButton("2");
1411: Component component3 = new JButton("3");
1412: panel1.add(panel2);
1413: panel2.add(component1);
1414: panel1.add(component3);
1415: panel1.add(component2);
1416: window.getContentPane().add(panel1);
1417: window.pack();
1418: window.show();
1419: assertTrue(SwingUtilities.getDeepestComponentAt(window
1420: .getContentPane(), 0, 0) == panel1);
1421: assertTrue(SwingUtilities.getDeepestComponentAt(window
1422: .getContentPane(), 5, 5) == panel2);
1423: assertTrue(SwingUtilities.getDeepestComponentAt(window
1424: .getContentPane(), 10, 10) == component1);
1425: assertTrue(SwingUtilities.getDeepestComponentAt(window
1426: .getContentPane(), 100, 20) == component3);
1427: assertTrue(SwingUtilities.getDeepestComponentAt(window
1428: .getContentPane(), 110, 20) == component2);
1429: assertNull(SwingUtilities.getDeepestComponentAt(window
1430: .getContentPane(), 210, 20));
1431: assertTrue(SwingUtilities
1432: .getDeepestComponentAt(window, 100, 20) == window);
1433: }
1434:
1435: public void testGetRoot() {
1436: JWindow window1 = new JWindow();
1437: // Code should be enabled when JApplet is supported.
1438: //JApplet window2 = new JApplet();
1439: JWindow window2 = new JWindow();
1440: JComponent panel1 = new JPanel();
1441: JComponent panel2 = new JPanel();
1442: JComponent panel3 = new JPanel();
1443: JComponent panel4 = new JPanel();
1444: panel1.setSize(new Dimension(100, 100));
1445: panel1.setBackground(Color.GREEN);
1446: panel2.setSize(new Dimension(101, 101));
1447: panel2.setBackground(Color.YELLOW);
1448: panel3.setPreferredSize(new Dimension(110, 110));
1449: panel3.setBackground(Color.WHITE);
1450: panel1.setLocation(50, 50);
1451: panel2.setLocation(70, 70);
1452: panel3.setLocation(150, 150);
1453: panel1.add(panel2);
1454: panel1.setBorder(new EmptyBorder(15, 15, 15, 15));
1455: panel2.setBorder(new EmptyBorder(27, 27, 27, 27));
1456: panel3.setBorder(new EmptyBorder(17, 17, 17, 17));
1457: window1.setLocation(100, 100);
1458: window1.getContentPane().add(panel1);
1459: window2.getContentPane().add(panel4);
1460: assertEquals(window1, SwingUtilities.getRoot(panel1));
1461: assertEquals(window1, SwingUtilities.getRoot(panel2));
1462: assertNull(SwingUtilities.getRoot(panel3));
1463: assertEquals(window2, SwingUtilities.getRoot(panel4));
1464: assertEquals(window1, SwingUtilities.getRoot(window1));
1465: assertEquals(window2, SwingUtilities.getRoot(window2));
1466: }
1467:
1468: /**
1469: * this method is supposed to be tested by FocusManager.getCurrentManager().getFocusOwner()
1470: */
1471: public void testFindFocusOwner() {
1472: }
1473:
1474: /**
1475: * this method is supposed to be tested by EventQueue.invokeLater()
1476: */
1477: public void testInvokeLater() {
1478: }
1479:
1480: /**
1481: * this method is supposed to be tested by EventQueue.invokeAndWait()
1482: */
1483: public void testInvokeAndWait() {
1484: }
1485:
1486: public void testIsRightMouseButton() {
1487: JComponent panel = new JPanel();
1488: panel.setPreferredSize(new Dimension(100, 100));
1489: MouseEvent event1 = new MouseEvent(panel, 100, 100,
1490: InputEvent.BUTTON1_DOWN_MASK, 50, 50, 1, false);
1491: MouseEvent event2 = new MouseEvent(panel, 100, 100,
1492: InputEvent.BUTTON2_DOWN_MASK, 50, 50, 1, false);
1493: MouseEvent event3 = new MouseEvent(panel, 100, 100,
1494: InputEvent.BUTTON3_DOWN_MASK, 50, 50, 1, false);
1495: assertFalse(SwingUtilities.isRightMouseButton(event1));
1496: assertFalse(SwingUtilities.isRightMouseButton(event2));
1497: assertTrue(SwingUtilities.isRightMouseButton(event3));
1498: }
1499:
1500: public void testIsMiddleMouseButton() {
1501: JComponent panel = new JPanel();
1502: panel.setPreferredSize(new Dimension(100, 100));
1503: MouseEvent event1 = new MouseEvent(panel, 100, 100,
1504: InputEvent.BUTTON1_DOWN_MASK, 50, 50, 1, false);
1505: MouseEvent event2 = new MouseEvent(panel, 100, 100,
1506: InputEvent.BUTTON2_DOWN_MASK, 50, 50, 1, false);
1507: MouseEvent event3 = new MouseEvent(panel, 100, 100,
1508: InputEvent.BUTTON3_DOWN_MASK, 50, 50, 1, false);
1509: assertFalse(SwingUtilities.isMiddleMouseButton(event1));
1510: assertTrue(SwingUtilities.isMiddleMouseButton(event2));
1511: assertFalse(SwingUtilities.isMiddleMouseButton(event3));
1512: }
1513:
1514: public void testIsLeftMouseButton() {
1515: JComponent panel = new JPanel();
1516: panel.setPreferredSize(new Dimension(100, 100));
1517: MouseEvent event1 = new MouseEvent(panel, 100, 100,
1518: InputEvent.BUTTON1_DOWN_MASK, 50, 50, 1, false);
1519: MouseEvent event2 = new MouseEvent(panel, 100, 100,
1520: InputEvent.BUTTON2_DOWN_MASK, 50, 50, 1, false);
1521: MouseEvent event3 = new MouseEvent(panel, 100, 100,
1522: InputEvent.BUTTON3_DOWN_MASK, 50, 50, 1, false);
1523: assertTrue(SwingUtilities.isLeftMouseButton(event1));
1524: assertFalse(SwingUtilities.isLeftMouseButton(event2));
1525: assertFalse(SwingUtilities.isLeftMouseButton(event3));
1526: }
1527:
1528: public void testProcessKeyBindings() {
1529: class ActionListenerDummy implements ActionListener {
1530: public ActionEvent event = null;
1531:
1532: public void actionPerformed(final ActionEvent e) {
1533: event = e;
1534: }
1535: }
1536: ;
1537: ActionListenerDummy action1 = new ActionListenerDummy();
1538: ActionListenerDummy action2 = new ActionListenerDummy();
1539: ActionListenerDummy action3 = new ActionListenerDummy();
1540: ActionListenerDummy action41 = new ActionListenerDummy();
1541: ActionListenerDummy action42 = new ActionListenerDummy();
1542: ActionListenerDummy action51 = new ActionListenerDummy();
1543: ActionListenerDummy action52 = new ActionListenerDummy();
1544: ActionListenerDummy action53 = new ActionListenerDummy();
1545: ActionListenerDummy action54 = new ActionListenerDummy();
1546: JComponent component1 = new JPanel();
1547: Component component2 = new Panel();
1548: JComponent component3 = new JButton("3");
1549: JWindow component4 = new JWindow();
1550: component4.getContentPane().add(component1);
1551: component1.add(component2);
1552: component1.add(component3);
1553: KeyEvent event1 = new KeyEvent(component1,
1554: KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_A, 'a');
1555: KeyEvent event2 = new KeyEvent(component2,
1556: KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_B, 'b');
1557: KeyEvent event3 = new KeyEvent(component3,
1558: KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_C, 'c');
1559: KeyEvent event4 = new KeyEvent(component2,
1560: KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_D, 'd');
1561: KeyEvent event5 = new KeyEvent(component1,
1562: KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_ENTER, '\n');
1563: KeyStroke keyStroke1 = KeyStroke.getKeyStrokeForEvent(event1);
1564: KeyStroke keyStroke2 = KeyStroke.getKeyStrokeForEvent(event2);
1565: KeyStroke keyStroke3 = KeyStroke.getKeyStrokeForEvent(event3);
1566: KeyStroke keyStroke4 = KeyStroke.getKeyStrokeForEvent(event4);
1567: KeyStroke keyStroke5 = KeyStroke.getKeyStrokeForEvent(event5);
1568: component1.registerKeyboardAction(action1, keyStroke1,
1569: JComponent.WHEN_FOCUSED);
1570: component1.registerKeyboardAction(action2, keyStroke2,
1571: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
1572: component3.registerKeyboardAction(action3, keyStroke3,
1573: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
1574: component1.registerKeyboardAction(action41, keyStroke4,
1575: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
1576: component3.registerKeyboardAction(action42, keyStroke4,
1577: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
1578: component3.registerKeyboardAction(action53, keyStroke5,
1579: JComponent.WHEN_FOCUSED);
1580: component1.registerKeyboardAction(action51, keyStroke5,
1581: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
1582: boolean result = SwingUtilities.processKeyBindings(event1);
1583: assertTrue(result);
1584: assertTrue("event1: actionPerformed called for component",
1585: action1.event != null);
1586: assertFalse(event1.isConsumed());
1587: action1.event = null;
1588: result = SwingUtilities.processKeyBindings(event2);
1589: assertTrue(result);
1590: assertNull("event2: wrong actionPerformed called for parent",
1591: action1.event);
1592: assertTrue("event2: right actionPerformed called for parent",
1593: action2.event != null);
1594: assertFalse(event2.isConsumed());
1595: action2.event = null;
1596: result = SwingUtilities.processKeyBindings(event3);
1597: assertTrue(result);
1598: assertNull("event3: actionPerformed called for parent",
1599: action1.event);
1600: assertNull("event3: actionPerformed called for brother",
1601: action2.event);
1602: assertTrue("event3: actionPerformed called for component",
1603: action3.event != null);
1604: assertFalse(event3.isConsumed());
1605: action3.event = null;
1606: result = SwingUtilities.processKeyBindings(event4);
1607: assertTrue(result);
1608: assertNull("event4: actionPerformed called for parent",
1609: action1.event);
1610: assertNull("event4: actionPerformed called for brother",
1611: action2.event);
1612: assertNull("event4: actionPerformed called for component",
1613: action3.event);
1614: assertTrue("event4: actionPerformed called for brother",
1615: action41.event != null);
1616: assertNull("event4: actionPerformed called for brother",
1617: action42.event);
1618: assertFalse(event4.isConsumed());
1619: result = SwingUtilities.processKeyBindings(event5);
1620: assertTrue(result);
1621: assertTrue("event5: actionPerformed called for parent",
1622: action51.event != null);
1623: assertNull("event5: actionPerformed called for parent",
1624: action53.event);
1625: assertFalse(event5.isConsumed());
1626: JComponent panel1 = new JPanel();
1627: JComponent panel2 = new JPanel();
1628: JTextField editor = new JTextField();
1629: KeyEvent event6 = new KeyEvent(editor, KeyEvent.KEY_PRESSED, 0,
1630: 0, KeyEvent.VK_ENTER, '\n');
1631: panel1.registerKeyboardAction(action52, keyStroke5,
1632: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
1633: panel2.registerKeyboardAction(action54, keyStroke5,
1634: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
1635: panel2.add(panel1);
1636: panel1.add(editor);
1637: result = SwingUtilities.processKeyBindings(event6);
1638: assertTrue(result);
1639: assertTrue("event5: actionPerformed called for parent",
1640: action52.event != null);
1641: assertNull("event5: actionPerformed called for parent",
1642: action54.event);
1643: assertFalse(event5.isConsumed());
1644: }
1645:
1646: public void testUpdateComponentTreeUI() throws Exception {
1647: LookAndFeel laf = UIManager.getLookAndFeel();
1648: try {
1649: JPanel panel1 = new JPanel();
1650: JPanel panel2 = new JPanel();
1651: JButton button1 = new JButton("1");
1652: JButton button2 = new JButton("2");
1653: panel1.add(button1);
1654: panel1.add(button2);
1655: panel2.add(panel1);
1656: String lookAndFeel1 = "org.apache.harmony.x.swing.plaf.metal.MetalLookAndFeel";
1657: try {
1658: UIManager.setLookAndFeel(lookAndFeel1);
1659: } catch (ClassNotFoundException e) {
1660: } catch (UnsupportedLookAndFeelException e) {
1661: } catch (Exception e) {
1662: }
1663: ComponentUI ui1 = button1.getUI();
1664: ComponentUI ui2 = button2.getUI();
1665: ComponentUI ui3 = panel1.getUI();
1666: ComponentUI ui4 = panel2.getUI();
1667: assertTrue(ui1.getClass().getName().endsWith(
1668: "MetalButtonUI"));
1669: assertTrue(ui2.getClass().getName().endsWith(
1670: "MetalButtonUI"));
1671: assertTrue(ui3.getClass().getName()
1672: .endsWith("BasicPanelUI"));
1673: assertTrue(ui4.getClass().getName()
1674: .endsWith("BasicPanelUI"));
1675: button1.setUI(null);
1676: button2.setUI(null);
1677: panel1.setUI(null);
1678: panel2.setUI(null);
1679: SwingUtilities.updateComponentTreeUI(panel1);
1680: ui1 = button1.getUI();
1681: ui2 = button2.getUI();
1682: ui3 = panel1.getUI();
1683: ui4 = panel2.getUI();
1684: assertTrue(ui1.getClass().getName().endsWith(
1685: "MetalButtonUI"));
1686: assertTrue(ui2.getClass().getName().endsWith(
1687: "MetalButtonUI"));
1688: assertTrue(ui3.getClass().getName()
1689: .endsWith("BasicPanelUI"));
1690: assertNull(ui4);
1691: } finally {
1692: UIManager.setLookAndFeel(laf);
1693: }
1694: }
1695:
1696: public void testGetAccessibleIndexInParent() {
1697: assertTrue(SwingUtilities
1698: .getAccessibleIndexInParent(new JButton()) == -1);
1699: JPanel panel = new JPanel();
1700: assertTrue(SwingUtilities.getAccessibleIndexInParent(panel) == -1);
1701: Component component = new JPanel();
1702: panel.add(component);
1703: assertTrue(SwingUtilities.getAccessibleIndexInParent(component) == 0);
1704: final Component container = new Container();
1705: panel.add(container);
1706: testExceptionalCase(new NullPointerCase() {
1707: @Override
1708: public void exceptionalAction() throws Exception {
1709: SwingUtilities.getAccessibleIndexInParent(container);
1710: }
1711: });
1712: component = new JPanel();
1713: panel.add(component);
1714: assertTrue(SwingUtilities.getAccessibleIndexInParent(component) == 1);
1715: }
1716:
1717: public void testGetAccessibleChildrenCount() {
1718: testExceptionalCase(new NullPointerCase() {
1719: @Override
1720: public void exceptionalAction() throws Exception {
1721: SwingUtilities.getAccessibleChildrenCount(null);
1722: }
1723: });
1724: assertTrue(SwingUtilities
1725: .getAccessibleChildrenCount(new JButton()) == 0);
1726: JPanel panel = new JPanel();
1727: assertTrue(SwingUtilities.getAccessibleChildrenCount(panel) == 0);
1728: panel.add(new JPanel());
1729: assertTrue(SwingUtilities.getAccessibleChildrenCount(panel) == 1);
1730: panel.add(new Container());
1731: assertTrue(SwingUtilities.getAccessibleChildrenCount(panel) == 1);
1732: panel.add(new JPanel());
1733: assertTrue(SwingUtilities.getAccessibleChildrenCount(panel) == 2);
1734: panel.add(new Container());
1735: assertTrue(SwingUtilities.getAccessibleChildrenCount(panel) == 2);
1736: }
1737:
1738: public void testIsEventDispatchThread() {
1739: assertTrue(SwingUtilities.isEventDispatchThread());
1740: }
1741: }
|