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

« back to all changes in this revision

Viewing changes to swingx-core/src/test/java/org/jdesktop/swingx/XLocalizeTest.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: XLocalizeTest.java 3473 2009-08-27 13:17:10Z 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 java.awt.Component;
 
25
import java.awt.GraphicsEnvironment;
 
26
import java.awt.event.ActionEvent;
 
27
import java.beans.PropertyChangeEvent;
 
28
import java.beans.PropertyChangeListener;
 
29
import java.util.Locale;
 
30
import java.util.logging.Logger;
 
31
 
 
32
import javax.swing.Action;
 
33
import javax.swing.JComponent;
 
34
 
 
35
import org.jdesktop.swingx.action.AbstractActionExt;
 
36
import org.jdesktop.swingx.plaf.LookAndFeelAddons;
 
37
import org.jdesktop.swingx.plaf.UIManagerExt;
 
38
import org.jdesktop.swingx.search.PatternModel;
 
39
import org.jdesktop.test.PropertyChangeReport;
 
40
import org.jdesktop.test.TestUtils;
 
41
import org.junit.Test;
 
42
import org.junit.runner.RunWith;
 
43
import org.junit.runners.JUnit4;
 
44
 
 
45
 
 
46
/**
 
47
 * Test to expose known issues around <code>Locale</code> setting.
 
48
 * 
 
49
 * Ideally, there would be at least one failing test method per open
 
50
 * Issue in the issue tracker. Plus additional failing test methods for
 
51
 * not fully specified or not yet decided upon features/behaviour.
 
52
 * 
 
53
 * @author Jeanette Winzenburg
 
54
 */
 
55
@RunWith(JUnit4.class)
 
