2
* $Id: JXPanelTest.java 3660 2010-04-13 12:38:05Z kleopatra $
4
* Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
package org.jdesktop.swingx;
24
import static org.hamcrest.CoreMatchers.instanceOf;
25
import static org.hamcrest.CoreMatchers.is;
26
import static org.hamcrest.CoreMatchers.not;
27
import static org.hamcrest.CoreMatchers.notNullValue;
28
import static org.hamcrest.CoreMatchers.nullValue;
29
import static org.junit.Assert.assertThat;
31
import java.awt.Color;
32
import java.util.logging.Logger;
34
import javax.swing.plaf.ColorUIResource;
35
import javax.swing.plaf.UIResource;
37
import junit.framework.TestCase;
39
import org.jdesktop.swingx.painter.MattePainter;
40
import org.jdesktop.swingx.painter.Painter;
41
import org.jdesktop.swingx.plaf.PainterUIResource;
42
import org.jdesktop.test.EDTRunner;
43
import org.jdesktop.test.PropertyChangeReport;
44
import org.jdesktop.test.TestUtils;
45
import org.junit.Ignore;
46
import org.junit.Test;
47
import org.junit.runner.RunWith;
52
* @author Karl Schaefer
54
@RunWith(EDTRunner.class)
55
public class JXPanelTest extends TestCase {
56
@SuppressWarnings("unused")
57
private static final Logger LOG = Logger.getLogger(JXPanelTest.class
61
* Issue #1199-swingx: must listen to change on painter
64
public void testPainterChangeListener() {
65
JXPanel panel = new JXPanel();
66
final MattePainter painter = new MattePainter(Color.RED);
67
int listenerCount = painter.getPropertyChangeListeners().length;
68
panel.setBackgroundPainter(painter);
69
assertEquals(listenerCount +1, painter.getPropertyChangeListeners().length);
70
panel.setBackgroundPainter(null);
71
assertEquals(listenerCount, painter.getPropertyChangeListeners().length);
75
public void testScrollableWidthTrackProperty() {
76
JXPanel panel = new JXPanel();
77
ScrollableSizeHint oldTrack = panel.getScrollableWidthHint();
78
PropertyChangeReport report = new PropertyChangeReport(panel);
79
ScrollableSizeHint none = ScrollableSizeHint.HORIZONTAL_STRETCH;
80
panel.setScrollableWidthHint(none);
81
assertSame(none, panel.getScrollableWidthHint());
82
TestUtils.assertPropertyChangeEvent(report, "scrollableWidthHint", oldTrack, none);
85
public void testScrollableHeightTrackProperty() {
86
JXPanel panel = new JXPanel();
87
ScrollableSizeHint oldTrack = panel.getScrollableHeightHint();
88
PropertyChangeReport report = new PropertyChangeReport(panel);
89
ScrollableSizeHint none = ScrollableSizeHint.VERTICAL_STRETCH;
90
panel.setScrollableHeightHint(none);
91
assertSame(none, panel.getScrollableHeightHint());
92
TestUtils.assertPropertyChangeEvent(report, "scrollableHeightHint", oldTrack, none);
95
@Test (expected = NullPointerException.class)
96
public void testScrollableHeightTrackNull() {
97
new JXPanel().setScrollableHeightHint(null);
100
@Test (expected = IllegalArgumentException.class)
101
public void testScrollableHeightTrackIllegal() {
102
new JXPanel().setScrollableHeightHint(ScrollableSizeHint.HORIZONTAL_STRETCH);
105
@Test (expected = NullPointerException.class)
106
public void testScrollableWidthTrackNull() {
107
new JXPanel().setScrollableWidthHint(null);
110
@Test (expected = IllegalArgumentException.class)
111
public void testScrollableWidthTrackIllegal() {
112
new JXPanel().setScrollableWidthHint(ScrollableSizeHint.VERTICAL_STRETCH);
116
* Initial value (for backward compatibility: FIT)
120
public void testScrollableSizeTrackProperty() {
121
JXPanel panel = new JXPanel();
122
assertSame(ScrollableSizeHint.FIT, panel.getScrollableWidthHint());
123
assertSame(ScrollableSizeHint.FIT, panel.getScrollableHeightHint());
127
* Sanity: compatibility for width tracking.
130
public void testScrollableTracksWidth() {
131
JXPanel panel = new JXPanel();
132
assertTrue(panel.getScrollableTracksViewportWidth());
133
panel.setScrollableTracksViewportWidth(false);
134
assertFalse(panel.getScrollableTracksViewportWidth());
138
* Sanity: compatibility for height tracking.
141
public void testScrollableTracksHeight() {
142
JXPanel panel = new JXPanel();
143
assertTrue(panel.getScrollableTracksViewportHeight());
144
panel.setScrollableTracksViewportHeight(false);
145
assertFalse(panel.getScrollableTracksViewportHeight());
148
//----------------- test scrollable Size Track
151
public void testOrientationCompatible() {
152
assertVerticalCompatible(true, ScrollableSizeHint.NONE, ScrollableSizeHint.FIT,
153
ScrollableSizeHint.VERTICAL_STRETCH);
154
assertVerticalCompatible(false, ScrollableSizeHint.HORIZONTAL_STRETCH);
155
assertHorizontalCompatible(true, ScrollableSizeHint.NONE, ScrollableSizeHint.FIT,
156
ScrollableSizeHint.HORIZONTAL_STRETCH);
157
assertHorizontalCompatible(false, ScrollableSizeHint.VERTICAL_STRETCH);
162
private void assertVerticalCompatible(boolean compatible, ScrollableSizeHint... tracks) {
163
for (ScrollableSizeHint track : tracks) {
164
assertEquals("vertical expected on " + track, compatible, track.isVerticalCompatible());
170
private void assertHorizontalCompatible(boolean compatible, ScrollableSizeHint... tracks) {
171
for (ScrollableSizeHint track : tracks) {
172
assertEquals("horizontal expected on " + track, compatible, track.isHorizontalCompatible());
177
* Test contract - NPE on null component
180
public void testScrollableSizeTrackNPE() {
181
for (ScrollableSizeHint behaviour : ScrollableSizeHint.values()) {
183
behaviour.getTracksParentSize(null);
184
fail("null component must throw NPE, didn't on " + behaviour);
185
} catch (NullPointerException e) {
194
* SwingX #962: ensure that background painter is initially {@code null}.
196
* Added this test with the rollback of changes for SwingX #964. Remove when
200
public void testBackgroundPainterIsNull() {
201
Painter<?> painter = new JXPanel().getBackgroundPainter();
203
assertThat(painter, is(nullValue()));
207
* SwingX #962: ensure that background painter is initially {@code null}.
209
* SwingX #964: UI-delegate Painters can hide user-specified background
210
* color. No longer return {@code null}, we now pass the background color to
211
* the painter. Painter should start as {@code UIResource}.
214
@Ignore("reactivate with #964")
215
public void testBackgroundPainterIsUIResource() {
216
Painter<?> painter = new JXPanel().getBackgroundPainter();
218
assertThat(painter, is(instanceOf(UIResource.class)));
222
* SwingX #964: ensure setting background color sets painter.
225
@Ignore("reactivate with #964")
226
public void testSetBackgroundSetsPainter() {
227
JXPanel panel = new JXPanel();
229
//assure painter is null
230
panel.setBackgroundPainter(null);
232
panel.setBackground(Color.BLACK);
234
assertThat(panel.getBackgroundPainter(), is(notNullValue()));
238
* SwingX #964: ensure setting background color sets painter with a {@code
239
* UIResource} set the background painter with a {@code UIResource} if the
240
* background painter is {@code null} or a {@code UIResource}.
243
@SuppressWarnings("unchecked")
244
@Ignore("reactivate with #964")
245
public void testSetBackgroundWithUIResourceSetsPainterWithUIResource() {
246
JXPanel panel = new JXPanel();
248
//assure painter is null
249
panel.setBackgroundPainter(null);
251
panel.setBackground(new ColorUIResource(Color.BLACK));
253
assertThat(panel.getBackgroundPainter(), is(instanceOf(UIResource.class)));
255
Painter myResource = new PainterUIResource(new MattePainter(Color.BLACK));
256
panel.setBackgroundPainter(myResource);
258
panel.setBackground(new ColorUIResource(Color.BLACK));
260
assertThat(panel.getBackgroundPainter(), is(instanceOf(UIResource.class)));
261
assertThat(panel.getBackgroundPainter(), is(not(myResource)));