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 Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import junit.framework.TestCase;
023:
024: public class EventTest extends TestCase {
025:
026: /*
027: * @see TestCase#setUp()
028: */
029: @Override
030: protected void setUp() throws Exception {
031: super .setUp();
032: }
033:
034: /*
035: * @see TestCase#tearDown()
036: */
037: @Override
038: protected void tearDown() throws Exception {
039: super .tearDown();
040: }
041:
042: public final void testParamString() {
043: Button button = new Button("Test paramString()");
044: int id = Event.GOT_FOCUS;
045: Event evt = new Event(button, id, null);
046: assertEquals("id=" + id + ",x=0,y=0,target="
047: + evt.target.toString(), evt.paramString());
048: id = Event.ACTION_EVENT;
049: Object obj = new Object();
050: evt = new Event(button, id, obj);
051: assertEquals("id=" + id + ",x=0,y=0,target=" + button + ",arg="
052: + obj, evt.paramString());
053: id = Event.KEY_PRESS;
054: evt = new Event(button, 1048576l, id, 0, 0, Event.ENTER,
055: Event.CTRL_MASK | Event.META_MASK | Event.SHIFT_MASK);
056: assertEquals("id=" + id + ",x=0,y=0,key=" + evt.key
057: + ",shift,control,meta,target=" + button, evt
058: .paramString());
059: }
060:
061: /*
062: * Class under test for void Event(java.lang.Object, int, java.lang.Object)
063: */
064: public final void testEventObjectintObject() {
065: String buttonLabel = "Test Event";
066: Button button = new Button(buttonLabel);
067: int id = Event.ACTION_EVENT;
068: Event evt = new Event(button, id, button.getLabel());
069: assertSame(button, evt.target);
070: assertEquals(id, evt.id);
071: assertSame(buttonLabel, evt.arg);
072: }
073:
074: /*
075: * Class under test for void Event(java.lang.Object, long, int, int, int, int, int)
076: */
077: public final void testEventObjectlongintintintintint() {
078: Button button = new Button("Test constructor");
079: int id = Event.MOUSE_DOWN;
080: long when = 16 * 1048576l;
081: int mod = Event.CTRL_MASK;
082: Event evt = new Event(button, when, id, 100, 200, Event.ENTER,
083: mod);
084: assertSame(button, evt.target);
085: assertEquals(id, evt.id);
086: assertEquals(100, evt.x);
087: assertEquals(200, evt.y);
088: assertEquals(mod, evt.modifiers);
089: assertEquals(when, evt.when);
090: assertEquals(0, evt.clickCount);
091: }
092:
093: /*
094: * Class under test for void Event(java.lang.Object, long, int, int, int, int, int, java.lang.Object)
095: */
096: public final void testEventObjectlongintintintintintObject() {
097: Button button = new Button("Test constructor");
098: int id = Event.KEY_ACTION_RELEASE, x = 13, y = 666;
099: int key = Event.ESCAPE;
100: long when = 32 * 1048576l;
101: int mod = Event.SHIFT_MASK | Event.META_MASK;
102: Object obj = new Object();
103: Event evt = new Event(button, when, id, x, y, key, mod, obj);
104: assertSame(button, evt.target);
105: assertEquals(id, evt.id);
106: assertEquals(x, evt.x);
107: assertEquals(y, evt.y);
108: assertEquals(mod, evt.modifiers);
109: assertEquals(when, evt.when);
110: assertSame(obj, evt.arg);
111: }
112:
113: public final void testTranslate() {
114: Button button = new Button("Test translate");
115: int id = Event.MOUSE_MOVE, x = 20, y = -5;
116: int key = 0;
117: long when = 32 * 1048576l;
118: int mod = 0;
119: Event evt = new Event(button, when, id, x, y, key, mod);
120: evt.translate(-20, 15);
121: assertEquals(0, evt.x);
122: assertEquals(10, evt.y);
123: }
124:
125: public final void testControlDown() {
126: Button button = new Button("Test control down");
127: int id = Event.KEY_ACTION, x = 0, y = -10;
128: int key = Event.DOWN;
129: long when = 32 * 1048576l;
130: int mod = 0;
131: Event evt = new Event(button, when, id, x, y, key, mod);
132: assertFalse(evt.controlDown());
133: evt.modifiers |= Event.SHIFT_MASK | Event.ALT_MASK;
134: assertFalse(evt.controlDown());
135: evt.modifiers = Event.CTRL_MASK;
136: assertTrue(evt.controlDown());
137: }
138:
139: public final void testMetaDown() {
140: Button button = new Button("Test META down");
141: int id = Event.MOUSE_DRAG, x = -20, y = 30;
142: int key = 0;
143: long when = 32 * 1048576l;
144: int mod = 0;
145: Event evt = new Event(button, when, id, x, y, key, mod);
146: assertFalse(evt.metaDown());
147: evt.modifiers |= Event.META_MASK;
148: assertTrue(evt.metaDown());
149: evt.modifiers = Event.ALT_MASK;
150: assertFalse(evt.metaDown());
151: evt.modifiers |= Event.SHIFT_MASK | Event.CTRL_MASK
152: | Event.META_MASK;
153: assertTrue(evt.metaDown());
154: }
155:
156: public final void testShiftDown() {
157: Button button = new Button("Test shift down");
158: int id = Event.MOUSE_MOVE, x = 20, y = -30;
159: int key = 'a';
160: long when = 32 * 1048576l;
161: int mod = 0;
162: Event evt = new Event(button, when, id, x, y, key, mod);
163: assertFalse(evt.shiftDown());
164: evt.modifiers |= Event.META_MASK;
165: assertFalse(evt.shiftDown());
166: evt.modifiers = Event.SHIFT_MASK;
167: assertTrue(evt.shiftDown());
168: evt.modifiers |= Event.CTRL_MASK | Event.ALT_MASK;
169: assertTrue(evt.shiftDown());
170: }
171:
172: public void testDispatchEvent() {
173: // Regression test for HARMONY-2460
174: new MenuItem().dispatchEvent(new AWTEventImpl(new Button(), 1));
175: }
176:
177: class AWTEventImpl extends AWTEvent {
178: public AWTEventImpl(Object source, int id) {
179: super (source, id);
180: }
181: }
182:
183: public static void main(String[] args) {
184: junit.textui.TestRunner.run(EventTest.class);
185: }
186: }
|