001: package abbot.util;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.util.ArrayList;
006: import java.util.Iterator;
007: import javax.swing.*;
008: import abbot.tester.JTreeLocation;
009: import abbot.tester.JTreeTester;
010:
011: import junit.extensions.abbot.*;
012: import java.util.List;
013:
014: public class EventNormalizerTest extends ComponentTestFixture {
015:
016: private class Listener implements AWTEventListener {
017: public int disposedCount = 0;
018: public List events = new ArrayList();
019:
020: public void eventDispatched(AWTEvent e) {
021: if (e.getID() == WindowEvent.WINDOW_CLOSED) {
022: ++disposedCount;
023: }
024: events.add(e);
025: }
026: }
027:
028: private AWTEvent findEvent(List events, Object source, int id) {
029: for (Iterator i = events.iterator(); i.hasNext();) {
030: AWTEvent e = (AWTEvent) i.next();
031: if (e.getID() == id && e.getSource() == source) {
032: return e;
033: }
034: }
035: return null;
036: }
037:
038: public void testForwardNativeDragEvents() {
039: EventNormalizer n = new EventNormalizer(true);
040: Listener x = new Listener();
041: n.startListening(x, InputEvent.MOUSE_EVENT_MASK
042: | InputEvent.MOUSE_MOTION_EVENT_MASK);
043: JTree tree = new JTree();
044: // 1.4 only
045: tree.setDragEnabled(true);
046: JTextField text = new JTextField(40);
047: JPanel p = new JPanel(new BorderLayout());
048: p.add(tree, BorderLayout.CENTER);
049: p.add(text, BorderLayout.NORTH);
050: showFrame(p);
051: JTreeTester tester = new JTreeTester();
052: tester.actionClick(tree, new JTreeLocation(0));
053: tester.actionDrag(tree, new JTreeLocation(0));
054: tester.actionDrop(text);
055:
056: assertEquals("Drag/drop operation did not complete", "JTree",
057: text.getText());
058: assertNotNull("Expected a 'drag' event on tree: "
059: + events(x.events), findEvent(x.events, tree,
060: MouseEvent.MOUSE_DRAGGED));
061: assertNotNull("Expected a 'drag' event on text: "
062: + events(x.events), findEvent(x.events, text,
063: MouseEvent.MOUSE_DRAGGED));
064: assertNotNull("Expected a 'drop' event on text: "
065: + events(x.events), findEvent(x.events, text,
066: MouseEvent.MOUSE_RELEASED));
067: }
068:
069: private String events(List x) {
070: StringBuffer buf = new StringBuffer();
071: for (Iterator i = x.iterator(); i.hasNext();) {
072: buf.append("\n\t" + String.valueOf(i.next()));
073: }
074: return buf.toString();
075: }
076:
077: public void testFilterDuplicateDispose() {
078: EventNormalizer n = new EventNormalizer();
079: Listener l = new Listener();
080: n.startListening(l, WindowEvent.WINDOW_EVENT_MASK);
081: Frame f = showFrame(new JLabel(getName()));
082: f.dispose();
083: getRobot().waitForIdle();
084: assertEquals(
085: "One call to dispose should result in one event (1)",
086: 1, l.disposedCount);
087: f.dispose();
088: getRobot().waitForIdle();
089: assertEquals("Multiple dispose events should be filtered (1)",
090: 1, l.disposedCount);
091:
092: l.disposedCount = 0;
093: showWindow(f);
094: f.dispose();
095: // wait for dispose to finish
096: getRobot().waitForIdle();
097: assertEquals("One call to dispose should result in one event "
098: + "(component disposed and shown)", 1, l.disposedCount);
099: f.dispose();
100: getRobot().waitForIdle();
101: assertEquals("Multiple dispose events should be filtered "
102: + "(component disposed and shown)", 1, l.disposedCount);
103: }
104:
105: public static void main(String[] args) {
106: TestHelper.runTests(args, EventNormalizerTest.class);
107: }
108: }
|