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

« back to all changes in this revision

Viewing changes to src/test/org/jdesktop/swingx/renderer/TableRendererTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2008-03-08 16:18:24 UTC
  • Revision ID: james.westby@ubuntu.com-20080308161824-wsahvl9pwzjcea3g
Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: TableRendererTest.java,v 1.8 2007/08/23 13:54:29 kleopatra Exp $
 
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.renderer;
 
23
 
 
24
import java.awt.Color;
 
25
import java.awt.Component;
 
26
import java.io.Serializable;
 
27
import java.text.DateFormat;
 
28
import java.util.Date;
 
29
import java.util.logging.Logger;
 
30
 
 
31
import javax.swing.Icon;
 
32
import javax.swing.ImageIcon;
 
33
import javax.swing.JComponent;
 
34
import javax.swing.JLabel;
 
35
import javax.swing.JTable;
 
36
import javax.swing.UIManager;
 
37
import javax.swing.border.Border;
 
38
import javax.swing.table.DefaultTableCellRenderer;
 
39
import javax.swing.table.DefaultTableModel;
 
40
import javax.swing.table.TableCellRenderer;
 
41
import javax.swing.table.TableModel;
 
42
 
 
43
import org.jdesktop.swingx.InteractiveTestCase;
 
44
import org.jdesktop.swingx.JXTable;
 
45
import org.jdesktop.swingx.test.XTestUtils;
 
46
import org.jdesktop.test.SerializableSupport;
 
47
 
 
48
/**
 
49
 * Tests behaviour of SwingX renderers. Currently: mostly characterization to
 
50
 * guarantee that they behave similar to the standard.
 
51
 * 
 
52
 * @author Jeanette Winzenburg
 
53
 */
 
