Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 417   Methods: 23
NCLOC: 169   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MouseEventData.java 100% 95.5% 91.3% 95.1%
coverage coverage
 1   
 package junit.extensions.jfcunit.eventdata;
 2   
 
 3   
 import junit.extensions.jfcunit.JFCTestCase;
 4   
 import junit.extensions.jfcunit.xml.JFCXMLConstants;
 5   
 
 6   
 import org.w3c.dom.Element;
 7   
 
 8   
 import java.awt.AWTEvent;
 9   
 import java.awt.Component;
 10   
 import java.awt.Point;
 11   
 import java.awt.Rectangle;
 12   
 import java.awt.event.MouseEvent;
 13   
 import javax.swing.JViewport;
 14   
 
 15   
 /**
 16   
  * Data container class that holds all the data necessary for jfcUnit to fire mouse events.
 17   
  *
 18   
  * @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
 19   
  */
 20   
 public class MouseEventData
 21   
     extends AbstractMouseEventData {
 22   
     /**
 23   
      * Default value specifying whether the mouse event being fired
 24   
      * would trigger a popup or not.
 25   
      */
 26   
     public static final boolean DEFAULT_ISPOPUPTRIGGER = false;
 27   
 
 28   
     /**
 29   
      * The Component on which to trigger the event.
 30   
      */
 31   
     private Component m_comp;
 32   
 
 33   
     /**
 34   
      * Default Constructor.
 35   
      */
 36  629
     public MouseEventData() {
 37  629
         super();
 38  629
         setValid(false);
 39   
     }
 40   
 
 41   
     /**
 42   
      * Constructor.
 43   
      *
 44   
      * @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 45   
      * @param comp     The component on which to trigger the event.
 46   
      */
 47  220
     public MouseEventData(final JFCTestCase testCase, final Component comp) {
 48  220
         this(testCase, comp, DEFAULT_NUMBEROFCLICKS);
 49   
     }
 50   
 
 51   
     /**
 52   
      * Constructor.
 53   
      *
 54   
      * @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 55   
      * @param comp     The component on which to trigger the event.
 56   
      * @param numberOfClicks
 57   
      *                 Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 58   
      */
 59  343
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 60   
                           final int numberOfClicks) {
 61  343
         this(testCase, comp, numberOfClicks, DEFAULT_MOUSE_MODIFIERS);
 62   
     }
 63   
 
 64   
     /**
 65   
      * Constructor.
 66   
      *
 67   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 68   
      * @param comp      The component on which to trigger the event.
 69   
      * @param sleepTime
 70   
      *                  The wait time in ms between each event.
 71   
      */
 72  2
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 73   
                           final long sleepTime) {
 74  2
         this(testCase, comp, DEFAULT_NUMBEROFCLICKS, sleepTime);
 75   
     }
 76   
 
 77   
     /**
 78   
      * Constructor.
 79   
      *
 80   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 81   
      * @param comp      The component on which to trigger the event.
 82   
      * @param numberOfClicks
 83   
      *                  Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 84   
      * @param modifiers The modifier key values that need to be passed onto the event.
 85   
      */
 86  409
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 87   
                           final int numberOfClicks, final int modifiers) {
 88  409
         this(testCase, comp, numberOfClicks, modifiers, DEFAULT_ISPOPUPTRIGGER);
 89   
     }
 90   
 
 91   
     /**
 92   
      * Constructor.
 93   
      *
 94   
      * @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 95   
      * @param comp     The component on which to trigger the event.
 96   
      * @param numberOfClicks
 97   
      *                 Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 98   
      * @param isPopupTrigger
 99   
      *                 boolean specifying whether this event will show a popup.
 100   
      */
 101  4
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 102   
                           final int numberOfClicks,
 103   
                           final boolean isPopupTrigger) {
 104  4
         this(testCase, comp, numberOfClicks,
 105   
              getDefaultModifiers(isPopupTrigger), isPopupTrigger);
 106   
     }
 107   
 
 108   
     /**
 109   
      * Constructor.
 110   
      *
 111   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 112   
      * @param comp      The component on which to trigger the event.
 113   
      * @param numberOfClicks
 114   
      *                  Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 115   
      * @param sleepTime
 116   
      *                  The wait time in ms between each event.
 117   
      */
 118  4
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 119   
                           final int numberOfClicks, final long sleepTime) {
 120  4
         this(testCase, comp, numberOfClicks, DEFAULT_MOUSE_MODIFIERS,
 121   
              DEFAULT_ISPOPUPTRIGGER, sleepTime);
 122   
     }
 123   
 
 124   
     /**
 125   
      * Constructor.
 126   
      *
 127   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 128   
      * @param comp      The component on which to trigger the event.
 129   
      * @param numberOfClicks
 130   
      *                  Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 131   
      * @param modifiers The modifier key values that need to be passed onto the event.
 132   
      * @param isPopupTrigger
 133   
      *                  boolean specifying whether this event will show a popup.
 134   
      */
 135  415
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 136   
                           final int numberOfClicks, final int modifiers,
 137   
                           final boolean isPopupTrigger) {
 138  415
         this(testCase, comp, numberOfClicks, modifiers, isPopupTrigger,
 139   
              DEFAULT_SLEEPTIME);
 140   
     }
 141   
 
 142   
     /**
 143   
      * Constructor.
 144   
      *
 145   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 146   
      * @param comp      The component on which to trigger the event.
 147   
      * @param numberOfClicks
 148   
      *                  Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 149   
      * @param isPopupTrigger
 150   
      *                  boolean specifying whether this event will show a popup.
 151   
      * @param sleepTime
 152   
      *                  The wait time in ms between each event.
 153   
      */
 154  2
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 155   
                           final int numberOfClicks,
 156   
                           final boolean isPopupTrigger,
 157   
                           final long sleepTime) {
 158  2
         this(testCase, comp, numberOfClicks,
 159   
              getDefaultModifiers(isPopupTrigger), isPopupTrigger, sleepTime);
 160   
     }
 161   
 
 162   
     /**
 163   
      * Constructor.
 164   
      *
 165   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 166   
      * @param comp      The component on which to trigger the event.
 167   
      * @param numberOfClicks
 168   
      *                   Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 169   
      * @param modifiers The modifier key values that need to be passed onto the event.
 170   
      * @param isPopupTrigger
 171   
      *                   boolean specifying whether this event will show a popup.
 172   
      * @param sleepTime
 173   
      *                   The wait time in ms between each event.
 174   
      */
 175  423
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 176   
                           final int numberOfClicks, final int modifiers,
 177   
                           final boolean isPopupTrigger, final long sleepTime) {
 178  423
         this(testCase, comp, numberOfClicks, modifiers, isPopupTrigger,
 179   
              sleepTime, DEFAULT_POSITION, null);
 180   
     }
 181   
 
 182   
     /**
 183   
      * Constructor.
 184   
      *
 185   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 186   
      * @param comp      The component on which to trigger the event.
 187   
      * @param numberOfClicks
 188   
      *                   Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 189   
      * @param modifiers The modifier key values that need to be passed onto the event.
 190   
      * @param isPopupTrigger
 191   
      *                   boolean specifying whether this event will show a popup.
 192   
      * @param sleepTime
 193   
      *                   The wait time in ms between each event.
 194   
      * @param position  The relative mouse position within the cell.
 195   
      */
 196  34
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 197   
                           final int numberOfClicks, final int modifiers,
 198   
                           final boolean isPopupTrigger, final long sleepTime,
 199   
                           final int position) {
 200  34
         this(testCase, comp, numberOfClicks, modifiers, isPopupTrigger,
 201   
              sleepTime, position, null);
 202   
     }
 203   
 
 204   
     /**
 205   
      * Constructor.
 206   
      *
 207   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 208   
      * @param comp      The component on which to trigger the event.
 209   
      * @param numberOfClicks
 210   
      *                   Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 211   
      * @param modifiers The modifier key values that need to be passed onto the event.
 212   
      * @param isPopupTrigger
 213   
      *                   boolean specifying whether this event will show a popup.
 214   
      * @param sleepTime
 215   
      *                   The wait time in ms between each event.
 216   
      * @param referencePoint     The CUSTOM mouse position within the cell.
 217   
      */
 218  2
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 219   
                           final int numberOfClicks, final int modifiers,
 220   
                           final boolean isPopupTrigger, final long sleepTime,
 221   
                           final Point referencePoint) {
 222  2
         this(testCase, comp, numberOfClicks, modifiers, isPopupTrigger,
 223   
              sleepTime, CUSTOM, referencePoint);
 224   
     }
 225   
 
 226   
     /**
 227   
      * Constructor.
 228   
      *
 229   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 230   
      * @param comp      The component on which to trigger the event.
 231   
      * @param numberOfClicks
 232   
      *                   Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 233   
      * @param modifiers The modifier key values that need to be passed onto the event.
 234   
      * @param isPopupTrigger
 235   
      *                   boolean specifying whether this event will show a popup.
 236   
      * @param sleepTime
 237   
      *                   The wait time in ms between each event.
 238   
      * @param position  The relative mouse position within the cell.
 239   
      * @param referencePoint
 240   
      *                                   If _position is CUSTOM then the point is a offset from
 241   
      *                                   the location of the component. If the _position is PERCENT
 242   
      *                                   then the location is a percentage offset of the hight and width.
 243   
      *                                   Otherwise, the _referencePoint is unused.
 244   
      */
 245  633
     public MouseEventData(final JFCTestCase testCase, final Component comp,
 246   
                           final int numberOfClicks, final int modifiers,
 247   
                           final boolean isPopupTrigger, final long sleepTime,
 248   
                           final int position,
 249   
                           final Point referencePoint) {
 250  633
         setTestCase(testCase);
 251  633
         setSource(comp);
 252  633
         setNumberOfClicks(numberOfClicks);
 253  633
         setModifiers(modifiers);
 254  633
         setPopupTrigger(isPopupTrigger);
 255  633
         setSleepTime(sleepTime);
 256  633
         setPosition(position);
 257  633
         setReferencePoint(referencePoint);
 258  633
         setValid(true);
 259   
     }
 260   
 
 261   
     /**
 262   
      * The component on which the event has to be fired.
 263   
      *
 264   
      * @return The component.
 265   
      */
 266  822
     public Component getComponent() {
 267   
         // by default, the component is the same as the source
 268  822
         return getSource();
 269   
     }
 270   
 
 271   
     /**
 272   
      * Set the attribute value.
 273   
      *
 274   
      * @param comp   The new value of the attribute.
 275   
      */
 276  785
     public void setSource(final Component comp) {
 277  785
         m_comp = comp;
 278   
     }
 279   
 
 280   
     /**
 281   
      * Get the attribute value.
 282   
      *
 283   
      * @return Component    The value of the attribute.
 284   
      */
 285  967
     public Component getSource() {
 286  967
         return m_comp;
 287   
     }
 288   
 
 289   
     /**
 290   
      * Returns true if the event can be consumed by
 291   
      * this instnace of event data.
 292   
      *
 293   
      * @param ae Event to be consumed.
 294   
      * @return true if the event was consumed.
 295   
      */
 296  490
     public boolean canConsume(final AWTEvent ae) {
 297  490
         if (isValid()) {
 298  121
             if (ae.getSource() != getSource()) {
 299  1
                 return false;
 300   
             }
 301   
         }
 302   
 
 303  489
         return super.canConsume(ae);
 304   
     }
 305   
 
 306   
     /**
 307   
      * Consume the event.
 308   
      * @param ae AWTEvent to be consumed.
 309   
      * @return boolean true if the event was consumed.
 310   
      */
 311  256
     public boolean consume(final AWTEvent ae) {
 312  256
         if (super.consume(ae)) {
 313  104
             return true;
 314   
         }
 315   
 
 316  152
         MouseEvent me = (MouseEvent) ae;
 317  152
         Component source = (Component) me.getSource();
 318  152
         setSource(source);
 319  152
         setModifiers(me.getModifiers());
 320  152
         setNumberOfClicks(me.getClickCount());
 321  152
         setPopupTrigger(me.isPopupTrigger());
 322  152
         setSleepTime(getDefaultSleepTime());
 323   
 
 324  152
         Point p = new Point(
 325   
             me.getX(),
 326   
             me.getY());
 327   
 
 328  152
         try {
 329  152
             Point screen = source.getLocationOnScreen();
 330  152
             screen.translate(p.x, p.y);
 331  152
             setLocationOnScreen(screen);
 332   
 
 333  152
             setPosition(CUSTOM);
 334  152
             setReferencePoint(p);
 335   
         } catch (java.awt.IllegalComponentStateException exception) {
 336   
             // Could not get the location on the screen. Ignore. The
 337   
             // component should be good enough.
 338   
         }
 339   
 
 340  152
         setValid(true);
 341   
 
 342  152
         return true;
 343   
     }
 344   
 
 345   
     /**
 346   
      * Compare to event data and deterime if they are equal.
 347   
      *
 348   
      * @param o Object to be compared.
 349   
      * @return true if the events are the same.
 350   
      */
 351  28
     public boolean equals(final Object o) {
 352  28
         return super.equals(o);
 353   
     }
 354   
 
 355   
     /**
 356   
      * Get the hashCode.
 357   
      * @return int hashCode.
 358   
      */
 359  0
     public int hashCode() {
 360  0
         return super.hashCode();
 361   
     }
 362   
 
 363   
     /**
 364   
      * Populate the XML Element with this objects data.
 365   
      * @param e XML Element to be populated.
 366   
      */
 367  0
     public void populate(final Element e) {
 368  0
         super.populate(e);
 369  0
         e.setAttribute(JFCXMLConstants.TYPE, "MouseEventData");
 370   
     }
 371   
 
 372   
     /**
 373   
      * Prepare the component for firing the mouse event.
 374   
      *
 375   
      * @return boolean true if the component is ready.
 376   
      */
 377  597
     public boolean prepareComponent() {
 378  597
         if (!isValidForProcessing(m_comp)) {
 379  1
             return false;
 380   
         }
 381   
 
 382   
         // Note: don't follow the scroll issue from the other MouseEventData sub-classes here
 383   
         // that is done only for specialized EventData classes.
 384  596
         Rectangle bounds = m_comp.getBounds();
 385  596
         insureVisibleLocation(calculatePoint(bounds));
 386   
 
 387  596
         bounds.setLocation(m_comp.getLocationOnScreen());
 388  596
         setLocationOnScreen(calculatePoint(bounds));
 389   
 
 390  596
         return true;
 391   
     }
 392   
 
 393   
     /**
 394   
      * Insure the the point p is visible in the object.
 395   
      * If not then attempt to locate a viewport to
 396   
      * make the point visible.
 397   
      * @param p Point to be made visible.
 398   
      */
 399  596
     public void insureVisibleLocation(final Point p) {
 400   
         // Need to get the parent viewport.
 401  596
         Component parent = m_comp.getParent();
 402  596
         while (parent != null
 403   
                && !(parent instanceof JViewport)) {
 404  4160
             parent = parent.getParent();
 405   
         }
 406  596
         if (parent != null && parent instanceof JViewport) {
 407  4
             JViewport port = (JViewport) parent;
 408   
 
 409  4
             Rectangle r = port.getViewRect();
 410  4
             r.x = p.x - (r.width / 2);
 411  4
             r.y = p.y - (r.height / 2);
 412   
 
 413  4
             port.scrollRectToVisible(r);
 414   
         }
 415   
     }
 416   
 }
 417