56
public class XLocalizeTest extends InteractiveTestCase {
 
57
    @SuppressWarnings("all")
 
58
    private static final Logger LOG = Logger.getLogger(XLocalizeTest.class
 
59
            .getName());
 
60
    private static final Locale A_LOCALE = Locale.FRENCH;
 
61
    private static final Locale OTHER_LOCALE = Locale.GERMAN;
 
62
 
 
63
 
 
64
    private Locale originalLocale;
 
65
    // test scope is static anyway...
 
66
    static {
 
67
        // force the addon to load
 
68
        LookAndFeelAddons.getAddon();
 
69
    }
 
70
    public static void main(String[] args) {
 
71
//      setSystemLF(true);
 
72
      XLocalizeTest test = new XLocalizeTest();
 
73
      try {
 
74
        test.runInteractiveTests();
 
75
//          test.runInteractiveTests("interactive.*TwoTable.*");
 
76
      } catch (Exception e) {
 
77
          System.err.println("exception when executing interactive tests:");
 
78
          e.printStackTrace();
 
79
      }
 
80
 
 
81
    }
 
82
 
 
83
    @Override
 
84
    protected void setUp() throws Exception {
 
85
        originalLocale = Locale.getDefault();
 
86
        super.setUp();
 
87
    }
 
88
    
 
89
    
 
90
 
 
91
    @Override
 
92
    protected void tearDown() throws Exception {
 
93
        Locale.setDefault(originalLocale);
 
94
        super.tearDown();
 
95
    }
 
96
 
 
97
    /**
 
98
     * test correct PropertyChangeNotification: must fire after
 
99
     * all internal state is set ... dooohhh.
 
100
     * 
 
101
     * Here: test JXDialog.
 
102
     */
 
103
    /**
 
104
     * test correct PropertyChangeNotification: must fire after
 
105
     * all internal state is set ...
 
106
     * 
 
107
     * Here: test FindPanel
 
108
     */
 
109
    @Test
 
110
    public void testLocaleDialogPropertyNotificationInListener() {
 
111
        // This test will not work in a headless configuration.
 
112
        if (GraphicsEnvironment.isHeadless()) {
 
113
            LOG.fine("cannot run localeDialogPropertyNotificationInListener - headless environment");
 
114
            return;
 
115
        }
 
116
        final String prefix = PatternModel.SEARCH_PREFIX;
 
117
        final JXFindPanel findPanel = new JXFindPanel();
 
118
        final JXDialog dialog = new JXDialog(findPanel);
 
119
        final String titleKey = AbstractPatternPanel.SEARCH_TITLE;
 
120
        // JW: arrrgghh ... dirty! Consequence of dirty initialization 
 
121
        // of AbstractPatternPanel subclasses ...
 
122
        findPanel.addNotify();
 
123
        String name = dialog.getTitle();
 
124
        String uiValue = UIManagerExt.getString(prefix + titleKey, findPanel
 
125
                .getLocale());
 
126
        // sanity
 
127
        assertNotNull(uiValue);
 
128
        assertEquals(name, uiValue);
 
129
        final Locale alternative = getAlternativeLocale(dialog);
 
130
        PropertyChangeListener report = new PropertyChangeListener() {
 
131
 
 
132
            public void propertyChange(PropertyChangeEvent evt) {
 
133
                // sanity
 
134
                // wrong assumption: find widgets name is changed as well
 
135
//                assertTrue("locale property changed, instead: " + evt.getPropertyName(), "locale".equals(evt.getPropertyName()));
 
136
                if (!"locale".equals(evt.getPropertyName())) return;
 
137
                String altUIValue = UIManagerExt.getString(prefix + titleKey,
 
138
                        alternative);
 
139
                String altName = dialog.getTitle();
 
140
                assertEquals("name must be updated before fire propertyChange", 
 
141
                        altUIValue, altName);
 
142
 
 
143
                
 
144
            }};
 
145
        dialog.addPropertyChangeListener(report);
 
146
        PropertyChangeReport r = new PropertyChangeReport();
 
147
        dialog.addPropertyChangeListener(r);
 
148
        dialog.setLocale(alternative);
 
149
        // sanity: guarantee that we got a locale change notification
 
150
        assertEquals(1, r.getEventCount("locale"));
 
151
    }
 
152
 
 
153
 
 
154
    /**
 
155
     * test correct PropertyChangeNotification: must fire after
 
156
     * all internal state is set ... dooohhh.
 
157
     * 
 
158
     * Here: test FindBar.
 
159
     */
 
160
    @Test
 
161
    public void testLocaleFindBarPropertyNotificationInListener() {
 
162
        final String prefix = PatternModel.SEARCH_PREFIX;
 
163
        final JXFindBar findPanel = new JXFindBar();
 
164
        final String actionCommand = JXFindBar.FIND_NEXT_ACTION_COMMAND;
 
165
        // JW: arrrgghh ... dirty! Consequence of dirty initialization 
 
166
        // of AbstractPatternPanel subclasses ...
 
167
        findPanel.addNotify();
 
168
        
 
169
        final Action action = findPanel.getActionMap().get(actionCommand);
 
170
        String name = (String) action.getValue(Action.NAME);
 
171
        String uiValue = UIManagerExt.getString(prefix + actionCommand, findPanel
 
172
                .getLocale());
 
173
        // sanity
 
174
        assertNotNull(uiValue);
 
175
        assertEquals(name, uiValue);
 
176
        final Locale alternative = getAlternativeLocale(findPanel);
 
177
        PropertyChangeListener report = new PropertyChangeListener() {
 
178
 
 
179
            public void propertyChange(PropertyChangeEvent evt) {
 
180
                // sanity
 
181
//                assertTrue("locale property changed", "locale".equals(evt.getPropertyName()));
 
182
                if (!"locale".equals(evt.getPropertyName())) return;
 
183
                String altUIValue = UIManagerExt.getString(prefix + actionCommand,
 
184
                        alternative);
 
185
                String altName = (String) action.getValue(Action.NAME);
 
186
                assertEquals("name must be updated before fire propertyChange", 
 
187
                        altUIValue, altName);
 
188
 
 
189
                
 
190
            }};
 
191
            PropertyChangeReport r = new PropertyChangeReport();
 
192
            findPanel.addPropertyChangeListener(r);
 
193
            findPanel.setLocale(alternative);
 
194
            assertEquals(1, r.getEventCount("locale"));
 
195
    }
 
196
 
 
197
 
 
198
    /**
 
199
     * test correct PropertyChangeNotification: must fire after
 
200
     * all internal state is set ...
 
201
     * 
 
202
     * Here: test FindPanel
 
203
     */
 
204
    @Test
 
205
    public void testLocaleFindPanelPropertyNotificationInListener() {
 
206
        final String prefix = PatternModel.SEARCH_PREFIX;
 
207
        final JXFindPanel findPanel = new JXFindPanel();
 
208
        final String actionCommand = AbstractPatternPanel.MATCH_ACTION_COMMAND;
 
209
        // JW: arrrgghh ... dirty! Consequence of dirty initialization 
 
210
        // of AbstractPatternPanel subclasses ...
 
211
        findPanel.addNotify();
 
212
        
 
213
        final Action action = findPanel.getActionMap().get(actionCommand);
 
214
        String name = (String) action.getValue(Action.NAME);
 
215
        String uiValue = UIManagerExt.getString(prefix + actionCommand, findPanel
 
216
                .getLocale());
 
217
        // sanity
 
218
        assertNotNull(uiValue);
 
219
        assertEquals(name, uiValue);
 
220
        final Locale alternative = getAlternativeLocale(findPanel);
 
221
        PropertyChangeListener report = new PropertyChangeListener() {
 
222
 
 
223
            public void propertyChange(PropertyChangeEvent evt) {
 
224
                // sanity
 
225
                // wrong assumption: find widgets name is changed as well
 
226
//                assertTrue("locale property changed, instead: " + evt.getPropertyName(), "locale".equals(evt.getPropertyName()));
 
227
                if (!"locale".equals(evt.getPropertyName())) return;
 
228
                String altUIValue = UIManagerExt.getString(prefix + actionCommand,
 
229
                        alternative);
 
230
                String altName = (String) action.getValue(Action.NAME);
 
231
                assertEquals("name must be updated before fire propertyChange", 
 
232
                        altUIValue, altName);
 
233
 
 
234
                
 
235
            }};
 
236
        findPanel.addPropertyChangeListener(report);
 
237
        PropertyChangeReport r = new PropertyChangeReport();
 
238
        findPanel.addPropertyChangeListener(r);
 
239
        findPanel.setLocale(alternative);
 
240
        assertEquals(1, r.getEventCount("locale"));
 
241
    }
 
242
 
 
243
    /**
 
244
     * test correct PropertyChangeNotification: must fire after
 
245
     * all internal state is set ... dooohhh.
 
246
     */
 
247
    @Test
 
248
    public void testLocaleTablePropertyNotification() {
 
249
        String prefix = "JXTable.";
 
250
        JXTable table = new JXTable(10, 2);
 
251
        String actionCommand = JXTable.HORIZONTALSCROLL_ACTION_COMMAND;
 
252
        
 
253
        Action action = table.getActionMap().get(actionCommand);
 
254
        String name = (String) action.getValue(Action.NAME);
 
255
        String uiValue = UIManagerExt.getString(prefix + actionCommand, table
 
256
                .getLocale());
 
257
        // sanity
 
258
        assertNotNull(uiValue);
 
259
        assertEquals(name, uiValue);
 
260
        Locale old = table.getLocale();
 
261
        Locale alternative = getAlternativeLocale(table);
 
262
        PropertyChangeReport report = new PropertyChangeReport();
 
263
        table.addPropertyChangeListener(report);
 
264
        table.setLocale(alternative);
 
265
        TestUtils.assertPropertyChangeEvent(report, "locale", old, alternative);
 
266
    }
 
267
 
 
268
    /**
 
269
     * test correct PropertyChangeNotification: must fire after
 
270
     * all internal state is set ... 
 
271
     * 
 
272
     * Here: test JXTable.
 
273
     */
 
274
    @Test
 
275
    public void testLocaleTablePropertyNotificationInListener() {
 
276
        final String prefix = "JXTable.";
 
277
        final JXTable table = new JXTable(10, 2);
 
278
        final String actionCommand = JXTable.HORIZONTALSCROLL_ACTION_COMMAND;
 
279
        
 
280
        final Action action = table.getActionMap().get(actionCommand);
 
281
        String name = (String) action.getValue(Action.NAME);
 
282
        String uiValue = UIManagerExt.getString(prefix + actionCommand, table
 
283
                .getLocale());
 
284
        // sanity
 
285
        assertNotNull(uiValue);
 
286
        assertEquals(name, uiValue);
 
287
        final Locale alternative = getAlternativeLocale(table);
 
288
        PropertyChangeListener report = new PropertyChangeListener() {
 
289
 
 
290
            public void propertyChange(PropertyChangeEvent evt) {
 
291
                // sanity
 
292
                assertTrue("locale property changed", "locale".equals(evt.getPropertyName()));
 
293
                String altUIValue = UIManagerExt.getString(prefix + actionCommand,
 
294
                        alternative);
 
295
                String altName = (String) action.getValue(Action.NAME);
 
296
                assertEquals("name must be updated before fire propertyChange", 
 
297
                        altUIValue, altName);
 
298
 
 
299
                
 
300
            }};
 
301
        table.addPropertyChangeListener(report);
 
302
        table.setLocale(alternative);
 
303
    }
 
304
 
 
305
    private Locale getAlternativeLocale(final Component table) {
 
306
        Locale alternative = OTHER_LOCALE;
 
307
        if (alternative.getLanguage().equals(table.getLocale().getLanguage())) {
 
308
            alternative = A_LOCALE;
 
309
        }
 
310
        return alternative;
 
311
    }
 
312
 
 
313
 
 
314
    /**
 
315
     * Issue #635-swingx: find widgets must support dynamic localization
 
316
     * Here: test findPanel's actions (incomplete ..)
 
317
     */
 
318
    @Test
 
319
    public void testLocaleFindPanel() {
 
320
        JXFindPanel panel = new JXFindPanel();
 
321
        // JW: arrrgghh ... dirty! Consequence of dirty initialization 
 
322
        // of AbstractPatternPanel subclasses ...
 
323
        panel.addNotify();
 
324
        String prefix = PatternModel.SEARCH_PREFIX;
 
325
        assertLocaleActionUpdate(panel, prefix, AbstractPatternPanel.MATCH_ACTION_COMMAND);
 
326
        assertLocaleActionUpdate(panel, prefix, JXFindPanel.FIND_NEXT_ACTION_COMMAND);
 
327
        assertLocaleActionUpdate(panel, prefix, JXFindPanel.FIND_PREVIOUS_ACTION_COMMAND);
 
328
    }
 
329
    
 
330
    /**
 
331
     * Issue #459-swingx: JXTable setLocale doesn't update localized column
 
332
     * control properties. <p>
 
333
     * 
 
334
     * Pass/fail expectation:
 
335
     * <ul>
 
336
     * <li> fails always with jdk5 independent of LookAndFeelAddon resource
 
337
     * bundle registration.
 
338
     * <li> fails with jdk6 and LookAndFeelAddon copy resource bundle values.
 
339
     * <li> passes with jdk6 and LookAndFeelAddon addResourceBundle.
 
340
     * </ul>
 
341
     */
 
342
    @Test
 
343
    public void testLocaleColumnControl() {
 
344
       String prefix = "JXTable.";
 
345
       JXTable table = new JXTable(10, 2);
 
346
       assertLocaleActionUpdate(table, prefix, JXTable.HORIZONTALSCROLL_ACTION_COMMAND);
 
347
       assertLocaleActionUpdate(table, prefix, JXTable.PACKALL_ACTION_COMMAND);
 
348
       assertLocaleActionUpdate(table, prefix, JXTable.PACKSELECTED_ACTION_COMMAND);
 
349
    }
 
350
 
 
351
    private void assertLocaleActionUpdate(JComponent table, String prefix, String actionCommand) {
 
352
        Action action = table.getActionMap().get(actionCommand);
 
353
        String name = (String) action.getValue(Action.NAME);
 
354
        String uiValue = UIManagerExt.getString(prefix + actionCommand, table
 
355
                .getLocale());
 
356
        // sanity
 
357
        assertNotNull(uiValue);
 
358
        assertEquals(name, uiValue);
 
359
        Locale alternative = OTHER_LOCALE;
 
360
        if (alternative.getLanguage().equals(table.getLocale().getLanguage())) {
 
361
            alternative = A_LOCALE;
 
362
        }
 
363
        table.setLocale(alternative);
 
364
        String altUIValue = UIManagerExt.getString(prefix + actionCommand,
 
365
                table.getLocale());
 
366
        // sanity
 
367
        assertNotNull(altUIValue);
 
368
        // sanity to track unexpected failure during refactoring
 
369
        assertFalse("new uiValue  must be different: " + uiValue + "/"
 
370
                + altUIValue, uiValue.equals(altUIValue));
 
371
        String altName = (String) action.getValue(Action.NAME);
 
372
        // here are the real asserts
 
373
        assertFalse("new action name must be different: " + name + "/"
 
374
                + altName, name.equals(altName));
 
375
        assertEquals(altName, altUIValue);
 
376
    }
 
377
    
 
378
    /**
 
379
     * Issue #466-swingx: locale-dependent values not accessible. This looks
 
380
     * like a side-effect of the first go on #159-swingx (not all values of
 
381
     * resourceBundle available).
 
382
     * <p>
 
383
     * 
 
384
     * Could be core problem (in jdk5, fixed in jdk6) around classloader and
 
385
     * ResourceBundle: <a
 
386
     * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4834404">
 
387
     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4834404 </a>
 
388
     * 
 
389
     * 
 
390
     */
 
391
    @Test
 
392
    public void testGetLocaleUIDefaults() {
 
393
        String key = "JXTable.column.packAll";
 
394
        Object alternativeValue = UIManagerExt.getString(key, OTHER_LOCALE);
 
395
        // sanity - the value must be available
 
396
        assertNotNull(alternativeValue);
 
397
        Object defaultValue = UIManagerExt.getString(key, A_LOCALE);
 
398
        // sanity - the value must be available
 
399
        assertNotNull(defaultValue);
 
400
        assertFalse("values must be different: " + defaultValue + "/" + alternativeValue, defaultValue.equals(alternativeValue));
 
401
    }
 
402
    
 
403
    /**
 
404
     * Issue #459-swingx: columnControl properties not updated on locale setting.
 
405
     *
 
406
     */
 
407
    public void interactiveLocaleColumnControl() {
 
408
        final JXTable table = new JXTable(10, 4);
 
409
        table.setColumnControlVisible(true);
 
410
        table.getColumnExt(0).setTitle(table.getLocale().getLanguage());
 
411
        Action toggleLocale = new AbstractActionExt("toggleLocale") {
 
412
 
 
413
            public void actionPerformed(ActionEvent e) {
 
414
                Locale old = table.getLocale();
 
415
                table.setLocale(old == A_LOCALE ? OTHER_LOCALE : A_LOCALE);
 
416
                table.getColumnExt(0).setTitle(table.getLocale().getLanguage());
 
417
                
 
418
            }
 
419
            
 
420
        };
 
421
        JXFrame frame = wrapWithScrollingInFrame(table, "toggle locale on table - column control not updated");
 
422
        addAction(frame, toggleLocale);
 
423
        frame.setVisible(true);
 
424
    }
 
425
    
 
426
    /**
 
427
     * Issue #459-swingx: columnControl properties not updated on locale setting.
 
428
     *
 
429
     * 
 
430
     */
 
431
    public void interactiveLocaleColumnControlTwoTables() {
 
432
        final JXTable table = new JXTable(10, 4);
 
433
        table.setColumnControlVisible(true);
 
434
        table.getColumnExt(0).setTitle(table.getLocale().getLanguage());
 
435
        JXTable other = new JXTable(10, 4);
 
436
        other.setColumnControlVisible(true);
 
437
        other.setLocale(A_LOCALE);
 
438
        other.getColumnExt(0).setTitle(other.getLocale().getLanguage());
 
439
        JXFrame frame = wrapWithScrollingInFrame(table, other, "different locals: de <--> vs fr");
 
440
        addSearchModeToggle(frame);
 
441
        addMessage(frame, "Find panel/bar should be localized per-table");
 
442
        frame.pack();
 
443
        frame.setVisible(true);
 
444
    }
 
445
 
 
446
 
 
447
}