001    /*
002     * Created on Dec 1, 2009
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2009-2010 the original author or authors.
014     */
015    package org.fest.swing.driver;
016    
017    import static java.util.concurrent.TimeUnit.SECONDS;
018    import static org.fest.assertions.Assertions.assertThat;
019    import static org.fest.swing.driver.JProgressBarIndeterminateQuery.isIndeterminate;
020    import static org.fest.swing.driver.JProgressBarMinimumAndMaximumQuery.minimumAndMaximumOf;
021    import static org.fest.swing.driver.JProgressBarStringQuery.stringOf;
022    import static org.fest.swing.driver.JProgressBarValueQuery.valueOf;
023    import static org.fest.swing.driver.JProgressBarWaitUntilIsDeterminate.waitUntilValueIsDeterminate;
024    import static org.fest.swing.driver.JProgressBarWaitUntilValueIsEqualToExpectedTask.waitUntilValueIsEqualToExpected;
025    import static org.fest.swing.driver.TextAssert.verifyThat;
026    import static org.fest.swing.timing.Timeout.timeout;
027    import static org.fest.util.Strings.concat;
028    
029    import java.util.regex.Pattern;
030    
031    import javax.swing.JProgressBar;
032    
033    import org.fest.swing.annotation.RunsInEDT;
034    import org.fest.swing.core.Robot;
035    import org.fest.swing.exception.WaitTimedOutError;
036    import org.fest.swing.timing.Timeout;
037    import org.fest.swing.util.Pair;
038    
039    /**
040     * Understands functional testing of <code>{@link JProgressBar}</code>s:
041     * <ul>
042     * <li>state verification</li>
043     * <li>property value query</li>
044     * </ul>
045     * This class is intended for internal use only. Please use the classes in the package
046     * <code>{@link org.fest.swing.fixture}</code> in your tests.
047     *
048     * @author Alex Ruiz
049     *
050     * @since 1.2
051     */
052    public class JProgressBarDriver extends JComponentDriver implements TextDisplayDriver<JProgressBar> {
053    
054      private static final Timeout DEFAULT_TIMEOUT = timeout(30, SECONDS);
055      private static final String TEXT_PROPERTY = "string";
056    
057      /**
058       * Creates a new </code>{@link JProgressBarDriver}</code>.
059       * @param robot the robot to use to simulate user input.
060       */
061      public JProgressBarDriver(Robot robot) {
062        super(robot);
063      }
064    
065      /**
066       * Asserts that the text of the <code>{@link JProgressBar}</code> is equal to the specified <code>String</code>.
067       * @param progressBar the target <code>JProgressBar</code>.
068       * @param expected the text to match.
069       * @throws AssertionError if the text of the <code>JProgressBar</code> is not equal to the given one.
070       * @see JProgressBar#getString()
071       */
072      @RunsInEDT
073      public void requireText(JProgressBar progressBar, String expected) {
074        verifyThat(stringOf(progressBar)).as(propertyName(progressBar, TEXT_PROPERTY)).isEqualOrMatches(expected);
075      }
076    
077      /**
078       * Asserts that the text of the <code>{@link JProgressBar}</code> matches the given regular expression pattern.
079       * @param progressBar the target <code>JProgressBar</code>.
080       * @param pattern the regular expression pattern to match.
081       * @throws AssertionError if the text of the <code>JProgressBar</code> does not match the given regular expression pattern.
082       * @throws NullPointerException if the given regular expression pattern is <code>null</code>.
083       * @see JProgressBar#getString()
084       */
085      @RunsInEDT
086      public void requireText(JProgressBar progressBar, Pattern pattern) {
087        verifyThat(stringOf(progressBar)).as(propertyName(progressBar, TEXT_PROPERTY)).matches(pattern);
088      }
089    
090      /**
091       * Verifies that the value of the given <code>{@link JProgressBar}</code> is equal to the given one.
092       * @param progressBar the target <code>JProgressBar</code>.
093       * @param value the expected value.
094       * @throws AssertionError if the value of the <code>JProgressBar</code> is not equal to the given one.
095       */
096      @RunsInEDT
097      public void requireValue(JProgressBar progressBar, int value) {
098        assertThat(valueOf(progressBar)).as(propertyName(progressBar, "value")).isEqualTo(value);
099      }
100    
101      /**
102       * Verifies that the given <code>{@link JProgressBar}</code> is in indeterminate mode.
103       * @param progressBar the target <code>JProgressBar</code>.
104       * @throws AssertionError if the given <code>JProgressBar</code> is not in indeterminate mode.
105       */
106      @RunsInEDT
107      public void requireIndeterminate(JProgressBar progressBar) {
108        requireIndeterminate(progressBar, true);
109      }
110    
111      /**
112       * Verifies that the given <code>{@link JProgressBar}</code> is in determinate mode.
113       * @param progressBar the target <code>JProgressBar</code>.
114       * @throws AssertionError if the given <code>JProgressBar</code> is not in determinate mode.
115       */
116      @RunsInEDT
117      public void requireDeterminate(JProgressBar progressBar) {
118        requireIndeterminate(progressBar, false);
119      }
120    
121      @RunsInEDT
122      private void requireIndeterminate(JProgressBar progressBar, boolean indeterminate) {
123        assertThat(isIndeterminate(progressBar)).as(propertyName(progressBar, "indeterminate")).isEqualTo(indeterminate);
124      }
125    
126      /**
127       * Waits until the value of the given <code>{@link JProgressBar}</code> is equal to the given value.
128       * @param progressBar the target <code>JProgressBar</code>.
129       * @param value the expected value.
130       * @throws IllegalArgumentException if the given value is less than the <code>JProgressBar</code>'s minimum value.
131       * @throws IllegalArgumentException if the given value is greater than the <code>JProgressBar</code>'s maximum value.
132       * @throws WaitTimedOutError if the value of the <code>JProgressBar</code> does not reach the expected value within
133       * 30 seconds.
134       */
135      @RunsInEDT
136      public void waitUntilValueIs(JProgressBar progressBar, int value) {
137        waitUntilValueIs(progressBar, value, DEFAULT_TIMEOUT);
138      }
139    
140      /**
141       * Waits until the value of the given <code>{@link JProgressBar}</code> is equal to the given value.
142       * @param progressBar the target <code>JProgressBar</code>.
143       * @param value the expected value.
144       * @param timeout the amount of time to wait.
145       * @throws IllegalArgumentException if the given value is less than the <code>JProgressBar</code>'s minimum value.
146       * @throws IllegalArgumentException if the given value is greater than the <code>JProgressBar</code>'s maximum value.
147       * @throws NullPointerException if the given timeout is <code>null</code>.
148       * @throws WaitTimedOutError if the value of the <code>JProgressBar</code> does not reach the expected value within
149       * the specified timeout.
150       */
151      @RunsInEDT
152      public void waitUntilValueIs(JProgressBar progressBar, int value, Timeout timeout) {
153        assertIsInBetweenMinAndMax(progressBar, value);
154        validateIsNotNull(timeout);
155        waitUntilValueIsEqualToExpected(progressBar, value, timeout);
156      }
157    
158      @RunsInEDT
159      private void assertIsInBetweenMinAndMax(JProgressBar progressBar, int value) {
160        Pair<Integer, Integer> minAndMax = minimumAndMaximumOf(progressBar);
161        assertIsInBetweenMinAndMax(value, minAndMax.i, minAndMax.ii);
162      }
163    
164      private void assertIsInBetweenMinAndMax(int value, int min, int max) {
165        if (value >= min && value <= max) return;
166        throw new IllegalArgumentException(concat("Value <", value, "> should be between <[", min, ", ", max, "]>"));
167      }
168    
169      /**
170       * Waits until the value of the given <code>{@link JProgressBar}</code> is in determinate mode.
171       * @param progressBar the target <code>JProgressBar</code>.
172       * @throws WaitTimedOutError if the <code>JProgressBar</code> does not reach determinate mode within 30 seconds.
173       */
174      @RunsInEDT
175      public void waitUntilIsDeterminate(JProgressBar progressBar) {
176        waitUntilIsDeterminate(progressBar, DEFAULT_TIMEOUT);
177      }
178    
179      /**
180       * Waits until the value of the given <code>{@link JProgressBar}</code> is in determinate mode.
181       * @param progressBar the target <code>JProgressBar</code>.
182       * @param timeout the amount of time to wait.
183       * @throws NullPointerException if the given timeout is <code>null</code>.
184       * @throws WaitTimedOutError if the <code>JProgressBar</code> does not reach determinate mode within the specified
185       * timeout.
186       */
187      @RunsInEDT
188      public void waitUntilIsDeterminate(JProgressBar progressBar, Timeout timeout) {
189        validateIsNotNull(timeout);
190        waitUntilValueIsDeterminate(progressBar, timeout);
191      }
192    
193      private void validateIsNotNull(Timeout timeout) {
194        if (timeout == null) throw new NullPointerException("Timeout should not be null");
195      }
196    
197      /**
198       * Returns the text of the given <code>{@link JProgressBar}</code>.
199       * @param progressBar the target <code>JProgressBar</code>.
200       * @return the text of the given <code>{@link JProgressBar}</code>.
201       */
202      @RunsInEDT
203      public String textOf(JProgressBar progressBar) {
204        return stringOf(progressBar);
205      }
206    }