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 Michael Danilov
019: * @version $Revision$
020: */package java.awt.event;
021:
022: import java.awt.Button;
023: import java.awt.Component;
024: import java.awt.Frame;
025: import java.awt.MouseInfo;
026: import java.awt.Point;
027: import java.awt.Robot;
028:
029: import junit.framework.TestCase;
030:
031: public class MouseEventTest extends TestCase {
032:
033: public final void testMouseEventComponentintlongintintintintboolean() {
034: Button button = new Button("Button");
035: MouseEvent event = new MouseEvent(button,
036: MouseEvent.MOUSE_PRESSED, 1000000000,
037: InputEvent.BUTTON2_DOWN_MASK, 100, 200, 10, true);
038:
039: assertEquals(event.getSource(), button);
040: assertEquals(event.getID(), MouseEvent.MOUSE_PRESSED);
041: assertEquals(event.getButton(), MouseEvent.NOBUTTON);
042: assertEquals(event.getClickCount(), 10);
043: assertEquals(event.getPoint(), new Point(100, 200));
044: assertEquals(event.getX(), 100);
045: assertEquals(event.getY(), 200);
046: assertTrue(event.isPopupTrigger());
047: }
048:
049: public final void testMouseEventComponentintlongintintintintbooleanint() {
050: final Button button = new Button("Button");
051: MouseEvent event = new MouseEvent(button,
052: MouseEvent.MOUSE_PRESSED, 1000000000,
053: InputEvent.BUTTON2_DOWN_MASK, 100, 200, 10, true,
054: MouseEvent.BUTTON1);
055:
056: assertEquals(button, event.getSource());
057: assertEquals(MouseEvent.MOUSE_PRESSED, event.getID());
058: assertEquals(MouseEvent.BUTTON1, event.getButton());
059: assertEquals(10, event.getClickCount());
060: assertEquals(new Point(100, 200), event.getPoint());
061: assertEquals(100, event.getX());
062: assertEquals(200, event.getY());
063: assertTrue(event.isPopupTrigger());
064: assertEquals(InputEvent.BUTTON1_MASK, event.getModifiers());
065: assertEquals(InputEvent.BUTTON2_DOWN_MASK, event
066: .getModifiersEx());
067:
068: event = new MouseEvent(button, MouseEvent.MOUSE_PRESSED, 0,
069: InputEvent.BUTTON1_MASK, 0, 0, 0, true,
070: MouseEvent.NOBUTTON);
071: assertEquals(InputEvent.BUTTON1_MASK, event.getModifiers());
072: assertEquals(InputEvent.BUTTON1_DOWN_MASK, event
073: .getModifiersEx());
074: assertEquals(MouseEvent.BUTTON1, event.getButton());
075:
076: event = new MouseEvent(button, MouseEvent.MOUSE_PRESSED, 0, 0,
077: 0, 0, 0, true, MouseEvent.BUTTON1);
078: assertEquals(InputEvent.BUTTON1_MASK, event.getModifiers());
079: assertEquals(MouseEvent.BUTTON1, event.getButton());
080:
081: event = new MouseEvent(button, MouseEvent.MOUSE_RELEASED,
082: 1000000000, 0, 100, 200, 10, true, MouseEvent.BUTTON1);
083: assertEquals(InputEvent.BUTTON1_MASK, event.getModifiers());
084: assertEquals(0, event.getModifiersEx());
085:
086: event = new MouseEvent(button, MouseEvent.MOUSE_CLICKED,
087: 1000000000, 0, 100, 200, 10, true, MouseEvent.BUTTON1);
088: assertEquals(InputEvent.BUTTON1_MASK, event.getModifiers());
089: assertEquals(0, event.getModifiersEx());
090:
091: event = new MouseEvent(button, MouseEvent.MOUSE_PRESSED, 0,
092: InputEvent.BUTTON1_DOWN_MASK
093: | InputEvent.BUTTON2_DOWN_MASK
094: | InputEvent.BUTTON3_DOWN_MASK, 0, 0, 0, true,
095: MouseEvent.BUTTON3);
096: assertEquals(InputEvent.BUTTON3_MASK, event.getModifiers());
097: assertEquals(InputEvent.BUTTON1_DOWN_MASK
098: | InputEvent.BUTTON2_DOWN_MASK
099: | InputEvent.BUTTON3_DOWN_MASK, event.getModifiersEx());
100: }
101:
102: public final void testTranslatePoint() {
103: Button button = new Button("Button");
104: MouseEvent event = new MouseEvent(button,
105: MouseEvent.MOUSE_PRESSED, 1000000000,
106: InputEvent.BUTTON2_DOWN_MASK, 100, 200, 10, true);
107:
108: event.translatePoint(10, 10);
109: assertEquals(event.getPoint(), new Point(110, 210));
110: event.translatePoint(-20, -20);
111: assertEquals(event.getPoint(), new Point(90, 190));
112: }
113:
114: public final void testGetMouseModifiersText() {
115: assertTrue(MouseEvent
116: .getMouseModifiersText(InputEvent.ALT_MASK).indexOf(
117: "Alt") != -1); //$NON-NLS-1$
118: assertTrue(MouseEvent.getMouseModifiersText(
119: InputEvent.ALT_GRAPH_MASK).indexOf("Alt Graph") != -1); //$NON-NLS-1$
120: assertTrue(MouseEvent.getMouseModifiersText(
121: InputEvent.CTRL_MASK).indexOf("Ctrl") != -1); //$NON-NLS-1$
122: assertTrue(MouseEvent.getMouseModifiersText(
123: InputEvent.SHIFT_MASK).indexOf("Shift") != -1); //$NON-NLS-1$
124: assertTrue(MouseEvent.getMouseModifiersText(
125: InputEvent.META_MASK).indexOf("Meta") != -1); //$NON-NLS-1$
126: assertTrue(MouseEvent.getMouseModifiersText(
127: InputEvent.BUTTON1_MASK).indexOf("Button1") != -1); //$NON-NLS-1$
128: assertTrue(MouseEvent.getMouseModifiersText(
129: InputEvent.BUTTON2_MASK).indexOf("Button2") != -1); //$NON-NLS-1$
130: assertTrue(MouseEvent.getMouseModifiersText(
131: InputEvent.BUTTON3_MASK).indexOf("Button3") != -1); //$NON-NLS-1$
132: assertTrue(MouseEvent.getMouseModifiersText(
133: InputEvent.BUTTON3_MASK).indexOf("Meta") != -1); //$NON-NLS-1$
134:
135: // Regression for HARMONY-2403
136: assertEquals("Meta+Shift+Alt Graph+Button1+Button3", MouseEvent //$NON-NLS-1$
137: .getMouseModifiersText(MouseEvent.MOUSE_PRESSED));
138: }
139:
140: public final void testParamString() {
141: Button button = new Button("Button");
142: MouseEvent event = new MouseEvent(button,
143: MouseEvent.MOUSE_PRESSED, 1000000000,
144: InputEvent.BUTTON2_DOWN_MASK, 100, 200, 10, true,
145: MouseEvent.BUTTON1);
146:
147: assertEquals(
148: event.paramString(),
149: "MOUSE_PRESSED,(100,200),button=1,modifiers=Button1,extModifiers=Button2,clickCount=10");
150: event = new MouseEvent(button, MouseEvent.MOUSE_PRESSED + 1024,
151: 1000000000, InputEvent.BUTTON2_MASK, 100, 200, 10,
152: true, MouseEvent.BUTTON1);
153: assertEquals(
154: event.paramString(),
155: "unknown type,(100,200),button=1,modifiers=Alt+Button2,extModifiers=Alt+Button2,clickCount=10");
156: }
157:
158: public void testHarmony5215() throws Exception {
159: final Frame f = new Frame();
160: final Point l = MouseInfo.getPointerInfo().getLocation();
161: final Robot r = new Robot();
162: final MouseEvent[] e = new MouseEvent[1];
163:
164: f.addMouseListener(new MouseAdapter() {
165: public void mouseReleased(final MouseEvent event) {
166: synchronized (e) {
167: e[0] = event;
168: e.notify();
169: }
170: }
171:
172: public void mousePressed(final MouseEvent event) {
173: synchronized (e) {
174: e[0] = event;
175: e.notify();
176: }
177: }
178: });
179:
180: f.setSize(100, 100);
181: f.setVisible(true);
182: r.mouseMove(f.getX() + 50, f.getY() + 50);
183:
184: try {
185: synchronized (e) {
186: r.mousePress(InputEvent.BUTTON1_MASK);
187: e.wait(5000);
188: assertEquals(InputEvent.BUTTON1_MASK, e[0]
189: .getModifiers());
190: assertEquals(InputEvent.BUTTON1_DOWN_MASK, e[0]
191: .getModifiersEx());
192:
193: r.mouseRelease(InputEvent.BUTTON1_MASK);
194: e.wait(5000);
195: assertEquals(InputEvent.BUTTON1_MASK, e[0]
196: .getModifiers());
197: assertEquals(0, e[0].getModifiersEx());
198:
199: r.mousePress(InputEvent.BUTTON1_MASK);
200: e.wait(5000);
201: r.mousePress(InputEvent.BUTTON2_MASK);
202: e.wait(5000);
203: assertEquals(InputEvent.BUTTON2_MASK, e[0]
204: .getModifiers());
205: assertEquals(InputEvent.BUTTON1_DOWN_MASK
206: | InputEvent.BUTTON2_DOWN_MASK, e[0]
207: .getModifiersEx());
208:
209: r.mouseRelease(InputEvent.BUTTON2_MASK);
210: e.wait(5000);
211: assertEquals(InputEvent.BUTTON2_MASK, e[0]
212: .getModifiers());
213: assertEquals(InputEvent.BUTTON1_DOWN_MASK, e[0]
214: .getModifiersEx());
215:
216: r.mousePress(InputEvent.BUTTON2_MASK);
217: e.wait(5000);
218: r.mousePress(InputEvent.BUTTON3_MASK);
219: e.wait(5000);
220: assertEquals(InputEvent.BUTTON3_MASK, e[0]
221: .getModifiers());
222: assertEquals(InputEvent.BUTTON1_DOWN_MASK
223: | InputEvent.BUTTON2_DOWN_MASK
224: | InputEvent.BUTTON3_DOWN_MASK, e[0]
225: .getModifiersEx());
226:
227: r.mouseRelease(InputEvent.BUTTON3_MASK);
228: e.wait(5000);
229: assertEquals(InputEvent.BUTTON3_MASK, e[0]
230: .getModifiers());
231: assertEquals(InputEvent.BUTTON1_DOWN_MASK
232: | InputEvent.BUTTON2_DOWN_MASK, e[0]
233: .getModifiersEx());
234: }
235: } finally {
236: r.mouseRelease(InputEvent.BUTTON1_MASK);
237: r.mouseRelease(InputEvent.BUTTON2_MASK);
238: r.mouseRelease(InputEvent.BUTTON3_MASK);
239: r.mouseMove(l.x, l.y);
240: f.dispose();
241: }
242: }
243: }
|