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 Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.Insets;
025: import java.awt.Point;
026: import java.awt.Rectangle;
027: import java.beans.PropertyChangeEvent;
028: import java.beans.PropertyChangeListener;
029: import java.util.List;
030: import java.util.Vector;
031: import javax.accessibility.AccessibleContext;
032: import javax.accessibility.AccessibleRole;
033: import javax.swing.JViewport.ViewListener;
034: import javax.swing.event.ChangeEvent;
035: import javax.swing.event.ChangeListener;
036: import javax.swing.plaf.basic.BasicViewportUI;
037:
038: public class JViewportTest extends SwingTestCase {
039: private JViewport port;
040:
041: private JList list;
042:
043: private TestListener listener;
044:
045: private JFrame frame;
046:
047: @Override
048: public void setUp() {
049: port = new JViewport();
050: listener = new TestListener();
051: }
052:
053: @Override
054: public void tearDown() {
055: port = null;
056: listener = null;
057: if (frame != null) {
058: frame.dispose();
059: frame = null;
060: }
061: }
062:
063: public void testAddRemoveChangeListener() {
064: final List<String> test = new Vector<String>();
065: assertEquals(0, port.getChangeListeners().length);
066: ChangeListener listener = new ChangeListener() {
067: public void stateChanged(final ChangeEvent e) {
068: test.add("");
069: }
070: };
071: port.addChangeListener(listener);
072: assertEquals(1, port.getChangeListeners().length);
073: assertEquals(listener, port.getChangeListeners()[0]);
074: assertEquals(0, test.size());
075: port.fireStateChanged();
076: assertEquals(1, test.size());
077: port.removeChangeListener(listener);
078: assertEquals(0, port.getChangeListeners().length);
079: }
080:
081: public void testAddRemovePropertyChangeListener() {
082: final List<String> test = new Vector<String>();
083: assertEquals(0, port.getPropertyChangeListeners().length);
084: PropertyChangeListener listener = new PropertyChangeListener() {
085: public void propertyChange(final PropertyChangeEvent e) {
086: test.add("");
087: }
088: };
089: port.addPropertyChangeListener(listener);
090: assertEquals(1, port.getPropertyChangeListeners().length);
091: assertEquals(listener, port.getPropertyChangeListeners()[0]);
092: assertEquals(0, test.size());
093: port.setBackground(Color.CYAN);
094: assertEquals(1, test.size());
095: port.removePropertyChangeListener(listener);
096: assertEquals(0, port.getPropertyChangeListeners().length);
097: }
098:
099: public void testGetExtentSize() {
100: assertEquals(new Dimension(0, 0), port.getExtentSize());
101: Dimension d = new Dimension(3, 5);
102: port.setExtentSize(d);
103: assertEquals(d, port.getExtentSize());
104: assertFalse(d == port.getExtentSize());
105: }
106:
107: public void testGetInsets() {
108: assertEquals(new Insets(0, 0, 0, 0), port.getInsets());
109: assertEquals(new Insets(0, 0, 0, 0), port.getInsets(new Insets(
110: 1, 2, 3, 4)));
111: }
112:
113: public void testGetScrollMode() {
114: assertEquals(1, port.getScrollMode());
115: port.setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
116: assertEquals(2, port.getScrollMode());
117: port.setScrollMode(JViewport.BLIT_SCROLL_MODE);
118: assertEquals(1, port.getScrollMode());
119: port.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
120: assertEquals(0, port.getScrollMode());
121: propertyChangeController = new PropertyChangeController();
122: port.addPropertyChangeListener(propertyChangeController);
123: port.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
124: assertEquals(0, port.getScrollMode());
125: assertFalse(propertyChangeController.isChanged());
126: }
127:
128: public void testGetUI() {
129: assertNotNull(port.getUI());
130: assertEquals("ViewportUI", port.getUIClassID());
131: }
132:
133: public void testGetView() {
134: assertNull(port.getView());
135: JLabel l = new JLabel();
136: port.add(l);
137: assertEquals(l, port.getView());
138: JPanel panel = new JPanel();
139: port.add(panel);
140: assertEquals(panel, port.getView());
141: }
142:
143: public void testGetSetViewPosition() {
144: JLabel l = new JLabel();
145: l.setPreferredSize(new Dimension(200, 200));
146: port.add(l);
147: port.addChangeListener(listener);
148: assertEquals(new Point(0, 0), port.getViewPosition());
149: Point point = new Point(2, 6);
150: port.setViewPosition(point);
151: assertNotNull(listener.getEvent());
152: assertEquals(port, listener.getEvent().getSource());
153: assertEquals(point, port.getViewPosition());
154: listener.reset();
155: port.setViewPosition(new Point(2, 6));
156: assertNull(listener.getEvent());
157: }
158:
159: public void testGetViewRect() {
160: JLabel l = new JLabel();
161: l.setPreferredSize(new Dimension(20, 40));
162: port.add(l);
163: assertEquals(new Rectangle(0, 0, 0, 0), port.getViewRect());
164: }
165:
166: public void testGetSetViewSize() {
167: JLabel l = new JLabel();
168: port.add(l);
169: Dimension dimension = new Dimension(20, 50);
170: l.setPreferredSize(dimension);
171: assertEquals(dimension, port.getViewSize());
172: dimension = new Dimension(20, 20);
173: port.setViewSize(dimension);
174: assertEquals(dimension, port.getViewSize());
175: assertEquals(dimension, port.getView().getSize());
176: dimension = new Dimension(100, 50);
177: l.setSize(dimension);
178: assertEquals(dimension, port.getViewSize());
179: }
180:
181: public void testSetBorder() {
182: try {
183: port.setBorder(BorderFactory.createEmptyBorder());
184: fail("must throw an exception");
185: } catch (IllegalArgumentException e) {
186: }
187: assertNull(port.getBorder());
188: port.setBorder(null);
189: assertNull(port.getBorder());
190: }
191:
192: public void testIsOptimizedDrawingEnabled() {
193: assertFalse(port.isOptimizedDrawingEnabled());
194: }
195:
196: public void testParamString() {
197: assertTrue(port.paramString().indexOf(",isViewSizeSet=") > 0);
198: assertTrue(port.paramString().indexOf(",lastPaintPosition=") > 0);
199: assertTrue(port.paramString().indexOf(",scrollUnderway=") > 0);
200: }
201:
202: @SuppressWarnings("deprecation")
203: public void testScrollRectToVisible() throws Exception {
204: JLabel label = new JLabel();
205: label.setPreferredSize(new Dimension(300, 300));
206: label.setBackground(Color.RED);
207: label.setOpaque(true);
208: final JScrollPane pane = new JScrollPane(label);
209: // pane.setPreferredSize(new Dimension(118, 118));
210: pane.setPreferredSize(new Dimension(120, 119));
211: frame = new JFrame();
212: frame.getContentPane().add(pane);
213: frame.pack();
214: frame.show();
215: port = pane.getViewport();
216: assertEquals(new Dimension(100, 100), port.getExtentSize());
217: port.setViewPosition(new Point(0, 0));
218: port.scrollRectToVisible(new Rectangle(50, 50, 100, 100));
219: assertEquals(new Point(50, 50), port.getViewPosition());
220: port.setViewPosition(new Point(0, 0));
221: port.scrollRectToVisible(new Rectangle(50, 50, 50, 50));
222: assertEquals(new Point(0, 0), port.getViewPosition());
223: port.setViewPosition(new Point(0, 0));
224: port.scrollRectToVisible(new Rectangle(50, 50, 60, 60));
225: assertEquals(new Point(10, 10), port.getViewPosition());
226: port.setViewPosition(new Point(0, 0));
227: port.scrollRectToVisible(new Rectangle(0, 0, 60, 60));
228: assertEquals(new Point(0, 0), port.getViewPosition());
229: port.setViewPosition(new Point(100, 100));
230: port.scrollRectToVisible(new Rectangle(0, 0, 60, 60));
231: assertEquals(new Point(100, 100), port.getViewPosition());
232: port.setViewPosition(new Point(100, 100));
233: port.scrollRectToVisible(new Rectangle(50, 50, 60, 60));
234: assertEquals(new Point(110, 110), port.getViewPosition());
235: port.setViewPosition(new Point(100, 100));
236: port.scrollRectToVisible(new Rectangle(-50, -50, 60, 60));
237: assertEquals(new Point(50, 50), port.getViewPosition());
238: port.setViewPosition(new Point(100, 100));
239: port.scrollRectToVisible(new Rectangle(-50, -50, 130, 130));
240: assertEquals(new Point(80, 80), port.getViewPosition());
241: port.setViewPosition(new Point(100, 100));
242: port.scrollRectToVisible(new Rectangle(-10, -10, 130, 130));
243: assertEquals(new Point(100, 100), port.getViewPosition());
244: port.setViewPosition(new Point(100, 100));
245: port.scrollRectToVisible(new Rectangle(-50, -50, 200, 200));
246: assertEquals(new Point(100, 100), port.getViewPosition());
247: port.setViewPosition(new Point(0, 0));
248: port.scrollRectToVisible(new Rectangle(1000, 1000, 100, 100));
249: assertEquals(new Point(200, 200), port.getViewPosition());
250: port.scrollRectToVisible(new Rectangle(1000, 1000, 100, 100));
251: assertEquals(new Point(200, 200), port.getViewPosition());
252: port.setViewPosition(new Point(0, 0));
253: port.scrollRectToVisible(new Rectangle(-1000, 1000, 100, 100));
254: assertEquals(new Point(0, 200), port.getViewPosition());
255: port.scrollRectToVisible(new Rectangle(-1000, 1000, 100, 100));
256: assertEquals(new Point(0, 200), port.getViewPosition());
257: port.setViewPosition(new Point(0, 0));
258: port.scrollRectToVisible(new Rectangle(-1000, -1000, 100, 100));
259: assertEquals(new Point(0, 0), port.getViewPosition());
260: port.scrollRectToVisible(new Rectangle(-1000, -1000, 100, 100));
261: assertEquals(new Point(0, 0), port.getViewPosition());
262: }
263:
264: public void testSetUI() {
265: assertEquals("ViewportUI", port.getUIClassID());
266: assertTrue(port.getUI() instanceof BasicViewportUI);
267: }
268:
269: public void testToViewCoordinates() {
270: JLabel l = new JLabel();
271: Dimension dimension = new Dimension(20, 50);
272: l.setPreferredSize(dimension);
273: port.add(l);
274: Dimension dim = new Dimension(1000, 200);
275: assertEquals(dim, port.toViewCoordinates(dim));
276: Point point = new Point(1000, 200);
277: assertEquals(point, port.toViewCoordinates(point));
278: }
279:
280: public void testCreateLayoutManager() {
281: JLabel l = new JLabel();
282: Dimension dimension = new Dimension(20, 50);
283: l.setPreferredSize(dimension);
284: port.add(l);
285: assertTrue(port.createLayoutManager() instanceof ViewportLayout);
286: assertTrue((port.getComponent(0) == l));
287: assertTrue((port.getView() == l));
288: }
289:
290: public void testCreateViewListener() {
291: ViewListener viewListener = port.createViewListener();
292: assertNotNull(viewListener);
293: }
294:
295: public void testGetAccessibleContext() {
296: AccessibleContext accessibleContext = port
297: .getAccessibleContext();
298: assertNotNull(accessibleContext);
299: assertTrue(AccessibleRole.VIEWPORT == accessibleContext
300: .getAccessibleRole());
301: }
302:
303: public void testSetView() {
304: port.setView(null);
305: JComponent comp = newJComponent();
306: port.setView(comp);
307: assertEquals(1, port.getComponentCount());
308: assertEquals(comp, port.getView());
309: assertEquals(comp, port.getComponent(0));
310: port.setView(new JButton());
311: assertEquals(1, port.getComponentCount());
312: }
313:
314: public void testEnsureIndexIsVisible() throws Exception {
315: list = new JList(new Object[] { "a", "b", "c" });
316: JScrollPane scroller = insertListToFrame();
317: assertNotNull(scroller);
318: Rectangle bounds = list.getCellBounds(1, 1);
319: assertFalse(list.getVisibleRect().contains(bounds));
320: list.ensureIndexIsVisible(1);
321: assertTrue(list.getVisibleRect().contains(bounds));
322: }
323:
324: public void testIsOpaque() throws Exception {
325: assertTrue(port.isOpaque());
326: }
327:
328: private JScrollPane insertListToFrame() {
329: return insertListToFrame(25);
330: }
331:
332: private JScrollPane insertListToFrame(final int preferredHeight) {
333: frame = new JFrame();
334: frame.setLocation(100, 100);
335: JScrollPane result = new JScrollPane(list) {
336: private static final long serialVersionUID = 1L;
337:
338: @Override
339: public Dimension getPreferredSize() {
340: return new Dimension(100, preferredHeight);
341: }
342: };
343: frame.getContentPane().add(result);
344: frame.pack();
345: return result;
346: }
347:
348: private JComponent newJComponent() {
349: return new JComponent() {
350: private static final long serialVersionUID = 1L;
351: };
352: }
353:
354: private class TestListener implements ChangeListener {
355: private ChangeEvent event;
356:
357: public void stateChanged(final ChangeEvent e) {
358: event = e;
359: }
360:
361: public ChangeEvent getEvent() {
362: return event;
363: }
364:
365: public void reset() {
366: event = null;
367: }
368: }
369: }
|