~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to src/test/org/jdesktop/swingx/JXPanelTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-06 00:28:45 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110306002845-escned3cbqp5qx0t
Tags: 1:1.6.2-1
* New upstream release.
* Switch to maven as build system:
  - d/control: drop ant, add maven-debian-helper
  - d/rules: use maven.mk
* d/patches/pom.diff: drop, uneeded since upstream fixed its dependencies.
* d/watch: update to use java.net directly.
* d/rules: force debian version for JARs (Closes: #603495).
* d/copyright: Update to lastest DEP-5 r166.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: JXPanelTest.java 3660 2010-04-13 12:38:05Z kleopatra $
3
 
 *
4
 
 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
5
 
 * Santa Clara, California 95054, U.S.A. All rights reserved.
6
 
 *
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.
11
 
 * 
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.
16
 
 * 
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
20
 
 *
21
 
 */
22
 
package org.jdesktop.swingx;
23
 
 
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;
30
 
 
31
 
import java.awt.Color;
32
 
import java.util.logging.Logger;
33
 
 
34
 
import javax.swing.plaf.ColorUIResource;
35
 
import javax.swing.plaf.UIResource;
36
 
 
37
 
import junit.framework.TestCase;
38
 
 
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;
48
 
 
49
 
/**
50
 
 * Tests JXPanel.
51
 
 * 
52
 
 * @author Karl Schaefer
53
 
 */
54
 
@RunWith(EDTRunner.class)
55
 
public class JXPanelTest extends TestCase {
56
 
    @SuppressWarnings("unused")
57
 
    private static final Logger LOG = Logger.getLogger(JXPanelTest.class
58
 
            .getName());
59
 
    
60
 
    /**
61
 
     * Issue #1199-swingx: must listen to change on painter
62
 
     */
63
 
    @Test
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);
72
 
    }
73
 
    
74
 
    @Test
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);
83
 
    }
84
 
    @Test
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);
93
 
    }
94
 
    
95
 
    @Test (expected = NullPointerException.class)
96
 
    public void testScrollableHeightTrackNull() {
97
 
        new JXPanel().setScrollableHeightHint(null);
98
 
    }
99
 
    
100
 
    @Test (expected = IllegalArgumentException.class)
101
 
    public void testScrollableHeightTrackIllegal() {
102
 
        new JXPanel().setScrollableHeightHint(ScrollableSizeHint.HORIZONTAL_STRETCH);
103
 
    }
104
 
    
105
 
    @Test (expected = NullPointerException.class)
106
 
    public void testScrollableWidthTrackNull() {
107
 
        new JXPanel().setScrollableWidthHint(null);
108
 
    }
109
 
    
110
 
    @Test (expected = IllegalArgumentException.class)
111
 
    public void testScrollableWidthTrackIllegal() {
112
 
        new JXPanel().setScrollableWidthHint(ScrollableSizeHint.VERTICAL_STRETCH);
113
 
    }
114
 
    
115
 
    /**
116
 
     * Initial value (for backward compatibility: FIT)
117
 
     * 
118
 
     */
119
 
    @Test
120
 
    public void testScrollableSizeTrackProperty() {
121
 
        JXPanel panel = new JXPanel();
122
 
        assertSame(ScrollableSizeHint.FIT, panel.getScrollableWidthHint());
123
 
        assertSame(ScrollableSizeHint.FIT, panel.getScrollableHeightHint());
124
 
    }
125
 
    
126
 
    /**
127
 
     * Sanity: compatibility for width tracking.
128
 
     */
129
 
    @Test
130
 
    public void testScrollableTracksWidth() {
131
 
        JXPanel panel = new JXPanel();
132
 
        assertTrue(panel.getScrollableTracksViewportWidth());
133
 
        panel.setScrollableTracksViewportWidth(false);
134
 
        assertFalse(panel.getScrollableTracksViewportWidth());
135
 
    }
136
 
    
137
 
    /**
138
 
     * Sanity: compatibility for height tracking.
139
 
     */
140
 
    @Test
141
 
    public void testScrollableTracksHeight() {
142
 
        JXPanel panel = new JXPanel();
143
 
        assertTrue(panel.getScrollableTracksViewportHeight());
144
 
        panel.setScrollableTracksViewportHeight(false);
145
 
        assertFalse(panel.getScrollableTracksViewportHeight());
146
 
    }
