Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
33   161   16   2.36
2   93   0.48   14
14     1.14  
1    
 
  JSliderDriver       Line # 46 33 0% 16 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Jan 27, 2008
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10    * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11    * specific language governing permissions and limitations under the License.
12    *
13    * Copyright @2008-2010 the original author or authors.
14    */
15    package org.fest.swing.driver;
16   
17    import static org.fest.swing.driver.ComponentStateValidator.validateIsEnabledAndShowing;
18    import static org.fest.swing.driver.JSliderSetValueTask.setValue;
19    import static org.fest.swing.edt.GuiActionRunner.execute;
20    import static org.fest.util.Strings.concat;
21   
22    import java.awt.Point;
23   
24    import javax.swing.JSlider;
25   
26    import org.fest.swing.annotation.RunsInCurrentThread;
27    import org.fest.swing.annotation.RunsInEDT;
28    import org.fest.swing.core.Robot;
29    import org.fest.swing.edt.GuiQuery;
30    import org.fest.swing.util.GenericRange;
31    import org.fest.swing.util.Pair;
32   
33    /**
34    * Understands functional testing of <code>{@link JSlider}</code>s:
35    * <ul>
36    * <li>user input simulation</li>
37    * <li>state verification</li>
38    * <li>property value query</li>
39    * </ul>
40    * This class is intended for internal use only. Please use the classes in the package
41    * <code>{@link org.fest.swing.fixture}</code> in your tests.
42    *
43    * @author Alex Ruiz
44    * @author Yvonne Wang
45    */
 
46    public class JSliderDriver extends JComponentDriver {
47   
48    private final JSliderLocation location;
49   
50    /**
51    * Creates a new </code>{@link JSliderDriver}</code>.
52    * @param robot the robot to use to simulate user input.
53    */
 
54  57 toggle public JSliderDriver(Robot robot) {
55  57 super(robot);
56  57 location = new JSliderLocation();
57    }
58   
59    /**
60    * Slides the knob to its maximum.
61    * @param slider the target <code>JSlider</code>.
62    * @throws IllegalStateException if the <code>JSlider</code> is disabled.
63    * @throws IllegalStateException if the <code>JSlider</code> is not showing on the screen.
64    */
 
65  6 toggle @RunsInEDT
66    public void slideToMaximum(JSlider slider) {
67  6 slide(slider, validateAndFindSlideToMaximumInfo(slider, location));
68    }
69   
 
70  6 toggle @RunsInEDT
71    private static Pair<Integer, GenericRange<Point>> validateAndFindSlideToMaximumInfo(final JSlider slider,
72    final JSliderLocation location) {
73  6 return execute(new GuiQuery<Pair<Integer, GenericRange<Point>>>() {
 
74  6 toggle protected Pair<Integer, GenericRange<Point>> executeInEDT() {
75  6 validateIsEnabledAndShowing(slider);
76  2 int value = slider.getMaximum();
77  2 GenericRange<Point> fromAndTo = slideInfo(slider, location, value);
78  2 return new Pair<Integer, GenericRange<Point>>(value, fromAndTo);
79    }
80    });
81    }
82   
83    /**
84    * Slides the knob to its minimum.
85    * @param slider the target <code>JSlider</code>.
86    * @throws IllegalStateException if the <code>JSlider</code> is disabled.
87    * @throws IllegalStateException if the <code>JSlider</code> is not showing on the screen.
88    */
 
89  6 toggle @RunsInEDT
90    public void slideToMinimum(JSlider slider) {
91  6 slide(slider, validateAndFindSlideToMinimumInfo(slider, location));
92    }
93   
 
94  6 toggle @RunsInEDT
95    private static Pair<Integer, GenericRange<Point>> validateAndFindSlideToMinimumInfo(final JSlider slider,
96    final JSliderLocation location) {
97  6 return execute(new GuiQuery<Pair<Integer, GenericRange<Point>>>() {
 
98  6 toggle protected Pair<Integer, GenericRange<Point>> executeInEDT() {
99  6 validateIsEnabledAndShowing(slider);
100  2 int value = slider.getMinimum();
101  2 GenericRange<Point> fromAndTo = slideInfo(slider, location, value);
102  2 return new Pair<Integer, GenericRange<Point>>(value, fromAndTo);
103    }
104    });
105    }
106   
 
107  4 toggle @RunsInEDT
108    private void slide(JSlider slider, Pair<Integer, GenericRange<Point>> slideInfo) {
109  4 slide(slider, slideInfo.i, slideInfo.ii);
110    }
111   
112    /**
113    * Slides the knob to the requested value.
114    * @param slider the target <code>JSlider</code>.
115    * @param value the requested value.
116    * @throws IllegalStateException if the <code>JSlider</code> is disabled.
117    * @throws IllegalStateException if the <code>JSlider</code> is not showing on the screen.
118    * @throws IllegalArgumentException if the given position is not within the <code>JSlider</code> bounds.
119    */
 
120  16 toggle @RunsInEDT
121    public void slide(JSlider slider, int value) {
122  16 GenericRange<Point> slideInfo = validateAndFindSlideInfo(slider, location, value);
123  8 slide(slider, value, slideInfo);
124    }
125   
 
126  12 toggle @RunsInEDT
127    private void slide(JSlider slider, int value, GenericRange<Point> fromAndTo) {
128  12 moveMouseIgnoringAnyError(slider, fromAndTo.from);
129  12 moveMouseIgnoringAnyError(slider, fromAndTo.to);
130  12 setValue(slider, value);
131  12 robot.waitForIdle();
132    }
133   
 
134  16 toggle @RunsInEDT
135    private static GenericRange<Point> validateAndFindSlideInfo(final JSlider slider, final JSliderLocation location,
136    final int value) {
137  16 return execute(new GuiQuery<GenericRange<Point>>() {
 
138  16 toggle protected GenericRange<Point> executeInEDT() {
139  16 validateValue(slider, value);
140  12 validateIsEnabledAndShowing(slider);
141  8 return slideInfo(slider, location, value);
142    }
143    });
144    }
145   
 
146  16 toggle @RunsInCurrentThread
147    private static void validateValue(JSlider slider, int value) {
148  16 int min = slider.getMinimum();
149  16 int max = slider.getMaximum();
150  12 if (value >= min && value <= max) return;
151  4 throw new IllegalArgumentException(
152    concat("Value <", value, "> is not within the JSlider bounds of <", min, "> and <", max, ">"));
153    }
154   
 
155  12 toggle @RunsInCurrentThread
156    private static GenericRange<Point> slideInfo(JSlider slider, JSliderLocation location, int value) {
157  12 Point from = location.pointAt(slider, slider.getValue());
158  12 Point to = location.pointAt(slider, value);
159  12 return new GenericRange<Point>(from, to);
160    }
161    }