Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 220   Methods: 25
NCLOC: 179   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
AbstractRecorderFixture.java 70.6% 94.6% 100% 89.5%
coverage coverage
 1   
 package abbot.editor.recorder;
 2   
 
 3   
 import java.awt.*;
 4   
 import java.awt.event.*;
 5   
 import javax.swing.*;
 6   
 import java.util.*;
 7   
 
 8   
 import junit.extensions.abbot.ComponentTestFixture;
 9   
 import abbot.*;
 10   
 import abbot.script.*;
 11   
 import abbot.tester.*;
 12   
 import abbot.tester.Robot;
 13   
 import abbot.util.*;
 14   
 
 15   
 /** Provide generic support for checking contents of recorded steps. */
 16   
 // FIXME still needs some refactoring
 17   
 public abstract class AbstractRecorderFixture extends ComponentTestFixture {
 18   
 
 19   
     private EventWatcher watcher;
 20   
     private Recorder recorder;
 21   
     private RecordingFailedException failure;
 22   
 
 23  84
     public AbstractRecorderFixture(String name) {
 24  84
         super(name);
 25   
     }
 26   
 
 27   
     /** Provide a recorder to handle the input stream. */
 28   
     protected abstract Recorder getRecorder();
 29   
 
 30  84
     public void runBare() throws Throwable {
 31   
         // Skip all recorder tests if not in robot mode, since there's no point
 32   
         // in recording AWT-stuffed events.
 33  84
         if (Robot.getEventMode() == Robot.EM_ROBOT) {
 34  84
             try {
 35  84
                 super.runBare();
 36   
             }
 37   
             finally {
 38  84
                 if (failure != null) {
 39  0
                     throw failure.getReason() != null
 40   
                         ? failure.getReason() : failure;
 41   
                 }
 42   
             }
 43   
         }
 44   
     }
 45   
 
 46  84
     protected void fixtureTearDown() throws Throwable {
 47  84
         stopRecording();
 48  84
         recorder = null;
 49  84
         if (watcher != null) {
 50  82
             watcher.clear();
 51  82
             watcher = null;
 52   
         }
 53  84
         super.fixtureTearDown();
 54   
     }
 55   
 
 56  88
     protected void startRecording() {
 57  88
         recorder = getRecorder();
 58  88
         watcher = new EventWatcher();
 59  88
         watcher.startListening(recorder.getEventMask());
 60   
     }
 61   
 
 62  98
     protected synchronized void stopRecording() {
 63  98
         if (watcher != null)
 64  96
             watcher.stopListening();
 65   
     }
 66   
 
 67   
     private static final String LS = System.getProperty("line.separator");
 68   
 
 69  288
     protected String bugInfo(String msg) {
 70  288
         return "(" + new BugReport(msg) + ")";
 71   
     }
 72   
 
 73  41
     public void assertStep(String pattern, Step step) {
 74  41
         assertStep(pattern, step, null);
 75   
     }
 76   
 
 77   
     /** Clear the current state. */
 78  118
     protected void clear() {
 79  118
         clearEvents();
 80   
     }
 81   
 
 82   
     /** Clear the event history. */
 83  248
     protected synchronized void clearEvents() {
 84  248
         if (watcher != null)
 85  182
             watcher.clear();
 86   
     }
 87   
 
 88   
     /** Allow derived classes to insert an event into the event stream to be
 89   
         recorded. */
 90  3
     protected void insertEvent(AWTEvent e) {
 91  3
         if (watcher != null)
 92  3
             watcher.eventDispatched(e);
 93   
     }
 94   
 
 95  307
     protected synchronized String listEvents() {
 96  307
         return watcher != null ? watcher.listEvents() : "<empty>";
 97   
     }
 98   
 
 99  158
     public synchronized void assertStep(String pattern, Step step,
 100   
                                         String events) {
 101  158
         String bugInfo;
 102  158
         if (events == null) {
 103  46
             bugInfo = bugInfo(listEvents());
 104  46
             clearEvents();
 105   
         }
 106   
         else {
 107  112
             bugInfo = bugInfo(events);
 108   
         }
 109  158
         Log.log(step != null ? ("Examining step: " + step) 
 110   
                 : "No step available (expected '" + pattern + "')");
 111  158
         assertTrue("Expected <" + pattern + ">, but no step was captured "
 112   
                    + bugInfo, step != null);
 113  158
         assertTrue("Incorrect step, expected <" + pattern + ">, but got <"
 114   
                    + step + "> " + bugInfo, 
 115   
                    Regexp.stringContainsMatch(pattern, step.toString()));
 116   
     }
 117   
 
 118  19
     public String toString(Sequence seq) {
 119  19
         String steps = "The recorded steps were the following:";
 120  19
         for (int i=0;i < seq.size();i++) {
 121  48
             steps += LS + seq.getStep(i);
 122   
         }
 123  19
         return steps;
 124   
     }
 125   
 
 126  18
     public void assertStepCount(int count, Sequence seq) {
 127  18
         String steps = toString(seq);
 128  18
         String bugInfo = bugInfo(listEvents() + LS + LS + steps);
 129  18
         assertTrue("Wrong number of steps captured, expected <" + count
 130   
                    + ">, but got <" + seq.size() + "> " + bugInfo, 
 131   
                    count == seq.size());
 132   
     }
 133   
 
 134   
     protected class EventWatcher implements AWTEventListener {
 135   
         protected ArrayList events = new ArrayList();
 136   
         private EventNormalizer normalizer = new EventNormalizer();
 137   
         
 138  88
         public void startListening(long mask) {
 139  88
             synchronized(events) {
 140  88
                 events.clear();
 141   
             }
 142  88
             normalizer.startListening(this, mask);
 143   
         }
 144   
         
 145  96
         public void stopListening() {
 146  96
             normalizer.stopListening();
 147   
         }
 148   
         
 149  4398
         public void eventDispatched(AWTEvent event) {
 150  4398
             if (Boolean.getBoolean("abbot.recorder.log_events"))
 151  0
                 Log.log(Robot.toString(event));
 152  4398
             synchronized(events) {
 153  4398
                 events.add(event);
 154   
             }
 155  4398
             Recorder r = recorder;
 156  4398
             if (r != null) {
 157  1913
                 try {
 158  1913
                     r.record(event);
 159   
                 }
 160   
                 catch(RecordingFailedException e) {
 161  0
                     failure = e;
 162   
                 }
 163   
             }
 164   
         }
 165   
         
 166  264
         public void clear() {
 167  264
             synchronized(events) {
 168  264
                 events.clear();
 169   
             }
 170   
         }
 171   
 
 172  307
         public String listEvents() {
 173  307
             String msg = "The generated event stream was the following:";
 174  307
             Iterator iter;
 175  307
             synchronized(events) {
 176  307
                 iter = new ArrayList(events).iterator();
 177   
             }
 178  307
             if (!iter.hasNext()) {
 179  66
                 msg += LS + "(No events were captured)";
 180   
             }
 181  241
             else while (iter.hasNext()) {
 182  2725
                 AWTEvent ev = (AWTEvent)iter.next();
 183  2725
                 msg += LS + ComponentTester.toString(ev);
 184   
             }
 185  307
             return msg;
 186   
         }
 187   
     }
 188   
 
 189   
     private abstract class PopupListener extends MouseAdapter {
 190   
         protected abstract void showPopup(MouseEvent e);
 191  6
         public void mousePressed(MouseEvent e) {
 192  6
             if (e.isPopupTrigger()) 
 193  0
                 showPopup(e);
 194   
         }
 195  6
         public void mouseReleased(MouseEvent e) {
 196  6
             if (e.isPopupTrigger())
 197  6
                 showPopup(e);
 198   
         }
 199   
     }
 200   
 
 201   
     /** Hook up the popup to be displayed on the given component. */
 202  3
     protected void addPopup(Component c, final PopupMenu popup) {
 203  3
         c.add(popup);
 204  3
         c.addMouseListener(new PopupListener() {
 205  3
             protected void showPopup(MouseEvent e) {
 206  3
                 popup.show(e.getComponent(), e.getX(), e.getY());
 207   
             }
 208   
         });
 209   
     }
 210   
 
 211   
     /** Hook up the popup to be displayed on the given component. */
 212  3
     protected void addPopup(Component c, final JPopupMenu popup) {
 213  3
         c.addMouseListener(new PopupListener() {
 214  3
             protected void showPopup(MouseEvent e) {
 215  3
                 popup.show(e.getComponent(), e.getX(), e.getY());
 216   
             }
 217   
         });
 218   
     }
 219   
 }
 220