Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 124   Methods: 11
NCLOC: 96   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JScrollBarTester.java 94.4% 95.6% 100% 95.9%
coverage coverage
 1   
 package abbot.tester;
 2   
 
 3   
 import java.awt.*;
 4   
 import javax.swing.*;
 5   
 
 6   
 import abbot.i18n.*;
 7   
 
 8   
 /** Provides user actions on a JScrollPane. */
 9   
 // TODO: ScrollBarLocation, which provides locations for thumb, arrows, etc
 10   
 // TODO: multi-scroll when arrows pressed and held
 11   
 // TODO: multi-scroll when block (outside thumb) pressed and held
 12   
 public class JScrollBarTester extends JComponentTester {
 13   
 
 14   
     private static final int BLOCK_OFFSET = 4;
 15   
 
 16  4
     protected Point getThumbLocation(JScrollBar bar, int value) {
 17  4
         boolean horizontal = bar.getOrientation() == JScrollBar.HORIZONTAL;
 18  4
         double fraction =
 19   
             (double)value / (bar.getMaximum() - bar.getMinimum());
 20  4
         if (horizontal) {
 21  2
             int arrow = bar.getHeight();
 22  2
             return new Point(arrow
 23   
                              + (int)(fraction * (bar.getWidth() - 2*arrow)),
 24   
                              arrow / 2);
 25   
         }
 26   
         else {
 27  2
             int arrow = bar.getWidth();
 28  2
             return new Point(arrow / 2,
 29   
                              arrow
 30   
                              + (int)(fraction * (bar.getHeight() - 2*arrow)));
 31   
         }
 32   
     }
 33   
 
 34  8
     protected Point getUnitLocation(JScrollBar bar, boolean up) {
 35  8
         boolean horizontal = bar.getOrientation() == JScrollBar.HORIZONTAL;
 36  8
         int arrow = horizontal ? bar.getHeight() : bar.getWidth();
 37  8
         if (up) {
 38  4
             return horizontal 
 39   
                 ? new Point(bar.getWidth() - arrow/2, arrow/2)
 40   
                     : new Point(arrow/2, bar.getHeight() - arrow/2);
 41   
         }
 42  4
         return new Point(arrow/2, arrow/2);
 43   
     }
 44   
 
 45  4
     protected Point getBlockLocation(JScrollBar bar, boolean up) {
 46  4
         boolean horizontal = bar.getOrientation() == JScrollBar.HORIZONTAL;
 47  4
         Point p = getUnitLocation(bar, up);
 48  4
         int offset = up ? BLOCK_OFFSET : -BLOCK_OFFSET;
 49  4
         if (horizontal)
 50  2
             p.x += offset;
 51   
         else
 52  2
             p.y += offset;
 53  4
         return p;
 54   
     }
 55   
 
 56  10
     protected void scroll(final JScrollBar bar, final int value) {
 57  10
         invokeLater(bar, new Runnable() {
 58  10
             public void run() {
 59  10
                 bar.setValue(value);
 60   
             }
 61   
         });
 62   
     }
 63   
 
 64  8
     protected void scroll(JScrollBar bar, int count, boolean block) {
 65   
         // For now, do it programmatically, faking the mouse movement and
 66   
         // clicking 
 67  8
         Point where = block
 68   
             ? getBlockLocation(bar, count >= 0)
 69   
             : getUnitLocation(bar, count >= 0);
 70   
         // Don't really know if this is where the UI puts them
 71   
         // mouseClick(bar, where.x, where.y);
 72  8
         mouseMove(bar, where.x, where.y);
 73  8
         int value = bar.getValue() + count * (block
 74   
                                               ? bar.getBlockIncrement() 
 75   
                                               : bar.getUnitIncrement());
 76  8
         scroll(bar, value);
 77   
     }
 78   
 
 79   
     /** Scroll up (or right) one unit (usually a line). */
 80  2
     public void actionScrollUnitUp(Component c) {
 81  2
         scroll((JScrollBar)c, 1, false);
 82  2
         waitForIdle();
 83   
     }
 84   
     /** Scroll down (or left) one unit (usually a line). */
 85  2
     public void actionScrollUnitDown(Component c) {
 86  2
         scroll((JScrollBar)c, -1, false);
 87  2
         waitForIdle();
 88   
     }
 89   
     /** Scroll up (or right) one block (usually a page). */
 90  2
     public void actionScrollBlockUp(Component c) {
 91  2
         scroll((JScrollBar)c, 1, true);
 92  2
         waitForIdle();
 93   
     }
 94   
     /** Scroll down (or left) one block (usually a page). */
 95  2
     public void actionScrollBlockDown(Component c) {
 96  2
         scroll((JScrollBar)c, -1, true);
 97  2
         waitForIdle();
 98   
     }
 99   
 
 100   
     /** Scroll to the given scroll position. */
 101  2
     public void actionScrollTo(Component c, final int position) {
 102  2
         JScrollBar bar = (JScrollBar)c;
 103  2
         int min = bar.getMinimum();
 104  2
         int max = bar.getMaximum();
 105  2
         if (position < min || position > max) {
 106  0
             String msg = Strings.get("tester.JScrollBar.out_of_range",
 107   
                                      new Object[] {
 108   
                                          new Integer(position),
 109   
                                          new Integer(min), new Integer(max),
 110   
                                      });
 111  0
             throw new ActionFailedException(msg);
 112   
         }
 113   
 
 114  2
         Point thumb = getThumbLocation(bar, bar.getValue());
 115  2
         mouseMove(bar, thumb.x, thumb.y);
 116   
 
 117  2
         thumb = getThumbLocation(bar, position);
 118  2
         mouseMove(bar, thumb.x, thumb.y);
 119   
 
 120  2
         scroll(bar, position);
 121  2
         waitForIdle();
 122   
     }
 123   
 }
 124