001: /*
002: * @(#)VirtualWindowUTest.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.uicapture.v1;
028:
029: import java.awt.Robot;
030:
031: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
032: import junit.framework.Test;
033: import junit.framework.TestCase;
034: import junit.framework.TestSuite;
035:
036: import net.sourceforge.groboutils.uicapture.v1.event.*;
037: import java.awt.*;
038: import java.awt.event.*;
039: import java.util.*;
040:
041: /**
042: * Tests the VirtualWindow class.
043: *
044: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
045: * @version $Date: 2003/02/10 22:52:34 $
046: * @since Jan 6, 2002
047: */
048: public class VirtualWindowUTest extends TestCase {
049: //-------------------------------------------------------------------------
050: // Standard JUnit Class-specific declarations
051:
052: private static final Class THIS_CLASS = VirtualWindowUTest.class;
053: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
054:
055: public VirtualWindowUTest(String name) {
056: super (name);
057: }
058:
059: private VirtualWindow vw;
060:
061: public class MyCaptureListener implements ICaptureListener {
062: ArrayList mwm = new ArrayList();
063: ArrayList mm = new ArrayList();
064: ArrayList mp = new ArrayList();
065: ArrayList mr = new ArrayList();
066: ArrayList kp = new ArrayList();
067: ArrayList kr = new ArrayList();
068:
069: public void mouseWheelMoved(MouseWheelCaptureEvent ce) {
070: mwm.add(ce);
071: }
072:
073: public void mouseMoved(MouseMovedCaptureEvent ce) {
074: mm.add(ce);
075: }
076:
077: public void mousePressed(MousePressedCaptureEvent ce) {
078: mp.add(ce);
079: }
080:
081: public void mouseReleased(MouseReleasedCaptureEvent ce) {
082: mr.add(ce);
083: }
084:
085: public void keyPressed(KeyPressedCaptureEvent ce) {
086: kp.add(ce);
087: }
088:
089: public void keyReleased(KeyReleasedCaptureEvent ce) {
090: kr.add(ce);
091: }
092: }
093:
094: //-------------------------------------------------------------------------
095: // Tests
096:
097: public void testInstantiation1() {
098: this .vw = createVirtualWindow();
099: assertTrue("Default glass pane state is true.", this .vw
100: .isGlassEnabled());
101: }
102:
103: public void testInstantiation2() {
104: this .vw = createVirtualWindow(null, true);
105: assertTrue("Manually set glass pane state to true.", this .vw
106: .isGlassEnabled());
107: }
108:
109: public void testInstantiation3() {
110: this .vw = createVirtualWindow(null, false);
111: assertTrue("Manually set glass pane state to false.", !this .vw
112: .isGlassEnabled());
113: }
114:
115: public void testMouseWheelMoved1() {
116: this .vw = createVirtualWindow();
117: MyCaptureListener mcl1 = createCaptureListener();
118: MyCaptureListener mcl2 = createCaptureListener();
119: this .vw.addCaptureListener(mcl1);
120: this .vw.addCaptureListener(mcl2);
121:
122: MouseWheelEvent mwe = new MouseWheelEvent(getUIComponent(), 1,
123: System.currentTimeMillis(), 0, 0, 0, 2, false,
124: MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, 1);
125: this .vw.mouseWheelMoved(mwe);
126:
127: assertEquals("Did not fire a wheel event to listener 1.", 1,
128: mcl1.mwm.size());
129: assertEquals("Did not fire a wheel event to listener 2.", 1,
130: mcl2.mwm.size());
131: }
132:
133: public void testMouseDragged1() {
134: this .vw = createVirtualWindow();
135: MyCaptureListener mcl1 = createCaptureListener();
136: MyCaptureListener mcl2 = createCaptureListener();
137: this .vw.addCaptureListener(mcl1);
138: this .vw.addCaptureListener(mcl2);
139:
140: MouseEvent me = new MouseEvent(getUIComponent(), 1, System
141: .currentTimeMillis(), 0, 0, 0, 0, // click count
142: false, 0);
143: this .vw.mouseDragged(me);
144: }
145:
146: public void testMouseMoved1() {
147: this .vw = createVirtualWindow();
148: MyCaptureListener mcl1 = createCaptureListener();
149: MyCaptureListener mcl2 = createCaptureListener();
150: this .vw.addCaptureListener(mcl1);
151: this .vw.addCaptureListener(mcl2);
152:
153: MouseEvent me = new MouseEvent(getUIComponent(), 1, System
154: .currentTimeMillis(), 0, 0, 0, 0, // click count
155: false, 0);
156: this .vw.mouseMoved(me);
157:
158: assertEquals("Did not fire a move event to listener 1.", 1,
159: mcl1.mm.size());
160: assertEquals("Did not fire a move event to listener 2.", 1,
161: mcl2.mm.size());
162: }
163:
164: public void testMousePressed1() {
165: this .vw = createVirtualWindow();
166: MyCaptureListener mcl1 = createCaptureListener();
167: MyCaptureListener mcl2 = createCaptureListener();
168: this .vw.addCaptureListener(mcl1);
169: this .vw.addCaptureListener(mcl2);
170:
171: MouseEvent me = new MouseEvent(getUIComponent(), 1, System
172: .currentTimeMillis(), 0, 0, 0, 1, // click count
173: false, 0);
174: this .vw.mousePressed(me);
175:
176: assertEquals("Did not fire a mouse press event to listener 1.",
177: 1, mcl1.mp.size());
178: assertEquals("Did not fire a mouse press event to listener 2.",
179: 1, mcl2.mp.size());
180: }
181:
182: public void testMouseReleased1() {
183: this .vw = createVirtualWindow();
184: MyCaptureListener mcl1 = createCaptureListener();
185: MyCaptureListener mcl2 = createCaptureListener();
186: this .vw.addCaptureListener(mcl1);
187: this .vw.addCaptureListener(mcl2);
188:
189: MouseEvent me = new MouseEvent(getUIComponent(), 1, System
190: .currentTimeMillis(), 0, 0, 0, 0, // click count
191: false, 0);
192: this .vw.mouseReleased(me);
193:
194: assertEquals(
195: "Did not fire a mouse release event to listener 1.", 1,
196: mcl1.mr.size());
197: assertEquals(
198: "Did not fire a mouse release event to listener 2.", 1,
199: mcl2.mr.size());
200: }
201:
202: public void testKeyPressed1() {
203: this .vw = createVirtualWindow();
204: MyCaptureListener mcl1 = createCaptureListener();
205: MyCaptureListener mcl2 = createCaptureListener();
206: this .vw.addCaptureListener(mcl1);
207: this .vw.addCaptureListener(mcl2);
208:
209: KeyEvent ke = new KeyEvent(getUIComponent(), 1, System
210: .currentTimeMillis(), 0, KeyEvent.VK_A, 'a', 0);
211: this .vw.keyPressed(ke);
212:
213: assertEquals("Did not fire a key press event to listener 1.",
214: 1, mcl1.kp.size());
215: assertEquals("Did not fire a key press event to listener 2.",
216: 1, mcl2.kp.size());
217: }
218:
219: public void testKeyReleased1() {
220: this .vw = createVirtualWindow();
221: MyCaptureListener mcl1 = createCaptureListener();
222: MyCaptureListener mcl2 = createCaptureListener();
223: this .vw.addCaptureListener(mcl1);
224: this .vw.addCaptureListener(mcl2);
225:
226: KeyEvent ke = new KeyEvent(getUIComponent(), 1, System
227: .currentTimeMillis(), 0, KeyEvent.VK_A, 'a', 0);
228: this .vw.keyReleased(ke);
229:
230: assertEquals("Did not fire a key release event to listener 1.",
231: 1, mcl1.kr.size());
232: assertEquals("Did not fire a key release event to listener 2.",
233: 1, mcl2.kr.size());
234: }
235:
236: //-------------------------------------------------------------------------
237:
238: protected VirtualWindow createVirtualWindow() {
239: try {
240: return new VirtualWindow();
241: } catch (java.awt.AWTException ae) {
242: fail("JDK implementation does not support low-level Robot "
243: + "actions: " + ae);
244: // unreachable
245: return null;
246: }
247: }
248:
249: protected VirtualWindow createVirtualWindow(String title,
250: boolean enable) {
251: try {
252: return new VirtualWindow(title, enable);
253: } catch (java.awt.AWTException ae) {
254: fail("JDK implementation does not support low-level Robot "
255: + "actions: " + ae);
256: // unreachable
257: return null;
258: }
259: }
260:
261: protected Component getUIComponent() {
262: return this .vw.getWindow();
263: }
264:
265: protected MyCaptureListener createCaptureListener() {
266: return new MyCaptureListener();
267: }
268:
269: //-------------------------------------------------------------------------
270: // Standard JUnit declarations
271:
272: public static Test suite() {
273: TestSuite suite = new TestSuite(THIS_CLASS);
274:
275: return suite;
276: }
277:
278: public static void main(String[] args) {
279: String[] name = { THIS_CLASS.getName() };
280:
281: // junit.textui.TestRunner.main( name );
282: // junit.swingui.TestRunner.main( name );
283:
284: junit.textui.TestRunner.main(name);
285: }
286:
287: /**
288: *
289: * @exception Exception thrown under any exceptional condition.
290: */
291: protected void setUp() throws Exception {
292: super .setUp();
293:
294: // set ourself up
295: }
296:
297: /**
298: *
299: * @exception Exception thrown under any exceptional condition.
300: */
301: protected synchronized void tearDown() throws Exception {
302: // tear ourself down
303: if (this.vw != null) {
304: this.vw.dispose();
305: this.vw = null;
306: }
307:
308: super.tearDown();
309: }
310: }
|