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: * ScrollPaneAdjustableTest
029: */
030: public class ScrollPaneAdjustableTest extends TestCase {
031: ScrollPane scrollPane;
032: Frame f;
033: ScrollPaneAdjustable hAdjustable, vAdjustable;
034: private final int HSIZE = 1500;
035: private final int VSIZE = 200;
036:
037: @SuppressWarnings("deprecation")
038: @Override
039: protected void setUp() throws Exception {
040: super .setUp();
041: f = new Frame();
042: scrollPane = new ScrollPane();
043: hAdjustable = (ScrollPaneAdjustable) scrollPane
044: .getHAdjustable();
045: checkAdjustable(hAdjustable, Adjustable.HORIZONTAL, 0, 0, 0, 0,
046: 1, 1);
047: vAdjustable = (ScrollPaneAdjustable) scrollPane
048: .getVAdjustable();
049: checkAdjustable(vAdjustable, Adjustable.VERTICAL, 0, 0, 0, 0,
050: 1, 1);
051: Button b = new Button();
052: b.setPreferredSize(new Dimension(HSIZE, VSIZE));
053: scrollPane.add(b);
054: f.add(scrollPane);
055: f.show();
056: }
057:
058: @Override
059: protected void tearDown() throws Exception {
060: super .tearDown();
061: if (f != null) {
062: f.dispose();
063: }
064: }
065:
066: private void checkAdjustable(Adjustable adj, int orient, int value,
067: int vis, int min, int max, int unit, int block) {
068: assertEquals(orient, adj.getOrientation());
069: assertEquals(value, adj.getValue());
070: assertEquals(vis, adj.getVisibleAmount());
071: assertEquals(min, adj.getMinimum());
072: assertEquals(max, adj.getMaximum());
073: assertEquals(unit, adj.getUnitIncrement());
074: assertEquals(block, adj.getBlockIncrement());
075: }
076:
077: public final void testToString() {
078: assertEquals("toString() format is correct", vAdjustable
079: .getClass().getName()
080: + "[" + vAdjustable.paramString() + "]", vAdjustable
081: .toString());
082: }
083:
084: public final void testGetValue() {
085: assertEquals(0, vAdjustable.getValue());
086: assertEquals(0, hAdjustable.getValue());
087: }
088:
089: public final void testSetValue() {
090: int val = Integer.MIN_VALUE;
091: vAdjustable.setValue(val);
092: assertEquals(vAdjustable.getMinimum(), vAdjustable.getValue());
093: vAdjustable.setValue(val = (VSIZE / 2));
094: assertEquals(val, vAdjustable.getValue());
095: vAdjustable.setValue(val = VSIZE);
096: assertEquals(val - vAdjustable.getVisibleAmount(), vAdjustable
097: .getValue());
098: vAdjustable.setValue(val = Integer.MAX_VALUE);
099: assertEquals(vAdjustable.getMaximum()
100: - vAdjustable.getVisibleAmount(), vAdjustable
101: .getValue());
102: }
103:
104: public final void testGetBlockIncrement() {
105: int vis = vAdjustable.getVisibleAmount();
106: assertEquals(getBlockIncrFromVis(vis), vAdjustable
107: .getBlockIncrement());
108: vis = hAdjustable.getVisibleAmount();
109: assertEquals(getBlockIncrFromVis(vis), hAdjustable
110: .getBlockIncrement());
111: }
112:
113: private int getBlockIncrFromVis(int vis) {
114: return Math.max(1, vis * 9 / 10);
115: }
116:
117: private int getVisFromSize(int size, int gap) {
118: return Math.max(1, size - gap);
119: }
120:
121: public final void testGetMaximum() {
122: assertEquals(VSIZE, vAdjustable.getMaximum());
123: assertEquals(HSIZE, hAdjustable.getMaximum());
124: }
125:
126: public final void testGetMinimum() {
127: assertEquals(0, vAdjustable.getMinimum());
128: assertEquals(0, hAdjustable.getMinimum());
129: }
130:
131: public final void testGetOrientation() {
132: assertEquals(Adjustable.HORIZONTAL, hAdjustable
133: .getOrientation());
134: assertEquals(Adjustable.VERTICAL, vAdjustable.getOrientation());
135: }
136:
137: public final void testGetUnitIncrement() {
138: assertEquals(1, vAdjustable.getUnitIncrement());
139: assertEquals(1, hAdjustable.getUnitIncrement());
140: }
141:
142: public final void testGetValueIsAdjusting() {
143: assertFalse(vAdjustable.getValueIsAdjusting());
144: assertFalse(hAdjustable.getValueIsAdjusting());
145: }
146:
147: public final void testGetVisibleAmount() {
148: int size = scrollPane.getHeight();
149: Insets insets = scrollPane.getInsets();
150: int vGap = insets.bottom + insets.top;
151: int hGap = insets.left + insets.right;
152: assertEquals(getVisFromSize(size, vGap), vAdjustable
153: .getVisibleAmount());
154: size = scrollPane.getWidth();
155: assertEquals(getVisFromSize(size, hGap), hAdjustable
156: .getVisibleAmount());
157: }
158:
159: public final void testParamString() {
160: String hStr = hAdjustable.paramString();
161: String vStr = vAdjustable.paramString();
162: assertTrue(vStr.indexOf("vertical") >= 0);
163: assertTrue(hStr.indexOf("horizontal") >= 0);
164: assertTrue(vStr.indexOf(",val=0") > 0);
165: assertTrue(vStr.indexOf(",vis="
166: + vAdjustable.getVisibleAmount()) > 0);
167: assertTrue(vStr.indexOf(",[" + vAdjustable.getMinimum() + ".."
168: + vAdjustable.getMaximum() + "]") > 0);
169: assertTrue(vStr.indexOf(",unit="
170: + vAdjustable.getUnitIncrement()) > 0);
171: assertTrue(vStr.indexOf(",block="
172: + vAdjustable.getBlockIncrement()) > 0);
173: assertTrue(vStr.indexOf(",isAdjusting=false") > 0);
174: }
175:
176: public final void testSetBlockIncrement() {
177: int blockIncr = 25;
178: hAdjustable.setBlockIncrement(blockIncr);
179: assertEquals(blockIncr, hAdjustable.getBlockIncrement());
180:
181: hAdjustable.setBlockIncrement(blockIncr = Integer.MIN_VALUE);
182: assertEquals(blockIncr, hAdjustable.getBlockIncrement());
183: hAdjustable.setBlockIncrement(blockIncr = Integer.MAX_VALUE);
184: assertEquals(blockIncr, hAdjustable.getBlockIncrement());
185: }
186:
187: public final void testSetMaximum() {
188: boolean errorCatched = false;
189: try {
190: hAdjustable.setMaximum(256);
191: } catch (AWTError err) {
192: errorCatched = true;
193: }
194: assertTrue(errorCatched);
195: }
196:
197: public final void testSetMinimum() {
198: boolean errorCatched = false;
199: try {
200: vAdjustable.setMinimum(-256);
201: } catch (AWTError err) {
202: errorCatched = true;
203: }
204: assertTrue(errorCatched);
205: }
206:
207: public final void testSetUnitIncrement() {
208: int unitIncr = 10;
209: vAdjustable.setUnitIncrement(unitIncr);
210: assertEquals(unitIncr, vAdjustable.getUnitIncrement());
211:
212: vAdjustable.setUnitIncrement(unitIncr = Integer.MIN_VALUE);
213: assertEquals(unitIncr, vAdjustable.getUnitIncrement());
214: vAdjustable.setUnitIncrement(unitIncr = Integer.MAX_VALUE);
215: assertEquals(unitIncr, vAdjustable.getUnitIncrement());
216: }
217:
218: public final void testSetValueIsAdjusting() {
219: vAdjustable.setValueIsAdjusting(true);
220: assertTrue(vAdjustable.getValueIsAdjusting());
221: vAdjustable.setValueIsAdjusting(false);
222: assertFalse(vAdjustable.getValueIsAdjusting());
223: }
224:
225: public final void testSetVisibleAmount() {
226: boolean errorCatched = false;
227: try {
228: hAdjustable.setVisibleAmount(HSIZE / 2);
229: } catch (AWTError err) {
230: errorCatched = true;
231: }
232: assertTrue(errorCatched);
233: }
234:
235: public void testAddGetRemoveAdjustmentListener() {
236: assertEquals(0, vAdjustable.getAdjustmentListeners().length);
237:
238: AdjustmentListener listener = new AdjustmentListener() {
239: public void adjustmentValueChanged(AdjustmentEvent ae) {
240: }
241: };
242: vAdjustable.addAdjustmentListener(listener);
243: assertEquals(1, vAdjustable.getAdjustmentListeners().length);
244: assertSame(listener, vAdjustable.getAdjustmentListeners()[0]);
245:
246: vAdjustable.removeAdjustmentListener(listener);
247: assertEquals(0, vAdjustable.getAdjustmentListeners().length);
248: }
249:
250: }
|