001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.util.Arrays;
023: import javax.accessibility.AccessibleContext;
024: import javax.accessibility.AccessibleState;
025: import javax.accessibility.AccessibleValue;
026: import javax.swing.event.ChangeEvent;
027: import javax.swing.event.ChangeListener;
028: import javax.swing.plaf.ProgressBarUI;
029: import org.apache.harmony.x.swing.StringConstants;
030:
031: public class JProgressBarTest extends BasicSwingTestCase {
032: private JProgressBar progressBar;
033:
034: private DefaultBoundedRangeModel model;
035:
036: private ChangeController controller;
037:
038: @Override
039: public void setUp() {
040: propertyChangeController = new PropertyChangeController();
041: model = new DefaultBoundedRangeModel(0, 0, 0, 256);
042: controller = new ChangeController();
043: progressBar = new JProgressBar(model);
044: progressBar.addChangeListener(controller);
045: progressBar.addPropertyChangeListener(propertyChangeController);
046: }
047:
048: @Override
049: public void tearDown() {
050: propertyChangeController = null;
051: progressBar = null;
052: model = null;
053: controller = null;
054: }
055:
056: public void testJProgressBar() {
057: progressBar = new JProgressBar();
058: assertEquals(SwingConstants.HORIZONTAL, progressBar.orientation);
059: assertNull(progressBar.progressString);
060: assertFalse(progressBar.paintString);
061: assertTrue(progressBar.paintBorder);
062: assertFalse(progressBar.isIndeterminate());
063: assertNotNull(progressBar.getUI());
064: assertNotNull(progressBar.changeListener);
065: progressBar = new JProgressBar(model);
066: assertSame(model, progressBar.model);
067: assertNotNull(progressBar.getUI());
068: testExceptionalCase(new IllegalArgumentCase() {
069: @Override
070: public void exceptionalAction() {
071: progressBar = new JProgressBar(
072: SwingConstants.HORIZONTAL
073: + SwingConstants.VERTICAL + 1);
074: }
075: });
076: progressBar = new JProgressBar(1, 23);
077: assertNotNull(progressBar.getUI());
078: assertEquals(1, progressBar.getMinimum());
079: assertEquals(23, progressBar.getMaximum());
080: assertEquals(1, progressBar.getValue());
081: }
082:
083: public void testSetGetOrientation() {
084: progressBar.setOrientation(SwingConstants.VERTICAL);
085: assertEquals(SwingConstants.VERTICAL, progressBar
086: .getOrientation());
087: testExceptionalCase(new IllegalArgumentCase() {
088: @Override
089: public void exceptionalAction() {
090: progressBar.setOrientation(SwingConstants.HORIZONTAL
091: + SwingConstants.VERTICAL + 1);
092: }
093: });
094: assertTrue(propertyChangeController
095: .isChanged(StringConstants.ORIENTATION));
096: }
097:
098: public void testSetGetModel() {
099: DefaultBoundedRangeModel newModel = new DefaultBoundedRangeModel(
100: 1, 0, 1, 12);
101: progressBar.setModel(newModel);
102: assertSame(newModel, progressBar.getModel());
103: assertTrue(Arrays.asList(newModel.getChangeListeners())
104: .contains(progressBar.changeListener));
105: progressBar.setModel(null);
106: assertNull(progressBar.getModel());
107: }
108:
109: public void testProgressString() {
110: assertFalse(progressBar.isStringPainted());
111: String string = "someDisplayString";
112: progressBar.setString(string);
113: assertSame(string, progressBar.getString());
114: assertTrue(propertyChangeController.isChanged("string"));
115: progressBar.setStringPainted(true);
116: assertTrue(progressBar.isStringPainted());
117: assertTrue(propertyChangeController.isChanged("stringPainted"));
118: }
119:
120: public void testSetIsIndeterminate() {
121: assertFalse(progressBar.isIndeterminate());
122: progressBar.setIndeterminate(true);
123: assertTrue(progressBar.isIndeterminate());
124: assertTrue(propertyChangeController.isChanged("indeterminate"));
125: }
126:
127: public void testAccessibleContext() {
128: progressBar.add(new JProgressBar());
129: AccessibleContext accessibleContext = progressBar
130: .getAccessibleContext();
131: assertNull(accessibleContext.getAccessibleName());
132: assertNull(accessibleContext.getAccessibleDescription());
133: assertNull(accessibleContext.getAccessibleAction());
134: assertSame(accessibleContext.getAccessibleValue(),
135: accessibleContext);
136: assertTrue(accessibleContext.getAccessibleStateSet().contains(
137: AccessibleState.HORIZONTAL));
138: AccessibleValue value = (AccessibleValue) accessibleContext;
139: int currentValue = (progressBar.getMinimum() + progressBar
140: .getMaximum()) / 2;
141: progressBar.setValue(currentValue);
142: assertTrue(controller.isChanged());
143: assertEquals(new Integer(currentValue), value
144: .getCurrentAccessibleValue());
145: assertEquals(new Integer(progressBar.getMaximum()), value
146: .getMaximumAccessibleValue());
147: assertEquals(new Integer(progressBar.getMinimum()), value
148: .getMinimumAccessibleValue());
149: value.setCurrentAccessibleValue(new Integer(currentValue + 1));
150: assertTrue(controller.isChanged());
151: assertEquals(currentValue + 1, progressBar.getValue());
152: assertEquals(new Integer(currentValue + 1), value
153: .getCurrentAccessibleValue());
154: assertTrue(value.setCurrentAccessibleValue(new Integer(
155: currentValue + 1)));
156: }
157:
158: public void testSetGetUpdateUI() {
159: assertEquals("ProgressBarUI", progressBar.getUIClassID());
160: ProgressBarUI defaultUI = progressBar.getUI();
161: assertNotNull(defaultUI);
162: ProgressBarUI ui = new ProgressBarUI() {
163: };
164: progressBar.setUI(ui);
165: assertEquals(ui, progressBar.getUI());
166: progressBar.updateUI();
167: assertNotSame(ui, progressBar.getUI());
168: }
169:
170: public void testSetGetValue() {
171: int currentValue = (progressBar.getMinimum() + progressBar
172: .getMaximum()) / 2;
173: progressBar.setValue(currentValue);
174: assertEquals(currentValue, progressBar.getValue());
175: assertTrue(controller.isChanged());
176: progressBar.setValue(progressBar.getMaximum() + 10);
177: assertEquals(progressBar.getMaximum(), progressBar.getValue());
178: }
179:
180: public void testSetGetMinimum() {
181: progressBar.setMinimum(1);
182: assertEquals(1, progressBar.getMinimum());
183: assertTrue(controller.isChanged());
184: }
185:
186: public void testSetGetMaximum() {
187: progressBar.setMaximum(257);
188: assertEquals(257, progressBar.getMaximum());
189: assertTrue(controller.isChanged());
190: }
191:
192: public void testSetIsBorderPainted() {
193: assertTrue(progressBar.isBorderPainted());
194: progressBar.setBorderPainted(false);
195: assertFalse(progressBar.isBorderPainted());
196: assertTrue(propertyChangeController.isChanged("borderPainted"));
197: }
198:
199: public void testGetPercentComplete() {
200: controller.setVerbose(false);
201: progressBar.setMinimum(25);
202: int range = progressBar.getMaximum() - progressBar.getMinimum();
203: int min = progressBar.getMinimum();
204: for (int i = progressBar.getMinimum(); i < progressBar
205: .getMaximum() + 1; i++) {
206: progressBar.setValue(i);
207: assertEquals(1. * (i - min) / (range), progressBar
208: .getPercentComplete(), 0.0000000001);
209: }
210: progressBar.setMinimum(progressBar.getMaximum());
211: assertTrue(Double.isNaN(progressBar.getPercentComplete()));
212: }
213:
214: public void testCreateChangeListener() {
215: ChangeListener listener = progressBar.createChangeListener();
216: assertNotNull(listener);
217: }
218:
219: public void testAddRemoveGetChangeListener() {
220: assertEquals(2, progressBar.getChangeListeners().length);
221: assertTrue(Arrays.asList(progressBar.getChangeListeners())
222: .contains(controller));
223: ChangeListener listener = new ChangeListener() {
224: public void stateChanged(ChangeEvent e) {
225: }
226: };
227: progressBar.addChangeListener(listener);
228: assertTrue(Arrays.asList(progressBar.getChangeListeners())
229: .contains(listener));
230: progressBar.removeChangeListener(controller);
231: progressBar.removeChangeListener(listener);
232: assertEquals(1, progressBar.getChangeListeners().length);
233: }
234:
235: public void testChangeEvent() {
236: progressBar.setValue(95);
237: assertSame(progressBar, progressBar.changeEvent.getSource());
238: assertTrue(controller.isChanged());
239: }
240: }
|