001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 16.12.2004
021:
022: */package javax.swing;
023:
024: import java.awt.Component;
025: import java.awt.Dimension;
026: import java.awt.FontMetrics;
027: import java.awt.Frame;
028: import java.awt.Rectangle;
029: import java.awt.event.InputEvent;
030: import java.awt.event.MouseEvent;
031:
032: public class SwingUtilitiesRTest extends SwingTestCase {
033: public void testLayoutCompoundLabel() {
034: JComponent c = new JButton();
035: FontMetrics fm = c.getFontMetrics(c.getFont());
036: Rectangle viewRectangle = new Rectangle(1000, 1000);
037: Rectangle iconRectangle = new Rectangle();
038: Rectangle textRectangle = new Rectangle();
039: SwingUtilities.layoutCompoundLabel(c, fm, "text", null,
040: SwingConstants.CENTER, SwingConstants.LEADING,
041: SwingConstants.CENTER, SwingConstants.TRAILING,
042: viewRectangle, iconRectangle, textRectangle, 0);
043: int textHeight = fm.getHeight();
044: assertEquals(new Rectangle(0,
045: (viewRectangle.height - textHeight) / 2 + textHeight
046: / 2, 0, 0), iconRectangle);
047: }
048:
049: public void testIsLeftMiddleRightMouseButton() {
050: JComponent panel = new JPanel();
051: panel.setPreferredSize(new Dimension(100, 100));
052: MouseEvent button1DownEvent = new MouseEvent(panel, 100, 0,
053: InputEvent.BUTTON1_DOWN_MASK, 50, 50, 1, false);
054: MouseEvent button1PressedEvent = new MouseEvent(panel,
055: MouseEvent.MOUSE_PRESSED, 0, 0, 50, 50, 1, false,
056: MouseEvent.BUTTON1);
057: MouseEvent button1Released = new MouseEvent(panel,
058: MouseEvent.MOUSE_RELEASED, 0, 0, 50, 50, 1, false,
059: MouseEvent.BUTTON1);
060: MouseEvent button1Clicked = new MouseEvent(panel,
061: MouseEvent.MOUSE_CLICKED, 0, 0, 50, 50, 1, false,
062: MouseEvent.BUTTON1);
063: MouseEvent button1DraggedEvent = new MouseEvent(panel,
064: MouseEvent.MOUSE_DRAGGED, 0, 0, 50, 50, 1, false,
065: MouseEvent.BUTTON1);
066: MouseEvent button2DownEvent = new MouseEvent(panel, 100, 0,
067: InputEvent.BUTTON2_DOWN_MASK, 50, 50, 1, false);
068: MouseEvent button2PressedEvent = new MouseEvent(panel,
069: MouseEvent.MOUSE_PRESSED, 0, 0, 50, 50, 1, false,
070: MouseEvent.BUTTON2);
071: MouseEvent button2Released = new MouseEvent(panel,
072: MouseEvent.MOUSE_RELEASED, 0, 0, 50, 50, 1, false,
073: MouseEvent.BUTTON2);
074: MouseEvent button2Clicked = new MouseEvent(panel,
075: MouseEvent.MOUSE_CLICKED, 0, 0, 50, 50, 1, false,
076: MouseEvent.BUTTON2);
077: MouseEvent button2DraggedEvent = new MouseEvent(panel,
078: MouseEvent.MOUSE_DRAGGED, 0, 0, 50, 50, 1, false,
079: MouseEvent.BUTTON2);
080: MouseEvent button3DownEvent = new MouseEvent(panel, 100, 0,
081: InputEvent.BUTTON3_DOWN_MASK, 50, 50, 1, false);
082: MouseEvent button3PressedEvent = new MouseEvent(panel,
083: MouseEvent.MOUSE_PRESSED, 0, 0, 50, 50, 1, false,
084: MouseEvent.BUTTON3);
085: MouseEvent button3Released = new MouseEvent(panel,
086: MouseEvent.MOUSE_RELEASED, 0, 0, 50, 50, 1, false,
087: MouseEvent.BUTTON3);
088: MouseEvent button3Clicked = new MouseEvent(panel,
089: MouseEvent.MOUSE_CLICKED, 0, 0, 50, 50, 1, false,
090: MouseEvent.BUTTON3);
091: MouseEvent button3DraggedEvent = new MouseEvent(panel,
092: MouseEvent.MOUSE_DRAGGED, 0, 0, 50, 50, 1, false,
093: MouseEvent.BUTTON3);
094: assertTrue(SwingUtilities.isLeftMouseButton(button1DownEvent));
095: assertFalse(SwingUtilities.isLeftMouseButton(button2DownEvent));
096: assertFalse(SwingUtilities.isLeftMouseButton(button3DownEvent));
097: assertTrue(SwingUtilities
098: .isLeftMouseButton(button1PressedEvent));
099: assertTrue(SwingUtilities.isLeftMouseButton(button1Released));
100: assertTrue(SwingUtilities.isLeftMouseButton(button1Clicked));
101: assertFalse(SwingUtilities
102: .isLeftMouseButton(button1DraggedEvent));
103: assertFalse(SwingUtilities
104: .isMiddleMouseButton(button1DownEvent));
105: assertTrue(SwingUtilities.isMiddleMouseButton(button2DownEvent));
106: assertFalse(SwingUtilities
107: .isMiddleMouseButton(button3DownEvent));
108: assertTrue(SwingUtilities
109: .isMiddleMouseButton(button2PressedEvent));
110: assertTrue(SwingUtilities.isMiddleMouseButton(button2Released));
111: assertTrue(SwingUtilities.isMiddleMouseButton(button2Clicked));
112: assertFalse(SwingUtilities
113: .isMiddleMouseButton(button2DraggedEvent));
114: assertFalse(SwingUtilities.isRightMouseButton(button1DownEvent));
115: assertFalse(SwingUtilities.isRightMouseButton(button2DownEvent));
116: assertTrue(SwingUtilities.isRightMouseButton(button3DownEvent));
117: assertTrue(SwingUtilities
118: .isRightMouseButton(button3PressedEvent));
119: assertTrue(SwingUtilities.isRightMouseButton(button3Released));
120: assertTrue(SwingUtilities.isRightMouseButton(button3Clicked));
121: assertFalse(SwingUtilities
122: .isRightMouseButton(button3DraggedEvent));
123: }
124:
125: public void testGetAncestorOfClass() {
126: final Frame f = new Frame();
127: final JDialog dialog = new JDialog(f);
128: assertSame(f, SwingUtilities.getAncestorOfClass(Frame.class,
129: dialog));
130: }
131:
132: public void testDeadLoop_4820() {
133: final int DEAD_LOOP_TIMEOUT = 1000;
134: final int VALID_NUMBER_OF_CALLS = 15;
135: final int counter[] = { 0 };
136:
137: class MFrame extends Frame {
138: MFrame() {
139: setSize(300, 200);
140: show();
141: }
142:
143: public Component locate(int x, int y) {
144: counter[0]++;
145: return super .locate(x, y);
146: }
147:
148: public Component getComponentAt(int x, int y) {
149: counter[0]++;
150: return super .getComponentAt(x, y);
151: }
152:
153: public Component getComponentAt(java.awt.Point p) {
154: counter[0]++;
155: return super .getComponentAt(p);
156: }
157: }
158:
159: Frame f = new MFrame();
160:
161: SwingUtilities.getDeepestComponentAt(f, 10, 10);
162: try {
163: Thread.sleep(DEAD_LOOP_TIMEOUT);
164: } catch (Exception e) {
165: }
166:
167: f.dispose();
168:
169: assertTrue("Dead loop occured",
170: counter[0] <= VALID_NUMBER_OF_CALLS);
171: }
172:
173: }
|