001: package abbot.editor.recorder;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import java.util.*;
007:
008: import junit.extensions.abbot.ComponentTestFixture;
009: import junit.extensions.abbot.ResolverFixture;
010: import abbot.*;
011: import abbot.script.*;
012: import abbot.tester.*;
013: import abbot.tester.Robot;
014: import abbot.util.*;
015:
016: /** Provide generic support for checking contents of recorded steps. */
017: // FIXME still needs some refactoring
018: public abstract class AbstractRecorderFixture extends ResolverFixture {
019:
020: private EventWatcher watcher;
021: private Recorder recorder;
022: private RecordingFailedException failure;
023:
024: public AbstractRecorderFixture() {
025: }
026:
027: public AbstractRecorderFixture(String name) {
028: super (name);
029: }
030:
031: /** Provide a recorder to handle the input stream. */
032: protected abstract Recorder getRecorder();
033:
034: public void runBare() throws Throwable {
035: // Skip all recorder tests if not in robot mode, since there's no point
036: // in recording AWT-stuffed events.
037: if (Robot.getEventMode() == Robot.EM_ROBOT) {
038: try {
039: super .runBare();
040: } finally {
041: if (failure != null) {
042: throw failure.getReason() != null ? failure
043: .getReason() : failure;
044: }
045: }
046: }
047: }
048:
049: protected void fixtureTearDown() throws Throwable {
050: stopRecording();
051: recorder = null;
052: if (watcher != null) {
053: watcher.clear();
054: watcher = null;
055: }
056: super .fixtureTearDown();
057: }
058:
059: protected void startRecording() {
060: Log.debug("start recording");
061: recorder = getRecorder();
062: watcher = new EventWatcher();
063: watcher.startListening(recorder.getEventMask());
064: }
065:
066: protected void stopRecording() {
067: EventWatcher w = watcher;
068: if (w != null)
069: w.stopListening();
070: }
071:
072: protected String bugInfo(String msg) {
073: return "(" + new BugReport(msg) + ")";
074: }
075:
076: public void assertStep(String pattern, Step step) {
077: assertStep(pattern, step, null);
078: }
079:
080: /** Clear the current state. */
081: protected void clear() {
082: clearEvents();
083: }
084:
085: /** Clear the event history. */
086: protected synchronized void clearEvents() {
087: if (watcher != null)
088: watcher.clear();
089: }
090:
091: /** Allow derived classes to insert an event into the event stream to be
092: recorded. */
093: protected void insertEvent(AWTEvent e) {
094: if (watcher != null)
095: watcher.eventDispatched(e);
096: }
097:
098: protected synchronized String listEvents() {
099: return watcher != null ? watcher.listEvents() : "<empty>";
100: }
101:
102: public synchronized void assertStep(String pattern, Step step,
103: String events) {
104: String bugInfo;
105: if (events == null) {
106: bugInfo = bugInfo(listEvents());
107: clearEvents();
108: } else {
109: bugInfo = bugInfo(events);
110: }
111: Log.log(step != null ? ("Examining step: " + step)
112: : "No step available (expected '" + pattern + "')");
113: assertTrue("Expected <" + pattern
114: + ">, but no step was captured " + bugInfo,
115: step != null);
116: assertTrue("Incorrect step, expected <" + pattern
117: + ">, but got <" + step + "> " + bugInfo, Regexp
118: .stringContainsMatch(pattern, step.toString()));
119: }
120:
121: public String toString(Sequence seq) {
122: String steps = "The recorded steps were the following:";
123: for (int i = 0; i < seq.size(); i++) {
124: steps += "\n" + seq.getStep(i);
125: }
126: return steps;
127: }
128:
129: public void assertStepCount(int count, Sequence seq) {
130: String steps = toString(seq);
131: String bugInfo = bugInfo(listEvents() + "\n\n" + steps);
132: assertTrue("Wrong number of steps captured, expected <" + count
133: + ">, but got <" + seq.size() + "> " + bugInfo,
134: count == seq.size());
135: }
136:
137: protected class EventWatcher implements AWTEventListener {
138: protected ArrayList events = new ArrayList();
139: private EventNormalizer normalizer = new EventNormalizer(true);
140:
141: public void startListening(long mask) {
142: Log.debug("start listening, mask=0x"
143: + Integer.toHexString((int) mask));
144: clear();
145: Log.debug("events cleared");
146: normalizer.startListening(this , mask);
147: }
148:
149: public void stopListening() {
150: normalizer.stopListening();
151: }
152:
153: public void eventDispatched(AWTEvent event) {
154: if (Boolean.getBoolean("abbot.recorder.log_events"))
155: Log.log(Robot.toString(event));
156: synchronized (events) {
157: events.add(event);
158: }
159: Recorder r = recorder;
160: if (r != null) {
161: try {
162: r.record(event);
163: } catch (RecordingFailedException e) {
164: failure = e;
165: }
166: }
167: }
168:
169: public void clear() {
170: synchronized (events) {
171: events.clear();
172: }
173: }
174:
175: public String listEvents() {
176: String msg = "The generated event stream was the following:";
177: Iterator iter;
178: synchronized (events) {
179: iter = new ArrayList(events).iterator();
180: }
181: if (!iter.hasNext()) {
182: msg += "\n(No events were captured)";
183: } else
184: while (iter.hasNext()) {
185: AWTEvent ev = (AWTEvent) iter.next();
186: msg += "\n" + ComponentTester.toString(ev);
187: }
188: return msg;
189: }
190: }
191:
192: private abstract class PopupListener extends MouseAdapter {
193: protected abstract void showPopup(MouseEvent e);
194:
195: public void mousePressed(MouseEvent e) {
196: if (e.isPopupTrigger())
197: showPopup(e);
198: }
199:
200: public void mouseReleased(MouseEvent e) {
201: if (e.isPopupTrigger())
202: showPopup(e);
203: }
204: }
205:
206: /** Hook up the popup to be displayed on the given component. */
207: protected void addPopup(Component c, final PopupMenu popup) {
208: c.add(popup);
209: c.addMouseListener(new PopupListener() {
210: protected void showPopup(MouseEvent e) {
211: popup.show(e.getComponent(), e.getX(), e.getY());
212: }
213: });
214: }
215:
216: /** Hook up the popup to be displayed on the given component. */
217: protected void addPopup(Component c, final JPopupMenu popup) {
218: c.addMouseListener(new PopupListener() {
219: protected void showPopup(MouseEvent e) {
220: popup.show(e.getComponent(), e.getX(), e.getY());
221: }
222: });
223: }
224: }
|