001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.assertion.UISpecAssert;
005: import org.uispec4j.utils.AssertionFailureNotDetectedError;
006: import org.uispec4j.utils.UIComponentFactory;
007: import org.uispec4j.utils.Utils;
008: import org.uispec4j.xml.XmlAssert;
009:
010: import javax.swing.*;
011:
012: public class ProgressBarTest extends UIComponentTestCase {
013: private ProgressBar progressBar;
014: private JProgressBar jProgressBar;
015:
016: protected void setUp() throws Exception {
017: super .setUp();
018: jProgressBar = new JProgressBar();
019: jProgressBar.setName("myProgressBar");
020: progressBar = (ProgressBar) UIComponentFactory
021: .createUIComponent(jProgressBar);
022: }
023:
024: public void testGetComponentTypeName() throws Exception {
025: assertEquals("progressBar", progressBar
026: .getDescriptionTypeName());
027: }
028:
029: public void testGetDescription() throws Exception {
030: XmlAssert.assertEquivalent(
031: "<progressBar name='myProgressBar'/>", progressBar
032: .getDescription());
033: }
034:
035: public void testFactory() throws Exception {
036: checkFactory(new JProgressBar(), ProgressBar.class);
037: }
038:
039: protected UIComponent createComponent() {
040: return progressBar;
041: }
042:
043: public void testAssertValueEquals() throws Exception {
044: setProgressValues(5, 15, 10);
045: assertTrue(progressBar.completionEquals(50));
046: checkAssertCompletionError(10, 50);
047: }
048:
049: public void testCompleted() throws Exception {
050: setProgressValues(5, 15, 15);
051: assertTrue(progressBar.completionEquals(100));
052: assertTrue(progressBar.isCompleted());
053:
054: setProgressValues(0, 10, 5);
055: try {
056: assertTrue(progressBar.isCompleted());
057: throw new AssertionFailureNotDetectedError();
058: } catch (AssertionFailedError e) {
059: assertEquals(
060: "Unexpected completion rate - expected:<100> but was:<50>",
061: e.getMessage());
062: }
063:
064: jProgressBar.setIndeterminate(true);
065: try {
066: assertTrue(progressBar.isCompleted());
067: throw new AssertionFailureNotDetectedError();
068: } catch (AssertionFailedError e) {
069: assertEquals(
070: "Unexpected completion rate - expected:<100> but was:<-1>",
071: e.getMessage());
072: }
073: }
074:
075: public void testAssertCompletionEqualsAcceptsValuesBetween0And100()
076: throws Exception {
077: checkAssertCompletionError(-2,
078: "Expected value should be in range [0,100]");
079: checkAssertCompletionError(101,
080: "Expected value should be in range [0,100]");
081: }
082:
083: public void testExpectedValueIsMinusOneWhenTheProgressBarIsUndeterminate()
084: throws Exception {
085: jProgressBar.setIndeterminate(false);
086: try {
087: assertTrue(progressBar.completionEquals(-1));
088: throw new AssertionFailureNotDetectedError();
089: } catch (AssertionFailedError e) {
090: assertEquals(
091: "The progress bar status is not undeterminate", e
092: .getMessage());
093: }
094: }
095:
096: public void testAssertCompletionEqualsChecksTheValidityOfTheMinMaxRange()
097: throws Exception {
098: setProgressValues(10, -5, 8);
099: checkAssertCompletionError(8, "Invalid range [-5,-5]");
100: setProgressValues(10, 10, 10);
101: checkAssertCompletionError(10, "Invalid range [10,10]");
102: }
103:
104: public void testAssertValueWhenProgressBarIsInIndeterminateMode()
105: throws Exception {
106: jProgressBar.setIndeterminate(true);
107: assertTrue(progressBar.completionEquals(-1));
108: }
109:
110: public void testUsingAPrecision() throws Exception {
111: setProgressValues(0, 100, 23);
112: assertTrue(progressBar.completionEquals(22));
113: assertTrue(progressBar.completionEquals(24));
114:
115: progressBar.setPrecision(5);
116: assertTrue(progressBar.completionEquals(28));
117: checkAssertCompletionError(29, 23);
118: assertTrue(progressBar.completionEquals(18));
119: checkAssertCompletionError(17, 23);
120: }
121:
122: public void testWaitForCompletion() throws Exception {
123: checkWaitForCompletion();
124: }
125:
126: public void testWaitForCompletionWithIndeterminateMode()
127: throws Exception {
128: jProgressBar.setIndeterminate(true);
129: checkWaitForCompletion();
130: }
131:
132: public void testAssertDisplayedValueEquals() throws Exception {
133: assertTrue(progressBar.displayedValueEquals(jProgressBar
134: .getString()));
135: jProgressBar.setString("done");
136: assertTrue(progressBar.displayedValueEquals("done"));
137: try {
138: assertTrue(progressBar.displayedValueEquals("unexpected"));
139: throw new AssertionFailureNotDetectedError();
140: } catch (AssertionFailedError e) {
141: assertEquals("expected:<unexpected> but was:<done>", e
142: .getMessage());
143: }
144: }
145:
146: private void setProgressValues(int min, int max, int value) {
147: jProgressBar.setMinimum(min);
148: jProgressBar.setMaximum(max);
149: jProgressBar.setValue(value);
150: }
151:
152: private void checkAssertCompletionError(int expectedValue,
153: String errorMessage) {
154: try {
155: assertTrue(progressBar.completionEquals(expectedValue));
156: throw new AssertionFailureNotDetectedError();
157: } catch (AssertionFailedError e) {
158: assertEquals(errorMessage, e.getMessage());
159: }
160: }
161:
162: private void checkAssertCompletionError(int expectedValue,
163: int actualValue) {
164: checkAssertCompletionError(expectedValue,
165: "Unexpected completion rate - expected:<"
166: + expectedValue + "> but was:<" + actualValue
167: + ">");
168: }
169:
170: private void checkWaitForCompletion() throws InterruptedException {
171: setProgressValues(0, 100, 0);
172: Thread thread = new Thread() {
173: public void run() {
174: Utils.sleep(50);
175: jProgressBar.setValue(100);
176: jProgressBar.setIndeterminate(false);
177: jProgressBar.setValue(100);
178: }
179: };
180: thread.start();
181: UISpecAssert.waitUntil(progressBar.isCompleted(), 200);
182: progressBar.completionEquals(100);
183: thread.join(1000);
184: }
185: }
|