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 Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.event.AdjustmentEvent;
023: import java.awt.event.AdjustmentListener;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * ScrollbarTest
029: */
030: public class ScrollbarTest extends TestCase {
031: Scrollbar scrollbar;
032: private boolean eventProcessed;
033:
034: @Override
035: protected void setUp() throws Exception {
036: super .setUp();
037: scrollbar = new Scrollbar();
038: }
039:
040: private void checkScrollbar(int orientation, int value,
041: int visible, int min, int max, int unitInc, int blockInc) {
042: assertNotNull(scrollbar);
043: assertEquals(orientation, scrollbar.getOrientation());
044: assertEquals(value, scrollbar.getValue());
045: assertEquals(visible, scrollbar.getVisibleAmount());
046: assertEquals(min, scrollbar.getMinimum());
047: assertEquals(max, scrollbar.getMaximum());
048: assertEquals(unitInc, scrollbar.getUnitIncrement());
049: assertEquals(blockInc, scrollbar.getBlockIncrement());
050: }
051:
052: /**
053: * @param orientation
054: * @param val
055: * @param vis
056: * @param min
057: * @param max
058: */
059: private void checkScrollbar(int orientation, int val, int vis,
060: int min, int max) {
061: checkScrollbar(orientation, val, vis, min, max, 1, 10);
062: }
063:
064: private void checkScrollbar(int val, int vis, int min, int max) {
065: checkScrollbar(Scrollbar.VERTICAL, val, vis, min, max, 1, 10);
066: }
067:
068: private void checkScrollbar() {
069: checkScrollbar(Scrollbar.VERTICAL);
070: }
071:
072: private void checkScrollbar(int orientation) {
073: checkScrollbar(orientation, 0, 10, 0, 100, 1, 10);
074: }
075:
076: public final void testParamString() {
077: String str = scrollbar.paramString();
078: assertTrue(str.indexOf("scrollbar0") >= 0);
079: assertTrue(str.indexOf(",val=0") > 0);
080: assertTrue(str.indexOf(",vis=10") > 0);
081: assertTrue(str.indexOf(",min=0") > 0);
082: assertTrue(str.indexOf(",max=100") > 0);
083: assertTrue(str.indexOf(",vert") > 0);
084: assertTrue(str.indexOf(",isAdjusting=false") > 0);
085: }
086:
087: public final void testScrollbar() {
088: checkScrollbar();
089: }
090:
091: public final void testScrollbarint() {
092: int orientation = Scrollbar.HORIZONTAL;
093: scrollbar = new Scrollbar(orientation);
094: checkScrollbar(orientation);
095: boolean iaeCaught = false;
096: orientation = -1;
097: try {
098: scrollbar = new Scrollbar(orientation);
099: } catch (IllegalArgumentException iae) {
100: iaeCaught = true;
101: }
102: assertTrue(iaeCaught);
103: orientation = Scrollbar.VERTICAL;
104: scrollbar = new Scrollbar(orientation);
105: checkScrollbar(orientation);
106: }
107:
108: public final void testScrollbarintintintintint() {
109: int orientation = Scrollbar.VERTICAL;
110: int val = 100;
111: int vis = 200;
112: int min = -500;
113: int max = 1500;
114: scrollbar = new Scrollbar(orientation, val, vis, min, max);
115: checkScrollbar(orientation, val, vis, min, max);
116: }
117:
118: public final void testGetOrientation() {
119: assertEquals(Scrollbar.VERTICAL, scrollbar.getOrientation());
120: }
121:
122: public final void testSetOrientation() {
123: int orientation = Scrollbar.HORIZONTAL;
124: scrollbar.setOrientation(orientation);
125: checkScrollbar(orientation);
126: boolean iaeCaught = false;
127: orientation = 10000;
128: try {
129: scrollbar.setOrientation(orientation);
130: } catch (IllegalArgumentException iae) {
131: iaeCaught = true;
132: }
133: assertTrue(iaeCaught);
134: checkScrollbar(Scrollbar.HORIZONTAL);
135: orientation = Scrollbar.VERTICAL;
136: scrollbar.setOrientation(orientation);
137: checkScrollbar(orientation);
138: }
139:
140: public final void testGetValue() {
141: assertEquals(0, scrollbar.getValue());
142: }
143:
144: public final void testSetValue() {
145: final int NORMAL_VAL = 66;
146: final int SMALL_VAL = -10;
147: final int BIG_VAL = 91;
148: int val = NORMAL_VAL;
149: scrollbar.setValue(val);
150: assertEquals(val, scrollbar.getValue());
151: scrollbar.setValue(val = SMALL_VAL);
152: assertEquals(0, scrollbar.getValue());
153: scrollbar.setValue(val = BIG_VAL);
154: assertEquals(90, scrollbar.getValue());
155: }
156:
157: public final void testGetMinimum() {
158: assertEquals(0, scrollbar.getMinimum());
159: }
160:
161: public final void testSetMinimum() {
162: final int NORMAL_MIN = -100;
163: final int MAX_MIN = Integer.MAX_VALUE;
164: final int MIN_MIN = Integer.MIN_VALUE;
165: int min = NORMAL_MIN;
166: scrollbar.setMinimum(min);
167: checkScrollbar(0, 10, min, 100);
168: scrollbar.setMinimum(min = MIN_MIN);
169: checkScrollbar(min + MAX_MIN - 10, 10, min, min + MAX_MIN);
170: scrollbar = new Scrollbar(); //reset params
171: scrollbar.setMinimum(min = 50);
172: checkScrollbar(min, 10, min, 100);
173: scrollbar.setMinimum(min = 95);
174: checkScrollbar(min, 100 - min, min, 100);
175: scrollbar.setMinimum(min = 100);
176: checkScrollbar(min, 1, min, 101);
177: scrollbar.setMinimum(min = 1000);
178: checkScrollbar(min, 1, min, 1001);
179: scrollbar.setMinimum(min = MAX_MIN);
180: checkScrollbar(min - 1, 1, min - 1, min);
181: }
182:
183: public final void testGetMaximum() {
184: assertEquals(100, scrollbar.getMaximum());
185: }
186:
187: public final void testSetMaximum() {
188: scrollbar = new Scrollbar(Scrollbar.VERTICAL, -100, 10, -100, 0);
189: final int NORMAL_MAX = 120;
190: final int MAX_MAX = Integer.MAX_VALUE;
191: final int MIN_MAX = Integer.MIN_VALUE;
192: int max = NORMAL_MAX;
193: scrollbar.setMaximum(max);
194: checkScrollbar(-100, 10, -100, max);
195: scrollbar.setMaximum(max = MAX_MAX);
196: checkScrollbar(-100, 10, -100, MAX_MAX - 100);
197: scrollbar = new Scrollbar(); //reset params
198: scrollbar.setMaximum(max = 50);
199: checkScrollbar(0, 10, 0, max);
200: scrollbar.setMaximum(max = 5);
201: checkScrollbar(0, max, 0, max);
202: scrollbar.setMaximum(max = 0);
203: checkScrollbar(-1, 1, -1, 0);
204: scrollbar.setMaximum(max = -1000);
205: checkScrollbar(-1001, 1, -1001, -1000);
206: scrollbar.setMaximum(max = MIN_MAX);
207: checkScrollbar(MIN_MAX, 1, MIN_MAX, MIN_MAX + 1);
208: }
209:
210: public final void testGetVisibleAmount() {
211: assertEquals(10, scrollbar.getVisibleAmount());
212: }
213:
214: @SuppressWarnings("deprecation")
215: public final void testGetVisible() {
216: assertEquals(10, scrollbar.getVisible());
217: }
218:
219: public final void testSetVisibleAmount() {
220: final int NORMAL_AMOUNT = 15;
221: final int SMALL_AMOUNT = -1;
222: final int BIG_AMOUNT = 1000;
223: int vis = NORMAL_AMOUNT;
224: scrollbar.setVisibleAmount(vis);
225: checkScrollbar(0, vis, 0, 100);
226: scrollbar.setVisibleAmount(vis = SMALL_AMOUNT);
227: checkScrollbar(0, 1, 0, 100);
228: scrollbar.setVisibleAmount(vis = BIG_AMOUNT);
229: checkScrollbar(0, 100, 0, 100);
230: }
231:
232: public final void testGetUnitIncrement() {
233: assertEquals(1, scrollbar.getUnitIncrement());
234: }
235:
236: @SuppressWarnings("deprecation")
237: public final void testGetLineIncrement() {
238: assertEquals(1, scrollbar.getLineIncrement());
239: }
240:
241: public final void testSetUnitIncrement() {
242: int unitIncr = 5;
243: scrollbar.setUnitIncrement(unitIncr);
244: assertEquals(unitIncr, scrollbar.getUnitIncrement());
245: scrollbar.setUnitIncrement(unitIncr = 0);
246: assertEquals(1, scrollbar.getUnitIncrement());
247: scrollbar.setUnitIncrement(unitIncr = Integer.MAX_VALUE);
248: assertEquals(unitIncr, scrollbar.getUnitIncrement());
249: }
250:
251: @SuppressWarnings("deprecation")
252: public final void testSetLineIncrement() {
253: scrollbar.setLineIncrement(10);
254: assertEquals(10, scrollbar.getLineIncrement());
255: }
256:
257: public final void testGetBlockIncrement() {
258: assertEquals(10, scrollbar.getBlockIncrement());
259: }
260:
261: @SuppressWarnings("deprecation")
262: public final void testGetPageIncrement() {
263: assertEquals(10, scrollbar.getPageIncrement());
264: }
265:
266: public final void testSetBlockIncrement() {
267: int blockIncr = 150;
268: scrollbar.setBlockIncrement(blockIncr);
269: assertEquals(blockIncr, scrollbar.getBlockIncrement());
270: scrollbar.setBlockIncrement(blockIncr = 0);
271: assertEquals(1, scrollbar.getBlockIncrement());
272: scrollbar.setBlockIncrement(blockIncr = Integer.MAX_VALUE);
273: assertEquals(blockIncr, scrollbar.getBlockIncrement());
274: }
275:
276: @SuppressWarnings("deprecation")
277: public final void testSetPageIncrement() {
278: scrollbar.setPageIncrement(3);
279: assertEquals(3, scrollbar.getPageIncrement());
280: }
281:
282: public final void testGetValueIsAdjusting() {
283: assertFalse(scrollbar.getValueIsAdjusting());
284: }
285:
286: public final void testSetValueIsAdjusting() {
287: scrollbar.setValueIsAdjusting(true);
288: assertTrue(scrollbar.getValueIsAdjusting());
289: scrollbar.setValueIsAdjusting(false);
290: assertFalse(scrollbar.getValueIsAdjusting());
291: }
292:
293: public final void testSetValues() {
294: int val = 5, vis = 10, min = -100, max = 100;
295: scrollbar.setValues(val, vis, min, max);
296: checkScrollbar(val, vis, min, max);
297: scrollbar.setValues(val = 0, vis = 20, min = 100, max = 50);
298: checkScrollbar(min, 1, min, min + 1);
299: scrollbar.setValues(val = 20, vis = 200,
300: min = Integer.MIN_VALUE, max = Integer.MAX_VALUE);
301: checkScrollbar(-1 - vis, vis, min, -1);
302: scrollbar.setValues(val = 200, vis = -200, min = 0, max = 100);
303: checkScrollbar(99, 1, min, max);
304:
305: scrollbar.setValues(val = -5, vis = 200, min = 0, max = 100);
306: checkScrollbar(0, 100, min, max);
307: }
308:
309: public void testAddGetRemoveAdjustmentListener() {
310: assertEquals(0, scrollbar.getAdjustmentListeners().length);
311:
312: AdjustmentListener listener = new AdjustmentListener() {
313: public void adjustmentValueChanged(AdjustmentEvent ae) {
314: }
315: };
316: scrollbar.addAdjustmentListener(listener);
317: assertEquals(1, scrollbar.getAdjustmentListeners().length);
318: assertSame(listener, scrollbar.getAdjustmentListeners()[0]);
319:
320: scrollbar.removeAdjustmentListener(listener);
321: assertEquals(0, scrollbar.getAdjustmentListeners().length);
322: }
323:
324: public void testProcessAdjustmentEvent() {
325: eventProcessed = false;
326: scrollbar.addAdjustmentListener(new AdjustmentListener() {
327: public void adjustmentValueChanged(AdjustmentEvent arg0) {
328: eventProcessed = true;
329: }
330: });
331: scrollbar.processEvent(new AdjustmentEvent(scrollbar,
332: AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
333: AdjustmentEvent.UNIT_INCREMENT, 1));
334: assertTrue(eventProcessed);
335: }
336:
337: public void testGetListenersClass() {
338: Class<AdjustmentListener> cls = AdjustmentListener.class;
339: assertEquals(0, scrollbar.getListeners(cls).length);
340:
341: AdjustmentListener listener = new AdjustmentListener() {
342: public void adjustmentValueChanged(AdjustmentEvent arg0) {
343: }
344: };
345: scrollbar.addAdjustmentListener(listener);
346: assertEquals(1, scrollbar.getListeners(cls).length);
347: assertSame(listener, scrollbar.getListeners(cls)[0]);
348:
349: scrollbar.removeAdjustmentListener(listener);
350: assertEquals(0, scrollbar.getListeners(cls).length);
351: }
352:
353: }
|