001: package abbot.tester;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import javax.swing.event.*;
007:
008: import junit.extensions.abbot.*;
009: import junit.extensions.abbot.Timer;
010: import abbot.util.Bugs;
011:
012: /** Unit test to verify Robot drag/drop operation. Include "Native" in
013: * the test name if the test depends on native drag and drop.
014: */
015: // FIXME w32 has sporadic errors (4 failures on a repeat of 50) where the drag
016: // gesture is not recognized (it might be that post-press mouse move is missed)
017: // The failure is also present on non-native drags
018: public class RobotDragDropTest extends ComponentTestFixture {
019:
020: /** Ensure drag gestures are recognized. */
021: public void testNativeDragDrop() throws Exception {
022: DragLabel c = new DragLabel(getName());
023: showFrame(c);
024: robot.drag(c, c.getWidth() / 2, c.getHeight() / 2);
025: Timer timer = new Timer();
026: while (!c.dragStarted) {
027: if (timer.elapsed() > EVENT_GENERATION_DELAY) {
028: fail("Drag gesture not recognized");
029: }
030: if (c.exception != null)
031: throw c.exception;
032: robot.sleep();
033: }
034:
035: robot.drop(c, 1, 1);
036: timer.reset();
037: while (!c.dropAccepted || !c.dragEnded) {
038: if (timer.elapsed() > EVENT_GENERATION_DELAY) {
039: assertTrue("Drag never ended", c.dragEnded);
040: assertTrue("Drag not accepted", c.dropAccepted);
041: }
042: robot.sleep();
043: }
044: // Fails on OSX 1.3.1, 1.4.1 (VM bug)
045: assertTrue("Drag ended, but drop failed", c.dropSuccessful);
046: }
047:
048: public void testNativeDragDropAcrossComponents() throws Exception {
049: DragLabel c1 = new DragLabel("Start drag here", false);
050: DropLabel c2 = new DropLabel("Drop here");
051: JPanel pane = new JPanel();
052: pane.add(c1);
053: pane.add(c2);
054:
055: showFrame(pane);
056: robot.drag(c1, c1.getWidth() / 2, c1.getHeight() / 2);
057: Timer timer = new Timer();
058: while (!c1.dragStarted) {
059: if (timer.elapsed() > EVENT_GENERATION_DELAY) {
060: fail("Drag gesture not recognized");
061: }
062: if (c1.exception != null)
063: throw c1.exception;
064: robot.sleep();
065: }
066: robot.drop(c2, c2.getWidth() / 2, c2.getHeight() / 2);
067: timer.reset();
068: // source drag exit is not reliable; w32 sends it after drop accepted,
069: // OSX never sends it
070: while (!c2.dragEntered || !c2.dropAccepted || !c1.dragEnded) {
071: if (timer.elapsed() > EVENT_GENERATION_DELAY) {
072: assertTrue("Never received drag enter on target",
073: c2.dragEntered);
074: assertTrue("Never received drag end", c1.dragEnded);
075: assertTrue("Drop not accepted", c2.dropAccepted);
076: }
077: robot.sleep();
078: }
079: // Fails on OSX 1.3.1, 1.4.1 (VM bug)
080: assertTrue("Drag ended, but drop failed", c1.dropSuccessful);
081: }
082:
083: public void testNativeDragDropToJTree() throws Exception {
084: DragLabel label = new DragLabel("Start drag here", false);
085: DropTree tree = new DropTree();
086: JPanel pane = new JPanel();
087: pane.add(label);
088: pane.add(tree);
089: showFrame(pane);
090: robot.drag(label, label.getWidth() / 2, label.getHeight() / 2);
091: Timer timer = new Timer();
092: while (!label.dragStarted) {
093: if (timer.elapsed() > EVENT_GENERATION_DELAY) {
094: fail("Drag gesture not recognized");
095: }
096: if (label.exception != null)
097: throw label.exception;
098: robot.sleep();
099: }
100: Rectangle rect = tree.getRowBounds(2);
101: robot.drop(tree, rect.x + rect.width / 2, rect.y + rect.height
102: / 2);
103: timer.reset();
104: while (!tree.dragEntered || !tree.dropAccepted
105: || !label.dragEnded) {
106: if (timer.elapsed() > EVENT_GENERATION_DELAY) {
107: assertTrue("Drag never entered drop target",
108: tree.dragEntered);
109: assertTrue("Drag never ended", label.dragEnded);
110: assertTrue("Drag not accepted", tree.dropAccepted);
111: }
112: robot.sleep();
113: }
114: // Fails on OSX 1.3.1, 1.4.1 (VM bug)
115: assertTrue("Drag ended, but drop failed", label.dropSuccessful);
116: }
117:
118: public void testNativeDragDropToJTable() throws Exception {
119: DragLabel label = new DragLabel("Start drag here", false);
120: DropTable table = new DropTable();
121: JPanel pane = new JPanel();
122: pane.add(label);
123: pane.add(table);
124: showFrame(pane);
125: robot.drag(label, label.getWidth() / 2, label.getHeight() / 2);
126: Timer timer = new Timer();
127: while (!label.dragStarted) {
128: if (timer.elapsed() > EVENT_GENERATION_DELAY) {
129: fail("Drag gesture not recognized");
130: }
131: if (label.exception != null)
132: throw label.exception;
133: robot.sleep();
134: }
135: Rectangle rect = table.getCellRect(1, 1, false);
136: robot.drop(table, rect.x + rect.width / 2, rect.y + rect.height
137: / 2);
138: timer.reset();
139: while (!table.dragEntered || !table.dropAccepted
140: || !label.dragEnded) {
141: if (timer.elapsed() > EVENT_GENERATION_DELAY) {
142: assertTrue("Drag never entered drop target",
143: table.dragEntered);
144: assertTrue("Drag never ended", label.dragEnded);
145: assertTrue("Drag not accepted", table.dropAccepted);
146: }
147: robot.sleep();
148: }
149: // Fails on OSX 1.3.1, 1.4.1 (VM bug)
150: assertTrue("Drag ended, but drop failed", label.dropSuccessful);
151: }
152:
153: public void testJavaDragDrop() {
154: JLabel label = new JLabel(getName());
155: showFrame(label);
156: class DragListener extends MouseInputAdapter {
157: public boolean gotDrag = false;
158:
159: public void mouseDragged(MouseEvent me) {
160: gotDrag = true;
161: }
162: }
163: DragListener ma = new DragListener();
164: label.addMouseListener(ma);
165: label.addMouseMotionListener(ma);
166: getRobot().drag(label, 1, 1);
167: getRobot().waitForIdle();
168: getRobot().drop(label, label.getWidth() / 2,
169: label.getHeight() / 2);
170: getRobot().waitForIdle();
171: assertTrue("Should have seen a drag event", ma.gotDrag);
172: }
173:
174: /** Note the event mode when reporting this test's name. */
175: public String getName() {
176: return Robot.getEventMode() == Robot.EM_AWT ? super .getName()
177: + " (AWT mode)" : super .getName();
178: }
179:
180: public void runBare() throws Throwable {
181: if (!Bugs.dragDropRequiresNativeEvents()
182: || Robot.getEventMode() == Robot.EM_ROBOT
183: || getName().indexOf("Native") == -1) {
184: super .runBare();
185: } else {
186: System.err.println("Skipping test " + getName());
187: }
188: }
189:
190: private Frame frame;
191:
192: protected Frame showFrame(Component c) {
193: return frame = super .showFrame(c);
194: }
195:
196: private Robot robot;
197:
198: protected void setUp() {
199: robot = getRobot();
200: }
201:
202: protected void tearDown() {
203: if (frame != null) {
204: // Clear the DnD state
205: robot.click(frame);
206: }
207: }
208:
209: /** Provide for repetitive testing on individual tests. */
210: public static void main(String[] args) {
211: RepeatHelper.runTests(args, RobotDragDropTest.class);
212: }
213: }
|