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.MouseEvent;
023: import java.awt.event.MouseWheelEvent;
024: import java.awt.event.MouseWheelListener;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * ScrollPaneTest
030: */
031: public class ScrollPaneTest extends TestCase {
032:
033: ScrollPane scrollPane;
034: boolean eventProcessed;
035:
036: /*
037: * @see TestCase#setUp()
038: */
039: @Override
040: protected void setUp() throws Exception {
041: super .setUp();
042: scrollPane = new ScrollPane();
043: }
044:
045: /*
046: * @see TestCase#tearDown()
047: */
048: @Override
049: protected void tearDown() throws Exception {
050: super .tearDown();
051: }
052:
053: public final void testGetAccessibleContext() {
054: //TODO Implement getAccessibleContext().
055: }
056:
057: public final void testDoLayout() {
058: Component comp = new Button();
059: comp.setLocation(100, 100);
060: //comp.setSize(1000, 1000);
061: scrollPane.add(comp);
062: scrollPane.doLayout();
063: assertEquals(scrollPane.getSize(), comp.getSize());
064: assertEquals(new Point(), comp.getLocation());
065: Dimension size = new Dimension(50, 1000);
066: comp.setPreferredSize(size);
067: scrollPane.doLayout();
068: size = new Dimension(100, 1000);
069: assertEquals(size, comp.getSize());
070:
071: size = new Dimension(160, 200);
072: comp.setPreferredSize(size);
073: scrollPane.doLayout();
074: scrollPane.setScrollPosition(50, 75);
075: // If the new preferred size of the child causes the current scroll
076: // position to be invalid, the scroll position
077: // is set to the closest valid position.
078: size = new Dimension(110, 120);
079: comp.setPreferredSize(size);
080: scrollPane.doLayout();
081:
082: assertEquals(size, comp.getSize());
083: int viewHeight = scrollPane.getViewportSize().height;
084: int viewWidth = scrollPane.getViewportSize().width;
085: Point pos = new Point(size.width - viewWidth, size.height
086: - viewHeight);
087: assertEquals(pos, scrollPane.getScrollPosition());
088: }
089:
090: public final void testParamString() {
091: String str = scrollPane.paramString();
092: assertEquals(0, str.indexOf("scrollpane"));
093: assertTrue(str.indexOf("ScrollPosition=(0,0)") > 0);
094: assertTrue(str.indexOf("Insets=(0,0,0,0)") > 0);
095: assertTrue(str.indexOf("ScrollbarDisplayPolicy=as-needed") > 0);
096: assertTrue(str.indexOf("wheelScrollingEnabled=true") > 0);
097: }
098:
099: public final void testProcessMouseWheelEvent() {
100: eventProcessed = false;
101: scrollPane.addMouseWheelListener(new MouseWheelListener() {
102:
103: public void mouseWheelMoved(MouseWheelEvent arg0) {
104: eventProcessed = true;
105: }
106:
107: });
108:
109: scrollPane.processEvent(new MouseWheelEvent(scrollPane,
110: MouseEvent.MOUSE_WHEEL, 0l, 0, 100, 200, 0, false,
111: MouseWheelEvent.WHEEL_BLOCK_SCROLL, 10, 10));
112: assertTrue(eventProcessed);
113: }
114:
115: public final void testAddImpl() {
116: assertEquals(0, scrollPane.getComponentCount());
117: Component c = new Button();
118: scrollPane.add(c);
119: assertEquals(1, scrollPane.getComponentCount());
120: assertSame(c, scrollPane.getComponent(0));
121: scrollPane.add(c = new Checkbox());
122: assertEquals(1, scrollPane.getComponentCount());
123: assertSame(c, scrollPane.getComponent(0));
124: boolean iae = false;
125: try {
126: scrollPane.add(c = new Label(), 2);
127: } catch (IllegalArgumentException e) {
128: iae = true;
129: }
130: assertTrue(iae);
131: iae = false;
132: scrollPane.add(c = new Label(), 0);
133: assertEquals(1, scrollPane.getComponentCount());
134: assertSame(c, scrollPane.getComponent(0));
135:
136: }
137:
138: public final void testSetLayout() {
139: assertNull(scrollPane.getLayout());
140: boolean error = false;
141: try {
142: scrollPane.setLayout(new BorderLayout());
143: } catch (AWTError err) {
144: error = true;
145: }
146: assertTrue(error);
147: assertNull(scrollPane.getLayout());
148: }
149:
150: /*
151: * Class under test for void ScrollPane()
152: */
153: public final void testScrollPane() {
154: assertNotNull(scrollPane);
155: Dimension defaultSize = new Dimension(100, 100);
156: assertEquals(defaultSize, scrollPane.getSize());
157: assertEquals(defaultSize, scrollPane.getMinimumSize());
158: assertEquals(defaultSize, scrollPane.getPreferredSize());
159: }
160:
161: /*
162: * Class under test for void ScrollPane(int)
163: */
164: public final void testScrollPaneint() {
165: int policy = -1;
166: boolean iae = false;
167: scrollPane = null;
168: try {
169: scrollPane = new ScrollPane(policy);
170: } catch (IllegalArgumentException ex) {
171: iae = true;
172: }
173: assertTrue(iae);
174: assertNull(scrollPane);
175: scrollPane = new ScrollPane(
176: policy = ScrollPane.SCROLLBARS_ALWAYS);
177: assertNotNull(scrollPane);
178: assertEquals(policy, scrollPane.getScrollbarDisplayPolicy());
179: assertTrue(scrollPane.isWheelScrollingEnabled());
180: scrollPane = new ScrollPane(
181: policy = ScrollPane.SCROLLBARS_NEVER);
182: assertEquals(policy, scrollPane.getScrollbarDisplayPolicy());
183: assertTrue(scrollPane.isWheelScrollingEnabled());
184: assertEquals(new Dimension(100, 100), scrollPane.getSize());
185: }
186:
187: public final void testEventTypeEnabled() {
188: assertTrue(scrollPane.eventTypeEnabled(MouseEvent.MOUSE_WHEEL));
189: assertFalse(scrollPane
190: .eventTypeEnabled(MouseEvent.MOUSE_PRESSED));
191: }
192:
193: public final void testGetHAdjustable() {
194: assertTrue(scrollPane.getHAdjustable() instanceof ScrollPaneAdjustable);
195: }
196:
197: public final void testGetHScrollbarHeight() {
198: assertEquals(0, scrollPane.getHScrollbarHeight());
199: }
200:
201: public final void testGetScrollPosition() {
202: boolean npe = false;
203: try {
204: scrollPane.getScrollPosition();
205: } catch (NullPointerException e) {
206: npe = true;
207: }
208: assertTrue(npe);
209: scrollPane.add(new Button());
210: assertEquals(new Point(), scrollPane.getScrollPosition());
211: }
212:
213: public final void testGetScrollbarDisplayPolicy() {
214: assertEquals(ScrollPane.SCROLLBARS_AS_NEEDED, scrollPane
215: .getScrollbarDisplayPolicy());
216: }
217:
218: public final void testGetVAdjustable() {
219: assertTrue(scrollPane.getVAdjustable() instanceof ScrollPaneAdjustable);
220: }
221:
222: public final void testGetVScrollbarWidth() {
223: assertEquals(0, scrollPane.getVScrollbarWidth());
224: }
225:
226: public final void testGetViewportSize() {
227: Dimension size = scrollPane.getSize();
228: Insets ins = scrollPane.getInsets();
229: Dimension viewSize = size.getSize();
230: viewSize.width -= ins.left + ins.right;
231: viewSize.height -= ins.top + ins.bottom;
232: assertEquals(viewSize, scrollPane.getViewportSize());
233: }
234:
235: public final void testIsWheelScrollingEnabled() {
236: assertTrue(scrollPane.isWheelScrollingEnabled());
237: }
238:
239: /*
240: * Class under test for void setScrollPosition(Point)
241: */
242: public final void testSetScrollPositionPoint() {
243: boolean npe = false;
244: try {
245: scrollPane.setScrollPosition(new Point());
246: } catch (NullPointerException e) {
247: npe = true;
248: }
249: assertTrue(npe);
250: Component c = new Button();
251: Dimension prefSize = new Dimension(500, 300);
252: c.setPreferredSize(prefSize);
253: scrollPane.add(c);
254: scrollPane.doLayout();
255: Point pos = new Point(250, 150);
256: scrollPane.setScrollPosition(pos);
257: assertEquals(pos, scrollPane.getScrollPosition());
258: scrollPane.setScrollPosition(pos = new Point(-10, 15));
259: pos.x = 0;
260: assertEquals(pos, scrollPane.getScrollPosition());
261: scrollPane.setScrollPosition(pos = new Point(13, 250));
262: int vis = scrollPane.getViewportSize().height;
263: pos.y = c.getSize().height - vis;
264: assertEquals(pos, scrollPane.getScrollPosition());
265: }
266:
267: public final void testSetWheelScrollingEnabled() {
268: scrollPane.setWheelScrollingEnabled(false);
269: assertFalse(scrollPane.isWheelScrollingEnabled());
270: assertFalse(scrollPane.eventTypeEnabled(MouseEvent.MOUSE_WHEEL));
271: scrollPane.setWheelScrollingEnabled(true);
272: assertTrue(scrollPane.isWheelScrollingEnabled());
273: assertTrue(scrollPane.eventTypeEnabled(MouseEvent.MOUSE_WHEEL));
274: }
275:
276: }
|