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 Sergey Burlak
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Dimension;
023:
024: public class ScrollPaneLayoutTest extends SwingTestCase {
025: private ScrollPaneLayout layout;
026:
027: private JScrollPane pane;
028:
029: private JLabel label;
030:
031: @Override
032: protected void setUp() throws Exception {
033: label = new JLabel();
034: label.setPreferredSize(new Dimension(500, 500));
035: pane = new JScrollPane(label);
036: layout = (ScrollPaneLayout) pane.getLayout();
037: }
038:
039: @Override
040: protected void tearDown() throws Exception {
041: layout = null;
042: pane = null;
043: label = null;
044: }
045:
046: public void testGetPreferredLayoutSize() throws Exception {
047: layout.colHead = new JViewport();
048: layout.colHead.setPreferredSize(new Dimension(100, 30));
049: layout.rowHead = new JViewport();
050: layout.rowHead.setPreferredSize(new Dimension(50, 20));
051: pane
052: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
053: pane
054: .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
055: pane.setBorder(BorderFactory.createEmptyBorder(51, 101, 151,
056: 202));
057: pane.setViewportBorder(BorderFactory.createEmptyBorder(51, 101,
058: 151, 202));
059: int width = layout.viewport.getPreferredSize().width
060: + layout.rowHead.getPreferredSize().width
061: + (layout.vsb == null ? 0
062: : layout.vsb.getBounds().width)
063: + pane.getInsets().right + pane.getInsets().left + 101
064: + 202;
065: int height = layout.viewport.getPreferredSize().height
066: + layout.colHead.getPreferredSize().height
067: + (layout.hsb == null ? 0
068: : layout.hsb.getBounds().height)
069: + pane.getInsets().top + pane.getInsets().bottom + 51
070: + 151;
071: assertEquals(width, layout.preferredLayoutSize(pane).width);
072: assertEquals(height, layout.preferredLayoutSize(pane).height);
073: try {
074: layout.preferredLayoutSize(new JButton());
075: fail("Class cast exception shall be thrown");
076: } catch (ClassCastException e) {
077: }
078: //regression for HARMONY-1735
079: try {
080: layout.preferredLayoutSize(null);
081: fail("No expected exception");
082: } catch (NullPointerException e) {
083: //expected
084: }
085: }
086:
087: public void testDefaultLayout() throws Exception {
088: ScrollPaneLayout l = new ScrollPaneLayout();
089: assertNull(l.colHead);
090: assertNull(l.lowerLeft);
091: assertNull(l.lowerRight);
092: assertNull(l.upperLeft);
093: assertNull(l.upperRight);
094: assertEquals(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
095: l.vsbPolicy);
096: assertEquals(
097: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED,
098: l.hsbPolicy);
099: }
100:
101: public void testSetHorizontalPolicy() throws Exception {
102: assertEquals(
103: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED,
104: layout.getHorizontalScrollBarPolicy());
105: pane
106: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
107: layout.syncWithScrollPane(pane);
108: assertEquals(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS,
109: layout.getHorizontalScrollBarPolicy());
110: layout
111: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
112: assertEquals(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER,
113: layout.getHorizontalScrollBarPolicy());
114: // regression 1 for HARMONY-1737
115: try {
116: layout
117: .setHorizontalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
118: fail("No expected IllegalArgumentException");
119: } catch (IllegalArgumentException e) {
120: //expected
121: }
122: }
123:
124: public void testSetVerticalPolicy() throws Exception {
125: assertEquals(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
126: layout.getVerticalScrollBarPolicy());
127: pane
128: .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
129: layout.syncWithScrollPane(pane);
130: assertEquals(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
131: layout.getVerticalScrollBarPolicy());
132: layout
133: .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
134: assertEquals(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
135: layout.getVerticalScrollBarPolicy());
136: // regression 2 for HARMONY-1737
137: try {
138: layout
139: .setVerticalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
140: fail("No expected IllegalArgumentException");
141: } catch (IllegalArgumentException e) {
142: //expected
143: }
144: }
145:
146: public void testGetViewport() throws Exception {
147: assertEquals(layout.viewport, layout.getViewport());
148: layout.viewport = null;
149: assertNull(layout.getViewport());
150: }
151:
152: public void testGetHorizontalScrollbar() throws Exception {
153: assertEquals(layout.hsb, layout.getHorizontalScrollBar());
154: layout.hsb = null;
155: assertNull(layout.getHorizontalScrollBar());
156: }
157:
158: public void testGetVerticalScrollbar() throws Exception {
159: assertEquals(layout.vsb, layout.getVerticalScrollBar());
160: layout.vsb = null;
161: assertNull(layout.getVerticalScrollBar());
162: }
163:
164: public void testGetCorner() throws Exception {
165: JButton lowerLeftButton = new JButton();
166: JButton upperLeftButton = new JButton();
167: JButton lowerRightButton = new JButton();
168: JButton upperRightButton = new JButton();
169: layout.lowerLeft = lowerLeftButton;
170: layout.upperLeft = upperLeftButton;
171: layout.lowerRight = lowerRightButton;
172: layout.upperRight = upperRightButton;
173: assertEquals(lowerLeftButton, layout
174: .getCorner(ScrollPaneConstants.LOWER_LEFT_CORNER));
175: assertEquals(upperLeftButton, layout
176: .getCorner(ScrollPaneConstants.UPPER_LEFT_CORNER));
177: assertEquals(lowerRightButton, layout
178: .getCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER));
179: assertEquals(upperRightButton, layout
180: .getCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER));
181: assertNull(layout.getCorner("something"));
182: }
183:
184: public void testGetRowHeader() throws Exception {
185: assertNull(layout.getRowHeader());
186: layout.rowHead = new JViewport();
187: assertEquals(layout.rowHead, layout.getRowHeader());
188: }
189:
190: public void testGetColumnHeader() throws Exception {
191: assertNull(layout.getColumnHeader());
192: layout.colHead = new JViewport();
193: assertEquals(layout.colHead, layout.getColumnHeader());
194: }
195:
196: public void testSyncWithScrollPane() throws Exception {
197: ScrollPaneLayout l = new ScrollPaneLayout();
198: assertNull(l.viewport);
199: assertNull(l.rowHead);
200: assertNull(l.colHead);
201: assertNull(l.lowerLeft);
202: assertNull(l.lowerRight);
203: assertNull(l.upperLeft);
204: assertNull(l.upperRight);
205: assertNull(l.hsb);
206: assertNull(l.vsb);
207: l.syncWithScrollPane(pane);
208: assertEquals(pane.getViewport(), l.viewport);
209: assertEquals(pane.getRowHeader(), l.rowHead);
210: assertEquals(pane.getColumnHeader(), l.colHead);
211: assertEquals(pane.getHorizontalScrollBar(), l.hsb);
212: assertEquals(pane.getVerticalScrollBar(), l.vsb);
213: assertEquals(pane
214: .getCorner(ScrollPaneConstants.LOWER_LEFT_CORNER),
215: l.lowerLeft);
216: assertEquals(pane
217: .getCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER),
218: l.lowerRight);
219: assertEquals(pane
220: .getCorner(ScrollPaneConstants.UPPER_LEFT_CORNER),
221: l.upperLeft);
222: assertEquals(pane
223: .getCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER),
224: l.upperRight);
225: assertEquals(pane.getHorizontalScrollBarPolicy(), l
226: .getHorizontalScrollBarPolicy());
227: assertEquals(pane.getVerticalScrollBarPolicy(), l
228: .getVerticalScrollBarPolicy());
229: try {
230: layout.syncWithScrollPane(null);
231: fail("NPE shall be thrown");
232: } catch (NullPointerException e) {
233: }
234: }
235:
236: public void testGetViewportBorderBounds() throws Exception {
237: assertEquals(pane.getViewportBorderBounds(), layout
238: .getViewportBorderBounds(pane));
239: }
240:
241: public void testRemoveLayoutComponent() throws Exception {
242: assertNotNull(layout.viewport);
243: layout.removeLayoutComponent(layout.viewport);
244: assertNull(layout.viewport);
245: assertNotNull(layout.vsb);
246: layout.removeLayoutComponent(layout.vsb);
247: assertNull(layout.vsb);
248: assertNotNull(layout.hsb);
249: layout.removeLayoutComponent(layout.hsb);
250: assertNull(layout.hsb);
251: layout.rowHead = new JViewport();
252: assertNotNull(layout.rowHead);
253: layout.removeLayoutComponent(layout.rowHead);
254: assertNull(layout.rowHead);
255: layout.colHead = new JViewport();
256: assertNotNull(layout.colHead);
257: layout.removeLayoutComponent(layout.colHead);
258: assertNull(layout.colHead);
259: layout.lowerLeft = new JButton();
260: assertNotNull(layout.lowerLeft);
261: layout.removeLayoutComponent(layout.lowerLeft);
262: assertNull(layout.lowerLeft);
263: layout.lowerRight = new JButton();
264: assertNotNull(layout.lowerRight);
265: layout.removeLayoutComponent(layout.lowerRight);
266: assertNull(layout.lowerRight);
267: layout.upperLeft = new JButton();
268: assertNotNull(layout.upperLeft);
269: layout.removeLayoutComponent(layout.upperLeft);
270: assertNull(layout.upperLeft);
271: layout.upperRight = new JButton();
272: assertNotNull(layout.upperRight);
273: layout.removeLayoutComponent(layout.upperRight);
274: assertNull(layout.upperRight);
275: }
276:
277: public void testAddSingletonLayoutComponent() throws Exception {
278: JButton newButton = new JButton();
279: JButton button = new JButton();
280: pane.setCorner(ScrollPaneConstants.LOWER_LEFT_CORNER, button);
281: int componentCount = pane.getComponentCount();
282: assertEquals(newButton, layout.addSingletonComponent(button,
283: newButton));
284: assertEquals(componentCount - 1, pane.getComponentCount());
285: pane.setCorner(ScrollPaneConstants.LOWER_LEFT_CORNER, button);
286: componentCount = pane.getComponentCount();
287: assertNull(layout.addSingletonComponent(button, null));
288: assertEquals(componentCount - 1, pane.getComponentCount());
289: }
290:
291: public void testMinimumLayoutSize() throws Exception {
292: layout.colHead = new JViewport();
293: layout.colHead.setPreferredSize(new Dimension(100, 30));
294: layout.rowHead = new JViewport();
295: layout.rowHead.setPreferredSize(new Dimension(50, 20));
296: pane
297: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
298: pane
299: .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
300: pane.setBorder(BorderFactory.createEmptyBorder(51, 101, 151,
301: 202));
302: pane.setViewportBorder(BorderFactory.createEmptyBorder(51, 101,
303: 151, 202));
304: int width = layout.viewport.getMinimumSize().width
305: + layout.rowHead.getMinimumSize().width
306: + pane.getVerticalScrollBar().getMinimumSize().width
307: + pane.getInsets().right + pane.getInsets().left + 101
308: + 202;
309: int height = layout.viewport.getMinimumSize().height
310: + layout.colHead.getMinimumSize().height
311: + pane.getHorizontalScrollBar().getMinimumSize().height
312: + pane.getInsets().top + pane.getInsets().bottom + 51
313: + 151;
314: assertEquals(new Dimension(width, height), layout
315: .minimumLayoutSize(pane));
316: }
317: }
|