001: package abbot.script;
002:
003: import java.awt.*;
004: import javax.swing.JButton;
005:
006: import abbot.finder.matchers.*;
007: import abbot.tester.ComponentTester;
008: import junit.extensions.abbot.*;
009: import junit.extensions.abbot.Timer;
010:
011: public class AnnotationTest extends ResolverFixture {
012:
013: private Timer timer = new Timer();
014:
015: private class MyAnnotation extends Annotation {
016: public volatile boolean isRunning;
017:
018: public MyAnnotation() {
019: super (AnnotationTest.this .getResolver(), getName());
020: }
021:
022: public void runStep() throws Throwable {
023: isRunning = true;
024: try {
025: super .runStep();
026: } finally {
027: isRunning = false;
028: }
029: }
030: }
031:
032: private Throwable stepFailure = null;
033:
034: // FIXME sporadic linux failure when run with full suite
035: public void testShowUserDismiss() throws Throwable {
036: final MyAnnotation ann = new MyAnnotation();
037: ann.setTitle(getName());
038: ann
039: .setText("<html>This is a user-dismissed annotation.<br>It will remain until dismissed.</html>");
040: ann.setUserDismiss(true);
041: new Thread(getName()) {
042: public void run() {
043: try {
044: ann.run();
045: } catch (Throwable thrown) {
046: stepFailure = thrown;
047: }
048: }
049: }.start();
050: timer.reset();
051: Window w = wait(ann);
052: while (!w.isShowing()) {
053: if (timer.elapsed() > 10000)
054: fail("Annotation window never showed");
055: getRobot().sleep();
056: }
057: while (w.isShowing()) {
058: if (timer.elapsed() > ann.getDelayTime() + 1000)
059: break;
060: getRobot().sleep();
061: }
062: assertTrue("Annotation should not automatically close", ann
063: .isShowing());
064: assertTrue(
065: "Annotation should block script execution while displaying",
066: ann.isRunning);
067: ComponentTester tester = new ComponentTester();
068: Component button = getFinder().find(w,
069: new ClassMatcher(JButton.class));
070: tester.actionClick(button);
071: // Give the annotation thread time to clean up
072: Timer timer = new Timer();
073: while (ann.isShowing()) {
074: getRobot().sleep();
075: if (timer.elapsed() > 1000)
076: fail("Annotation should be hidden after dismiss button pressed");
077: }
078: timer.reset();
079: while (ann.isRunning) {
080: getRobot().sleep();
081: if (timer.elapsed() > 1000)
082: fail("Annotation step should have finished");
083: }
084: assertEquals("No exception should be thrown", (Throwable) null,
085: stepFailure);
086: }
087:
088: public void testShow() throws Throwable {
089: final Annotation ann = new Annotation(getResolver(), getName());
090: ann.setTitle(getName());
091: ann
092: .setText("<html>This is an <b>example</b> of an auto-closing annotation</html>");
093: new Thread(getName()) {
094: public void run() {
095: try {
096: ann.run();
097: } catch (Throwable thr) {
098: stepFailure = thr;
099: }
100: }
101: }.start();
102: timer.reset();
103: Window w = wait(ann);
104: while (!w.isShowing()) {
105: if (timer.elapsed() > 10000)
106: fail("Annotation window never showed");
107: getRobot().sleep();
108: }
109: while (w.isShowing()) {
110: if (timer.elapsed() > ann.getDelayTime() + 1000)
111: fail("Annotation window should have closed automatically");
112: getRobot().sleep();
113: }
114: assertEquals("No exception should be thrown", (Throwable) null,
115: stepFailure);
116: }
117:
118: private Window wait(Annotation ann) {
119: long start = System.currentTimeMillis();
120: while (System.currentTimeMillis() - start < ann.getDelayTime()) {
121: try {
122: return (Window) getFinder().find(
123: new ClassMatcher(
124: Annotation.AnnotationWindow.class) {
125: public boolean matches(Component c) {
126: return super .matches(c)
127: && ((Window) c).isShowing();
128: }
129: });
130: } catch (Exception e) {
131: }
132: }
133: throw new RuntimeException("Window never showed");
134: }
135:
136: public AnnotationTest(String name) {
137: super (name);
138: }
139:
140: public static void main(String[] args) {
141: TestHelper.runTests(args, AnnotationTest.class);
142: }
143: }
|