Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 545   Methods: 27
NCLOC: 231   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JSpinnerMouseEventData.java 43.8% 78.7% 85.2% 75.9%
coverage coverage
 1   
 package junit.extensions.jfcunit.eventdata;
 2   
 
 3   
 import junit.extensions.jfcunit.JFCTestCase;
 4   
 import junit.extensions.jfcunit.finder.ComponentFinder;
 5   
 import junit.extensions.jfcunit.finder.Finder;
 6   
 import junit.extensions.jfcunit.xml.JFCXMLConstants;
 7   
 
 8   
 import org.w3c.dom.Element;
 9   
 
 10   
 import java.awt.AWTEvent;
 11   
 import java.awt.Component;
 12   
 import java.awt.Container;
 13   
 import java.awt.Point;
 14   
 import java.awt.Rectangle;
 15   
 
 16   
 import javax.swing.JButton;
 17   
 import javax.swing.text.JTextComponent;
 18   
 
 19   
 
 20   
 /**
 21   
  * Data container class that holds all the data necessary for jfcUnit to fire mouse events.
 22   
  *
 23   
  * @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
 24   
  */
 25   
 public class JSpinnerMouseEventData extends AbstractMouseEventData {
 26   
     /** Class for spinner component. */
 27   
     private static Class s_spinnerClass = null;
 28   
 
 29   
     /**
 30   
      * Use reflection to keep 1.2 and 1.3 jdk compiling.
 31   
      */
 32   
     static {
 33  4
         try {
 34  4
             s_spinnerClass = Class.forName("javax.swing.JSpinner");
 35   
         } catch (Exception e) {
 36  0
             s_spinnerClass = null;
 37   
         }
 38   
     }
 39   
 
 40   
     /**
 41   
      * The Component on which to trigger the event.
 42   
      */
 43   
     private Component m_comp;
 44   
 
 45   
     /**
 46   
      * The Source on which to trigger the event.
 47   
      */
 48   
     private Component m_source;
 49   
 
 50   
     /**
 51   
      * Offset into the text component.
 52   
      */
 53   
     private int m_offset = INVALID_TEXT_OFFSET;
 54   
 
 55   
     /**
 56   
      * The subcomponent type to send click.
 57   
      */
 58   
     private int m_subComponent = INVALID_SUBCOMPONENT;
 59   
 
 60   
     /**
 61   
      * Default Constructor.
 62   
      */
 63  1
     public JSpinnerMouseEventData() {
 64  1
         super();
 65  1
         setValid(false);
 66   
     }
 67   
 
 68   
     /**
 69   
      * Constructor - Defualt click cell.
 70   
      *
 71   
      * @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 72   
      * @param comp     The component on which to trigger the event.
 73   
      * @param subComponent Sub-component of the JSpinner.
 74   
      */
 75  62
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 76   
         final Component comp, final int subComponent) {
 77  62
         this(testCase, comp, subComponent, DEFAULT_NUMBEROFCLICKS);
 78   
     }
 79   
 
 80   
     /**
 81   
      * Constructor.
 82   
      *
 83   
      * @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 84   
      * @param comp     The component on which to trigger the event.
 85   
      * @param subComponent Sub-component of the JSpinner.
 86   
      * @param numberOfClicks
 87   
      *                 Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 88   
      */
 89  64
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 90   
         final Component comp, final int subComponent, final int numberOfClicks) {
 91  64
         this(testCase, comp, subComponent, numberOfClicks,
 92   
             DEFAULT_MOUSE_MODIFIERS);
 93   
     }
 94   
 
 95   
     /**
 96   
      * Constructor.
 97   
      *
 98   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 99   
      * @param comp      The component on which to trigger the event.
 100   
      * @param subComponent Sub-component of the JSpinner.
 101   
      * @param sleepTime
 102   
      *                  The wait time in ms between each event.
 103   
      */
 104  2
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 105   
         final Component comp, final int subComponent, final long sleepTime) {
 106  2
         this(testCase, comp, subComponent, DEFAULT_NUMBEROFCLICKS, sleepTime);
 107   
     }
 108   
 
 109   
     /**
 110   
      * Constructor.
 111   
      *
 112   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 113   
      * @param comp      The component on which to trigger the event.
 114   
      * @param subComponent Sub-component of the JSpinner.
 115   
      * @param numberOfClicks
 116   
      *                  Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 117   
      * @param modifiers The modifier key values that need to be passed onto the event.
 118   
      */
 119  66
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 120   
         final Component comp, final int subComponent, final int numberOfClicks,
 121   
         final int modifiers) {
 122  66
         this(testCase, comp, subComponent, numberOfClicks, modifiers,
 123   
             DEFAULT_ISPOPUPTRIGGER);
 124   
     }
 125   
 
 126   
     /**
 127   
      * Constructor.
 128   
      *
 129   
      * @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 130   
      * @param comp     The component on which to trigger the event.
 131   
      * @param subComponent Sub-component of the JSpinner.
 132   
      * @param numberOfClicks
 133   
      *                 Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 134   
      * @param isPopupTrigger
 135   
      *                 boolean specifying whether this event will show a popup.
 136   
      */
 137  2
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 138   
         final Component comp, final int subComponent, final int numberOfClicks,
 139   
         final boolean isPopupTrigger) {
 140  2
         this(testCase, comp, subComponent, numberOfClicks,
 141   
             getDefaultModifiers(isPopupTrigger), isPopupTrigger);
 142   
     }
 143   
 
 144   
     /**
 145   
      * Constructor.
 146   
      *
 147   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 148   
      * @param comp      The component on which to trigger the event.
 149   
      * @param subComponent Sub-component of the JSpinner.
 150   
      * @param numberOfClicks
 151   
      *                  Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 152   
      * @param sleepTime
 153   
      *                  The wait time in ms between each event.
 154   
      */
 155  4
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 156   
         final Component comp, final int subComponent, final int numberOfClicks,
 157   
         final long sleepTime) {
 158  4
         this(testCase, comp, subComponent, numberOfClicks,
 159   
             DEFAULT_MOUSE_MODIFIERS, DEFAULT_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 subComponent Sub-component of the JSpinner.
 168   
      * @param numberOfClicks
 169   
      *                  Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 170   
      * @param modifiers The modifier key values that need to be passed onto the event.
 171   
      * @param isPopupTrigger
 172   
      *                  boolean specifying whether this event will show a popup.
 173   
      */
 174  70
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 175   
         final Component comp, final int subComponent, final int numberOfClicks,
 176   
         final int modifiers, final boolean isPopupTrigger) {
 177  70
         this(testCase, comp, subComponent, numberOfClicks, modifiers,
 178   
             isPopupTrigger, DEFAULT_SLEEPTIME);
 179   
     }
 180   
 
 181   
     /**
 182   
      * Constructor.
 183   
      *
 184   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 185   
      * @param comp      The component on which to trigger the event.
 186   
      * @param subComponent Sub-component of the JSpinner.
 187   
      * @param numberOfClicks
 188   
      *                  Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 189   
      * @param isPopupTrigger
 190   
      *                  boolean specifying whether this event will show a popup.
 191   
      * @param sleepTime
 192   
      *                  The wait time in ms between each event.
 193   
      */
 194  2
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 195   
         final Component comp, final int subComponent, final int numberOfClicks,
 196   
         final boolean isPopupTrigger, final long sleepTime) {
 197  2
         this(testCase, comp, subComponent, numberOfClicks,
 198   
             getDefaultModifiers(isPopupTrigger), isPopupTrigger, sleepTime);
 199   
     }
 200   
 
 201   
     /**
 202   
      * Constructor.
 203   
      *
 204   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 205   
      * @param comp      The component on which to trigger the event.
 206   
      * @param subComponent Sub-component of the JSpinner.
 207   
      * @param numberOfClicks
 208   
      *                   Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 209   
      * @param modifiers The modifier key values that need to be passed onto the event.
 210   
      * @param isPopupTrigger
 211   
      *                   boolean specifying whether this event will show a popup.
 212   
      * @param sleepTime
 213   
      *                   The wait time in ms between each event.
 214   
      */
 215  78
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 216   
         final Component comp, final int subComponent, final int numberOfClicks,
 217   
         final int modifiers, final boolean isPopupTrigger, final long sleepTime) {
 218  78
         this(testCase, comp, subComponent, numberOfClicks, modifiers,
 219   
             isPopupTrigger, sleepTime, DEFAULT_POSITION, null,
 220   
             INVALID_TEXT_OFFSET);
 221   
     }
 222   
 
 223   
     /**
 224   
      * Constructor.
 225   
      *
 226   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 227   
      * @param comp      The component on which to trigger the event.
 228   
      * @param subComponent Sub-component of the JSpinner.
 229   
      * @param numberOfClicks
 230   
      *                   Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 231   
      * @param modifiers The modifier key values that need to be passed onto the event.
 232   
      * @param isPopupTrigger
 233   
      *                   boolean specifying whether this event will show a popup.
 234   
      * @param sleepTime
 235   
      *                   The wait time in ms between each event.
 236   
      * @param position  The relative mouse position within the cell.
 237   
      */
 238  2
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 239   
         final Component comp, final int subComponent, final int numberOfClicks,
 240   
         final int modifiers, final boolean isPopupTrigger,
 241   
         final long sleepTime, final int position) {
 242  2
         this(testCase, comp, subComponent, numberOfClicks, modifiers,
 243   
             isPopupTrigger, sleepTime, position, null, INVALID_TEXT_OFFSET);
 244   
     }
 245   
 
 246   
     /**
 247   
      * Constructor.
 248   
      *
 249   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 250   
      * @param comp      The component on which to trigger the event.
 251   
      * @param subComponent Sub-component of the JSpinner.
 252   
      * @param numberOfClicks
 253   
      *                   Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 254   
      * @param modifiers The modifier key values that need to be passed onto the event.
 255   
      * @param isPopupTrigger
 256   
      *                   boolean specifying whether this event will show a popup.
 257   
      * @param sleepTime
 258   
      *                   The wait time in ms between each event.
 259   
      * @param referencePoint     The CUSTOM mouse position within the cell.
 260   
      */
 261  2
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 262   
         final Component comp, final int subComponent, final int numberOfClicks,
 263   
         final int modifiers, final boolean isPopupTrigger,
 264   
         final long sleepTime, final Point referencePoint) {
 265  2
         this(testCase, comp, subComponent, numberOfClicks, modifiers,
 266   
             isPopupTrigger, sleepTime, CUSTOM, referencePoint,
 267   
             INVALID_TEXT_OFFSET);
 268   
     }
 269   
 
 270   
     /**
 271   
      * Constructor.
 272   
      *
 273   
      * @param testCase  The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
 274   
      * @param comp      The component on which to trigger the event.
 275   
      * @param subComponent Sub-component of the JSpinner.
 276   
      * @param numberOfClicks
 277   
      *                   Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
 278   
      * @param modifiers The modifier key values that need to be passed onto the event.
 279   
      * @param isPopupTrigger
 280   
      *                   boolean specifying whether this event will show a popup.
 281   
      * @param sleepTime
 282   
      *                   The wait time in ms between each event.
 283   
      * @param position  The relative mouse position within the cell.
 284   
      * @param referencePoint
 285   
      *                                   If _position is CUSTOM then the point is a offset from
 286   
      *                                   the location of the component. If the _position is PERCENT
 287   
      *                                   then the location is a percentage offset of the hight and width.
 288   
          *                                   Otherwise, the _referencePoint is unused.
 289   
      * @param offset    The offset into the text.
 290   
      */
 291  101
     public JSpinnerMouseEventData(final JFCTestCase testCase,
 292   
         final Component comp, final int subComponent, final int numberOfClicks,
 293   
         final int modifiers, final boolean isPopupTrigger,
 294   
         final long sleepTime, final int position, final Point referencePoint,
 295   
         final int offset) {
 296  101
         setTestCase(testCase);
 297  101
         setSource(comp);
 298  101
         setSubComponent(subComponent);
 299  101
         setNumberOfClicks(numberOfClicks);
 300  101
         setModifiers(modifiers);
 301  101
         setPopupTrigger(isPopupTrigger);
 302  101
         setSleepTime(sleepTime);
 303  101
         setPosition(position);
 304  101
         setReferencePoint(referencePoint);
 305  101
         setOffset(offset);
 306  101
         setValid(true);
 307   
     }
 308   
 
 309   
     /**
 310   
      * Set the attribute value.
 311   
      *
 312   
      * @param comp   The new value of the attribute.
 313   
      */
 314  101
     public final void setSource(final Component comp) {
 315  101
         m_source = comp;
 316   
     }
 317   
 
 318   
     /**
 319   
      * Get the attribute value.
 320   
      *
 321   
      * @return Component    The value of the attribute
 322   
      * NOTE: This is left as a Component so that the
 323   
      *       class may still compile under jdk1.3
 324   
      *       and below where JSpinner is not implemented.
 325   
      */
 326  165
     public final Component getSource() {
 327  165
         return m_source;
 328   
     }
 329   
 
 330   
     /**
 331   
      * Set the sub-component.
 332   
      * @param subComponent SubComponent
 333   
      *                      UP_ARROW_COMPONENT,
 334   
      *                      DOWN_ARROW_COMPONENT,
 335   
      *                      EDITOR_COMPONENT
 336   
      */
 337  101
     public final void setSubComponent(final int subComponent) {
 338  101
         m_subComponent = subComponent;
 339   
     }
 340   
 
 341   
     /**
 342   
      * Get the sub-component.
 343   
      * @return _subComponent Component constant.
 344   
      */
 345  96
     public final int getSubComponent() {
 346  96
         return m_subComponent;
 347   
     }
 348   
 
 349   
     /**
 350   
      * The component on which the event has to be fired.
 351   
      *
 352   
      * @param comp The component.
 353   
      */
 354  66
     public void setComponent(final Component comp) {
 355   
         // by default, the component is the same as the source
 356  66
         m_comp = comp;
 357   
     }
 358   
 
 359   
     /**
 360   
      * The component on which the event has to be fired.
 361   
      *
 362   
      * @return The component.
 363   
      */
 364  116
     public Component getComponent() {
 365   
         // by default, the component is the same as the source
 366  116
         return m_comp;
 367   
     }
 368   
 
 369   
     /**
 370   
      * Set the offset into the text.
 371   
      *
 372   
      * @param offset The offset into the text.
 373   
      */
 374  101
     public final void setOffset(final int offset) {
 375  101
         m_offset = offset;
 376   
     }
 377   
 
 378   
     /**
 379   
      * Get the offset into the text.
 380   
      *
 381   
      * @return int offset into the text.
 382   
      */
 383  28
     public final int getOffset() {
 384  28
         return m_offset;
 385   
     }
 386   
 
 387   
     /**
 388   
      * Returns true if the event can be consumed by
 389   
      * this instnace of event data. However, since
 390   
      * the JSpinner class is a composite of Buttons
 391   
      * and JFormattedTextField, no events can be issued,
 392   
      * on the JSpinner. Thus this call always returns false.
 393   
      *
 394   
      * @param ae Event to be consumed.
 395   
      * @return true if the event was consumed.
 396   
      */
 397  0
     public boolean canConsume(final AWTEvent ae) {
 398  0
         return false;
 399   
     }
 400   
 
 401   
     /**
 402   
      * Consume the event. This method always
 403   
      * returns false (see canConsume).
 404   
      *
 405   
      * @param ae AWTEvent to be consumed.
 406   
      * @return boolean true if the event was consumed.
 407   
      */
 408  0
     public boolean consume(final AWTEvent ae) {
 409  0
         return false;
 410   
     }
 411   
 
 412   
     /**
 413   
      * Compare to event data and deterime if they are equal.
 414   
      *
 415   
      * @param o Object to be compared.
 416   
      * @return true if the events are the same.
 417   
      */
 418  12
     public boolean equals(final Object o) {
 419  12
         if (!super.equals(o)) {
 420  8
             return false;
 421   
         }
 422   
 
 423  4
         JSpinnerMouseEventData data = (JSpinnerMouseEventData) o;
 424   
 
 425  4
         return (data.getSource() == getSource())
 426   
             && (data.getSubComponent() == getSubComponent())
 427   
             && (data.getOffset() == getOffset());
 428   
     }
 429   
 
 430   
     /**
 431   
      * Get the hashCode.
 432   
      * @return int hashCode.
 433   
      */
 434  0
     public int hashCode() {
 435  0
         return super.hashCode() + this.m_offset;
 436   
     }
 437   
 
 438   
     /**
 439   
      * Populate the XML Element.
 440   
      * @param e element to be populated.
 441   
      */
 442  0
     public void populate(final Element e) {
 443  0
         super.populate(e);
 444  0
         e.setAttribute(JFCXMLConstants.TYPE, "JSpinnerMouseEventData");
 445   
 
 446  0
         if (getOffset() != INVALID_TEXT_OFFSET) {
 447  0
             e.setAttribute(JFCXMLConstants.SUBCOMPONENT, JFCXMLConstants.EDITOR);
 448  0
             e.setAttribute(JFCXMLConstants.INDEX, "" + getOffset());
 449  0
         } else if (getSubComponent() == UP_ARROW_SUBCOMPONENT) {
 450  0
             e.setAttribute(JFCXMLConstants.SUBCOMPONENT, JFCXMLConstants.UP);
 451  0
         } else if (getSubComponent() == DOWN_ARROW_SUBCOMPONENT) {
 452  0
             e.setAttribute(JFCXMLConstants.SUBCOMPONENT, JFCXMLConstants.DOWN);
 453   
         }
 454   
     }
 455   
 
 456   
     /**
 457   
      * Prepare the component for firing the mouse event.
 458   
      *
 459   
      * @return boolean true if the component is ready.
 460   
      */
 461  67
     public boolean prepareComponent() {
 462  67
         if (!isValidForProcessing(getSource()) || (s_spinnerClass == null)) {
 463  1
             return false;
 464   
         }
 465   
 
 466  66
         JFCTestCase testCase = getTestCase();
 467   
 
 468  66
         if (testCase != null) {
 469  66
             testCase.pauseAWT();
 470   
         }
 471   
 
 472  66
         Component c      = null;
 473  66
         Rectangle bounds = null;
 474  66
         Finder    f      = new ComponentFinder(JButton.class);
 475   
 
 476  66
         switch (getSubComponent()) {
 477  30
         case DOWN_ARROW_SUBCOMPONENT:
 478  30
             c = f.find((Container) getSource(), 1);
 479  30
             bounds = c.getBounds();
 480  30
             bounds.setLocation(c.getLocationOnScreen());
 481  30
             setLocationOnScreen(calculatePoint(bounds));
 482   
 
 483  30
             break;
 484   
 
 485  30
         case UP_ARROW_SUBCOMPONENT:
 486  30
             c = f.find((Container) getSource(), 0);
 487  30
             bounds = c.getBounds();
 488  30
             bounds.setLocation(c.getLocationOnScreen());
 489  30
             setLocationOnScreen(calculatePoint(bounds));
 490   
 
 491  30
             break;
 492   
 
 493  6
         case EDITOR_SUBCOMPONENT:
 494   
 
 495  6
             try {
 496  6
                 c = (Component) s_spinnerClass.getMethod(
 497   
                         "getEditor",
 498   
                         new Class[0]).invoke(
 499   
                         getSource(),
 500   
                         new Object[0]);
 501   
 
 502  6
                 Class defEditor = Class.forName(
 503   
                         "javax.swing.JSpinner$DefaultEditor");
 504   
 
 505   
                 // If we have a DefaultEditor then we know how to get the
 506   
                 // text field component.
 507  6
                 if (defEditor.isInstance(c)) {
 508  6
                     c = (JTextComponent) defEditor.getMethod(
 509   
                             "getTextField",
 510   
                             new Class[0]).invoke(
 511   
                             c,
 512   
                             new Object[0]);
 513   
                 }
 514   
 
 515  6
                 if ((getPosition() == OFFSET) && c instanceof JTextComponent) {
 516  6
                     setPosition(EAST);
 517   
 
 518  6
                     Point p = calculatePoint(
 519   
                             ((JTextComponent) c).modelToView(m_offset));
 520  6
                     Point screen = c.getLocationOnScreen();
 521  6
                     screen.translate(p.x, p.y);
 522  6
                     setLocationOnScreen(screen);
 523   
                 } else {
 524  0
                     Point p      = calculatePoint(c.getBounds());
 525  0
                     Point screen = c.getLocationOnScreen();
 526  0
                     screen.translate(p.x, p.y);
 527  0
                     setLocationOnScreen(screen);
 528   
                 }
 529   
             } catch (Exception e) {
 530  0
                 e.printStackTrace();
 531   
             }
 532   
 
 533  6
             break;
 534   
 
 535  0
         default:
 536  0
             throw new RuntimeException(
 537   
                 "JSpinnerMouseEventData invalid sub component type");
 538   
         }
 539   
 
 540  66
         setComponent(c);
 541   
 
 542  66
         return true;
 543   
     }
 544   
 }
 545