147
 
 
148
 
//----------------- test scrollable Size Track
149
 
    
150
 
    @Test
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);
158
 
    }
159
 
    /**
160
 
     * 
161
 
     */
162
 
    private void assertVerticalCompatible(boolean compatible, ScrollableSizeHint... tracks) {
163
 
        for (ScrollableSizeHint track : tracks) {
164
 
            assertEquals("vertical expected on " + track, compatible, track.isVerticalCompatible());
165
 
        }
166
 
    }
167
 
    /**
168
 
     * 
169
 
     */
170
 
    private void assertHorizontalCompatible(boolean compatible, ScrollableSizeHint... tracks) {
171
 
        for (ScrollableSizeHint track : tracks) {
172
 
            assertEquals("horizontal expected on " + track, compatible, track.isHorizontalCompatible());
173
 
        }
174
 
    }
175
 
 
176
 
    /**
177
 
     * Test contract - NPE on null component
178
 
     */
179
 
    @Test 
180
 
    public void testScrollableSizeTrackNPE() {
181
 
        for (ScrollableSizeHint behaviour : ScrollableSizeHint.values()) {
182
 
            try {
183
 
                behaviour.getTracksParentSize(null);
184
 
                fail("null component must throw NPE, didn't on " + behaviour);
185
 
            } catch (NullPointerException e) {
186
 
                // expected
187
 
            }
188
 
        }
189
 
    }
190
 
  
191
 
    
192
 
    
193
 
    /**
194
 
     * SwingX #962: ensure that background painter is initially {@code null}.
195
 
     * <p>
196
 
     * Added this test with the rollback of changes for SwingX #964. Remove when
197
 
     * #964 is solved.
198
 
     */
199
 
    @Test
200
 
    public void testBackgroundPainterIsNull() {
201
 
        Painter<?> painter = new JXPanel().getBackgroundPainter();
202
 
        
203
 
        assertThat(painter, is(nullValue()));
204
 
    }
205
 
    
206
 
    /**
207
 
     * SwingX #962: ensure that background painter is initially {@code null}.
208
 
     * <p>
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}.
212
 
     */
213
 
    @Test
214
 
    @Ignore("reactivate with #964")
215
 
    public void testBackgroundPainterIsUIResource() {
216
 
        Painter<?> painter = new JXPanel().getBackgroundPainter();
217
 
        
218
 
        assertThat(painter, is(instanceOf(UIResource.class)));
219
 
    }
220
 
    
221
 
    /**
222
 
     * SwingX #964: ensure setting background color sets painter.
223
 
     */
224
 
    @Test
225
 
    @Ignore("reactivate with #964")
226
 
    public void testSetBackgroundSetsPainter() {
227
 
        JXPanel panel = new JXPanel();
228
 
        
229
 
        //assure painter is null
230
 
        panel.setBackgroundPainter(null);
231
 
        
232
 
        panel.setBackground(Color.BLACK);
233
 
        
234
 
        assertThat(panel.getBackgroundPainter(), is(notNullValue()));
235
 
    }
236
 
    
237
 
    /**
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}.
241
 
     */
242
 
    @Test
243
 
    @SuppressWarnings("unchecked")
244
 
    @Ignore("reactivate with #964")
245
 
    public void testSetBackgroundWithUIResourceSetsPainterWithUIResource() {
246
 
        JXPanel panel = new JXPanel();
247
 
        
248
 
        //assure painter is null
249
 
        panel.setBackgroundPainter(null);
250
 
        
251
 
        panel.setBackground(new ColorUIResource(Color.BLACK));
252
 
        
253
 
        assertThat(panel.getBackgroundPainter(), is(instanceOf(UIResource.class)));
254
 
        
255
 
        Painter myResource = new PainterUIResource(new MattePainter(Color.BLACK));
256
 
        panel.setBackgroundPainter(myResource);
257
 
        
258
 
        panel.setBackground(new ColorUIResource(Color.BLACK));
259
 
        
260
 
        assertThat(panel.getBackgroundPainter(), is(instanceOf(UIResource.class)));
261
 
        assertThat(panel.getBackgroundPainter(), is(not(myResource)));
262
 
    }
263
 
}