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.plaf.basic;
021:
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.Frame;
025: import java.awt.Rectangle;
026: import java.beans.PropertyChangeEvent;
027: import javax.swing.BasicSwingTestCase;
028: import javax.swing.DefaultBoundedRangeModel;
029: import javax.swing.JButton;
030: import javax.swing.JFrame;
031: import javax.swing.JLabel;
032: import javax.swing.JScrollBar;
033: import javax.swing.JScrollPane;
034: import javax.swing.SwingConstants;
035: import javax.swing.SwingUtilities;
036: import javax.swing.SwingWaitTestCase;
037:
038: public class BasicScrollBarUITest extends BasicSwingTestCase {
039: private BasicScrollBarUI barUI;
040:
041: private JScrollBar bar;
042:
043: @Override
044: protected void setUp() throws Exception {
045: super .setUp();
046: bar = new JScrollBar();
047: barUI = new BasicScrollBarUI();
048: bar.setUI(barUI);
049: }
050:
051: @Override
052: protected void tearDown() throws Exception {
053: barUI = null;
054: bar = null;
055: super .tearDown();
056: }
057:
058: /**
059: * Regression test for HARMONY-2854
060: */
061: public void testGetTrackThumbNewUI() {
062: barUI = new BasicScrollBarUI();
063: assertNull(barUI.getTrackBounds());
064: assertNull(barUI.getThumbBounds());
065: }
066:
067: public void testSetThumbBounds() throws Exception {
068: Rectangle bounds = barUI.getThumbBounds();
069: barUI.setThumbBounds(2, 3, 4, 5);
070: assertEquals(new Rectangle(2, 3, 4, 5), barUI.getThumbBounds());
071: assertTrue(barUI.getThumbBounds() == bounds);
072: }
073:
074: public void testGetThumbBounds() throws Exception {
075: Frame f = new Frame();
076: f.setLayout(null);
077: f.add(bar);
078: f.setSize(200, 200);
079: f.setVisible(true);
080: SwingWaitTestCase.isRealized(f);
081: bar.setBounds(0, 0, 10, 132);
082: bar.setValues(0, 50, 0, 100);
083: waitForIdle();
084: checkIsCloseTo(0, barUI.getThumbBounds().x);
085: checkIsCloseTo(16, barUI.getThumbBounds().y);
086: checkIsCloseTo(10, barUI.getThumbBounds().width);
087: checkIsCloseTo(50, barUI.getThumbBounds().height);
088: bar.setValues(30, 50, 0, 100);
089: waitForIdle();
090: checkIsCloseTo(0, barUI.getThumbBounds().x);
091: checkIsCloseTo(46, barUI.getThumbBounds().y);
092: checkIsCloseTo(10, barUI.getThumbBounds().width);
093: checkIsCloseTo(50, barUI.getThumbBounds().height);
094: bar.setValues(50, 50, 0, 100);
095: waitForIdle();
096: checkIsCloseTo(0, barUI.getThumbBounds().x);
097: checkIsCloseTo(66, barUI.getThumbBounds().y);
098: checkIsCloseTo(10, barUI.getThumbBounds().width);
099: checkIsCloseTo(50, barUI.getThumbBounds().height);
100: f.dispose();
101: }
102:
103: public void testButtonInScrollbar() throws Exception {
104: assertEquals(2, bar.getComponentCount());
105: }
106:
107: public void testPreferredLayoutSize() throws Exception {
108: assertEquals(barUI.preferredLayoutSize(bar), barUI
109: .getPreferredSize(bar));
110: JLabel label = new JLabel();
111: label.setPreferredSize(new Dimension(500, 600));
112: label.setBackground(Color.RED);
113: label.setOpaque(true);
114: final JScrollPane pane = new JScrollPane(label);
115: pane.setPreferredSize(new Dimension(120, 120));
116: final JFrame f = createFrame(pane);
117: JScrollBar verticalScrollBar = pane.getVerticalScrollBar();
118: BasicScrollBarUI basicScrollBarUI = ((BasicScrollBarUI) verticalScrollBar
119: .getUI());
120: assertEquals(basicScrollBarUI
121: .getPreferredSize(verticalScrollBar), basicScrollBarUI
122: .preferredLayoutSize(verticalScrollBar));
123: f.dispose();
124: }
125:
126: public void testGetMinimumSize() throws Exception {
127: JLabel label = new JLabel();
128: label.setPreferredSize(new Dimension(500, 600));
129: label.setBackground(Color.RED);
130: label.setOpaque(true);
131: final JScrollPane pane = new JScrollPane(label);
132: pane.setPreferredSize(new Dimension(120, 120));
133: final JFrame f = createFrame(pane);
134: JScrollBar verticalScrollBar = pane.getVerticalScrollBar();
135: BasicScrollBarUI basicScrollBarUI = ((BasicScrollBarUI) verticalScrollBar
136: .getUI());
137: if (isHarmony()) {
138: checkIsCloseTo(17,
139: basicScrollBarUI.getMinimumSize(bar).width);
140: checkIsCloseTo(45,
141: basicScrollBarUI.getMinimumSize(bar).height);
142: } else {
143: assertEquals(new Dimension(15, 55), basicScrollBarUI
144: .getMinimumSize(bar));
145: }
146: f.dispose();
147: }
148:
149: public void testGetMaximumSize() throws Exception {
150: JLabel label = new JLabel();
151: label.setPreferredSize(new Dimension(500, 600));
152: label.setBackground(Color.RED);
153: label.setOpaque(true);
154: final JScrollPane pane = new JScrollPane(label);
155: pane.setPreferredSize(new Dimension(120, 120));
156: final JFrame f = createFrame(pane);
157: JScrollBar verticalScrollBar = pane.getVerticalScrollBar();
158: BasicScrollBarUI basicScrollBarUI = ((BasicScrollBarUI) verticalScrollBar
159: .getUI());
160: assertEquals(
161: new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE),
162: basicScrollBarUI.getMaximumSize(bar));
163: f.dispose();
164: }
165:
166: public void testCreateDecreaseButton() throws Exception {
167: final JButton b = barUI
168: .createDecreaseButton(SwingConstants.VERTICAL);
169: assertTrue(b instanceof BasicArrowButton);
170: assertFalse(b == barUI
171: .createDecreaseButton(SwingConstants.VERTICAL));
172: }
173:
174: public void testCreateIncreaseButton() throws Exception {
175: final JButton b = barUI
176: .createIncreaseButton(SwingConstants.VERTICAL);
177: assertTrue(b instanceof BasicArrowButton);
178: assertFalse(b == barUI
179: .createIncreaseButton(SwingConstants.VERTICAL));
180: }
181:
182: public void testGetMaximumThumbSize() throws Exception {
183: JLabel label = new JLabel();
184: label.setPreferredSize(new Dimension(500, 600));
185: label.setBackground(Color.RED);
186: label.setOpaque(true);
187: final JScrollPane pane = new JScrollPane(label);
188: pane.setPreferredSize(new Dimension(120, 120));
189: final JFrame f = createFrame(pane);
190: JScrollBar verticalScrollBar = pane.getVerticalScrollBar();
191: BasicScrollBarUI basicScrollBarUI = ((BasicScrollBarUI) verticalScrollBar
192: .getUI());
193: assertEquals(new Dimension(4096, 4096), basicScrollBarUI
194: .getMaximumThumbSize());
195: f.dispose();
196: }
197:
198: public void testCreateArrowButtonListener() throws Exception {
199: assertNotNull(barUI.createArrowButtonListener());
200: assertFalse(barUI.createArrowButtonListener() == barUI
201: .createArrowButtonListener());
202: }
203:
204: public void testCreatePropertyChangeListener() throws Exception {
205: assertNotNull(barUI.createPropertyChangeListener());
206: if (isHarmony()) {
207: assertFalse(barUI.createPropertyChangeListener() == barUI
208: .createPropertyChangeListener());
209: }
210: }
211:
212: public void testCreateModelListener() throws Exception {
213: assertNotNull(barUI.createModelListener());
214: assertFalse(barUI.createModelListener() == barUI
215: .createModelListener());
216: }
217:
218: public void testCreateTrackListener() throws Exception {
219: assertNotNull(barUI.createTrackListener());
220: assertFalse(barUI.createTrackListener() == barUI
221: .createTrackListener());
222: }
223:
224: public void testUninstallListeners() throws Exception {
225: assertEquals(2, barUI.incrButton.getMouseListeners().length);
226: assertEquals(1, bar.getMouseListeners().length);
227: assertEquals(1, bar.getPropertyChangeListeners().length);
228: assertEquals(2, ((DefaultBoundedRangeModel) bar.getModel())
229: .getChangeListeners().length);
230: barUI.uninstallListeners();
231: assertEquals(0, bar.getMouseListeners().length);
232: assertEquals(0, bar.getPropertyChangeListeners().length);
233: assertEquals(1, ((DefaultBoundedRangeModel) bar.getModel())
234: .getChangeListeners().length);
235: assertEquals(1, barUI.incrButton.getMouseListeners().length);
236: }
237:
238: public void testLayoutContainer() throws Exception {
239: JLabel label = new JLabel();
240: label.setPreferredSize(new Dimension(500, 600));
241: label.setBackground(Color.RED);
242: label.setOpaque(true);
243: final JScrollPane pane = new JScrollPane(label);
244: pane.setPreferredSize(new Dimension(120, 120));
245: final JFrame f = new JFrame();
246: f.getContentPane().add(pane);
247: JScrollBar verticalScrollBar = pane.getVerticalScrollBar();
248: BasicScrollBarUI basicScrollBarUI = ((BasicScrollBarUI) verticalScrollBar
249: .getUI());
250: assertEquals(new Rectangle(0, 0, 0, 0),
251: basicScrollBarUI.incrButton.getBounds());
252: SwingUtilities.invokeAndWait(new Runnable() {
253: public void run() {
254: f.pack();
255: f.setVisible(true);
256: }
257: });
258: SwingWaitTestCase.isRealized(f);
259: checkIsCloseTo(0, basicScrollBarUI.incrButton.getBounds().x);
260: checkIsCloseTo(85, basicScrollBarUI.incrButton.getBounds().y);
261: checkIsCloseTo(16,
262: basicScrollBarUI.incrButton.getBounds().width);
263: checkIsCloseTo(16,
264: basicScrollBarUI.incrButton.getBounds().height);
265: f.dispose();
266: }
267:
268: public void testConfigureScrollBarColors() {
269: try {
270: new BasicScrollBarUI().configureScrollBarColors();
271: fail("NPE expected");
272: } catch (NullPointerException npe) {
273: // PASSED
274: }
275: }
276:
277: private JFrame createFrame(final JScrollPane pane) throws Exception {
278: final JFrame f = new JFrame();
279: f.getContentPane().add(pane);
280: SwingUtilities.invokeAndWait(new Runnable() {
281: public void run() {
282: f.pack();
283: f.setVisible(true);
284: }
285: });
286: SwingWaitTestCase.isRealized(f);
287: return f;
288: }
289:
290: private void checkIsCloseTo(final int expected, final int actual) {
291: assertTrue("expected: " + expected + ", actual: " + actual,
292: Math.abs(expected - actual) <= 2);
293: }
294:
295: public void testPropertyChange() {
296: try {
297: BasicScrollBarUI sbr = new BasicScrollBarUI();
298: BasicScrollBarUI.PropertyChangeHandler h = sbr.new PropertyChangeHandler();
299: final Object object = new Object();
300: PropertyChangeEvent pce = new PropertyChangeEvent(object,
301: "name", object, object);
302: h.propertyChange(pce);
303: } catch (NullPointerException npe) {
304: fail("NPE thrown");
305: }
306: }
307: }
|