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 Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.InputStream;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import javax.swing.event.ChangeEvent;
028: import javax.swing.event.ChangeListener;
029:
030: public class DefaultBoundedRangeModel2Test extends SwingTestCase {
031: DefaultBoundedRangeModel brm;
032:
033: String fireOrder;
034:
035: SimpleChangeListener listener = new SimpleChangeListener("initial");
036:
037: final int MIN = 5;
038:
039: final int MAX = 30;
040:
041: final int VALUE = 10;
042:
043: final int EXTENT = 15;
044:
045: final boolean IS_ADJUSTING = false;
046:
047: @Override
048: protected void setUp() throws Exception {
049: brm = new DefaultBoundedRangeModel(VALUE, EXTENT, MIN, MAX);
050: brm.addChangeListener(listener);
051: fireOrder = "";
052: super .setUp();
053: }
054:
055: @Override
056: protected void tearDown() throws Exception {
057: super .tearDown();
058: }
059:
060: class SimpleChangeListener implements ChangeListener {
061: String name;
062:
063: ChangeEvent event;
064:
065: public SimpleChangeListener(final String s) {
066: name = s;
067: }
068:
069: public void stateChanged(final ChangeEvent e) {
070: event = e;
071: fireOrder += name;
072: }
073: }
074:
075: public void testAddRemoveGetChangeListener() {
076: brm.removeChangeListener(listener);
077: SimpleChangeListener listener1 = new SimpleChangeListener(
078: "first");
079: SimpleChangeListener listener2 = new SimpleChangeListener(
080: "second");
081: SimpleChangeListener listener3 = new SimpleChangeListener(
082: "third");
083: ChangeListener changeListsners[] = brm.getChangeListeners();
084: ChangeListener listeners[] = brm
085: .getListeners(ChangeListener.class);
086: assertNotNull(changeListsners);
087: assertNotNull(listeners);
088: assertEquals(0, changeListsners.length);
089: assertEquals(0, listeners.length);
090: brm.addChangeListener(listener1);
091: changeListsners = brm.getChangeListeners();
092: listeners = brm.getListeners(ChangeListener.class);
093: assertEquals(1, changeListsners.length);
094: assertEquals(1, listeners.length);
095: assertEquals(changeListsners[0], listener1);
096: brm.addChangeListener(listener2);
097: changeListsners = brm.getChangeListeners();
098: listeners = brm.getListeners(ChangeListener.class);
099: assertEquals(2, changeListsners.length);
100: assertEquals(2, listeners.length);
101: assertEquals(changeListsners[0], listener2);
102: assertEquals(changeListsners[1], listener1);
103: brm.addChangeListener(listener3);
104: changeListsners = brm.getChangeListeners();
105: listeners = brm.getListeners(ChangeListener.class);
106: assertEquals(3, changeListsners.length);
107: assertEquals(3, listeners.length);
108: assertEquals(changeListsners[0], listener3);
109: assertEquals(changeListsners[1], listener2);
110: assertEquals(changeListsners[2], listener1);
111: listeners = brm.listenerList.getListeners(ChangeListener.class);
112: assertEquals(3, listeners.length);
113: assertEquals(listeners[0], listener3);
114: assertEquals(listeners[1], listener2);
115: assertEquals(listeners[2], listener1);
116: brm.removeChangeListener(listener3);
117: changeListsners = brm.getChangeListeners();
118: listeners = brm.getListeners(ChangeListener.class);
119: assertEquals(2, changeListsners.length);
120: assertEquals(2, listeners.length);
121: assertEquals(changeListsners[0], listener2);
122: assertEquals(changeListsners[1], listener1);
123: brm.removeChangeListener(listener1);
124: changeListsners = brm.getChangeListeners();
125: listeners = brm.getListeners(ChangeListener.class);
126: assertEquals(1, changeListsners.length);
127: assertEquals(1, listeners.length);
128: assertEquals(changeListsners[0], listener2);
129: brm.removeChangeListener(listener2);
130: changeListsners = brm.getChangeListeners();
131: listeners = brm.getListeners(ChangeListener.class);
132: assertEquals(0, changeListsners.length);
133: assertEquals(0, listeners.length);
134: }
135:
136: public void testDefaultBoundedRangeModel() {
137: DefaultBoundedRangeModel brm1 = new DefaultBoundedRangeModel();
138: checkValues(brm1, 0, 0, 0, 100, false);
139: }
140:
141: public void testDefaultBoundedRangeModelintintintint() {
142: DefaultBoundedRangeModel brm1 = new DefaultBoundedRangeModel(4,
143: 5, -2, 49);
144: checkValues(brm1, 4, 5, -2, 49, false);
145: testExceptionalCase(new IllegalArgumentCase() {
146: @Override
147: public void exceptionalAction() throws Exception {
148: new DefaultBoundedRangeModel(4, 6, 5, 39);
149: }
150: });
151: testExceptionalCase(new IllegalArgumentCase() {
152: @Override
153: public void exceptionalAction() throws Exception {
154: new DefaultBoundedRangeModel(3, 5, 1, 2);
155: }
156: });
157: testExceptionalCase(new IllegalArgumentCase() {
158: @Override
159: public void exceptionalAction() throws Exception {
160: new DefaultBoundedRangeModel(1, -1, 1, 2);
161: }
162: });
163: testExceptionalCase(new IllegalArgumentCase() {
164: @Override
165: // Regression test for HARMONY-2621
166: public void exceptionalAction() throws Exception {
167: new DefaultBoundedRangeModel(Integer.MAX_VALUE, 1, 2, 4);
168: }
169: });
170: }
171:
172: public void testFireStateChanged() {
173: SimpleChangeListener listener1 = new SimpleChangeListener("1");
174: SimpleChangeListener listener2 = new SimpleChangeListener("2");
175: SimpleChangeListener listener3 = new SimpleChangeListener("3");
176: brm.addChangeListener(listener1);
177: brm.addChangeListener(listener2);
178: brm.addChangeListener(listener3);
179: brm.setValue(7);
180: assertEquals("321initial", fireOrder);
181: assertEquals(listener.event, brm.changeEvent);
182: assertEquals(brm, brm.changeEvent.getSource());
183: }
184:
185: public void testSetGetExtent() {
186: for (int i = -3; i < 100; i++) {
187: fireOrder = "";
188: int oldValue = brm.getValue();
189: int oldMin = brm.getMinimum();
190: int oldMax = brm.getMaximum();
191: int oldExtent = brm.getExtent();
192: brm.setExtent(i);
193: int newExtent = Math.min(Math.max(i, 0), oldMax - oldValue);
194: assertEquals(oldMin, brm.getMinimum());
195: assertEquals(oldValue, brm.getValue());
196: assertEquals(newExtent, brm.getExtent());
197: assertEquals(oldMax, brm.getMaximum());
198: if (oldExtent != newExtent) {
199: assertEquals("initial", fireOrder);
200: }
201: }
202: }
203:
204: public void testSetGetMaximum() {
205: brm.setMaximum(brm.getMaximum());
206: assertEquals("", fireOrder);
207: for (int i = -3; i < 100; i++) {
208: fireOrder = "";
209: int oldValue = brm.getValue();
210: int oldExtent = brm.getExtent();
211: int oldMin = brm.getMinimum();
212: brm.setMaximum(i);
213: int newValue = Math.min(oldValue, i);
214: int newMin = Math.min(i, oldMin);
215: int newMax = i;
216: int newExtent = Math.min(i - newValue, oldExtent);
217: assertEquals(newMin, brm.getMinimum());
218: assertEquals(newValue, brm.getValue());
219: assertEquals(newExtent, brm.getExtent());
220: assertEquals(newMax, brm.getMaximum());
221: assertEquals("initial", fireOrder);
222: }
223: }
224:
225: public void testSetGetMinimum() {
226: brm.setMinimum(brm.getMinimum());
227: assertEquals("", fireOrder);
228: for (int i = -3; i < 100; i++) {
229: fireOrder = "";
230: int oldValue = brm.getValue();
231: int oldMax = brm.getMaximum();
232: int oldExtent = brm.getExtent();
233: brm.setMinimum(i);
234: int newValue = Math.max(i, oldValue);
235: int newMin = i;
236: int newMax = Math.max(i, oldMax);
237: int newExtent = (brm.getMaximum() >= brm.getValue()
238: + oldExtent) ? oldExtent : brm.getMaximum()
239: - brm.getValue();
240: assertEquals(newMin, brm.getMinimum());
241: assertEquals(newValue, brm.getValue());
242: assertEquals(newExtent, brm.getExtent());
243: assertEquals(newMax, brm.getMaximum());
244: assertEquals("initial", fireOrder);
245: }
246: }
247:
248: public void testSetGetValue() {
249: for (int i = -3; i < 100; i++) {
250: fireOrder = "";
251: int oldMin = brm.getMinimum();
252: int oldMax = brm.getMaximum();
253: int oldExtent = brm.getExtent();
254: int oldValue = brm.getValue();
255: brm.setValue(i);
256: int newValue = Math.min(Math.max(i, oldMin), oldMax
257: - oldExtent);
258: assertEquals(oldMin, brm.getMinimum());
259: assertEquals(newValue, brm.getValue());
260: assertEquals(oldExtent, brm.getExtent());
261: assertEquals(oldMax, brm.getMaximum());
262: if (oldValue != newValue) {
263: assertEquals("initial", fireOrder);
264: }
265: }
266: }
267:
268: public void testSetRangeProperties() {
269: brm.setRangeProperties(VALUE, EXTENT, MIN, MAX, IS_ADJUSTING);
270: assertEquals("", fireOrder);
271: brm.setRangeProperties(6, 7, -4, 200, true);
272: checkValues(6, 7, -4, 200, true);
273: assertEquals("initial", fireOrder);
274: fireOrder = "";
275: brm.setRangeProperties(5, 7, 6, 200, true);
276: checkValues(5, 7, 5, 200, true);
277: assertEquals("initial", fireOrder);
278: fireOrder = "";
279: brm.setRangeProperties(5, 7, 4, 0, true);
280: assertEquals(5, brm.getValue());
281: assertTrue(brm.getMinimum() <= brm.getValue());
282: assertTrue(brm.getExtent() >= 0);
283: assertTrue(brm.getValue() + brm.getExtent() <= brm.getMaximum());
284: assertEquals("initial", fireOrder);
285: fireOrder = "";
286: }
287:
288: void checkValues(final BoundedRangeModel model, final int value,
289: final int extent, final int min, final int max,
290: final boolean isAdjusting) {
291: assertEquals(value, model.getValue());
292: assertEquals(extent, model.getExtent());
293: assertEquals(min, model.getMinimum());
294: assertEquals(max, model.getMaximum());
295: assertEquals(isAdjusting, model.getValueIsAdjusting());
296: }
297:
298: void checkValues(final int value, final int extent, final int min,
299: final int max, final boolean isAdjusting) {
300: checkValues(brm, value, extent, min, max, isAdjusting);
301: }
302:
303: void resetValues() {
304: brm.setRangeProperties(VALUE, EXTENT, MIN, MAX, IS_ADJUSTING);
305: checkValues(VALUE, EXTENT, MIN, MAX, IS_ADJUSTING);
306: }
307:
308: public void testSetGetValueIsAdjusting() {
309: assertFalse(brm.getValueIsAdjusting());
310: brm.setValueIsAdjusting(false);
311: assertEquals("", fireOrder);
312: brm.setValueIsAdjusting(true);
313: assertTrue(brm.getValueIsAdjusting());
314: assertEquals("initial", fireOrder);
315: assertEquals(listener.event, brm.changeEvent);
316: assertEquals(brm, brm.changeEvent.getSource());
317: }
318:
319: public void testToString() {
320: assertEquals("javax.swing.DefaultBoundedRangeModel[value=10, "
321: + "extent=15, min=5, max=30, adj=false]", brm
322: .toString());
323: }
324:
325: public void testSerializable() throws Exception {
326: brm.addChangeListener(listener);
327: brm.setValueIsAdjusting(true);
328: DefaultBoundedRangeModel brm1 = new DefaultBoundedRangeModel();
329: ByteArrayOutputStream fo = new ByteArrayOutputStream();
330: ObjectOutputStream so = new ObjectOutputStream(fo);
331: so.writeObject(brm);
332: so.flush();
333: so.close();
334: InputStream fi = new ByteArrayInputStream(fo.toByteArray());
335: ObjectInputStream si = new ObjectInputStream(fi);
336: brm1 = (DefaultBoundedRangeModel) si.readObject();
337: si.close();
338: assertEquals(brm.getMinimum(), brm1.getMinimum());
339: assertEquals(brm.getValue(), brm1.getValue());
340: assertEquals(brm.getExtent(), brm1.getExtent());
341: assertEquals(brm.getMaximum(), brm1.getMaximum());
342: assertEquals(brm.getValueIsAdjusting(), brm1
343: .getValueIsAdjusting());
344: ChangeListener listeners[] = brm1.getChangeListeners();
345: assertEquals(0, listeners.length);
346: }
347: }
|