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.util.EventListener;
023: import java.util.List;
024: import java.util.Vector;
025: import javax.swing.event.ChangeEvent;
026: import javax.swing.event.ChangeListener;
027:
028: public class DefaultBoundedRangeModelTest extends SwingTestCase {
029: private DefaultBoundedRangeModel model;
030:
031: private List<String> testList;
032:
033: @Override
034: public void setUp() throws Exception {
035: testList = new Vector<String>();
036: model = new DefaultBoundedRangeModel();
037: model.addChangeListener(new ChangeListener() {
038: public void stateChanged(final ChangeEvent e) {
039: testList.add("1");
040: }
041: });
042: }
043:
044: @Override
045: public void tearDown() throws Exception {
046: model = null;
047: testList = null;
048: }
049:
050: public void testSetExtent() throws Exception {
051: checkValues(0, 0, 0, 100, 0);
052: model.setExtent(10);
053: checkValues(0, 10, 0, 100, 1);
054: model.setValue(90);
055: checkValues(90, 10, 0, 100, 2);
056: model.setExtent(20);
057: checkValues(90, 10, 0, 100, 2);
058: model.setExtent(-10);
059: checkValues(90, 0, 0, 100, 3);
060: }
061:
062: public void testSetValue() throws Exception {
063: model.setValue(50);
064: checkValues(50, 0, 0, 100, 1);
065: model.setValue(99);
066: checkValues(99, 0, 0, 100, 2);
067: model.setValue(20);
068: model.setExtent(10);
069: model.setMinimum(5);
070: checkValues(20, 10, 5, 100, 5);
071: model.setValue(4);
072: checkValues(5, 10, 5, 100, 6);
073: model.setValue(96);
074: checkValues(90, 10, 5, 100, 7);
075: }
076:
077: public void testSetMaximum() throws Exception {
078: model.setExtent(10);
079: model.setValue(90);
080: checkValues(90, 10, 0, 100, 2);
081: model.setMaximum(90);
082: checkValues(80, 10, 0, 90, 3);
083: model.setMaximum(200);
084: checkValues(80, 10, 0, 200, 4);
085: model.setMinimum(20);
086: checkValues(80, 10, 20, 200, 5);
087: model.setValue(0);
088: model.setMaximum(23);
089: checkValues(20, 3, 20, 23, 7);
090: model.setMaximum(-1);
091: checkValues(-1, 0, -1, -1, 8);
092: model.setRangeProperties(50, 0, 30, 100, false);
093: checkValues(50, 0, 30, 100, 9);
094: model.setMaximum(20);
095: checkValues(20, 0, 20, 20, 10);
096: }
097:
098: public void testSetMinimum() throws Exception {
099: model.setExtent(10);
100: model.setValue(30);
101: checkValues(30, 10, 0, 100, 2);
102: model.setMinimum(20);
103: checkValues(30, 10, 20, 100, 3);
104: model.setMinimum(40);
105: checkValues(40, 10, 40, 100, 4);
106: model.setMinimum(20);
107: checkValues(40, 10, 20, 100, 5);
108: model.setMinimum(61);
109: checkValues(61, 10, 61, 100, 6);
110: model.setMinimum(99);
111: checkValues(99, 1, 99, 100, 7);
112: }
113:
114: public void testSetValueIsAdjusting() throws Exception {
115: assertFalse(model.getValueIsAdjusting());
116: model.setValueIsAdjusting(true);
117: assertTrue(model.getValueIsAdjusting());
118: assertEquals(1, testList.size());
119: }
120:
121: public void testSetRangeProperties() throws Exception {
122: checkValues(0, 0, 0, 100, 0);
123: model.setRangeProperties(0, 10, 0, 20, true);
124: checkValues(0, 10, 0, 20, 1);
125: model.setRangeProperties(300, 10, 0, 20, true);
126: checkValues(300, 0, 0, 300, 2);
127: model.setRangeProperties(100, 10, -40, 200, true);
128: checkValues(100, 10, -40, 200, 3);
129: model.setRangeProperties(50, 10, 100, 200, true);
130: checkValues(50, 10, 50, 200, 4);
131: }
132:
133: public void testListeners() throws Exception {
134: assertEquals(1, model.getChangeListeners().length);
135: ChangeListener l = new ChangeListener() {
136: public void stateChanged(final ChangeEvent e) {
137: }
138: };
139: model.addChangeListener(l);
140: assertEquals(2, model.getChangeListeners().length);
141: assertEquals(2, model.getListeners(ChangeListener.class).length);
142: model.removeChangeListener(l);
143: assertEquals(1, model.getChangeListeners().length);
144: assertEquals(1, model.getListeners(ChangeListener.class).length);
145: assertEquals(0, model.getListeners(EventListener.class).length);
146: }
147:
148: private void checkValues(final int value, final int extent,
149: final int min, final int max, final int eventNumber) {
150: assertEquals(value, model.getValue());
151: assertEquals(extent, model.getExtent());
152: assertEquals(min, model.getMinimum());
153: assertEquals(max, model.getMaximum());
154: assertEquals(eventNumber, testList.size());
155: }
156: }
|