54
public class TableRendererTest extends InteractiveTestCase {
 
55
 
 
56
    private static final Logger LOG = Logger.getLogger(TableRendererTest.class
 
57
            .getName());
 
58
    
 
59
    private JTable table;
 
60
    private int coreColumn;
 
61
    private DefaultTableCellRenderer coreTableRenderer;
 
62
    private int xColumn;
 
63
    private DefaultTableRenderer xTableRenderer;
 
64
 
 
65
    
 
66
    @Override
 
67
    protected void setUp() throws Exception {
 
68
        // setup table
 
69
        table = new JTable(10, 2);
 
70
        coreColumn = 0; 
 
71
        coreTableRenderer = new DefaultTableCellRenderer();
 
72
        table.getColumnModel().getColumn(coreColumn).setCellRenderer(coreTableRenderer);
 
73
        xColumn = 1;
 
74
        xTableRenderer = new DefaultTableRenderer();
 
75
        table.getColumnModel().getColumn(xColumn).setCellRenderer(xTableRenderer);
 
76
    }
 
77
    
 
78
    /**
 
79
     * Test constructors: here convenience with alignment and converter
 
80
     *
 
81
     */
 
82
    public void testConstructor() {
 
83
        FormatStringValue sv = new FormatStringValue(DateFormat.getTimeInstance());
 
84
        int align = JLabel.RIGHT;
 
85
        DefaultTableRenderer renderer = new DefaultTableRenderer(sv, align);
 
86
        assertEquals(sv, renderer.componentController.getToStringConverter());
 
87
        assertEquals(align, renderer.componentController.getHorizontalAlignment());
 
88
    }
 
89
    /**
 
90
     * test if icon handling is the same for core default and
 
91
     * swingx.
 
92
     *
 
93
     */
 
94
    public void testIcon() {
 
95
        TableModel model = createTableModelWithDefaultTypes();
 
96
        int iconColumn = 4;
 
97
        // sanity
 
98
        assertTrue(Icon.class.isAssignableFrom(model.getColumnClass(iconColumn)));
 
99
        Icon icon = (Icon) model.getValueAt(0, iconColumn);
 
100
        // default uses a different class for icon rendering
 
101
        DefaultTableCellRenderer coreIconRenderer = (DefaultTableCellRenderer) table.getDefaultRenderer(Icon.class);
 
102
        // core default can't cope with null component - can't really compare behaviour
 
103
        coreIconRenderer.getTableCellRendererComponent(table, icon, false, false, -1, -1);
 
104
        assertEquals(icon, coreIconRenderer.getIcon());
 
105
        assertEquals("", coreIconRenderer.getText());
 
106
        JXTable xTable = new JXTable();
 
107
        TableCellRenderer xIconRenderer = xTable.getDefaultRenderer(Icon.class);
 
108
        JLabel label = (JLabel) xIconRenderer.getTableCellRendererComponent(null, icon, false, false, -1, -1);
 
109
        assertEquals(icon, label.getIcon());
 
110
        assertEquals("", label.getText());
 
111
        // wrong assumption after fix of #591-swingx - default icon renderer
 
112
        // no longer tries to be clever
 
113
//        label = (JLabel) xIconRenderer.getTableCellRendererComponent(null, "dummy", false, false, -1, -1);
 
114
//        assertNull(label.getIcon());
 
115
//        assertEquals("dummy", label.getText());        
 
116
    }
 
117
        /**
 
118
     * @return
 
119
     */
 
120
    private TableModel createTableModelWithDefaultTypes() {
 
121
        String[] names = {"Object", "Number", "Double", "Date", "ImageIcon", "Boolean"};
 
122
        final Class[] types = {Object.class, Number.class, Double.class, Date.class, ImageIcon.class, Boolean.class};
 
123
        DefaultTableModel model = new DefaultTableModel(names, 0) {
 
124
 
 
125
            @Override
 
126
            public Class<?> getColumnClass(int columnIndex) {
 
127
                return types[columnIndex];
 
128
            }
 
129
            
 
130
        };
 
131
        Date today = new Date();
 
132
        Icon icon = XTestUtils.loadDefaultIcon();
 
133
        for (int i = 0; i < 10; i++) {
 
134
            Object[] values = new Object[] {"row " + i, i, Math.random() * 100, 
 
135
                    new Date(today.getTime() + i * 1000000), icon, i % 2 == 0};
 
136
            model.addRow(values);
 
137
        }
 
138
        return model;
 
139
    }
 
140
 
 
141
 
 
142
    
 
143
    /**
 
144
     * test serializable of default renderer.
 
145
     * 
 
146
     */
 
147
    public void testSerializeTableRenderer() {
 
148
        TableCellRenderer xListRenderer = new DefaultTableRenderer();
 
149
        try {
 
150
            SerializableSupport.serialize(xListRenderer);
 
151
        } catch (Exception e) {
 
152
            fail("not serializable " + e);
 
153
        } 
 
154
    }
 
155
 
 
156
 
 
157
    /**
 
158
     * base interaction with table: focused, selected uses UI border.
 
159
     */
 
160
    public void testTableFocusSelectedBorder() {
 
161
        // sanity to see test test validity
 
162
//        UIManager.put("Table.focusSelectedCellHighlightBorder", new LineBorder(Color.red));
 
163
        // access ui colors
 
164
        Border selectedFocusBorder = getFocusBorder(true);
 
165
        // sanity
 
166
        if (selectedFocusBorder == null) {
 
167
            LOG.info("cannot run focusSelectedBorder - UI has no selected focus border");
 
168
            return;
 
169
            
 
170
        }
 
171
        // need to prepare directly - focus is true only if table is focusowner
 
172
        JComponent coreComponent = (JComponent) coreTableRenderer.getTableCellRendererComponent(table, 
 
173
                null, true, true, 0, coreColumn);
 
174
        // sanity: known standard behaviour
 
175
        assertEquals(selectedFocusBorder, coreComponent.getBorder());
 
176
        // prepare extended
 
177
        JComponent xComponent = (JComponent) xTableRenderer.getTableCellRendererComponent(table, 
 
178
                null, true, true, 0, xColumn);
 
179
        // assert behaviour same as standard
 
180
        assertEquals(coreComponent.getBorder(), xComponent.getBorder());
 
181
    }
 
182
 
 
183
    private Border getFocusBorder(boolean lookup) {
 
184
        Border selectedFocusBorder = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
 
185
        if (lookup && (selectedFocusBorder == null)) {
 
186
            selectedFocusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
 
187
        }
 
188
        return selectedFocusBorder;
 
189
    }
 
190
 
 
191
    /**
 
192
     * base interaction with table: focused, not-selected uses UI border.
 
193
     * 
 
194
     *
 
195
     */
 
196
    public void testTableFocusBorder() {
 
197
        // access ui colors
 
198
        Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
 
199
//        Border selectedFocusBorder = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
 
200
        // sanity
 
201
        assertNotNull(focusBorder);
 
202
        // need to prepare directly - focus is true only if table is focusowner
 
203
        JComponent coreComponent = (JComponent) coreTableRenderer.getTableCellRendererComponent(table, 
 
204
                null, false, true, 0, coreColumn);
 
205
        // sanity: known standard behaviour
 
206
        assertEquals(focusBorder, coreComponent.getBorder());
 
207
        // prepare extended
 
208
        JComponent xComponent = (JComponent) xTableRenderer.getTableCellRendererComponent(table, 
 
209
                null, false, true, 0, xColumn);
 
210
        // assert behaviour same as standard
 
211
        assertEquals(coreComponent.getBorder(), xComponent.getBorder());
 
212
    }
 
213
    /**
 
214
     * base interaction with table: focused, not-selected and editable 
 
215
     * uses UI colors.
 
216
     *
 
217
     */
 
218
    public void testTableRendererExtFocusedNotSelectedEditable() {
 
219
        // sanity
 
220
        assertTrue(table.isCellEditable(0, coreColumn));
 
221
        // access ui colors
 
222
        Color uiForeground = UIManager.getColor("Table.focusCellForeground");
 
223
        Color uiBackground = UIManager.getColor("Table.focusCellBackground");
 
224
        // sanity
 
225
        assertNotNull(uiForeground);
 
226
        assertNotNull(uiBackground);
 
227
        Color background = Color.MAGENTA;
 
228
        Color foreground = Color.YELLOW;
 
229
        // prepare standard
 
230
        coreTableRenderer.setBackground(background);
 
231
        coreTableRenderer.setForeground(foreground);
 
232
        // need to prepare directly - focus is true only if table is focusowner
 
233
        Component coreComponent = coreTableRenderer.getTableCellRendererComponent(table, 
 
234
                null, false, true, 0, coreColumn);
 
235
        // sanity: known standard behaviour
 
236
        assertEquals(uiBackground, coreComponent.getBackground());
 
237
        assertEquals(uiForeground, coreComponent.getForeground());
 
238
        // prepare extended
 
239
        xTableRenderer.setBackground(background);
 
240
        xTableRenderer.setForeground(foreground);
 
241
        Component xComponent = xTableRenderer.getTableCellRendererComponent(table, 
 
242
                null, false, true, 0, xColumn);
 
243
        // assert behaviour same as standard
 
244
        assertEquals(coreComponent.getBackground(), xComponent.getBackground());
 
245
        assertEquals(coreComponent.getForeground(), xComponent.getForeground());
 
246
    }
 
247
    
 
248
    /**
 
249
     * base interaction with table: custom color of renderer precedes
 
250
     * table color.
 
251
     *
 
252
     */
 
253
    public void testTableRendererExtCustomColor() {
 
254
        Color background = Color.MAGENTA;
 
255
        Color foreground = Color.YELLOW;
 
256
        // prepare standard
 
257
        coreTableRenderer.setBackground(background);
 
258
        coreTableRenderer.setForeground(foreground);
 
259
        Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
 
260
        // sanity: known standard behaviour
 
261
        assertEquals(background, coreComponent.getBackground());
 
262
        assertEquals(foreground, coreComponent.getForeground());
 
263
        // prepare extended
 
264
        xTableRenderer.setBackground(background);
 
265
        xTableRenderer.setForeground(foreground);
 
266
        Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
 
267
        // assert behaviour same as standard
 
268
        assertEquals(coreComponent.getBackground(), xComponent.getBackground());
 
269
        assertEquals(coreComponent.getForeground(), xComponent.getForeground());
 
270
    }
 
271
 
 
272
    /**
 
273
     * base interaction with table: renderer uses table's selection color.
 
274
     *
 
275
     */
 
276
    public void testTableRendererExtSelectedColors() {
 
277
        // select first row
 
278
        table.setRowSelectionInterval(0, 0);
 
279
        // prepare standard
 
280
        Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
 
281
        // sanity: known standard behaviour
 
282
        assertEquals(table.getSelectionBackground(), coreComponent.getBackground());
 
283
        assertEquals(table.getSelectionForeground(), coreComponent.getForeground());
 
284
        // prepare extended
 
285
        Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
 
286
        // assert behaviour same as standard
 
287
        assertEquals(coreComponent.getBackground(), xComponent.getBackground());
 
288
        assertEquals(coreComponent.getForeground(), xComponent.getForeground());
 
289
    }
 
290
    
 
291
    /**
 
292
     * base interaction with table: renderer uses table's custom selection color.
 
293
     *
 
294
     */
 
295
    public void testTableRendererExtTableSelectedColors() {
 
296
        Color background = Color.MAGENTA;
 
297
        Color foreground = Color.YELLOW;
 
298
        table.setSelectionBackground(background);
 
299
        table.setSelectionForeground(foreground);
 
300
        // select first row
 
301
        table.setRowSelectionInterval(0, 0);
 
302
        // prepare standard
 
303
        Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
 
304
        // sanity: known standard behaviour
 
305
        assertEquals(table.getSelectionBackground(), coreComponent.getBackground());
 
306
        assertEquals(table.getSelectionForeground(), coreComponent.getForeground());
 
307
        // prepare extended
 
308
        Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
 
309
        // assert behaviour same as standard
 
310
        assertEquals(coreComponent.getBackground(), xComponent.getBackground());
 
311
        assertEquals(coreComponent.getForeground(), xComponent.getForeground());
 
312
    }
 
313
 
 
314
    /**
 
315
     * base interaction with table: renderer uses table's unselected colors.
 
316
     *
 
317
     */
 
318
    public void testTableRendererExtColors() {
 
319
        // prepare standard
 
320
        Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
 
321
        // sanity: known standard behaviour
 
322
        assertEquals(table.getBackground(), coreComponent.getBackground());
 
323
        assertEquals(table.getForeground(), coreComponent.getForeground());
 
324
        // prepare extended
 
325
        Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
 
326
        // assert behaviour same as standard
 
327
        assertEquals(coreComponent.getBackground(), xComponent.getBackground());
 
328
        assertEquals(coreComponent.getForeground(), xComponent.getForeground());
 
329
    }
 
330
    
 
331
    /**
 
332
     * base interaction with table: renderer uses table's unselected custom colors
 
333
     * 
 
334
     *
 
335
     */
 
336
    public void testTableRendererExtTableColors() {
 
337
        Color background = Color.MAGENTA;
 
338
        Color foreground = Color.YELLOW;
 
339
        table.setBackground(background);
 
340
        table.setForeground(foreground);
 
341
        // prepare standard
 
342
        Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
 
343
        // sanity: known standard behaviour
 
344
        assertEquals(table.getBackground(), coreComponent.getBackground());
 
345
        assertEquals(table.getForeground(), coreComponent.getForeground());
 
346
        // prepare extended
 
347
        Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
 
348
        // assert behaviour same as standard
 
349
        assertEquals(coreComponent.getBackground(), xComponent.getBackground());
 
350
        assertEquals(coreComponent.getForeground(), xComponent.getForeground());
 
351
    }
 
352
 
 
353
    /**
 
354
     * characterize opaqueness of rendering components.
 
355
     *
 
356
     */
 
357
    public void testTableOpaqueRenderer() {
 
358
        // sanity
 
359
        assertFalse(new JLabel().isOpaque());
 
360
        assertTrue(coreTableRenderer.isOpaque());
 
361
//        assertTrue(xTableRenderer.getRendererComponent().isOpaque());
 
362
    }
 
363
    
 
364
    /**
 
365
     * characterize opaqueness of rendering components.
 
366
     * 
 
367
     * that's useless: the opaque magic only applies if parent != null
 
368
     */
 
369
    public void testTableOpaqueRendererComponent() {
 
370
        // sanity
 
371
        assertFalse(new JLabel().isOpaque());
 
372
        Component coreComponent = table.prepareRenderer(coreTableRenderer, 0, coreColumn);
 
373
        // prepare extended
 
374
        assertTrue(coreComponent.isOpaque());
 
375
        Component xComponent = table.prepareRenderer(xTableRenderer, 0, xColumn);
 
376
        assertTrue(xComponent.isOpaque());
 
377
    }
 
378
 
 
379
 
 
380
    /**
 
381
     * base existence/type tests while adding DefaultTableCellRendererExt.
 
382
     *
 
383
     */
 
384
    public void testTableRendererExt() {
 
385
        DefaultTableRenderer renderer = new DefaultTableRenderer();
 
386
        assertTrue(renderer instanceof TableCellRenderer);
 
387
        assertTrue(renderer instanceof Serializable);
 
388
        
 
389
    }
 
390
}