001: package org.uispec4j;
002:
003: import junit.framework.Assert;
004: import junit.framework.AssertionFailedError;
005: import org.uispec4j.assertion.Assertion;
006:
007: import javax.swing.*;
008: import java.awt.*;
009:
010: /**
011: * Wrapper for JProgressBar components.
012: */
013: public class ProgressBar extends AbstractUIComponent {
014: public static final String TYPE_NAME = "progressBar";
015: public static final Class[] SWING_CLASSES = new Class[] { JProgressBar.class };
016: public static final int DEFAULT_PRECISION = 2;
017:
018: private JProgressBar jProgressBar;
019: private int precision = DEFAULT_PRECISION;
020:
021: public ProgressBar(JProgressBar progressBar) {
022: this .jProgressBar = progressBar;
023: }
024:
025: public String getDescriptionTypeName() {
026: return TYPE_NAME;
027: }
028:
029: public Component getAwtComponent() {
030: return jProgressBar;
031: }
032:
033: /**
034: * Checks the completion value as a percentage (0-100) of the available range.
035: * The actual completion must be equal to the given value plus or minus the progress bar precision.
036: *
037: * @param expectedValue an int between 0 and 100, or -1 if the status is undeterminate
038: * @see #setPrecision
039: */
040: public Assertion completionEquals(final int expectedValue) {
041: return new Assertion() {
042: public void check() {
043: if (expectedValue == -1) {
044: Assert
045: .assertTrue(
046: "The progress bar status is not undeterminate",
047: jProgressBar.isIndeterminate());
048: return;
049: }
050: if ((expectedValue < 0) || (expectedValue > 100)) {
051: Assert
052: .fail("Expected value should be in range [0,100]");
053: }
054: int actualValue = getActualValue();
055: if (!isRoughlyEqual(actualValue, expectedValue)) {
056: Assert.assertEquals("Unexpected completion rate -",
057: expectedValue, actualValue);
058: }
059: }
060: };
061: }
062:
063: /**
064: * Checks the completion of the progress bar.
065: */
066: public Assertion isCompleted() {
067: return completionEquals(100);
068: }
069:
070: public Assertion displayedValueEquals(
071: final String expectedProgressString) {
072: return new Assertion() {
073: public void check() {
074: Assert.assertEquals(expectedProgressString,
075: jProgressBar.getString());
076: }
077: };
078: }
079:
080: /**
081: * Sets the precision for the completion check. This precision is the greatest difference
082: * allowed between the actual and expected completion values (both are integers between 0
083: * and 100).<p/>
084: * The default precision is 2.
085: *
086: * @see #completionEquals
087: */
088: public void setPrecision(int value) {
089: this .precision = value;
090: }
091:
092: private boolean isRoughlyEqual(int actualValue, int expectedValue) {
093: return Math.abs(actualValue - expectedValue) <= precision;
094: }
095:
096: private int getActualValue() {
097: if (jProgressBar.isIndeterminate()) {
098: return -1;
099: }
100: checkRange();
101: return (int) (jProgressBar.getPercentComplete() * 100);
102: }
103:
104: private int checkRange() {
105: int range = jProgressBar.getMaximum()
106: - jProgressBar.getMinimum();
107: if (range <= 0) {
108: throw new AssertionFailedError("Invalid range ["
109: + jProgressBar.getMinimum() + ","
110: + jProgressBar.getMaximum() + "]");
111: }
112: return range;
113: }
114: }
|