001: package abbot.tester;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.lang.ref.WeakReference;
006:
007: import javax.swing.*;
008:
009: import junit.extensions.abbot.*;
010:
011: /** Verify proper operation of tracking input state. */
012: public class InputStateTest extends ComponentTestFixture {
013:
014: private Robot robot = null;
015: private InputState state = null;
016:
017: // sporadic w32 failures (mouse component not null after exit)
018: public void testTrackMouse() {
019: assertNull("Should be no mouse component w/no components",
020: state.getMouseComponent());
021: JButton button = new JButton(getName());
022: Frame frame = showFrame(button);
023: Point center = new Point(button.getWidth() / 2, button
024: .getHeight() / 2);
025: robot.mouseMove(button, center.x, center.y);
026: robot.waitForIdle();
027: assertEquals("Wrong mouse component", Robot.toString(button),
028: Robot.toString(state.getMouseComponent()));
029: assertEquals("Wrong relative location", center, state
030: .getMouseLocation());
031: Point loc = button.getLocationOnScreen();
032: loc.translate(center.x, center.y);
033: assertEquals("Wrong screen location", loc, state
034: .getMouseLocationOnScreen());
035:
036: // FIXME w/o the second wait, w32 fails this test about 1 in 4
037: // (26/100 when repeated). Not sure why the state is delayed in
038: // updating its internal state.
039: robot.mouseMove(frame, -1, -1);
040: robot.waitForIdle();
041: robot.waitForIdle();
042:
043: assertNull("Should be no mouse component after exit", state
044: .getMouseComponent());
045: assertNull("Should be no relative location", state
046: .getMouseLocation());
047: assertTrue("Should be a last known screen location", state
048: .getMouseLocationOnScreen() != null);
049: }
050:
051: public void testTrackNonNativeDrag() {
052: JButton one = new JButton("One");
053: JButton two = new JButton("Two");
054: JPanel pane = new JPanel();
055: pane.add(one);
056: pane.add(two);
057: Frame frame = showFrame(pane);
058: frame.addMouseListener(new MouseAdapter() {
059: });
060: frame.addMouseMotionListener(new MouseMotionAdapter() {
061: });
062: robot.mouseMove(one);
063: robot.mousePress(InputEvent.BUTTON1_MASK);
064: robot.mouseMove(one);
065: robot.waitForIdle();
066: assertEquals("Wrong drag start component", Robot.toString(one),
067: Robot.toString(state.getDragSource()));
068: assertEquals("Wrong mouse component", Robot.toString(one),
069: Robot.toString(state.getMouseComponent()));
070:
071: robot.mouseMove(two);
072: robot.waitForIdle();
073: assertEquals(
074: "Containing component should be tracked during drag",
075: Robot.toString(two), Robot.toString(state
076: .getMouseComponent()));
077:
078: // latest revs of 1.3, 1.4 don't provide enter/exit events while
079: // dragging
080: /*
081: if (Robot.getEventMode() == Robot.EM_ROBOT) {
082: robot.mouseMove(frame, frame.getWidth() * 2,
083: frame.getHeight() * 2);
084: robot.mouseMove(frame, frame.getWidth() * 2 + 1,
085: frame.getHeight() * 2);
086: robot.waitForIdle();
087: assertEquals("Drag outside should leave no current mouse component",
088: null, state.getMouseComponent());
089: }
090: */
091: }
092:
093: /** Not all components track mouse events. */
094: public void testMouseComponent() {
095: JLabel label = new JLabel(getName());
096: Frame f = showFrame(label);
097: robot.click(label);
098: robot.waitForIdle();
099: assertEquals("Wrong mouse component", f, state
100: .getMouseComponent());
101: assertEquals("Wrong ultimate mouse component", label, state
102: .getUltimateMouseComponent());
103: }
104:
105: public void testNoReferencesHeld() {
106: JLabel label = new JLabel(getName());
107: Frame f = showFrame(label);
108: WeakReference ref = new WeakReference(label);
109: robot.click(label);
110: robot.waitForIdle();
111:
112: // Ensure things get properly GCed
113: f.dispose();
114: f.remove(label);
115: f = null;
116: label = null;
117: System.gc();
118: assertNull("Someone still references the label", ref.get());
119:
120: assertTrue("Should be no mouse component", state
121: .getMouseComponent() == null);
122: }
123:
124: protected void setUp() {
125: state = new InputState();
126: robot = getRobot();
127: }
128:
129: protected void tearDown() {
130: state.dispose();
131: state = null;
132: robot = null;
133: }
134:
135: public InputStateTest(String name) {
136: super (name);
137: }
138:
139: public static void main(String[] args) {
140: RepeatHelper.runTests(args, InputStateTest.class);
141: }
142: }
|