test/jdk/javax/swing/JSpinner/WrongEditorTextFieldFont/FontSetToNull.java
changeset 52259 22517c8020d3
equal deleted inserted replaced
52258:02e4b0ab0f97 52259:22517c8020d3
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.awt.Font;
       
    25 
       
    26 import javax.swing.JSpinner;
       
    27 import javax.swing.SwingUtilities;
       
    28 import javax.swing.UIManager;
       
    29 import javax.swing.UnsupportedLookAndFeelException;
       
    30 import javax.swing.plaf.UIResource;
       
    31 
       
    32 import static javax.swing.JSpinner.DefaultEditor;
       
    33 import static javax.swing.UIManager.getInstalledLookAndFeels;
       
    34 
       
    35 /**
       
    36  * @test
       
    37  * @key headful
       
    38  * @bug 8210739
       
    39  */
       
    40 public final class FontSetToNull {
       
    41 
       
    42     private static final Font USERS_FONT = new Font("dialog", Font.BOLD, 41);
       
    43 
       
    44     public static void main(final String[] args) throws Exception {
       
    45         for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {
       
    46             SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
       
    47             SwingUtilities.invokeAndWait(() -> {
       
    48                 // default spinner
       
    49                 test(new JSpinner());
       
    50                 // spinner which always uses the null font
       
    51                 test(new JSpinner(){
       
    52                     @Override
       
    53                     public Font getFont() {
       
    54                         return null;
       
    55                     }
       
    56                 });
       
    57             });
       
    58         }
       
    59     }
       
    60 
       
    61     /**
       
    62      * A sequence of methods that test a possible NPE.
       
    63      */
       
    64     private static void test(JSpinner spinner) {
       
    65         final DefaultEditor de = (DefaultEditor) spinner.getEditor();
       
    66 
       
    67         spinner.setFont(null); // Check possible NPE
       
    68         SwingUtilities.updateComponentTreeUI(de); // Check possible NPE
       
    69         spinner.setFont(null); // Check possible NPE
       
    70 
       
    71         // should not replace the font of the TextField
       
    72         de.getTextField().setFont(USERS_FONT);
       
    73         spinner.setFont(null);
       
    74 
       
    75         final Font tff = de.getTextField().getFont();
       
    76         if (tff instanceof UIResource || !tff.equals(USERS_FONT)) {
       
    77             throw new RuntimeException("Wrong font: " + tff);
       
    78         }
       
    79 
       
    80         spinner.setEditor(new JSpinner().getEditor()); // Check possible NPE
       
    81     }
       
    82 
       
    83     private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {
       
    84         try {
       
    85             UIManager.setLookAndFeel(laf.getClassName());
       
    86             System.out.println("LookAndFeel: " + laf.getClassName());
       
    87         } catch (ClassNotFoundException | InstantiationException |
       
    88                 UnsupportedLookAndFeelException | IllegalAccessException e) {
       
    89             throw new RuntimeException(e);
       
    90         }
       
    91     }
       
    92 }