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 Sergey Burlak
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Adjustable;
023: import java.awt.event.AdjustmentEvent;
024: import java.awt.event.AdjustmentListener;
025: import java.beans.PropertyChangeEvent;
026: import java.beans.PropertyChangeListener;
027: import java.util.List;
028: import java.util.Vector;
029: import javax.accessibility.AccessibleRole;
030: import javax.accessibility.AccessibleState;
031:
032: public class JScrollBarTest extends SwingTestCase {
033: private JScrollBar bar;
034:
035: private List<String> testList;
036:
037: @Override
038: public void setUp() {
039: bar = new JScrollBar();
040: testList = new Vector<String>();
041: bar.addPropertyChangeListener(new PropertyChangeListener() {
042: public void propertyChange(final PropertyChangeEvent e) {
043: testList.add("1");
044: }
045: });
046: }
047:
048: @Override
049: protected void tearDown() throws Exception {
050: bar = null;
051: testList = null;
052: }
053:
054: public void testConstructor() throws Exception {
055: try {
056: new JScrollBar(2000, 512, 128, 16, 10001);
057: fail("IllegalArgumentException should be thrown");
058: } catch (NullPointerException npe) {
059: fail("NPE should not be thrown");
060: } catch (IllegalArgumentException iae) {
061: // PASSED
062: }
063: }
064:
065: public void testGetAccessibleContext() throws Exception {
066: JScrollBar.AccessibleJScrollBar accJScrollBar = (JScrollBar.AccessibleJScrollBar) bar
067: .getAccessibleContext();
068: assertNotNull(accJScrollBar);
069: assertTrue(bar.getAccessibleContext() == bar
070: .getAccessibleContext());
071: assertTrue(accJScrollBar.getAccessibleValue() == accJScrollBar
072: .getAccessibleValue());
073: assertTrue(bar.getAccessibleContext() == accJScrollBar
074: .getAccessibleValue());
075: assertEquals(new Integer(bar.getValue()), accJScrollBar
076: .getCurrentAccessibleValue());
077: accJScrollBar.setCurrentAccessibleValue(new Float(1));
078: assertEquals(new Integer(1), accJScrollBar
079: .getCurrentAccessibleValue());
080: assertEquals(new Integer(bar.getValue()), accJScrollBar
081: .getCurrentAccessibleValue());
082: bar.setMinimum(20);
083: assertEquals(new Integer(20), accJScrollBar
084: .getMinimumAccessibleValue());
085: assertEquals(new Integer(bar.getMinimum()), accJScrollBar
086: .getMinimumAccessibleValue());
087: bar.setMaximum(345);
088: assertEquals(new Integer(335), accJScrollBar
089: .getMaximumAccessibleValue());
090: bar.setVisibleAmount(20);
091: assertEquals(new Integer(325), accJScrollBar
092: .getMaximumAccessibleValue());
093: assertTrue(accJScrollBar.getAccessibleRole() == AccessibleRole.SCROLL_BAR);
094: assertEquals(5,
095: accJScrollBar.getAccessibleStateSet().toArray().length);
096: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
097: AccessibleState.ENABLED));
098: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
099: AccessibleState.FOCUSABLE));
100: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
101: AccessibleState.VISIBLE));
102: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
103: AccessibleState.OPAQUE));
104: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
105: AccessibleState.VERTICAL));
106: bar.setVisible(false);
107: assertEquals(4,
108: accJScrollBar.getAccessibleStateSet().toArray().length);
109: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
110: AccessibleState.ENABLED));
111: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
112: AccessibleState.FOCUSABLE));
113: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
114: AccessibleState.VISIBLE));
115: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
116: AccessibleState.OPAQUE));
117: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
118: AccessibleState.VERTICAL));
119: bar.setOrientation(Adjustable.HORIZONTAL);
120: assertEquals(4,
121: accJScrollBar.getAccessibleStateSet().toArray().length);
122: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
123: AccessibleState.ENABLED));
124: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
125: AccessibleState.FOCUSABLE));
126: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
127: AccessibleState.VISIBLE));
128: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
129: AccessibleState.OPAQUE));
130: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
131: AccessibleState.VERTICAL));
132: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
133: AccessibleState.HORIZONTAL));
134: bar.setEnabled(false);
135: assertEquals(3,
136: accJScrollBar.getAccessibleStateSet().toArray().length);
137: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
138: AccessibleState.ENABLED));
139: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
140: AccessibleState.FOCUSABLE));
141: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
142: AccessibleState.VISIBLE));
143: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
144: AccessibleState.OPAQUE));
145: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
146: AccessibleState.VERTICAL));
147: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
148: AccessibleState.HORIZONTAL));
149: bar.setOpaque(false);
150: assertEquals(2,
151: accJScrollBar.getAccessibleStateSet().toArray().length);
152: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
153: AccessibleState.ENABLED));
154: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
155: AccessibleState.FOCUSABLE));
156: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
157: AccessibleState.VISIBLE));
158: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
159: AccessibleState.OPAQUE));
160: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
161: AccessibleState.VERTICAL));
162: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
163: AccessibleState.HORIZONTAL));
164: bar.setFocusable(false);
165: assertEquals(1,
166: accJScrollBar.getAccessibleStateSet().toArray().length);
167: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
168: AccessibleState.ENABLED));
169: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
170: AccessibleState.FOCUSABLE));
171: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
172: AccessibleState.VISIBLE));
173: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
174: AccessibleState.OPAQUE));
175: assertFalse(accJScrollBar.getAccessibleStateSet().contains(
176: AccessibleState.VERTICAL));
177: assertTrue(accJScrollBar.getAccessibleStateSet().contains(
178: AccessibleState.HORIZONTAL));
179: }
180:
181: public void testGetOrientation() throws Exception {
182: assertEquals(SwingConstants.VERTICAL, bar.getOrientation());
183: bar.setOrientation(SwingConstants.HORIZONTAL);
184: assertEquals(SwingConstants.HORIZONTAL, bar.getOrientation());
185: try {
186: bar.setOrientation(200);
187: fail("illegal argument exception shal be thrown");
188: } catch (IllegalArgumentException e) {
189: }
190: assertEquals(1, testList.size());
191: }
192:
193: public void testGetValue() throws Exception {
194: assertEquals(0, bar.getValue());
195: assertEquals(bar.getModel().getValue(), bar.getValue());
196: bar.getModel().setValue(50);
197: assertEquals(50, bar.getValue());
198: assertEquals(bar.getModel().getValue(), bar.getValue());
199: bar.setValue(180);
200: assertEquals(90, bar.getValue());
201: assertEquals(bar.getModel().getValue(), bar.getValue());
202: assertEquals(0, testList.size());
203: }
204:
205: public void testGetModel() throws Exception {
206: assertEquals(0, testList.size());
207: bar.setModel(new DefaultBoundedRangeModel());
208: assertEquals(1, testList.size());
209: }
210:
211: public void testGetMinimum() throws Exception {
212: bar.setMinimum(-4);
213: assertEquals(-4, bar.getMinimum());
214: bar.setMinimum(200);
215: assertEquals(200, bar.getMinimum());
216: assertEquals(200, bar.getModel().getMinimum());
217: assertEquals(0, testList.size());
218: }
219:
220: public void testGetMaximum() throws Exception {
221: bar.setMaximum(-4);
222: assertEquals(-4, bar.getMaximum());
223: bar.setMaximum(200);
224: assertEquals(200, bar.getMaximum());
225: assertEquals(200, bar.getModel().getMaximum());
226: assertEquals(0, testList.size());
227: }
228:
229: public void testGetValueIsAdjusting() throws Exception {
230: assertFalse(bar.getValueIsAdjusting());
231: bar.setValueIsAdjusting(true);
232: assertTrue(bar.getValueIsAdjusting());
233: assertEquals(0, testList.size());
234: }
235:
236: public void testGetUnitIncrement() throws Exception {
237: assertEquals(1, bar.getUnitIncrement());
238: propertyChangeController = new PropertyChangeController();
239: bar.addPropertyChangeListener(propertyChangeController);
240: bar.setUnitIncrement(23);
241: assertEquals(23, bar.getUnitIncrement());
242: assertTrue(propertyChangeController.isChanged("unitIncrement"));
243: propertyChangeController.reset();
244: bar.setUnitIncrement(23);
245: assertFalse(propertyChangeController.isChanged("unitIncrement"));
246: assertEquals(23, bar.getUnitIncrement(-1));
247: assertEquals(23, bar.getUnitIncrement(1));
248: assertEquals(23, bar.getUnitIncrement(-134));
249: }
250:
251: public void testGetBlockIncrement() throws Exception {
252: assertEquals(10, bar.getBlockIncrement());
253: bar = new JScrollBar(Adjustable.VERTICAL, 0, 32, 0, 150);
254: assertEquals(32, bar.getBlockIncrement());
255: assertEquals(bar.getModel().getExtent(), bar
256: .getBlockIncrement());
257: propertyChangeController = new PropertyChangeController();
258: bar.addPropertyChangeListener(propertyChangeController);
259: bar.setBlockIncrement(40);
260: assertTrue(propertyChangeController.isChanged("blockIncrement"));
261: propertyChangeController.reset();
262: bar.setBlockIncrement(40);
263: assertFalse(propertyChangeController
264: .isChanged("blockIncrement"));
265: assertEquals(40, bar.getBlockIncrement());
266: assertEquals(32, bar.getModel().getExtent());
267: bar.setValues(0, 50, 0, 200);
268: assertEquals(40, bar.getBlockIncrement());
269: assertEquals(50, bar.getModel().getExtent());
270: assertEquals(0, testList.size());
271: assertEquals(40, bar.getBlockIncrement(1));
272: assertEquals(40, bar.getBlockIncrement(-1));
273: assertEquals(40, bar.getBlockIncrement(241));
274: }
275:
276: public void testGetVisibleAmount() throws Exception {
277: assertEquals(10, bar.getVisibleAmount());
278: bar.setVisibleAmount(20);
279: assertEquals(20, bar.getVisibleAmount());
280: assertEquals(20, bar.getModel().getExtent());
281: assertEquals(0, testList.size());
282: }
283:
284: public void testAdjustmentListener() throws Exception {
285: final List<String> test = new Vector<String>();
286: bar.addAdjustmentListener(new AdjustmentListener() {
287: public void adjustmentValueChanged(final AdjustmentEvent e) {
288: test.add("1");
289: }
290: });
291: assertEquals(0, test.size());
292: bar.setValue(300);
293: assertEquals(1, test.size());
294: }
295:
296: public void testGetAdjustmentListeners() throws Exception {
297: assertEquals(0, bar.getAdjustmentListeners().length);
298: AdjustmentListener l = new AdjustmentListener() {
299: public void adjustmentValueChanged(final AdjustmentEvent e) {
300: }
301: };
302: bar.addAdjustmentListener(l);
303: assertEquals(1, bar.getAdjustmentListeners().length);
304: bar.removeAdjustmentListener(l);
305: assertEquals(0, bar.getAdjustmentListeners().length);
306: }
307: }
|